X-Git-Url: https://git.openstreetmap.org./osqa.git/blobdiff_plain/4496d7188a21ea6706e3b8a9ca5742c64767c134..133a8f939cdfa69dd6a7caeafed158aa38d20092:/forum/models/node.py?ds=inline diff --git a/forum/models/node.py b/forum/models/node.py index 932452d..530981b 100644 --- a/forum/models/node.py +++ b/forum/models/node.py @@ -12,6 +12,10 @@ class NodeContent(models.Model): author = models.ForeignKey(User, related_name='%(class)ss') body = models.TextField() + @property + def user(self): + return self.author + @property def html(self): return mark_safe(sanitize_html(markdown.markdown(self.body))) @@ -20,10 +24,6 @@ class NodeContent(models.Model): def headline(self): return self.title - @property - def summary(self): - return strip_tags(self.html)[:300] - def tagname_list(self): if self.tagnames: return [name for name in self.tagnames.split(u' ')] @@ -58,17 +58,17 @@ class NodeMetaClass(models.Model.__metaclass__): name = node_cls.__name__.lower() def children(self): - return node_cls.objects.filter(parent=self) + if node_cls._meta.proxy: + return node_cls.objects.filter(node_type=name, parent=self) + else: + return node_cls.objects.filter(parent=self) def parent(self): p = self.__dict__.get('_%s_cache' % name, None) - if p is None: - try: - p = self.parent.leaf - self.__dict__['_%s_cache' % name] = p - except Exception, e: - pass + if p is None and (self.parent is not None) and self.parent.node_type == name: + p = self.parent.leaf + self.__dict__['_%s_cache' % name] = p return p @@ -76,6 +76,8 @@ class NodeMetaClass(models.Model.__metaclass__): Node.add_to_class(name, property(parent)) +node_create = django.dispatch.Signal(providing_args=['instance']) + class Node(BaseModel, NodeContent, DeletableContent): __metaclass__ = NodeMetaClass @@ -90,7 +92,7 @@ class Node(BaseModel, NodeContent, DeletableContent): vote_up_count = models.IntegerField(default=0) vote_down_count = models.IntegerField(default=0) - comment_count = models.PositiveIntegerField(default=0) + comment_count = models.PositiveIntegerField(default=0) offensive_flag_count = models.SmallIntegerField(default=0) last_edited_at = models.DateTimeField(null=True, blank=True) @@ -102,6 +104,10 @@ class Node(BaseModel, NodeContent, DeletableContent): def leaf(self): return NodeMetaClass.types[self.node_type].objects.get(id=self.id) + @property + def summary(self): + return strip_tags(self.html)[:300] + def create_revision(self, user, **kwargs): revision = NodeRevision(author=user, **kwargs) @@ -120,13 +126,17 @@ class Node(BaseModel, NodeContent, DeletableContent): self.title = revision.title self.tagnames = revision.tagnames self.body = revision.body - - self.last_edited_at = datetime.datetime.now() - self.last_edited_by = user + + old_revision = self.active_revision self.active_revision = revision self.save() + if not old_revision: + self.last_edited_at = datetime.datetime.now() + self.last_edited_by = user + node_create.send(sender=self.__class__, instance=self) + def get_tag_list_if_changed(self): dirty = self.get_dirty_fields()