Commits

Anonymous committed fe42c2c

Centralised auto-tag function into Post manager
Added auto-tag rescan action to admin

Comments (0)

Files changed (3)

flat_forum/admin.py

     list_display = ('subject', 'created', 'posted_by', 'reply_to',)
     date_hierarchy = 'created'
 
+    def rescan_tags(self, request, queryset):
+        for obj in queryset:
+            # Report on differences?
+            obj.tags.add(*models.Post.objects.tags_for_post(obj))
+    rescan_tags.short_description = 'Auto-tag selected posts'
+    actions = [rescan_tags,]
+
 admin.site.register(models.Post, PostAdmin)
 admin.site.register(models.Profile)

flat_forum/forms.py

 from django import forms
-import re
 from flat_forum import models
 from taggit.models import TaggedItem
 
         '''Auto-tag'''
         data = self.cleaned_data
         tags = set([ tag.lower() for tag in data.get('tags', []) ])
-        tags.update([
-            tag
-            for tag in TaggedItem.tags_for(models.Post)
-            if re.search(r'\b%s\b' % tag.name, data['content'], re.I|re.M)
-        ])
+        tags.update(models.Post.objects.tags_for_post(content=data['content']))
         data['tags'] = list(tags)
         return data
 

flat_forum/models.py

 from django.db import models
 from datetime import datetime
 
+from taggit.models import TaggedItem
 from taggit.managers import TaggableManager
 
+import re
+
 from flat_forum.fields import ProfileField
 
 class Profile(models.Model):
     def __unicode__(self):
         return self.user.get_full_name() or self.user.username
 
+class PostManager(models.Manager):
+    def tags_for_post(self, post=None, content=None):
+        if content is None:
+            content = post.content
+        return [
+            tag
+            for tag in TaggedItem.tags_for(Post)
+            if re.search(r'\b%s\b' % tag.name, content, re.I|re.M)
+        ]
+
 class Post(models.Model):
     reply_to = models.ForeignKey('self', blank=True, null=True, related_name='replies')
     posted_by = models.ForeignKey('Profile', related_name='posts')
 
     tags = TaggableManager(blank=True)
 
+    objects = PostManager()
+
     class Meta:
         get_latest_by = 'created'
         ordering = ('created',)
     @property
     def post_date(self):
         return self.created.date()
+