]> git.openstreetmap.org Git - osqa.git/blob - forum/models/node.py
bf82708cdf604644ec0c7a2b5e76a71708d0d9bb
[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(u' ') 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         self.update_last_activity(user)
363
364         self.save()
365
366     def get_active_users(self, active_users = None):
367         if not active_users:
368             active_users = set()
369
370         active_users.add(self.author)
371
372         for node in self.children.all():
373             if not node.nis.deleted:
374                 node.get_active_users(active_users)
375
376         return active_users
377
378     def _list_changes_in_tags(self):
379         dirty = self.get_dirty_fields()
380
381         if not 'tagnames' in dirty:
382             return None
383         else:
384             if self._original_state['tagnames']:
385                 old_tags = set(self._original_state['tagnames'].split())
386             else:
387                 old_tags = set()
388             new_tags = set(self.tagnames.split())
389
390             return dict(
391                     current=list(new_tags),
392                     added=list(new_tags - old_tags),
393                     removed=list(old_tags - new_tags)
394                     )
395
396     def _last_active_user(self):
397         return self.last_edited and self.last_edited.by or self.author
398
399     def _process_changes_in_tags(self):
400         tag_changes = self._list_changes_in_tags()
401
402         if tag_changes is not None:
403             for name in tag_changes['added']:
404                 try:
405                     tag = Tag.objects.get(name=name)
406                 except Tag.DoesNotExist:
407                     tag = Tag.objects.create(name=name, created_by=self._last_active_user())
408
409                 if not self.nis.deleted:
410                     tag.add_to_usage_count(1)
411                     tag.save()
412
413             if not self.nis.deleted:
414                 for name in tag_changes['removed']:
415                     try:
416                         tag = Tag.objects.get(name=name)
417                         tag.add_to_usage_count(-1)
418                         tag.save()
419                     except:
420                         pass
421
422             return True
423
424         return False
425
426     def mark_deleted(self, action):
427         self.nstate.deleted = action
428         self.save()
429
430         if action:
431             for tag in self.tags.all():
432                 tag.add_to_usage_count(-1)
433                 tag.save()
434         else:
435             for tag in Tag.objects.filter(name__in=self.tagname_list()):
436                 tag.add_to_usage_count(1)
437                 tag.save()
438
439     def delete(self, *args, **kwargs):
440         for tag in self.tags.all():
441             tag.add_to_usage_count(-1)
442             tag.save()
443
444         self.active_revision = None
445         self.save()
446
447         for n in self.children.all():
448             n.delete()
449
450         for a in self.actions.all():
451             a.cancel()
452
453         super(Node, self).delete(*args, **kwargs)
454
455     def save(self, *args, **kwargs):
456         if not self.id:
457             self.node_type = self.get_type()
458             super(BaseModel, self).save(*args, **kwargs)
459             self.active_revision = self._create_revision(self.author, 1, title=self.title, tagnames=self.tagnames,
460                                                          body=self.body)
461             self.activate_revision(self.author, self.active_revision)
462             self.update_last_activity(self.author, time=self.added_at)
463
464         if self.parent_id and not self.abs_parent_id:
465             self.abs_parent = self.parent.absolute_parent
466         
467         tags_changed = self._process_changes_in_tags()
468         
469         super(Node, self).save(*args, **kwargs)
470         if tags_changed:
471             self.tags = list(Tag.objects.filter(name__in=self.tagname_list()))
472
473     class Meta:
474         app_label = 'forum'
475
476
477 class NodeRevision(BaseModel, NodeContent):
478     node       = models.ForeignKey(Node, related_name='revisions')
479     summary    = models.CharField(max_length=300)
480     revision   = models.PositiveIntegerField()
481     revised_at = models.DateTimeField(default=datetime.datetime.now)
482
483     class Meta:
484         unique_together = ('node', 'revision')
485         app_label = 'forum'
486
487
488 class NodeState(models.Model):
489     node       = models.ForeignKey(Node, related_name='states')
490     state_type = models.CharField(max_length=16)
491     action     = models.OneToOneField('Action', related_name="node_state")
492
493     class Meta:
494         unique_together = ('node', 'state_type')
495         app_label = 'forum'
496
497