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