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
170 def get_revisions_url(self):
\r
171 return ('revisions', (), {'id': self.id})
\r
173 def update_last_activity(self, user, save=False):
\r
174 self.last_activity_by = user
\r
175 self.last_activity_at = datetime.datetime.now()
\r
178 self.parent.update_last_activity(user, save=True)
\r
183 def _create_revision(self, user, number, **kwargs):
\r
184 revision = NodeRevision(author=user, revision=number, node=self, **kwargs)
\r
188 def create_revision(self, user, **kwargs):
\r
189 number = self.revisions.aggregate(last=models.Max('revision'))['last'] + 1
\r
190 revision = self._create_revision(user, number, **kwargs)
\r
191 self.activate_revision(user, revision)
\r
194 def activate_revision(self, user, revision):
\r
195 self.title = revision.title
\r
196 self.tagnames = revision.tagnames
\r
197 self.body = revision.body
\r
199 self.active_revision = revision
\r
200 self.update_last_activity(user)
\r
204 def get_tag_list_if_changed(self):
\r
205 dirty = self.get_dirty_fields()
\r
206 active_user = self.last_edited and self.last_edited.by or self.author
\r
208 if 'tagnames' in dirty:
\r
209 new_tags = self.tagname_list()
\r
210 old_tags = dirty['tagnames']
\r
212 if old_tags is None or not old_tags:
\r
215 old_tags = [name for name in dirty['tagnames'].split(u' ')]
\r
219 for name in new_tags:
\r
221 tag = Tag.objects.get(name=name)
\r
223 tag = Tag.objects.create(name=name, created_by=active_user or self.author)
\r
225 tag_list.append(tag)
\r
227 if not name in old_tags:
\r
228 tag.used_count = tag.used_count + 1
\r
230 tag.unmark_deleted()
\r
233 for name in [n for n in old_tags if not n in new_tags]:
\r
234 tag = Tag.objects.get(name=name)
\r
235 tag.used_count = tag.used_count - 1
\r
236 if tag.used_count == 0:
\r
237 tag.mark_deleted(active_user)
\r
244 def save(self, *args, **kwargs):
\r
246 self.node_type = self.get_type()
\r
247 super(BaseModel, self).save(*args, **kwargs)
\r
248 self.active_revision = self._create_revision(self.author, 1, title=self.title, tagnames=self.tagnames, body=self.body)
\r
249 self.update_last_activity(self.author)
\r
251 if self.parent_id and not self.abs_parent_id:
\r
252 self.abs_parent = self.parent.absolute_parent
\r
254 tags = self.get_tag_list_if_changed()
\r
255 super(Node, self).save(*args, **kwargs)
\r
256 if tags is not None: self.tags = tags
\r
259 def isSpam(comment, data):
\r
260 if not settings.WORDPRESS_API_KEY:
\r
263 api = Akismet(settings.WORDPRESS_API_KEY, settings.APP_URL)
\r
264 return api.comment_check(comment, data)
\r
267 app_label = 'forum'
\r
270 class NodeRevision(BaseModel, NodeContent):
\r
271 node = models.ForeignKey(Node, related_name='revisions')
\r
272 summary = models.CharField(max_length=300)
\r
273 revision = models.PositiveIntegerField()
\r
274 revised_at = models.DateTimeField(default=datetime.datetime.now)
\r
277 unique_together = ('node', 'revision')
\r
278 app_label = 'forum'
\r