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.
This commit is contained in:
Vishnu KS
2019-09-13 17:05:28 +05:30
committed by Tim Abbott
parent 62a8e378a6
commit 139ebf387b
4 changed files with 19 additions and 23 deletions

View File

@@ -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:

View File

@@ -1,11 +1,11 @@
<span class="label">realm</span>
<h3><img src="{{ realm.realm_icon_url }}" class="support-realm-icon"> {{ realm.name }}</h3>
<h3><img src="{{ realm_icon_url(realm) }}" class="support-realm-icon"> {{ realm.name }}</h3>
<b>URL</b>: <a target="_blank" href="{{ realm.uri }}">{{ realm.uri }}</a> |
<a target="_blank" href="/stats/realm/{{ realm.string_id }}/">stats</a> |
<a target="_blank" href="/realm_activity/{{ realm.string_id }}/">activity</a><br>
<b>Date created</b>: {{ realm.date_created|timesince }} ago<br>
<b>Admins</b>: {{ realm.admin_emails }}
<a title="Copy emails" class="copy-button" data-admin-emails="{{ realm.admin_emails }}">
<b>Admins</b>: {{ realm_admin_emails(realm) }}
<a title="Copy emails" class="copy-button" data-admin-emails="{{ realm_admin_emails(realm) }}">
<i class="fa fa-copy"></i>
</a>
<form method="POST">
@@ -34,7 +34,7 @@
<b>Discount (use 85 for nonprofits)</b>:<br>
{{ csrf_input }}
<input type="hidden" name="realm_id" value="{{ realm.id }}" />
<input type="number" name="discount" value="{{ realm.default_discount}}" required>
<input type="number" name="discount" value="{{ get_discount_for_realm(realm) }}" required>
<button type="submit" class="button rounded small support-submit-button">Update</button>
</form>
<form method="POST" class="scrub-realm-form">

View File

@@ -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).

View File

@@ -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)