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 t = [name.strip() for name in self.tagnames.split(u' ') if name]
\r
34 return [name.strip() for name in self.tagnames.split(u' ') if name]
\r
38 def tagname_meta_generator(self):
\r
39 return u','.join([tag for tag in self.tagname_list()])
\r
45 class NodeMetaClass(BaseMetaClass):
\r
48 def __new__(cls, *args, **kwargs):
\r
49 new_cls = super(NodeMetaClass, cls).__new__(cls, *args, **kwargs)
\r
51 if not new_cls._meta.abstract and new_cls.__name__ is not 'Node':
\r
52 NodeMetaClass.types[new_cls.get_type()] = new_cls
\r
57 def setup_relations(cls):
\r
58 for node_cls in NodeMetaClass.types.values():
\r
59 NodeMetaClass.setup_relation(node_cls)
\r
62 def setup_relation(cls, node_cls):
\r
63 name = node_cls.__name__.lower()
\r
66 return node_cls.objects.filter(parent=self)
\r
69 p = self.__dict__.get('_%s_cache' % name, None)
\r
71 if p is None and (self.parent is not None) and self.parent.node_type == name:
\r
72 p = self.parent.leaf
\r
73 self.__dict__['_%s_cache' % name] = p
\r
77 Node.add_to_class(name + 's', property(children))
\r
78 Node.add_to_class(name, property(parent))
\r
81 class NodeQuerySet(CachedQuerySet):
\r
82 def obj_from_datadict(self, datadict):
\r
83 cls = NodeMetaClass.types.get(datadict.get("node_type", ""), None)
\r
86 obj.__dict__.update(datadict)
\r
89 return super(NodeQuerySet, self).obj_from_datadict(datadict)
\r
91 def get(self, *args, **kwargs):
\r
92 return super(NodeQuerySet, self).get(*args, **kwargs).leaf
\r
95 class NodeManager(CachedManager):
\r
96 use_for_related_fields = True
\r
98 def get_query_set(self):
\r
99 qs = NodeQuerySet(self.model)
\r
101 if self.model is not Node:
\r
102 return qs.filter(node_type=self.model.get_type())
\r
106 def get_for_types(self, types, *args, **kwargs):
\r
107 kwargs['node_type__in'] = [t.get_type() for t in types]
\r
108 return self.get(*args, **kwargs)
\r
111 class Node(BaseModel, NodeContent):
\r
112 __metaclass__ = NodeMetaClass
\r
114 node_type = models.CharField(max_length=16, default='node')
\r
115 parent = models.ForeignKey('Node', related_name='children', null=True)
\r
116 abs_parent = models.ForeignKey('Node', related_name='all_children', null=True)
\r
118 added_at = models.DateTimeField(default=datetime.datetime.now)
\r
119 score = models.IntegerField(default=0)
\r
121 deleted = models.ForeignKey('Action', null=True, unique=True, related_name="deleted_node")
\r
122 in_moderation = models.ForeignKey('Action', null=True, unique=True, related_name="moderated_node")
\r
123 last_edited = models.ForeignKey('Action', null=True, unique=True, related_name="edited_node")
\r
125 last_activity_by = models.ForeignKey(User, null=True)
\r
126 last_activity_at = models.DateTimeField(null=True, blank=True)
\r
128 tags = models.ManyToManyField('Tag', related_name='%(class)ss')
\r
129 active_revision = models.OneToOneField('NodeRevision', related_name='active', null=True)
\r
131 extra_ref = models.ForeignKey('Node', null=True)
\r
132 extra_count = models.IntegerField(default=0)
\r
133 extra_action = models.ForeignKey('Action', null=True, related_name="extra_node")
\r
135 marked = models.BooleanField(default=False)
\r
136 wiki = models.BooleanField(default=False)
\r
138 comment_count = DenormalizedField("children", node_type="comment", canceled=False)
\r
139 flag_count = DenormalizedField("flags")
\r
141 friendly_name = _("post")
\r
143 objects = NodeManager()
\r
146 def cache_key(cls, pk):
\r
147 return '%s:node:%s' % (settings.APP_URL, pk)
\r
151 return cls.__name__.lower()
\r
155 leaf_cls = NodeMetaClass.types.get(self.node_type, None)
\r
157 if leaf_cls is None:
\r
161 leaf.__dict__ = self.__dict__
\r
165 def absolute_parent(self):
\r
166 if not self.abs_parent_id:
\r
169 return self.abs_parent.leaf
\r
173 return strip_tags(self.html)[:300]
\r
176 def get_revisions_url(self):
\r
177 return ('revisions', (), {'id': self.id})
\r
179 def update_last_activity(self, user, save=False):
\r
180 self.last_activity_by = user
\r
181 self.last_activity_at = datetime.datetime.now()
\r
184 self.parent.update_last_activity(user, save=True)
\r
189 def _create_revision(self, user, number, **kwargs):
\r
190 revision = NodeRevision(author=user, revision=number, node=self, **kwargs)
\r
194 def create_revision(self, user, **kwargs):
\r
195 number = self.revisions.aggregate(last=models.Max('revision'))['last'] + 1
\r
196 revision = self._create_revision(user, number, **kwargs)
\r
197 self.activate_revision(user, revision)
\r
200 def activate_revision(self, user, revision):
\r
201 self.title = revision.title
\r
202 self.tagnames = revision.tagnames
\r
203 self.body = revision.body
\r
205 self.active_revision = revision
\r
206 self.update_last_activity(user)
\r
210 def _list_changes_in_tags(self):
\r
211 dirty = self.get_dirty_fields()
\r
213 if not 'tagnames' in dirty:
\r
216 if self._original_state['tagnames']:
\r
217 old_tags = set(name for name in self._original_state['tagnames'].split(u' '))
\r
220 new_tags = set(name for name in self.tagnames.split(u' ') if name)
\r
223 current=list(new_tags),
\r
224 added=list(new_tags - old_tags),
\r
225 removed=list(old_tags - new_tags)
\r
228 def _last_active_user(self):
\r
229 return self.last_edited and self.last_edited.by or self.author
\r
231 def _process_changes_in_tags(self):
\r
232 tag_changes = self._list_changes_in_tags()
\r
234 if tag_changes is not None:
\r
235 for name in tag_changes['added']:
\r
237 tag = Tag.objects.get(name=name)
\r
239 tag = Tag.objects.create(name=name, created_by=self._last_active_user())
\r
241 if not self.deleted:
\r
242 tag.used_count = models.F('used_count') + 1
\r
245 if not self.deleted:
\r
246 for name in tag_changes['removed']:
\r
248 tag = Tag.objects.get(name=name)
\r
249 tag.used_count = models.F('used_count') - 1
\r
251 if tag.used_count == 0:
\r
252 tag.mark_deleted(self._last_active_user())
\r
260 def mark_deleted(self, action):
\r
261 self.deleted = action
\r
265 for tag in self.tags.all():
\r
266 tag.used_count = models.F('used_count') - 1
\r
268 if tag.used_count == 0:
\r
269 tag.mark_deleted(self._last_active_user())
\r
271 for tag in Tag.objects.filter(name__in=self.tagname_list()):
\r
272 tag.used_count = models.F('used_count') + 1
\r
275 def save(self, *args, **kwargs):
\r
276 tags_changed = self._process_changes_in_tags()
\r
279 self.node_type = self.get_type()
\r
280 super(BaseModel, self).save(*args, **kwargs)
\r
281 self.active_revision = self._create_revision(self.author, 1, title=self.title, tagnames=self.tagnames, body=self.body)
\r
282 self.update_last_activity(self.author)
\r
284 if self.parent_id and not self.abs_parent_id:
\r
285 self.abs_parent = self.parent.absolute_parent
\r
287 super(Node, self).save(*args, **kwargs)
\r
288 if tags_changed: self.tags = list(Tag.objects.filter(name__in=self.tagname_list()))
\r
291 app_label = 'forum'
\r
294 class NodeRevision(BaseModel, NodeContent):
\r
295 node = models.ForeignKey(Node, related_name='revisions')
\r
296 summary = models.CharField(max_length=300)
\r
297 revision = models.PositiveIntegerField()
\r
298 revised_at = models.DateTimeField(default=datetime.datetime.now)
\r
301 unique_together = ('node', 'revision')
\r
302 app_label = 'forum'
\r