From bbd2786e0f8072728527ce3ab84258948850be68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Mon, 26 Jul 2021 14:27:03 +0300 Subject: [PATCH] Optimize queries and fix team member sorting --- hc/accounts/models.py | 3 --- hc/accounts/tests/test_transfer_project.py | 3 ++- hc/accounts/views.py | 2 ++ templates/accounts/project.html | 12 ++++++------ 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/hc/accounts/models.py b/hc/accounts/models.py index 67afdd6c..8c66ba44 100644 --- a/hc/accounts/models.py +++ b/hc/accounts/models.py @@ -338,9 +338,6 @@ class Project(models.Model): self.api_key_readonly = token_urlsafe(nbytes=24) self.save() - def team(self): - return User.objects.filter(memberships__project=self).order_by("email") - def invite_suggestions(self): q = User.objects.filter(memberships__project__owner_id=self.owner_id) q = q.exclude(memberships__project=self) diff --git a/hc/accounts/tests/test_transfer_project.py b/hc/accounts/tests/test_transfer_project.py index 6de29d70..bc25ab3e 100644 --- a/hc/accounts/tests/test_transfer_project.py +++ b/hc/accounts/tests/test_transfer_project.py @@ -97,7 +97,8 @@ class TransferProjectTestCase(BaseTestCase): self.assertEqual(self.project.owner, self.bob) # Alice, the previous owner, should now be a member - self.assertTrue(self.project.team().filter(email="alice@example.org").exists()) + m = Member.objects.get(project=self.project, user=self.alice) + self.assertEqual(m.role, Member.Role.REGULAR) def test_accept_requires_a_transfer_request(self): self.client.login(username="bob@example.org", password="password") diff --git a/hc/accounts/views.py b/hc/accounts/views.py index 10c5c778..62eb8893 100644 --- a/hc/accounts/views.py +++ b/hc/accounts/views.py @@ -457,6 +457,8 @@ def project(request, code): tr.transfer_request_date = None tr.save() + q = project.member_set.select_related("user").order_by("user__email") + ctx["memberships"] = list(q) return render(request, "accounts/project.html", ctx) diff --git a/templates/accounts/project.html b/templates/accounts/project.html index 57b7c492..d2efb516 100644 --- a/templates/accounts/project.html +++ b/templates/accounts/project.html @@ -152,7 +152,7 @@

Team Access

- {% if project.team.exists or invite_suggestions %} + {% if memberships or invite_suggestions %} @@ -164,7 +164,7 @@ - {% for m in project.member_set.all %} + {% for m in memberships %} @@ -514,7 +514,7 @@ -{% if not transfer_request %} +{% if is_owner and not transfer_request %}
EmailOwner
{{ m.get_role_display }}