]> git.openstreetmap.org Git - osqa.git/blob - forum/models/base.py
56124a82e9a66be5d37f5fee8f883e30c4a0dcc3
[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 django.conf import settings
18 from forum import const
19 import logging
20
21 from forum.const import *
22
23 class LazyQueryList(object):
24     def __init__(self, model, items):
25         self.model = model
26         self.items = items
27
28     def __getitem__(self, k):
29         return self.model.objects.get(id=self.items[k])
30
31     def __iter__(self):
32         for id in self.items:
33             yield self.model.objects.get(id=id)
34
35     def __len__(self):
36         return len(self.items)
37
38 class CachedQuerySet(models.query.QuerySet):
39     def lazy(self):
40         if len(self.query.extra) == 0:
41             return LazyQueryList(self.model, list(self.values_list('id', flat=True)))
42         else:
43             return self
44
45 from action import Action
46
47 class CachedManager(models.Manager):
48     use_for_related_fields = True
49     int_cache_re = re.compile('^_[\w_]+cache$')
50
51     def get_query_set(self):
52         return CachedQuerySet(self.model)
53
54     def cache_obj(self, obj):
55         int_cache_keys = [k for k in obj.__dict__.keys() if self.int_cache_re.match(k)]
56         d = obj.__dict__
57         for k in int_cache_keys:
58             if not isinstance(obj.__dict__[k], Action):
59                 del obj.__dict__[k]
60
61         cache.set(self.model.cache_key(obj.id), obj, 60 * 60)
62
63     def get(self, *args, **kwargs):
64         try:
65             pk = [v for (k,v) in kwargs.items() if k in ('pk', 'pk__exact', 'id', 'id__exact'
66                             ) or k.endswith('_ptr__pk') or k.endswith('_ptr__id')][0]
67         except:
68             pk = None
69
70         if pk is not None:
71             key = self.model.cache_key(pk)
72             obj = cache.get(key)
73
74             if obj is None:
75                 obj = super(CachedManager, self).get(*args, **kwargs)
76                 self.cache_obj(obj)
77             else:
78                 d = obj.__dict__
79
80             return obj
81         
82         return super(CachedManager, self).get(*args, **kwargs)
83
84     def get_or_create(self, *args, **kwargs):
85         try:
86             return self.get(*args, **kwargs)
87         except:
88             return super(CachedManager, self).get_or_create(*args, **kwargs)
89
90
91 class DenormalizedField(object):
92     def __init__(self, manager, **kwargs):
93         self.manager = manager
94         self.filter = kwargs
95
96     def setup_class(self, cls, name):
97         dict_name = '_%s_cache_' % name
98
99         def getter(inst):
100             val = inst.__dict__.get(dict_name, None)
101
102             if val is None:
103                 val = getattr(inst, self.manager).filter(**self.filter).count()
104                 inst.__dict__[dict_name] = val
105                 inst.__class__.objects.cache_obj(inst)
106
107             return val
108
109         def reset_cache(inst):
110             inst.__dict__.pop(dict_name, None)
111             inst.__class__.objects.cache_obj(inst)
112
113         cls.add_to_class(name, property(getter))
114         cls.add_to_class("reset_%s_cache" % name, reset_cache)
115
116
117 class BaseMetaClass(models.Model.__metaclass__):
118     to_denormalize = []
119
120     def __new__(cls, *args, **kwargs):
121         new_cls = super(BaseMetaClass, cls).__new__(cls, *args, **kwargs)
122
123         BaseMetaClass.to_denormalize.extend(
124             [(new_cls, name, field) for name, field in new_cls.__dict__.items() if isinstance(field, DenormalizedField)]
125         )
126
127         return new_cls
128
129     @classmethod
130     def setup_denormalizes(cls):
131         for new_cls, name, field in BaseMetaClass.to_denormalize:
132             field.setup_class(new_cls, name)
133
134
135 class BaseModel(models.Model):
136     __metaclass__ = BaseMetaClass
137
138     objects = CachedManager()
139
140     class Meta:
141         abstract = True
142         app_label = 'forum'
143
144     def __init__(self, *args, **kwargs):
145         super(BaseModel, self).__init__(*args, **kwargs)
146         self._original_state = dict([(k, v) for k,v in self.__dict__.items() if not k in kwargs])
147
148     @classmethod
149     def cache_key(cls, pk):
150         return '%s.%s:%s' % (settings.APP_URL, cls.__name__, pk)
151
152     def get_dirty_fields(self):
153         missing = object()
154         return dict([(k, self._original_state.get(k, None)) for k,v in self.__dict__.items()
155                  if self._original_state.get(k, missing) == missing or self._original_state[k] != v])
156
157     def save(self, *args, **kwargs):
158         put_back = [k for k, v in self.__dict__.items() if isinstance(v, models.expressions.ExpressionNode)]
159         super(BaseModel, self).save()
160
161         if put_back:
162             try:
163                 self.__dict__.update(
164                     self.__class__.objects.filter(id=self.id).values(*put_back)[0]
165                 )
166             except:
167                 logging.error("Unable to read %s from %s" % (", ".join(put_back), self.__class__.__name__))
168                 self.uncache()
169
170         self._original_state = dict(self.__dict__)
171         self.cache()
172
173     def cache(self):
174         self.__class__.objects.cache_obj(self)
175
176     def uncache(self):
177         cache.delete(self.cache_key(self.pk))
178
179     def delete(self):
180         self.uncache()
181         super(BaseModel, self).delete()
182
183
184 class ActiveObjectManager(models.Manager):
185     use_for_related_fields = True
186     def get_query_set(self):
187         return super(ActiveObjectManager, self).get_query_set().filter(canceled=False)
188
189 class UndeletedObjectManager(models.Manager):
190     def get_query_set(self):
191         return super(UndeletedObjectManager, self).get_query_set().filter(deleted=False)
192
193 class GenericContent(models.Model):
194     content_type   = models.ForeignKey(ContentType)
195     object_id      = models.PositiveIntegerField()
196     content_object = generic.GenericForeignKey('content_type', 'object_id')
197
198     class Meta:
199         abstract = True
200         app_label = 'forum'
201
202 class MetaContent(BaseModel):
203     node = models.ForeignKey('Node', null=True, related_name='%(class)ss')
204
205     def __init__(self, *args, **kwargs):
206         if 'content_object' in kwargs:
207             kwargs['node'] = kwargs['content_object']
208             del kwargs['content_object']
209
210         super (MetaContent, self).__init__(*args, **kwargs)
211     
212     @property
213     def content_object(self):
214         return self.node.leaf
215
216     class Meta:
217         abstract = True
218         app_label = 'forum'
219
220 from user import User
221
222 class UserContent(models.Model):
223     user = models.ForeignKey(User, related_name='%(class)ss')
224
225     class Meta:
226         abstract = True
227         app_label = 'forum'
228
229
230 class DeletableContent(models.Model):
231     deleted     = models.BooleanField(default=False)
232     deleted_at  = models.DateTimeField(null=True, blank=True)
233     deleted_by  = models.ForeignKey(User, null=True, blank=True, related_name='deleted_%(class)ss')
234
235     active = UndeletedObjectManager()
236
237     class Meta:
238         abstract = True
239         app_label = 'forum'
240
241     def mark_deleted(self, user):
242         if not self.deleted:
243             self.deleted = True
244             self.deleted_at = datetime.datetime.now()
245             self.deleted_by = user
246             self.save()
247             return True
248         else:
249             return False
250
251     def unmark_deleted(self):
252         if self.deleted:
253             self.deleted = False
254             self.save()
255             return True
256         else:
257             return False
258
259 mark_canceled = django.dispatch.Signal(providing_args=['instance'])
260
261 class CancelableContent(models.Model):
262     canceled = models.BooleanField(default=False)
263
264     def cancel(self):
265         if not self.canceled:
266             self.canceled = True
267             self.save()
268             mark_canceled.send(sender=self.__class__, instance=self)
269             return True
270             
271         return False
272
273     class Meta:
274         abstract = True
275         app_label = 'forum'
276
277
278 from node import Node, NodeRevision
279
280
281
282
283