5 from django.utils.translation import ugettext as _
\r
6 from django.utils.safestring import mark_safe
\r
7 from django.utils.html import strip_tags
\r
8 from forum.utils.html import sanitize_html
\r
10 class NodeContent(models.Model):
\r
11 title = models.CharField(max_length=300)
\r
12 tagnames = models.CharField(max_length=125)
\r
13 author = models.ForeignKey(User, related_name='%(class)ss')
\r
14 body = models.TextField()
\r
22 return self.as_markdown()
\r
24 def as_markdown(self, *extensions):
\r
25 return mark_safe(sanitize_html(markdown.markdown(self.body, extensions=extensions)))
\r
31 def tagname_list(self):
\r
33 return [name for name in self.tagnames.split(u' ')]
\r
37 def tagname_meta_generator(self):
\r
38 return u','.join([unicode(tag) for tag in self.tagname_list()])
\r
44 class NodeMetaClass(BaseMetaClass):
\r
47 def __new__(cls, *args, **kwargs):
\r
48 new_cls = super(NodeMetaClass, cls).__new__(cls, *args, **kwargs)
\r
50 if not new_cls._meta.abstract and new_cls.__name__ is not 'Node':
\r
51 NodeMetaClass.types[new_cls.get_type()] = new_cls
\r
56 def setup_relations(cls):
\r
57 for node_cls in NodeMetaClass.types.values():
\r
58 NodeMetaClass.setup_relation(node_cls)
\r
61 def setup_relation(cls, node_cls):
\r
62 name = node_cls.__name__.lower()
\r
65 return node_cls.objects.filter(parent=self)
\r
68 p = self.__dict__.get('_%s_cache' % name, None)
\r
70 if p is None and (self.parent is not None) and self.parent.node_type == name:
\r
71 p = self.parent.leaf
\r
72 self.__dict__['_%s_cache' % name] = p
\r
76 Node.add_to_class(name + 's', property(children))
\r
77 Node.add_to_class(name, property(parent))
\r
80 class NodeManager(CachedManager):
\r
81 use_for_related_fields = True
\r
83 def get_query_set(self):
\r
84 qs = super(NodeManager, self).get_query_set()
\r
86 if self.model is not Node:
\r
87 return qs.filter(node_type=self.model.get_type())
\r
91 def get(self, *args, **kwargs):
\r
92 node = super(NodeManager, self).get(*args, **kwargs)
\r
93 cls = NodeMetaClass.types.get(node.node_type, None)
\r
95 if cls and node.__class__ is not cls:
\r
99 def get_for_types(self, types, *args, **kwargs):
\r
100 kwargs['node_type__in'] = [t.get_type() for t in types]
\r
101 return self.get(*args, **kwargs)
\r
104 class Node(BaseModel, NodeContent):
\r
105 __metaclass__ = NodeMetaClass
\r
107 node_type = models.CharField(max_length=16, default='node')
\r
108 parent = models.ForeignKey('Node', related_name='children', null=True)
\r
109 abs_parent = models.ForeignKey('Node', related_name='all_children', null=True)
\r
111 added_at = models.DateTimeField(default=datetime.datetime.now)
\r
112 score = models.IntegerField(default=0)
\r
114 deleted = models.ForeignKey('Action', null=True, unique=True, related_name="deleted_node")
\r
115 in_moderation = models.ForeignKey('Action', null=True, unique=True, related_name="moderated_node")
\r
116 last_edited = models.ForeignKey('Action', null=True, unique=True, related_name="edited_node")
\r
118 last_activity_by = models.ForeignKey(User, null=True)
\r
119 last_activity_at = models.DateTimeField(null=True, blank=True)
\r
121 tags = models.ManyToManyField('Tag', related_name='%(class)ss')
\r
122 active_revision = models.OneToOneField('NodeRevision', related_name='active', null=True)
\r
124 extra_ref = models.ForeignKey('Node', null=True)
\r
125 extra_count = models.IntegerField(default=0)
\r
126 extra_action = models.ForeignKey('Action', null=True, related_name="extra_node")
\r
128 marked = models.BooleanField(default=False)
\r
129 wiki = models.BooleanField(default=False)
\r
131 comment_count = DenormalizedField("children", node_type="comment", canceled=False)
\r
132 flag_count = DenormalizedField("flags")
\r
134 friendly_name = _("post")
\r
136 objects = NodeManager()
\r
139 def cache_key(cls, pk):
\r
140 return '%s.node:%s' % (settings.APP_URL, pk)
\r
144 return cls.__name__.lower()
\r
148 leaf_cls = NodeMetaClass.types.get(self.node_type, None)
\r
150 if leaf_cls is None:
\r
154 leaf.__dict__ = self.__dict__
\r
158 def absolute_parent(self):
\r
159 if not self.abs_parent_id:
\r
162 return self.abs_parent.leaf
\r
166 return strip_tags(self.html)[:300]
\r
169 def get_revisions_url(self):
\r
170 return ('revisions', (), {'id': self.id})
\r
172 def update_last_activity(self, user, save=False):
\r
173 self.last_activity_by = user
\r
174 self.last_activity_at = datetime.datetime.now()
\r
177 self.parent.update_last_activity(user, save=True)
\r
182 def _create_revision(self, user, number, **kwargs):
\r
183 revision = NodeRevision(author=user, revision=number, node=self, **kwargs)
\r
187 def create_revision(self, user, **kwargs):
\r
188 number = self.revisions.aggregate(last=models.Max('revision'))['last'] + 1
\r
189 revision = self._create_revision(user, number, **kwargs)
\r
190 self.activate_revision(user, revision)
\r
193 def activate_revision(self, user, revision):
\r
194 self.title = revision.title
\r
195 self.tagnames = revision.tagnames
\r
196 self.body = revision.body
\r
198 self.active_revision = revision
\r
199 self.update_last_activity(user)
\r
203 def get_tag_list_if_changed(self):
\r
204 dirty = self.get_dirty_fields()
\r
205 active_user = self.last_edited and self.last_edited.by or self.author
\r
207 if 'tagnames' in dirty:
\r
208 new_tags = self.tagname_list()
\r
209 old_tags = dirty['tagnames']
\r
211 if old_tags is None or not old_tags:
\r
214 old_tags = [name for name in dirty['tagnames'].split(u' ')]
\r
218 for name in new_tags:
\r
220 tag = Tag.objects.get(name=name)
\r
222 tag = Tag.objects.create(name=name, created_by=active_user or self.author)
\r
224 tag_list.append(tag)
\r
226 if not name in old_tags:
\r
227 tag.used_count = tag.used_count + 1
\r
229 tag.unmark_deleted()
\r
232 for name in [n for n in old_tags if not n in new_tags]:
\r
233 tag = Tag.objects.get(name=name)
\r
234 tag.used_count = tag.used_count - 1
\r
235 if tag.used_count == 0:
\r
236 tag.mark_deleted(active_user)
\r
243 def save(self, *args, **kwargs):
\r
245 self.node_type = self.get_type()
\r
246 super(BaseModel, self).save(*args, **kwargs)
\r
247 self.active_revision = self._create_revision(self.author, 1, title=self.title, tagnames=self.tagnames, body=self.body)
\r
248 self.update_last_activity(self.author)
\r
250 if self.parent_id and not self.abs_parent_id:
\r
251 self.abs_parent = self.parent.absolute_parent
\r
253 tags = self.get_tag_list_if_changed()
\r
254 super(Node, self).save(*args, **kwargs)
\r
255 if tags is not None: self.tags = tags
\r
258 app_label = 'forum'
\r
261 class NodeRevision(BaseModel, NodeContent):
\r
262 node = models.ForeignKey(Node, related_name='revisions')
\r
263 summary = models.CharField(max_length=300)
\r
264 revision = models.PositiveIntegerField()
\r
265 revised_at = models.DateTimeField(default=datetime.datetime.now)
\r
268 unique_together = ('node', 'revision')
\r
269 app_label = 'forum'
\r