]> git.openstreetmap.org Git - osqa.git/blob - forum/models/node.py
72ef0c0737c12efa2a1b04abaa4fc18dd24660cc
[osqa.git] / forum / models / node.py
1 from base import *
2 import re
3 from tag import Tag
4
5 import markdown
6 from django.utils.translation import ugettext as _
7 from django.utils.safestring import mark_safe
8 from django.utils.html import strip_tags
9 from forum.utils.html import sanitize_html
10 from forum.utils.userlinking import auto_user_link
11 from forum.settings import SUMMARY_LENGTH
12 from utils import PickledObjectField
13
14 class NodeContent(models.Model):
15     title      = models.CharField(max_length=300)
16     tagnames   = models.CharField(max_length=125)
17     author     = models.ForeignKey(User, related_name='%(class)ss')
18     body       = models.TextField()
19
20     @property
21     def user(self):
22         return self.author
23
24     @property
25     def html(self):
26         return self.body
27
28     def rendered(self, content):
29         return auto_user_link(self, self._as_markdown(content, *['auto_linker']))
30
31     @classmethod
32     def _as_markdown(cls, content, *extensions):
33         try:
34             return mark_safe(sanitize_html(markdown.markdown(content, extensions=extensions)))
35         except Exception, e:
36             import traceback
37             logging.error("Caught exception %s in markdown parser rendering %s %s:\s %s" % (
38                 str(e), cls.__name__, str(e), traceback.format_exc()))
39             return ''
40
41     def as_markdown(self, *extensions):
42         return self._as_markdown(self.body, *extensions)
43
44     @property
45     def headline(self):
46         title = self.title
47
48         # Replaces multiple spaces with single ones.
49         title = re.sub(' +',' ', title)
50
51         return title
52
53     def tagname_list(self):
54         if self.tagnames:
55             return [name.strip() for name in self.tagnames.split() if name]
56         else:
57             return []
58
59     def tagname_meta_generator(self):
60         return u','.join([tag for tag in self.tagname_list()])
61
62     class Meta:
63         abstract = True
64         app_label = 'forum'
65
66 class NodeMetaClass(BaseMetaClass):
67     types = {}
68
69     def __new__(cls, *args, **kwargs):
70         new_cls = super(NodeMetaClass, cls).__new__(cls, *args, **kwargs)
71
72         if not new_cls._meta.abstract and new_cls.__name__ is not 'Node':
73             NodeMetaClass.types[new_cls.get_type()] = new_cls
74
75         return new_cls
76
77     @classmethod
78     def setup_relations(cls):
79         for node_cls in NodeMetaClass.types.values():
80             NodeMetaClass.setup_relation(node_cls)
81
82     @classmethod
83     def setup_relation(cls, node_cls):
84         name = node_cls.__name__.lower()
85
86         def children(self):
87             return node_cls.objects.filter(parent=self)
88
89         def parent(self):
90             if (self.parent is not None) and self.parent.node_type == name:
91                 return self.parent.leaf
92
93             return None
94
95         Node.add_to_class(name + 's', property(children))
96         Node.add_to_class(name, property(parent))
97
98
99 class NodeQuerySet(CachedQuerySet):
100     def obj_from_datadict(self, datadict):
101         cls = NodeMetaClass.types.get(datadict.get("node_type", ""), None)
102         if cls:
103             obj = cls()
104             obj.__dict__.update(datadict)
105             return obj
106         else:
107             return super(NodeQuerySet, self).obj_from_datadict(datadict)
108
109     def get(self, *args, **kwargs):
110         node = super(NodeQuerySet, self).get(*args, **kwargs).leaf
111
112         if not isinstance(node, self.model):
113             raise self.model.DoesNotExist()
114
115         return node
116
117     def any_state(self, *args):
118         filter = None
119
120         for s in args:
121             s_filter = models.Q(state_string__contains="(%s)" % s)
122             filter = filter and (filter | s_filter) or s_filter
123
124         if filter:
125             return self.filter(filter)
126         else:
127             return self
128
129     def all_states(self, *args):
130         filter = None
131
132         for s in args:
133             s_filter = models.Q(state_string__contains="(%s)" % s)
134             filter = filter and (filter & s_filter) or s_filter
135
136         if filter:
137             return self.filter(filter)
138         else:
139             return self
140
141     def filter_state(self, **kwargs):
142         apply_bool = lambda q, b: b and q or ~q
143         return self.filter(*[apply_bool(models.Q(state_string__contains="(%s)" % s), b) for s, b in kwargs.items()])
144
145     def children_count(self, child_type):
146         return NodeMetaClass.types[child_type].objects.filter_state(deleted=False).filter(parent__in=self).count()
147
148
149 class NodeManager(CachedManager):
150     use_for_related_fields = True
151
152     def get_query_set(self):
153         qs = NodeQuerySet(self.model)
154
155         # If the node is an answer, question or comment we filter the Node model by type
156         if self.model is not Node:
157             qs = qs.filter(node_type=self.model.get_type())
158
159         return qs
160
161     def get_for_types(self, types, *args, **kwargs):
162         kwargs['node_type__in'] = [t.get_type() for t in types]
163         return self.get(*args, **kwargs)
164
165     def filter_state(self, **kwargs):
166         return self.all().filter_state(**kwargs)
167
168
169 class NodeStateDict(object):
170     def __init__(self, node):
171         self.__dict__['_node'] = node
172
173     def __getattr__(self, name):
174         if self.__dict__.get(name, None):
175             return self.__dict__[name]
176
177         try:
178             node = self.__dict__['_node']
179             action = NodeState.objects.get(node=node, state_type=name).action
180             self.__dict__[name] = action
181             return action
182         except:
183             return None
184
185     def __setattr__(self, name, value):
186         current = self.__getattr__(name)
187
188         if value:
189             if current:
190                 current.action = value
191                 current.save()
192             else:
193                 node = self.__dict__['_node']
194                 state = NodeState(node=node, action=value, state_type=name)
195                 state.save()
196                 self.__dict__[name] = value
197
198                 if not "(%s)" % name in node.state_string:
199                     node.state_string = "%s(%s)" % (node.state_string, name)
200                     node.save()
201         else:
202             if current:
203                 node = self.__dict__['_node']
204                 node.state_string = "".join("(%s)" % s for s in re.findall('\w+', node.state_string) if s != name)
205                 node.save()
206                 current.node_state.delete()
207                 del self.__dict__[name]
208
209
210 class NodeStateQuery(object):
211     def __init__(self, node):
212         self.__dict__['_node'] = node
213
214     def __getattr__(self, name):
215         node = self.__dict__['_node']
216         return "(%s)" % name in node.state_string
217
218
219 class Node(BaseModel, NodeContent):
220     __metaclass__ = NodeMetaClass
221
222     node_type            = models.CharField(max_length=16, default='node')
223     parent               = models.ForeignKey('Node', related_name='children', null=True)
224     abs_parent           = models.ForeignKey('Node', related_name='all_children', null=True)
225
226     added_at             = models.DateTimeField(default=datetime.datetime.now)
227     score                 = models.IntegerField(default=0)
228
229     state_string          = models.TextField(default='')
230     last_edited           = models.ForeignKey('Action', null=True, unique=True, related_name="edited_node")
231
232     last_activity_by       = models.ForeignKey(User, null=True)
233     last_activity_at       = models.DateTimeField(null=True, blank=True)
234
235     tags                 = models.ManyToManyField('Tag', related_name='%(class)ss')
236     active_revision       = models.OneToOneField('NodeRevision', related_name='active', null=True)
237
238     extra = PickledObjectField()
239     extra_ref = models.ForeignKey('Node', null=True)
240     extra_count = models.IntegerField(default=0)
241
242     marked = models.BooleanField(default=False)
243
244     comment_count = DenormalizedField("children", node_type="comment", canceled=False)
245     flag_count = DenormalizedField("flags")
246
247     friendly_name = _("post")
248
249     objects = NodeManager()
250
251     def __unicode__(self):
252         return self.headline
253
254     @classmethod
255     def _generate_cache_key(cls, key, group="node"):
256         return super(Node, cls)._generate_cache_key(key, group)
257         
258     @classmethod
259     def get_type(cls):
260         return cls.__name__.lower()
261
262     @property
263     def leaf(self):
264         leaf_cls = NodeMetaClass.types.get(self.node_type, None)
265
266         if leaf_cls is None:
267             return self
268
269         leaf = leaf_cls()
270         leaf.__dict__ = self.__dict__
271         return leaf
272
273     @property
274     def nstate(self):
275         state = self.__dict__.get('_nstate', None)
276
277         if state is None:
278             state = NodeStateDict(self)
279             self._nstate = state
280
281         return state
282
283     @property
284     def nis(self):
285         nis = self.__dict__.get('_nis', None)
286
287         if nis is None:
288             nis = NodeStateQuery(self)
289             self._nis = nis
290
291         return nis
292
293     @property
294     def last_activity(self):
295         try:
296             return self.actions.order_by('-action_date')[0].action_date
297         except:
298             return self.last_seen
299
300     @property
301     def state_list(self):
302         return [s.state_type for s in self.states.all()]
303
304     @property
305     def deleted(self):
306         return self.nis.deleted
307
308     @property
309     def absolute_parent(self):
310         if not self.abs_parent_id:
311             return self
312
313         return self.abs_parent
314
315     @property
316     def summary(self):
317         content = strip_tags(self.html)[:SUMMARY_LENGTH]
318
319         # Remove multiple spaces.
320         content = re.sub(' +',' ', content)
321
322         # Remove line breaks. We don't need them at all.
323         content = content.replace("\n", '')
324
325         return content
326
327     @models.permalink
328     def get_revisions_url(self):
329         return ('revisions', (), {'id': self.id})
330
331     def update_last_activity(self, user, save=False, time=None):
332         if not time:
333             time = datetime.datetime.now()
334
335         self.last_activity_by = user
336         self.last_activity_at = time
337
338         if self.parent:
339             self.parent.update_last_activity(user, save=True, time=time)
340
341         if save:
342             self.save()
343
344     def _create_revision(self, user, number, **kwargs):
345         revision = NodeRevision(author=user, revision=number, node=self, **kwargs)
346         revision.save()
347         return revision
348
349     def create_revision(self, user, **kwargs):
350         number = self.revisions.aggregate(last=models.Max('revision'))['last'] + 1
351         revision = self._create_revision(user, number, **kwargs)
352         self.activate_revision(user, revision)
353         return revision
354
355     def activate_revision(self, user, revision):
356         self.title = revision.title
357         self.tagnames = revision.tagnames
358
359         self.body = self.rendered(revision.body)
360
361         self.active_revision = revision
362
363         # Try getting the previous revision
364         try:
365             prev_revision = NodeRevision.objects.get(node=self, revision=revision.revision-1)
366
367             update_activity = True
368
369             # Do not update the activity if only the tags are changed
370             if prev_revision.title == revision.title and prev_revision.body == revision.body \
371             and prev_revision.tagnames != revision.tagnames and not settings.UPDATE_LATEST_ACTIVITY_ON_TAG_EDIT:
372                 update_activity = False
373         except NodeRevision.DoesNotExist:
374             update_activity = True
375         finally:
376             if update_activity:
377                 self.update_last_activity(user)
378
379         self.save()
380
381     def get_active_users(self, active_users = None):
382         if not active_users:
383             active_users = set()
384
385         active_users.add(self.author)
386
387         for node in self.children.all():
388             if not node.nis.deleted:
389                 node.get_active_users(active_users)
390
391         return active_users
392
393     def get_last_edited(self):
394         if not self.last_edited:
395             try:
396                 le = self.actions.exclude(action_type__in=('voteup', 'votedown', 'flag'), canceled=True).order_by('-action_date')[0]
397                 self.last_edited = le
398                 self.save()
399             except:
400                 pass
401
402         return self.last_edited
403
404
405     def _list_changes_in_tags(self):
406         dirty = self.get_dirty_fields()
407
408         if not 'tagnames' in dirty:
409             return None
410         else:
411             if self._original_state['tagnames']:
412                 old_tags = set(self._original_state['tagnames'].split())
413             else:
414                 old_tags = set()
415             new_tags = set(self.tagnames.split())
416
417             return dict(
418                     current=list(new_tags),
419                     added=list(new_tags - old_tags),
420                     removed=list(old_tags - new_tags)
421                     )
422
423     def _last_active_user(self):
424         return self.last_edited and self.last_edited.by or self.author
425
426     def _process_changes_in_tags(self):
427         tag_changes = self._list_changes_in_tags()
428
429         if tag_changes is not None:
430             for name in tag_changes['added']:
431                 try:
432                     tag = Tag.objects.get(name=name)
433                 except Tag.DoesNotExist:
434                     tag = Tag.objects.create(name=name, created_by=self._last_active_user())
435
436                 if not self.nis.deleted:
437                     tag.add_to_usage_count(1)
438                     tag.save()
439
440             if not self.nis.deleted:
441                 for name in tag_changes['removed']:
442                     try:
443                         tag = Tag.objects.get(name=name)
444                         tag.add_to_usage_count(-1)
445                         tag.save()
446                     except:
447                         pass
448
449             return True
450
451         return False
452
453     def mark_deleted(self, action):
454         self.nstate.deleted = action
455         self.save()
456
457         if action:
458             for tag in self.tags.all():
459                 tag.add_to_usage_count(-1)
460                 tag.save()
461         else:
462             for tag in Tag.objects.filter(name__in=self.tagname_list()):
463                 tag.add_to_usage_count(1)
464                 tag.save()
465
466     def delete(self, *args, **kwargs):
467         for tag in self.tags.all():
468             tag.add_to_usage_count(-1)
469             tag.save()
470
471         self.active_revision = None
472         self.save()
473
474         for n in self.children.all():
475             n.delete()
476
477         for a in self.actions.all():
478             a.cancel()
479
480         super(Node, self).delete(*args, **kwargs)
481
482     def save(self, *args, **kwargs):
483         if not self.id:
484             self.node_type = self.get_type()
485             super(BaseModel, self).save(*args, **kwargs)
486             self.active_revision = self._create_revision(self.author, 1, title=self.title, tagnames=self.tagnames,
487                                                          body=self.body)
488             self.activate_revision(self.author, self.active_revision)
489             self.update_last_activity(self.author, time=self.added_at)
490
491         if self.parent_id and not self.abs_parent_id:
492             self.abs_parent = self.parent.absolute_parent
493         
494         tags_changed = self._process_changes_in_tags()
495         
496         super(Node, self).save(*args, **kwargs)
497         if tags_changed:
498             if self.tagnames.strip():
499                 self.tags = list(Tag.objects.filter(name__in=self.tagname_list()))
500             else:
501                 self.tags = []
502
503     class Meta:
504         app_label = 'forum'
505
506
507 class NodeRevision(BaseModel, NodeContent):
508     node       = models.ForeignKey(Node, related_name='revisions')
509     summary    = models.CharField(max_length=300)
510     revision   = models.PositiveIntegerField()
511     revised_at = models.DateTimeField(default=datetime.datetime.now)
512
513     class Meta:
514         unique_together = ('node', 'revision')
515         app_label = 'forum'
516
517
518 class NodeState(models.Model):
519     node       = models.ForeignKey(Node, related_name='states')
520     state_type = models.CharField(max_length=16)
521     action     = models.OneToOneField('Action', related_name="node_state")
522
523     class Meta:
524         unique_together = ('node', 'state_type')
525         app_label = 'forum'
526
527