]> git.openstreetmap.org Git - osqa.git/blob - forum/models/base.py
Fixes a possible future bug in edit user.
[osqa.git] / forum / models / base.py
1 import datetime
2 import re
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
18 import logging
19
20
21 class LazyQueryList(object):
22     def __init__(self, model, items):
23         self.model = model
24         self.items = items
25
26     def __getitem__(self, k):
27         return self.model.objects.get(id=self.items[k])
28
29     def __iter__(self):
30         for id in self.items:
31             yield self.model.objects.get(id=id)
32
33     def __len__(self):
34         return len(self.items)
35
36 class CachedQuerySet(models.query.QuerySet):
37     def lazy(self):
38         if len(self.query.extra) == 0:
39             return LazyQueryList(self.model, list(self.values_list('id', flat=True)))
40         else:
41             return self
42
43     def obj_from_datadict(self, datadict):
44         obj = self.model()
45         obj.__dict__.update(datadict)
46         return obj
47
48     def get(self, *args, **kwargs):
49         key = self.model.infer_cache_key(kwargs)
50
51         if key is not None:
52             obj = cache.get(key)
53
54             if obj is None:
55                 obj = super(CachedQuerySet, self).get(*args, **kwargs)
56                 obj.cache()
57             else:
58                 obj = self.obj_from_datadict(obj)
59                 obj.reset_original_state()
60
61             return obj
62
63         return super(CachedQuerySet, self).get(*args, **kwargs) 
64
65 class CachedManager(models.Manager):
66     use_for_related_fields = True
67
68     def get_query_set(self):
69         return CachedQuerySet(self.model)
70
71     def get_or_create(self, *args, **kwargs):
72         try:
73             return self.get(*args, **kwargs)
74         except:
75             return super(CachedManager, self).get_or_create(*args, **kwargs)
76
77
78 class DenormalizedField(object):
79     def __init__(self, manager, *args, **kwargs):
80         self.manager = manager
81         self.filter = (args, kwargs)
82
83     def setup_class(self, cls, name):
84         dict_name = '_%s_dencache_' % name
85
86         def getter(inst):
87             val = inst.__dict__.get(dict_name, None)
88
89             if val is None:
90                 val = getattr(inst, self.manager).filter(*self.filter[0], **self.filter[1]).count()
91                 inst.__dict__[dict_name] = val
92                 inst.cache()
93
94             return val
95
96         def reset_cache(inst):
97             inst.__dict__.pop(dict_name, None)
98             inst.uncache()
99
100         cls.add_to_class(name, property(getter))
101         cls.add_to_class("reset_%s_cache" % name, reset_cache)
102
103
104 class BaseMetaClass(models.Model.__metaclass__):
105     to_denormalize = []
106
107     def __new__(cls, *args, **kwargs):
108         new_cls = super(BaseMetaClass, cls).__new__(cls, *args, **kwargs)
109
110         BaseMetaClass.to_denormalize.extend(
111             [(new_cls, name, field) for name, field in new_cls.__dict__.items() if isinstance(field, DenormalizedField)]
112         )
113
114         return new_cls
115
116     @classmethod
117     def setup_denormalizes(cls):
118         for new_cls, name, field in BaseMetaClass.to_denormalize:
119             field.setup_class(new_cls, name)
120
121
122 class BaseModel(models.Model):
123     __metaclass__ = BaseMetaClass
124
125     objects = CachedManager()
126
127     class Meta:
128         abstract = True
129         app_label = 'forum'
130
131     def __init__(self, *args, **kwargs):
132         super(BaseModel, self).__init__(*args, **kwargs)
133         self.reset_original_state(kwargs.keys())
134
135     def reset_original_state(self, reset_fields=None):
136         self._original_state = self._as_dict()
137         
138         if reset_fields:
139             self._original_state.update(dict([(f, None) for f in reset_fields]))
140
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]]
143
144     def _as_dict(self):
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_')])
147         ])
148
149     def _get_update_kwargs(self):
150         return dict([
151             (f.name, getattr(self, f.name)) for f in self._meta.fields if self._original_state[f.attname] != self.__dict__[f.attname]
152         ])
153
154     def save(self, *args, **kwargs):
155         put_back = [k for k, v in self.__dict__.items() if isinstance(v, models.expressions.ExpressionNode)]
156
157         if self.id:
158             self.__class__.objects.filter(id=self.id).update(**self._get_update_kwargs())
159         else:
160             super(BaseModel, self).save()
161
162         if put_back:
163             try:
164                 self.__dict__.update(
165                     self.__class__.objects.filter(id=self.id).values(*put_back)[0]
166                 )
167             except:
168                 logging.error("Unable to read %s from %s" % (", ".join(put_back), self.__class__.__name__))
169                 self.uncache()
170
171         self.reset_original_state()
172         self.cache()
173
174     @classmethod
175     def _generate_cache_key(cls, key, group=None):
176         if group is None:
177             group = cls.__name__
178
179         return '%s:%s:%s' % (settings.APP_URL, group, key)
180
181     def cache_key(self):
182         return self._generate_cache_key(self.id)
183
184     @classmethod
185     def infer_cache_key(cls, querydict):
186         try:
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]
189
190             return cls._generate_cache_key(pk)
191         except:
192             return None
193
194     def cache(self):
195         cache.set(self.cache_key(), self._as_dict(), 60 * 60)
196
197     def uncache(self):
198         cache.delete(self.cache_key())
199
200     def delete(self):
201         self.uncache()
202         super(BaseModel, self).delete()
203
204
205 from user import User
206 from node import Node, NodeRevision, NodeManager
207 from action import Action
208
209
210
211
212