Commit e689f7f1 authored by Skia's avatar Skia

Add index and query reduction in clubs

parent 2f5bd7d2
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('club', '0007_auto_20170324_0917'),
]
operations = [
migrations.AlterField(
model_name='club',
name='id',
field=models.AutoField(primary_key=True, serialize=False, db_index=True),
),
]
...@@ -39,6 +39,7 @@ class Club(models.Model): ...@@ -39,6 +39,7 @@ class Club(models.Model):
""" """
The Club class, made as a tree to allow nice tidy organization The Club class, made as a tree to allow nice tidy organization
""" """
id = models.AutoField(primary_key=True, db_index=True)
name = models.CharField(_('name'), max_length=64) name = models.CharField(_('name'), max_length=64)
parent = models.ForeignKey('Club', related_name='children', null=True, blank=True) parent = models.ForeignKey('Club', related_name='children', null=True, blank=True)
unix_name = models.CharField(_('unix name'), max_length=30, unique=True, unix_name = models.CharField(_('unix name'), max_length=30, unique=True,
...@@ -151,11 +152,21 @@ class Club(models.Model): ...@@ -151,11 +152,21 @@ class Club(models.Model):
return False return False
return sub.is_subscribed return sub.is_subscribed
_memberships = {}
def get_membership_for(self, user): def get_membership_for(self, user):
""" """
Returns the current membership the given user Returns the current membership the given user
""" """
return self.members.filter(user=user.id).filter(end_date=None).first() try:
return Club._memberships[self.id][user.id]
except:
m = self.members.filter(user=user.id).filter(end_date=None).first()
try:
Club._memberships[self.id][user.id] = m
except:
Club._memberships[self.id] = {}
Club._memberships[self.id][user.id] = m
return m
class Membership(models.Model): class Membership(models.Model):
""" """
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment