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])
31 yield self.model.objects.get(id=id)
34 return len(self.items)
36 class CachedQuerySet(models.query.QuerySet):
38 if (not len(self.query.extra)) and (not len(self.query.aggregates)):
39 return LazyQueryList(self.model, list(self.values_list('id', flat=True)))
43 def obj_from_datadict(self, datadict):
45 obj.__dict__.update(datadict)
48 def get(self, *args, **kwargs):
49 key = self.model.infer_cache_key(kwargs)
55 obj = super(CachedQuerySet, self).get(*args, **kwargs)
58 obj = self.obj_from_datadict(obj)
59 obj.reset_original_state()
63 return super(CachedQuerySet, self).get(*args, **kwargs)
65 class CachedManager(models.Manager):
66 use_for_related_fields = True
68 def get_query_set(self):
69 return CachedQuerySet(self.model)
71 def get_or_create(self, *args, **kwargs):
73 return self.get(*args, **kwargs)
75 return super(CachedManager, self).get_or_create(*args, **kwargs)
78 class DenormalizedField(object):
79 def __init__(self, manager, *args, **kwargs):
80 self.manager = manager
81 self.filter = (args, kwargs)
83 def setup_class(self, cls, name):
84 dict_name = '_%s_dencache_' % name
87 val = inst.__dict__.get(dict_name, None)
90 val = getattr(inst, self.manager).filter(*self.filter[0], **self.filter[1]).count()
91 inst.__dict__[dict_name] = val
96 def reset_cache(inst):
97 inst.__dict__.pop(dict_name, None)
100 cls.add_to_class(name, property(getter))
101 cls.add_to_class("reset_%s_cache" % name, reset_cache)
104 class BaseMetaClass(models.Model.__metaclass__):
107 def __new__(cls, *args, **kwargs):
108 new_cls = super(BaseMetaClass, cls).__new__(cls, *args, **kwargs)
110 BaseMetaClass.to_denormalize.extend(
111 [(new_cls, name, field) for name, field in new_cls.__dict__.items() if isinstance(field, DenormalizedField)]
117 def setup_denormalizes(cls):
118 for new_cls, name, field in BaseMetaClass.to_denormalize:
119 field.setup_class(new_cls, name)
122 class BaseModel(models.Model):
123 __metaclass__ = BaseMetaClass
125 objects = CachedManager()
131 def __init__(self, *args, **kwargs):
132 super(BaseModel, self).__init__(*args, **kwargs)
133 self.reset_original_state(kwargs.keys())
135 def reset_original_state(self, reset_fields=None):
136 self._original_state = self._as_dict()
139 self._original_state.update(dict([(f, None) for f in reset_fields]))
141 def get_dirty_fields(self):
142 return [f.name for f in self._meta.fields if self._original_state[f.attname] != self.__dict__[f.attname]]
145 return dict([(name, getattr(self, name)) for name in
146 ([f.attname for f in self._meta.fields] + [k for k in self.__dict__.keys() if k.endswith('_dencache_')])
149 def _get_update_kwargs(self):
151 (f.name, getattr(self, f.name)) for f in self._meta.fields if self._original_state[f.attname] != self.__dict__[f.attname]
154 def save(self, full_save=False, *args, **kwargs):
155 put_back = [k for k, v in self.__dict__.items() if isinstance(v, models.expressions.ExpressionNode)]
157 if self.id and not full_save:
158 self.__class__.objects.filter(id=self.id).update(**self._get_update_kwargs())
160 super(BaseModel, self).save()
164 self.__dict__.update(
165 self.__class__.objects.filter(id=self.id).values(*put_back)[0]
168 logging.error("Unable to read %s from %s" % (", ".join(put_back), self.__class__.__name__))
171 self.reset_original_state()
175 def _generate_cache_key(cls, key, group=None):
179 return '%s:%s:%s' % (settings.APP_URL, group, key)
182 return self._generate_cache_key(self.id)
185 def infer_cache_key(cls, querydict):
187 pk = [v for (k,v) in querydict.items() if k in ('pk', 'pk__exact', 'id', 'id__exact'
188 ) or k.endswith('_ptr__pk') or k.endswith('_ptr__id')][0]
190 return cls._generate_cache_key(pk)
195 cache.set(self.cache_key(), self._as_dict(), 60 * 60)
198 cache.delete(self.cache_key())
202 super(BaseModel, self).delete()
205 from user import User
206 from node import Node, NodeRevision, NodeManager
207 from action import Action