3 from urllib import quote_plus, urlencode
4 from django.db import models, IntegrityError, connection, transaction
5 from django.utils.http import urlquote as django_urlquote
6 from django.utils.html import strip_tags
7 from django.core.urlresolvers import reverse
8 from django.contrib.contenttypes import generic
9 from django.contrib.contenttypes.models import ContentType
10 from django.core.cache import cache
11 from django.template.defaultfilters import slugify
12 from django.db.models.signals import post_delete, post_save, pre_save, pre_delete
13 from django.utils.translation import ugettext as _
14 from django.utils.safestring import mark_safe
15 from django.contrib.sitemaps import ping_google
16 import django.dispatch
17 from forum import settings
21 class LazyQueryList(object):
22 def __init__(self, model, items):
26 def __getitem__(self, k):
27 return self.model.objects.get(id=self.items[k][0])
31 yield self.model.objects.get(id=id[0])
34 return len(self.items)
36 class CachedQuerySet(models.query.QuerySet):
39 if not len(self.query.aggregates):
42 if len(self.query.extra):
43 extra_keys = self.query.extra.keys()
44 values_list += extra_keys
46 return LazyQueryList(self.model, list(self.values_list(*values_list)))
48 if len(self.query.extra):
49 print self.query.extra
52 def obj_from_datadict(self, datadict):
54 obj.__dict__.update(datadict)
57 def get(self, *args, **kwargs):
58 key = self.model.infer_cache_key(kwargs)
64 obj = super(CachedQuerySet, self).get(*args, **kwargs)
67 obj = self.obj_from_datadict(obj)
68 obj.reset_original_state()
72 return super(CachedQuerySet, self).get(*args, **kwargs)
74 class CachedManager(models.Manager):
75 use_for_related_fields = True
77 def get_query_set(self):
78 return CachedQuerySet(self.model)
80 def get_or_create(self, *args, **kwargs):
82 return self.get(*args, **kwargs)
84 return super(CachedManager, self).get_or_create(*args, **kwargs)
87 class DenormalizedField(object):
88 def __init__(self, manager, *args, **kwargs):
89 self.manager = manager
90 self.filter = (args, kwargs)
92 def setup_class(self, cls, name):
93 dict_name = '_%s_dencache_' % name
96 val = inst.__dict__.get(dict_name, None)
99 val = getattr(inst, self.manager).filter(*self.filter[0], **self.filter[1]).count()
100 inst.__dict__[dict_name] = val
105 def reset_cache(inst):
106 inst.__dict__.pop(dict_name, None)
109 cls.add_to_class(name, property(getter))
110 cls.add_to_class("reset_%s_cache" % name, reset_cache)
113 class BaseMetaClass(models.Model.__metaclass__):
116 def __new__(cls, *args, **kwargs):
117 new_cls = super(BaseMetaClass, cls).__new__(cls, *args, **kwargs)
119 BaseMetaClass.to_denormalize.extend(
120 [(new_cls, name, field) for name, field in new_cls.__dict__.items() if isinstance(field, DenormalizedField)]
126 def setup_denormalizes(cls):
127 for new_cls, name, field in BaseMetaClass.to_denormalize:
128 field.setup_class(new_cls, name)
131 class BaseModel(models.Model):
132 __metaclass__ = BaseMetaClass
134 objects = CachedManager()
140 def __init__(self, *args, **kwargs):
141 super(BaseModel, self).__init__(*args, **kwargs)
142 self.reset_original_state(kwargs.keys())
144 def reset_original_state(self, reset_fields=None):
145 self._original_state = self._as_dict()
148 self._original_state.update(dict([(f, None) for f in reset_fields]))
150 def get_dirty_fields(self):
151 return [f.name for f in self._meta.fields if self._original_state[f.attname] != self.__dict__[f.attname]]
154 return dict([(name, getattr(self, name)) for name in
155 ([f.attname for f in self._meta.fields] + [k for k in self.__dict__.keys() if k.endswith('_dencache_')])
158 def _get_update_kwargs(self):
160 (f.name, getattr(self, f.name)) for f in self._meta.fields if self._original_state[f.attname] != self.__dict__[f.attname]
163 def save(self, full_save=False, *args, **kwargs):
164 put_back = [k for k, v in self.__dict__.items() if isinstance(v, models.expressions.ExpressionNode)]
166 if self.id and not full_save:
167 self.__class__.objects.filter(id=self.id).update(**self._get_update_kwargs())
169 super(BaseModel, self).save()
173 self.__dict__.update(
174 self.__class__.objects.filter(id=self.id).values(*put_back)[0]
177 logging.error("Unable to read %s from %s" % (", ".join(put_back), self.__class__.__name__))
180 self.reset_original_state()
184 def _generate_cache_key(cls, key, group=None):
188 return '%s:%s:%s' % (settings.APP_URL, group, key)
191 return self._generate_cache_key(self.id)
194 def infer_cache_key(cls, querydict):
196 pk = [v for (k,v) in querydict.items() if k in ('pk', 'pk__exact', 'id', 'id__exact'
197 ) or k.endswith('_ptr__pk') or k.endswith('_ptr__id')][0]
199 return cls._generate_cache_key(pk)
204 cache.set(self.cache_key(), self._as_dict(), 60 * 60)
207 cache.delete(self.cache_key())
211 super(BaseModel, self).delete()
214 from user import User
215 from node import Node, NodeRevision, NodeManager
216 from action import Action