Skip to content

Commit

Permalink
🐎 Improve query speed for retreiving teams
Browse files Browse the repository at this point in the history
  • Loading branch information
Jip J. Dekker committed Apr 27, 2017
1 parent fac1e0f commit 4c625b2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 32 deletions.
49 changes: 18 additions & 31 deletions website/involvement/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,47 +170,34 @@ def get_manual_members(self):
]),
FieldPanel('group'),
ImageChooserPanel('logo'),
FieldRowPanel([
FieldPanel('leader_en'),
FieldPanel('leader_sv'),
]),
FieldPanel('email'),
FieldPanel('description_en'),
FieldPanel('description_sv'),
])]


def official_of(user, pk=False):
# TODO : Is this efficient?
applications = Application.objects.filter(
applicant=user,
status='appointed',
position__term_from__lte=date.today(),
position__term_to__gte=date.today(),
position__role__official=True,
position__role__team_id__isnull=False,
).select_related('position__role__team')
teams = []
for i in applications:
if pk:
teams.append(i.position.role.team.pk)
else:
teams.append(i.position.role.team)

return teams
teams = Team.objects.filter(
roles__official=True,
roles__positions__applications__applicant=user,
roles__positions__applications__status='appointed',
roles__positions__term_from__lte=date.today(),
roles__positions__term_to__gte=date.today(),
)
if pk:
return teams.values_list('pk', flat=True)
else:
return teams


def member_of(user, pk=False):
# TODO: Optimize.
groups = user.groups.all()
teams = []
for group in groups:
if hasattr(group, 'team'):
if pk:
teams.append(group.team.pk)
else:
teams.append(group.team)
return teams
teams = Team.objects.filter(
group__in=groups
)
if pk:
return teams.values_list('pk', flat=True)
else:
return teams


class Role(models.Model):
Expand Down
2 changes: 1 addition & 1 deletion website/involvement/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TeamAdmin(ModelAdmin):
menu_label = _('Teams')
menu_icon = 'fa-sitemap'
menu_order = 100
list_display = ('name_en', 'name_sv', 'email')
list_display = ('name_en', 'name_sv', 'group')
search_fields = ('name_en', 'name_sv', 'description_en', 'description_sv')
permission_helper_class = RulesPermissionHelper
inspect_view_enabled = True
Expand Down

0 comments on commit 4c625b2

Please sign in to comment.