Browse Source

Cache a zrychleni na rocniky (hodne) a tez soustredeni_resitele

remotes/origin/feincms
Tomas Gavenciak 10 years ago
parent
commit
93b66fe0ec
  1. 3
      seminar/admin.py
  2. 25
      seminar/models.py

3
seminar/admin.py

@ -90,6 +90,9 @@ class Soustredeni_UcastniciInline(admin.TabularInline):
fields = ['resitel', 'poznamka', ] fields = ['resitel', 'poznamka', ]
extra = 0 extra = 0
def get_queryset(self, request):
qs = super(Soustredeni_UcastniciInline, self).get_queryset(request)
return qs.select_related('resitel', 'soustredeni')
### Resitel ### Resitel

25
seminar/models.py

@ -10,6 +10,8 @@ from django.utils.encoding import python_2_unicode_compatible
from django.utils.encoding import force_unicode from django.utils.encoding import force_unicode
from django.utils.text import slugify from django.utils.text import slugify
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.core.cache import cache
from django_countries.fields import CountryField from django_countries.fields import CountryField
from solo.models import SingletonModel from solo.models import SingletonModel
@ -19,7 +21,6 @@ import reversion
from seminar.utils import roman from seminar.utils import roman
# TODO společná báze (admin url, url, veřejné, ...)
class SeminarModelBase(models.Model): class SeminarModelBase(models.Model):
class Meta: class Meta:
@ -230,6 +231,16 @@ class Rocnik(SeminarModelBase):
def verejne_url(self): def verejne_url(self):
return reverse('seminar_rocnik', kwargs={'pk': self.id}) return reverse('seminar_rocnik', kwargs={'pk': self.id})
@classmethod
def cached_rocnik(cls, r_id):
name = 'rocnik_%s' % (r_id, )
c = cache.get(name)
if c is None:
c = cls.objects.get(id=r_id)
cache.set(name, c, 300)
return c
@reversion.register(ignore_duplicate_revisions=True) @reversion.register(ignore_duplicate_revisions=True)
@python_2_unicode_compatible @python_2_unicode_compatible
@ -239,7 +250,7 @@ class Cislo(SeminarModelBase):
db_table = 'seminar_cisla' db_table = 'seminar_cisla'
verbose_name = u'Číslo' verbose_name = u'Číslo'
verbose_name_plural = u'Čísla' verbose_name_plural = u'Čísla'
ordering = ['rocnik_id', 'cislo'] ordering = ['rocnik__rocnik', 'cislo']
# Interní ID # Interní ID
id = models.AutoField(primary_key = True) id = models.AutoField(primary_key = True)
@ -265,9 +276,9 @@ class Cislo(SeminarModelBase):
kod.short_description = u'Kód čísla' kod.short_description = u'Kód čísla'
def __str__(self): def __str__(self):
#TODO: DB HOG # Potenciální DB HOG, pokud by se ročník neckešoval
#return force_unicode(u'%s' % (self.kod(),)) r = Rocnik.cached_rocnik(self.rocnik_id)
return force_unicode(u'%s' % (self.cislo,)) return force_unicode(u'%s.%s' % (r.rocnik, self.cislo, ))
def verejne(self): def verejne(self):
return self.verejne_db return self.verejne_db
@ -416,7 +427,7 @@ class Reseni(SeminarModelBase):
def __str__(self): def __str__(self):
return force_unicode(u"%s: %s (%sb)" % (self.resitel.plne_jmeno(), self.problem.nazev, self.body)) return force_unicode(u"%s: %s (%sb)" % (self.resitel.plne_jmeno(), self.problem.nazev, self.body))
#TODO: DB HOG # NOTE: Potenciální DB HOG (bez select_related)
# PrilohaReseni method # PrilohaReseni method
@ -516,7 +527,7 @@ class Soustredeni_Ucastnici(models.Model):
def __str__(self): def __str__(self):
return force_unicode(u'%s na %s' % (self.resitel, self.soustredeni, )) return force_unicode(u'%s na %s' % (self.resitel, self.soustredeni, ))
#TODO: DB HOG # NOTE: Poteciální DB HOG bez select_related
@python_2_unicode_compatible @python_2_unicode_compatible

Loading…
Cancel
Save