From b0f4bd3fce23eed22015170ad3d1ccefd4f2b0ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Sat, 2 Feb 2019 00:08:00 +0200 Subject: [PATCH] Show "grace" status in the "List of Projects" page. Fix the query for badges in top nav. --- hc/accounts/models.py | 24 ++++++++++++++++++++---- hc/front/views.py | 2 +- templates/front/projects.html | 6 +----- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/hc/accounts/models.py b/hc/accounts/models.py index e38501b4..5432a0a1 100644 --- a/hc/accounts/models.py +++ b/hc/accounts/models.py @@ -122,12 +122,16 @@ class Profile(models.Model): def annotated_projects(self): """ Return all projects, annotated with 'n_down'. """ - is_owner = Q(owner=self.user) - is_member = Q(member__user=self.user) - q = Project.objects.filter(is_owner | is_member) + # Subquery for getting project ids + project_ids = self.projects().values("id") + + # Main query with the n_down annotation. + # Must use the subquery, otherwise ORM gets confused by + # joins and group by's + q = Project.objects.filter(id__in=project_ids) n_down = Count("check", filter=Q(check__status="down")) q = q.annotate(n_down=n_down) - return q.distinct().order_by("name") + return q.order_by("name") def checks_from_all_projects(self): """ Return a queryset of checks from projects we have access to. """ @@ -256,6 +260,18 @@ class Project(models.Model): q.update(next_nag_date=timezone.now() + models.F("nag_period")) + def overall_status(self): + status = "up" + for check in self.check_set.all(): + check_status = check.get_status(with_started=False) + if status == "up" and check_status == "grace": + status = "grace" + + if check_status == "down": + status = "down" + break + return status + class Member(models.Model): user = models.ForeignKey(User, models.CASCADE, related_name="memberships") diff --git a/hc/front/views.py b/hc/front/views.py index 871255b7..9a5f136b 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -195,7 +195,7 @@ def switch_channel(request, code, channel_code): def index(request): if request.user.is_authenticated: - projects = list(request.profile.annotated_projects()) + projects = list(request.profile.projects()) if len(projects) == 1: return redirect("hc-checks", projects[0].code) diff --git a/templates/front/projects.html b/templates/front/projects.html index 2b728399..0e80fcf0 100644 --- a/templates/front/projects.html +++ b/templates/front/projects.html @@ -23,11 +23,7 @@
- {% if project.n_down %} -
- {% else %} -
- {% endif %} +

{{ project }}