]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/models/base.py
Removed an unnecessary link to feedback. It was causing an exception, apparently...
[osqa.git] / forum / models / base.py
index 78f12428b45c16d15fbe7e854f03fff534eb8675..1f15bde0bdc1dd304bd6f518ace1209d70e94bc4 100644 (file)
@@ -1,4 +1,5 @@
 import datetime
+import re
 from urllib import quote_plus, urlencode
 from django.db import models, IntegrityError, connection, transaction
 from django.utils.http import urlquote  as django_urlquote
@@ -13,18 +14,54 @@ from django.utils.translation import ugettext as _
 from django.utils.safestring import mark_safe
 from django.contrib.sitemaps import ping_google
 import django.dispatch
-from django.conf import settings
-from forum import const
+from forum import settings
 import logging
 
-from forum.const import *
+
+class LazyQueryList(object):
+    def __init__(self, model, items):
+        self.model = model
+        self.items = items
+
+    def __getitem__(self, k):
+        return self.model.objects.get(id=self.items[k])
+
+    def __iter__(self):
+        for id in self.items:
+            yield self.model.objects.get(id=id)
+
+    def __len__(self):
+        return len(self.items)
+
+class CachedQuerySet(models.query.QuerySet):
+    def lazy(self):
+        if len(self.query.extra) == 0:
+            return LazyQueryList(self.model, list(self.values_list('id', flat=True)))
+        else:
+            return self
+
+from action import Action
 
 class CachedManager(models.Manager):
     use_for_related_fields = True
+    int_cache_re = re.compile('^_[\w_]+cache$')
+
+    def get_query_set(self):
+        return CachedQuerySet(self.model)
+
+    def cache_obj(self, obj):
+        int_cache_keys = [k for k in obj.__dict__.keys() if self.int_cache_re.match(k)]
+        d = obj.__dict__
+        for k in int_cache_keys:
+            if not isinstance(obj.__dict__[k], Action):
+                del obj.__dict__[k]
+
+        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')][0]
+            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
 
@@ -34,7 +71,9 @@ class CachedManager(models.Manager):
 
             if obj is None:
                 obj = super(CachedManager, self).get(*args, **kwargs)
-                cache.set(key, obj, 60 * 60)
+                self.cache_obj(obj)
+            else:
+                d = obj.__dict__
 
             return obj
         
@@ -47,7 +86,53 @@ class CachedManager(models.Manager):
             return super(CachedManager, self).get_or_create(*args, **kwargs)
 
 
+class DenormalizedField(object):
+    def __init__(self, manager, **kwargs):
+        self.manager = manager
+        self.filter = kwargs
+
+    def setup_class(self, cls, name):
+        dict_name = '_%s_cache_' % name
+
+        def getter(inst):
+            val = inst.__dict__.get(dict_name, None)
+
+            if val is None:
+                val = getattr(inst, self.manager).filter(**self.filter).count()
+                inst.__dict__[dict_name] = val
+                inst.__class__.objects.cache_obj(inst)
+
+            return val
+
+        def reset_cache(inst):
+            inst.__dict__.pop(dict_name, None)
+            inst.__class__.objects.cache_obj(inst)
+
+        cls.add_to_class(name, property(getter))
+        cls.add_to_class("reset_%s_cache" % name, reset_cache)
+
+
+class BaseMetaClass(models.Model.__metaclass__):
+    to_denormalize = []
+
+    def __new__(cls, *args, **kwargs):
+        new_cls = super(BaseMetaClass, cls).__new__(cls, *args, **kwargs)
+
+        BaseMetaClass.to_denormalize.extend(
+            [(new_cls, name, field) for name, field in new_cls.__dict__.items() if isinstance(field, DenormalizedField)]
+        )
+
+        return new_cls
+
+    @classmethod
+    def setup_denormalizes(cls):
+        for new_cls, name, field in BaseMetaClass.to_denormalize:
+            field.setup_class(new_cls, name)
+
+
 class BaseModel(models.Model):
+    __metaclass__ = BaseMetaClass
+
     objects = CachedManager()
 
     class Meta:
@@ -68,12 +153,29 @@ class BaseModel(models.Model):
                  if self._original_state.get(k, missing) == missing or self._original_state[k] != v])
 
     def save(self, *args, **kwargs):
-        super(BaseModel, self).save(*args, **kwargs)
+        put_back = [k for k, v in self.__dict__.items() if isinstance(v, models.expressions.ExpressionNode)]
+        super(BaseModel, self).save()
+
+        if put_back:
+            try:
+                self.__dict__.update(
+                    self.__class__.objects.filter(id=self.id).values(*put_back)[0]
+                )
+            except:
+                logging.error("Unable to read %s from %s" % (", ".join(put_back), self.__class__.__name__))
+                self.uncache()
+
         self._original_state = dict(self.__dict__)
-        cache.set(self.cache_key(self.pk), self, 86400)
+        self.cache()
 
-    def delete(self):
+    def cache(self):
+        self.__class__.objects.cache_obj(self)
+
+    def uncache(self):
         cache.delete(self.cache_key(self.pk))
+
+    def delete(self):
+        self.uncache()
         super(BaseModel, self).delete()
 
 
@@ -86,10 +188,7 @@ class UndeletedObjectManager(models.Manager):
     def get_query_set(self):
         return super(UndeletedObjectManager, self).get_query_set().filter(deleted=False)
 
-class GenericContent(BaseModel):
-    """
-        Base class for Vote, Comment and FlaggedItem
-    """
+class GenericContent(models.Model):
     content_type   = models.ForeignKey(ContentType)
     object_id      = models.PositiveIntegerField()
     content_object = generic.GenericForeignKey('content_type', 'object_id')
@@ -126,8 +225,6 @@ class UserContent(models.Model):
         app_label = 'forum'
 
 
-marked_deleted = django.dispatch.Signal(providing_args=["instance", "deleted_by"])
-
 class DeletableContent(models.Model):
     deleted     = models.BooleanField(default=False)
     deleted_at  = models.DateTimeField(null=True, blank=True)
@@ -145,7 +242,6 @@ class DeletableContent(models.Model):
             self.deleted_at = datetime.datetime.now()
             self.deleted_by = user
             self.save()
-            marked_deleted.send(sender=self.__class__, instance=self, deleted_by=user)
             return True
         else:
             return False
@@ -179,19 +275,6 @@ class CancelableContent(models.Model):
 
 from node import Node, NodeRevision
 
-class QandA(Node):
-    wiki                 = models.BooleanField(default=False)
-    wikified_at          = models.DateTimeField(null=True, blank=True)
-
-    class Meta:
-        abstract = True
-        app_label = 'forum'
-
-    def wikify(self):
-        if not self.wiki:
-            self.wiki = True
-            self.wikified_at = datetime.datetime.now()
-            self.save()