Skip to content

Commit 6ada1dc

Browse files
committed
Optimize all teams query
1 parent 1391382 commit 6ada1dc

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

get_together/views/teams.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.contrib.auth.decorators import login_required
77
from django.contrib.sites.models import Site
88
from django.core.mail import send_mail
9+
from django.db.models import Q
910
from django.http import Http404, HttpResponse, JsonResponse
1011
from django.shortcuts import get_object_or_404, redirect, render
1112
from django.template.loader import get_template, render_to_string
@@ -63,12 +64,15 @@ def teams_list(request, *args, **kwargs):
6364

6465

6566
def teams_list_all(request, *args, **kwargs):
66-
teams = [
67-
team
68-
for team in Team.objects.all().select_related("city")
69-
if team.access == Team.PUBLIC
70-
or (team.access == Team.PRIVATE and request.user.profile.is_in_team(team))
71-
]
67+
if request.user.is_authenticated:
68+
teams = Team.objects.filter(
69+
Q(access=Team.PUBLIC)
70+
| Q(access=Team.PRIVATE, member__user=request.user.profile)
71+
)
72+
else:
73+
teams = Team.objects.filter(access=Team.PUBLIC)
74+
teams = teams.select_related("city")
75+
7276
try:
7377
geo_ip = location.get_geoip(request)
7478
context = {

0 commit comments

Comments
 (0)