]> git.openstreetmap.org Git - osqa.git/blob - forum/models/base.py
A bug in the after registration redirection.
[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         try:
50             pk = [v for (k,v) in kwargs.items() if k in ('pk', 'pk__exact', 'id', 'id__exact'
51                             ) or k.endswith('_ptr__pk') or k.endswith('_ptr__id')][0]
52         except:
53             pk = None
54
55         if pk is not None:
56             key = self.model.cache_key(pk)
57             obj = cache.get(key)
58
59             if obj is None:
60                 obj = super(CachedQuerySet, self).get(*args, **kwargs)
61                 obj.cache()
62             else:
63                 obj = self.obj_from_datadict(obj)
64                 obj.reset_original_state()
65
66             return obj
67
68         return super(CachedQuerySet, self).get(*args, **kwargs) 
69
70 class CachedManager(models.Manager):
71     use_for_related_fields = True
72
73     def get_query_set(self):
74         return CachedQuerySet(self.model)
75
76     def get_or_create(self, *args, **kwargs):
77         try:
78             return self.get(*args, **kwargs)
79         except:
80             return super(CachedManager, self).get_or_create(*args, **kwargs)
81
82
83 class DenormalizedField(object):
84     def __init__(self, manager, *args, **kwargs):
85         self.manager = manager
86         self.filter = (args, kwargs)
87
88     def setup_class(self, cls, name):
89         dict_name = '_%s_dencache_' % name
90
91         def getter(inst):
92             val = inst.__dict__.get(dict_name, None)
93
94             if val is None:
95                 val = getattr(inst, self.manager).filter(*self.filter[0], **self.filter[1]).count()
96                 inst.__dict__[dict_name] = val
97                 inst.cache()
98
99             return val
100
101         def reset_cache(inst):
102             inst.__dict__.pop(dict_name, None)
103             inst.uncache()
104
105         cls.add_to_class(name, property(getter))
106         cls.add_to_class("reset_%s_cache" % name, reset_cache)
107
108
109 class BaseMetaClass(models.Model.__metaclass__):
110     to_denormalize = []
111
112     def __new__(cls, *args, **kwargs):
113         new_cls = super(BaseMetaClass, cls).__new__(cls, *args, **kwargs)
114
115         BaseMetaClass.to_denormalize.extend(
116             [(new_cls, name, field) for name, field in new_cls.__dict__.items() if isinstance(field, DenormalizedField)]
117         )
118
119         return new_cls
120
121     @classmethod
122     def setup_denormalizes(cls):
123         for new_cls, name, field in BaseMetaClass.to_denormalize:
124             field.setup_class(new_cls, name)
125
126
127 class BaseModel(models.Model):
128     __metaclass__ = BaseMetaClass
129
130     objects = CachedManager()
131
132     class Meta:
133         abstract = True
134         app_label = 'forum'
135
136     def __init__(self, *args, **kwargs):
137         super(BaseModel, self).__init__(*args, **kwargs)
138         self.reset_original_state(kwargs.keys())
139
140     @classmethod
141     def cache_key(cls, pk):
142         return '%s:%s:%s' % (settings.APP_URL, cls.__name__, pk)
143
144     def reset_original_state(self, reset_fields=None):
145         self._original_state = self._as_dict()
146         
147         if reset_fields:
148             self._original_state.update(dict([(f, None) for f in reset_fields]))
149
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]]
152
153     def _as_dict(self):
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_')])
156         ])
157
158     def _get_update_kwargs(self):
159         return dict([
160             (f.name, getattr(self, f.name)) for f in self._meta.fields if self._original_state[f.attname] != self.__dict__[f.attname]
161         ])
162
163     def save(self, *args, **kwargs):
164         put_back = [k for k, v in self.__dict__.items() if isinstance(v, models.expressions.ExpressionNode)]
165
166         if self.id:
167             self.__class__.objects.filter(id=self.id).update(**self._get_update_kwargs())
168         else:
169             super(BaseModel, self).save()
170
171         if put_back:
172             try:
173                 self.__dict__.update(
174                     self.__class__.objects.filter(id=self.id).values(*put_back)[0]
175                 )
176             except:
177                 logging.error("Unable to read %s from %s" % (", ".join(put_back), self.__class__.__name__))
178                 self.uncache()
179
180         self.reset_original_state()
181         self.cache()
182
183     def cache(self):
184         cache.set(self.cache_key(self.id), self._as_dict(), 60 * 60)
185
186     def uncache(self):
187         cache.delete(self.cache_key(self.id))
188
189     def delete(self):
190         self.uncache()
191         super(BaseModel, self).delete()
192
193
194 from user import User
195 from node import Node, NodeRevision, NodeManager
196 from action import Action
197
198
199
200
201