Browse Source

Reduce query count for team lookups

pull/60/head
Pēteris Caune 9 years ago
parent
commit
00d18e86bd
4 changed files with 17 additions and 9 deletions
  1. +7
    -0
      hc/accounts/middleware.py
  2. +0
    -1
      hc/api/tests/test_create_check.py
  3. +3
    -2
      hc/front/views.py
  4. +7
    -6
      templates/base.html

+ 7
- 0
hc/accounts/middleware.py View File

@ -1,8 +1,15 @@
from hc.accounts.models import Profile
class TeamAccessMiddleware(object): class TeamAccessMiddleware(object):
def process_request(self, request): def process_request(self, request):
if not request.user.is_authenticated(): if not request.user.is_authenticated():
return return
teams_q = Profile.objects.filter(member__user_id=request.user.id)
teams_q = teams_q.select_related("user")
request.teams = list(teams_q)
profile = request.user.profile profile = request.user.profile
if profile.current_team: if profile.current_team:
request.team = profile.current_team request.team = profile.current_team


+ 0
- 1
hc/api/tests/test_create_check.py View File

@ -2,7 +2,6 @@ import json
from hc.api.models import Channel, Check from hc.api.models import Channel, Check
from hc.test import BaseTestCase from hc.test import BaseTestCase
from hc.accounts.models import Profile
class CreateCheckTestCase(BaseTestCase): class CreateCheckTestCase(BaseTestCase):


+ 3
- 2
hc/front/views.py View File

@ -27,11 +27,12 @@ def pairwise(iterable):
@login_required @login_required
def my_checks(request): def my_checks(request):
checks = Check.objects.filter(user=request.team.user).order_by("created")
q = Check.objects.filter(user=request.team.user).order_by("created")
checks = list(q)
counter = Counter() counter = Counter()
down_tags, grace_tags = set(), set() down_tags, grace_tags = set(), set()
for check in checks.iterator():
for check in checks:
status = check.get_status() status = check.get_status()
for tag in check.tags_list(): for tag in check.tags_list():
if tag == "": if tag == "":


+ 7
- 6
templates/base.html View File

@ -110,26 +110,27 @@
<ul class="nav navbar-nav navbar-right"> <ul class="nav navbar-nav navbar-right">
<li class="dropdown"> <li class="dropdown">
<a id="nav-email" href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"> <a id="nav-email" href="#" class="dropdown-toggle" data-toggle="dropdown" role="button">
{{ request.user.profile.current_team }} <span class="caret"></span>
{{ request.team }} <span class="caret"></span>
</a> </a>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
{% if request.teams %}
<li class="dropdown-header">{{ request.user.profile }}</li> <li class="dropdown-header">{{ request.user.profile }}</li>
<li> <li>
<a href="{% url 'hc-switch-team' request.user.username %}" class="active">Checks</a> <a href="{% url 'hc-switch-team' request.user.username %}" class="active">Checks</a>
</li> </li>
{% endif %}
<li><a href="{% url 'hc-profile' %}">Account Settings</a></li> <li><a href="{% url 'hc-profile' %}">Account Settings</a></li>
<li role="separator" class="divider"></li> <li role="separator" class="divider"></li>
{% for m in request.user.member_set.all %}
<li class="dropdown-header">{{ m.team }}</li>
{% for team in request.teams %}
<li class="dropdown-header">{{ team }}</li>
<li> <li>
<a href="{% url 'hc-switch-team' m.team.user.username %}">Checks</a>
<a href="{% url 'hc-switch-team' team.user.username %}">Checks</a>
</li> </li>
<li role="separator" class="divider"></li> <li role="separator" class="divider"></li>
{% endfor %} {% endfor %}
<li><a href="{% url 'hc-logout' %}">Log Out</a></li> <li><a href="{% url 'hc-logout' %}">Log Out</a></li>
</ul> </ul>
</li> </li>


Loading…
Cancel
Save