From 139ebf387bc05be51106e767d499a6f3549633e3 Mon Sep 17 00:00:00 2001 From: Vishnu KS Date: Fri, 13 Sep 2019 17:05:28 +0530 Subject: [PATCH] support: Pass various realm functions as template context. We currently have code to calculate the value of realm_icon_url, admin_emails and default_discount in two diffrent places. With the addition of showing confirmation links it would become three. The easiest way to deduplicate the code and make the view cleaner is by doing the calculations in template. Alternatively one can write a function that takes users, realms and confirmations as arguments and sets the value of realm_icon_url, admin_emails and default_discount appropriately in realm object according to the type of the confirmation. But that seems more messy than passing the functions directly to template approach. --- analytics/views.py | 27 +++++++++----------------- templates/analytics/realm_details.html | 8 ++++---- zerver/models.py | 2 +- zerver/tests/test_templates.py | 5 +++++ 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/analytics/views.py b/analytics/views.py index de8d5b7c7d..202e449898 100644 --- a/analytics/views.py +++ b/analytics/views.py @@ -1071,17 +1071,7 @@ def support(request: HttpRequest) -> HttpResponse: if query: key_words = get_invitee_emails_set(query) - users = UserProfile.objects.filter(delivery_email__in=key_words) - if users: - for user in users: - user.realm.realm_icon_url = realm_icon_url(user.realm) - user.realm.admin_emails = ", ".join( - user.realm.get_human_admin_users().values_list( - "delivery_email", - flat=True)) - user.realm.default_discount = get_discount_for_realm(user.realm) - context["users"] = users - + context["users"] = UserProfile.objects.filter(delivery_email__in=key_words) realms = set(Realm.objects.filter(string_id__in=key_words)) for key_word in key_words: @@ -1099,13 +1089,14 @@ def support(request: HttpRequest) -> HttpResponse: except ValidationError: pass - if realms: - for realm in realms: - realm.realm_icon_url = realm_icon_url(realm) - realm.admin_emails = ", ".join(realm.get_human_admin_users().values_list( - "delivery_email", flat=True)) - realm.default_discount = get_discount_for_realm(realm) - context["realms"] = realms + context["realms"] = realms + + def realm_admin_emails(realm: Realm) -> str: + return ", ".join(realm.get_human_admin_users().values_list("delivery_email", flat=True)) + + context["realm_admin_emails"] = realm_admin_emails + context["get_discount_for_realm"] = get_discount_for_realm + context["realm_icon_url"] = realm_icon_url return render(request, 'analytics/support.html', context=context) def get_user_activity_records_for_realm(realm: str, is_bot: bool) -> QuerySet: diff --git a/templates/analytics/realm_details.html b/templates/analytics/realm_details.html index 0fe44b0985..216ab7ebea 100644 --- a/templates/analytics/realm_details.html +++ b/templates/analytics/realm_details.html @@ -1,11 +1,11 @@ realm -

{{ realm.name }}

+

{{ realm.name }}

URL: {{ realm.uri }} | stats | activity
Date created: {{ realm.date_created|timesince }} ago
-Admins: {{ realm.admin_emails }} - +Admins: {{ realm_admin_emails(realm) }} +
@@ -34,7 +34,7 @@ Discount (use 85 for nonprofits):
{{ csrf_input }} - +
diff --git a/zerver/models.py b/zerver/models.py index fc91ffcb22..f327ed4f81 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -401,7 +401,7 @@ class Realm(models.Model): return UserProfile.objects.filter(realm=self, role=UserProfile.ROLE_REALM_ADMINISTRATOR, is_active=True) - def get_human_admin_users(self) -> Sequence['UserProfile']: + def get_human_admin_users(self) -> QuerySet: """Use this in contexts where we want only human users with administrative privileges, like sending an email to all of a realm's administrators (bots don't have real email addresses). diff --git a/zerver/tests/test_templates.py b/zerver/tests/test_templates.py index 8daa28c428..88557bb9f4 100644 --- a/zerver/tests/test_templates.py +++ b/zerver/tests/test_templates.py @@ -154,6 +154,7 @@ of syntax errors. There are two common causes for this test failing: """ user_profile = self.example_user('hamlet') + realm = user_profile.realm email = user_profile.email context = dict( @@ -194,6 +195,10 @@ of syntax errors. There are two common causes for this test failing: invite_as={"MEMBER": 1}, max_file_upload_size = 25, avatar_urls={"john@gmail.com": "www.zulip.com"}, + realm_admin_emails=lambda _: "admin emails", + get_discount_for_realm=lambda _: 0, + realm_icon_url=lambda _: "url", + realm=realm, ) context.update(kwargs)