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