From: hernani Date: Wed, 19 May 2010 23:29:52 +0000 (+0000) Subject: Some changes necessary to make cache work on Django 1.2. X-Git-Tag: live~824 X-Git-Url: https://git.openstreetmap.org./osqa.git/commitdiff_plain/b6f6103a8b578d13047219ef3812cea09001eea2 Some changes necessary to make cache work on Django 1.2. git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@307 0cfe37f9-358a-4d5e-be75-b63607b5c754 --- diff --git a/forum/models/action.py b/forum/models/action.py index 785ed2d..82ca34a 100644 --- a/forum/models/action.py +++ b/forum/models/action.py @@ -4,29 +4,28 @@ from threading import Thread from base import * import re +class ActionQuerySet(models.query.QuerySet): + def get(self, *args, **kwargs): + action = super(ActionQuerySet, self).get(*args, **kwargs) + if self.model == Action: + return action.leaf() + return action + class ActionManager(models.Manager): use_for_related_fields = True def get_query_set(self): - qs = super(ActionManager, self).get_query_set().filter(canceled=False) + qs = ActionQuerySet(self.model).filter(canceled=False) if self.model is not Action: return qs.filter(action_type=self.model.get_type()) else: return qs - def get(self, *args, **kwargs): - action = super(ActionManager, self).get(*args, **kwargs) - if self.model == Action: - return action.leaf() - return action - def get_for_types(self, types, *args, **kwargs): kwargs['action_type__in'] = [t.get_type() for t in types] return self.get(*args, **kwargs) - - class Action(models.Model): user = models.ForeignKey('User', related_name="actions") ip = models.CharField(max_length=16) diff --git a/forum/models/base.py b/forum/models/base.py index 627888f..276c8f6 100644 --- a/forum/models/base.py +++ b/forum/models/base.py @@ -40,6 +40,27 @@ class CachedQuerySet(models.query.QuerySet): else: return self + def get(self, *args, **kwargs): + try: + pk = [v for (k,v) in kwargs.items() if k in ('pk', 'pk__exact', 'id', 'id__exact' + ) or k.endswith('_ptr__pk') or k.endswith('_ptr__id')][0] + except: + pk = None + + if pk is not None: + key = self.model.cache_key(pk) + obj = cache.get(key) + + if obj is None: + obj = super(CachedQuerySet, self).get(*args, **kwargs) + obj.__class__.objects.cache_obj(obj) + else: + d = obj.__dict__ + + return obj + + return super(CachedQuerySet, self).get(*args, **kwargs) + from action import Action class CachedManager(models.Manager): @@ -58,27 +79,6 @@ class CachedManager(models.Manager): cache.set(self.model.cache_key(obj.id), obj, 60 * 60) - def get(self, *args, **kwargs): - try: - pk = [v for (k,v) in kwargs.items() if k in ('pk', 'pk__exact', 'id', 'id__exact' - ) or k.endswith('_ptr__pk') or k.endswith('_ptr__id')][0] - except: - pk = None - - if pk is not None: - key = self.model.cache_key(pk) - obj = cache.get(key) - - if obj is None: - obj = super(CachedManager, self).get(*args, **kwargs) - self.cache_obj(obj) - else: - d = obj.__dict__ - - return obj - - return super(CachedManager, self).get(*args, **kwargs) - def get_or_create(self, *args, **kwargs): try: return self.get(*args, **kwargs) diff --git a/forum/models/meta.py b/forum/models/meta.py index 3fd0680..fca763f 100644 --- a/forum/models/meta.py +++ b/forum/models/meta.py @@ -24,21 +24,26 @@ class Flag(models.Model): app_label = 'forum' unique_together = ('user', 'node') -class BadgeManager(models.Manager): - use_for_related_fields = True - +class BadgesQuerySet(models.query.QuerySet): def get(self, *args, **kwargs): try: pk = [v for (k,v) in kwargs.items() if k in ('pk', 'pk__exact', 'id', 'id__exact')][0] except: - return super(BadgeManager, self).get(*args, **kwargs) + return super(BadgesQuerySet, self).get(*args, **kwargs) from forum.badges.base import BadgesMeta badge = BadgesMeta.by_id.get(int(pk), None) if not badge: - return super(BadgeManager, self).get(*args, **kwargs) + return super(BadgesQuerySet, self).get(*args, **kwargs) return badge.ondb + +class BadgeManager(models.Manager): + use_for_related_fields = True + + def get_query_set(self): + return BadgesQuerySet(self.model) + class Badge(models.Model): GOLD = 1 SILVER = 2