1 from forum.akismet import *
\r
6 from django.utils.translation import ugettext as _
\r
7 from django.utils.safestring import mark_safe
\r
8 from django.utils.html import strip_tags
\r
9 from forum.utils.html import sanitize_html
\r
11 class NodeContent(models.Model):
\r
12 title = models.CharField(max_length=300)
\r
13 tagnames = models.CharField(max_length=125)
\r
14 author = models.ForeignKey(User, related_name='%(class)ss')
\r
15 body = models.TextField()
\r
23 return self.as_markdown()
\r
25 def as_markdown(self, *extensions):
\r
26 return mark_safe(sanitize_html(markdown.markdown(self.body, extensions=extensions)))
\r
32 def tagname_list(self):
\r
34 return [name for name in self.tagnames.split(u' ')]
\r
38 def tagname_meta_generator(self):
\r
39 return u','.join([unicode(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 NodeManager(CachedManager):
\r
82 use_for_related_fields = True
\r
84 def get_query_set(self):
\r
85 qs = super(NodeManager, self).get_query_set()
\r
87 if self.model is not Node:
\r
88 return qs.filter(node_type=self.model.get_type())
\r
92 def get(self, *args, **kwargs):
\r
93 node = super(NodeManager, self).get(*args, **kwargs)
\r
94 cls = NodeMetaClass.types.get(node.node_type, None)
\r
96 if cls and node.__class__ is not cls:
\r
100 def get_for_types(self, types, *args, **kwargs):
\r
101 kwargs['node_type__in'] = [t.get_type() for t in types]
\r
102 return self.get(*args, **kwargs)
\r
105 class Node(BaseModel, NodeContent):
\r
106 __metaclass__ = NodeMetaClass
\r
108 node_type = models.CharField(max_length=16, default='node')
\r
109 parent = models.ForeignKey('Node', related_name='children', null=True)
\r
110 abs_parent = models.ForeignKey('Node', related_name='all_children', null=True)
\r
112 added_at = models.DateTimeField(default=datetime.datetime.now)
\r
113 score = models.IntegerField(default=0)
\r
115 deleted = models.ForeignKey('Action', null=True, unique=True, related_name="deleted_node")
\r
116 in_moderation = models.ForeignKey('Action', null=True, unique=True, related_name="moderated_node")
\r
117 last_edited = models.ForeignKey('Action', null=True, unique=True, related_name="edited_node")
\r
119 last_activity_by = models.ForeignKey(User, null=True)
\r
120 last_activity_at = models.DateTimeField(null=True, blank=True)
\r
122 tags = models.ManyToManyField('Tag', related_name='%(class)ss')
\r
123 active_revision = models.OneToOneField('NodeRevision', related_name='active', null=True)
\r
125 extra_ref = models.ForeignKey('Node', null=True)
\r
126 extra_count = models.IntegerField(default=0)
\r
127 extra_action = models.ForeignKey('Action', null=True, related_name="extra_node")
\r
129 marked = models.BooleanField(default=False)
\r
130 wiki = models.BooleanField(default=False)
\r
132 comment_count = DenormalizedField("children", node_type="comment", canceled=False)
\r
133 flag_count = DenormalizedField("flags")
\r
135 friendly_name = _("post")
\r
137 objects = NodeManager()
\r
140 def cache_key(cls, pk):
\r
141 return '%s.node:%s' % (settings.APP_URL, pk)
\r
145 return cls.__name__.lower()
\r
149 leaf_cls = NodeMetaClass.types.get(self.node_type, None)
\r
151 if leaf_cls is None:
\r
155 leaf.__dict__ = self.__dict__
\r
159 def absolute_parent(self):
\r
160 if not self.abs_parent_id:
\r
163 return self.abs_parent.leaf
\r
167 return strip_tags(self.html)[:300]
\r
169 def update_last_activity(self, user, save=False):
\r
170 self.last_activity_by = user
\r
171 self.last_activity_at = datetime.datetime.now()
\r
174 self.parent.update_last_activity(user, save=True)
\r
179 def _create_revision(self, user, number, **kwargs):
\r
180 revision = NodeRevision(author=user, revision=number, node=self, **kwargs)
\r
184 def create_revision(self, user, action=None, **kwargs):
\r
185 number = self.revisions.aggregate(last=models.Max('revision'))['last'] + 1
\r
186 revision = self._create_revision(user, number, **kwargs)
\r
187 self.activate_revision(user, revision, action)
\r
190 def activate_revision(self, user, revision, action=None):
\r
191 self.title = revision.title
\r
192 self.tagnames = revision.tagnames
\r
193 self.body = revision.body
\r
195 self.active_revision = revision
\r
196 self.update_last_activity(user)
\r
199 self.last_edited = action
\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 def isSpam(comment, data):
\r
264 if api.comment_check(comment, data):
\r
271 app_label = 'forum'
\r
274 class NodeRevision(BaseModel, NodeContent):
\r
275 node = models.ForeignKey(Node, related_name='revisions')
\r
276 summary = models.CharField(max_length=300)
\r
277 revision = models.PositiveIntegerField()
\r
278 revised_at = models.DateTimeField(default=datetime.datetime.now)
\r
281 unique_together = ('node', 'revision')
\r
282 app_label = 'forum'
\r