Permalink
Browse files

finished fully implementing posts

  • Loading branch information...
1 parent b968a06 commit 8d2e93523040cc4d0d118c310e9afa0d26345d87 @brosner brosner committed Dec 16, 2011
Showing with 56 additions and 23 deletions.
  1. +1 −1 agora/admin.py
  2. +54 −21 agora/models.py
  3. +1 −1 agora/views.py
View
@@ -37,7 +37,7 @@ class ForumThreadReplyInline(admin.StackedInline):
"parent",
"category",
"view_count",
- "reply_count"
+ "post_count"
]
)
admin.site.register(models.ForumThread, ForumThreadAdmin)
View
@@ -61,15 +61,16 @@ class Forum(models.Model):
default = datetime.datetime.now,
editable = False
)
- last_reply = models.ForeignKey(
- "ForumReply",
+ last_thread = models.ForeignKey(
+ "ForumThread",
null = True,
editable = False,
- on_delete = models.SET_NULL
+ on_delete = models.SET_NULL,
+ related_name = "+"
)
view_count = models.IntegerField(default=0, editable=False)
- reply_count = models.IntegerField(default=0, editable=False)
+ post_count = models.IntegerField(default=0, editable=False)
@property
def thread_count(self):
@@ -88,28 +89,35 @@ def update_view_count(self):
self.view_count = view_count
self.save()
- def update_reply_count(self):
- reply_count = 0
+ def update_post_count(self):
+ post_count = 0
for forum in self.subforums.all():
- forum.update_reply_count()
- reply_count += forum.reply_count
+ forum.update_post_count()
+ post_count += forum.post_count
for thread in self.threads.all():
thread.update_reply_count()
- reply_count += thread.reply_count
- self.reply_count = reply_count
+ post_count += thread.reply_count + 1 # add one for the thread itself
+ self.post_count = post_count
self.save()
- def new_reply(self, reply):
- self.reply_count += 1 # if this gets out of sync run update_reply_count
- self.last_modified = reply.created
- self.last_reply = reply
+ def new_post(self, post):
+ self.post_count += 1 # if this gets out of sync run update_post_count
+ self.last_modified = post.created
+ self.last_thread = post.thread
self.save()
if self.parent:
- self.parent.new_reply(reply)
+ self.parent.new_post(post)
def __unicode__(self):
return self.title
+ @property
+ def last_post(self):
+ if self.last_thread_id is None:
+ return None
+ else:
+ return self.last_thread.last_post
+
def export(self, out=None):
if out is None:
out = "forum-export-%d.json" % self.id
@@ -121,9 +129,9 @@ def export(self, out=None):
"parent": self.parent_id,
"category": self.category_id,
"last_modified": self.last_modified.strftime("%Y-%m-%d %H:%M:%S"),
- "last_reply": self.last_reply_id,
+ "last_thread": self.last_thread_id,
"view_count": self.view_count,
- "reply_count": self.reply_count
+ "post_count": self.post_count
},
"threads": [
{
@@ -174,7 +182,7 @@ def restore(cls, in_):
category_id = data["self"]["category"],
last_modified = data["self"]["last_modified"],
view_count = data["self"]["view_count"],
- reply_count = data["self"]["reply_count"]
+ post_count = data["self"]["post_count"]
))
forum._importing = True
forum.save()
@@ -212,7 +220,7 @@ def restore(cls, in_):
)).save()
thread.last_reply_id = thread_data["last_reply"]
thread.save()
- forum.last_reply_id = data["self"]["last_reply"]
+ forum.last_thread_id = data["self"]["last_thread"]
forum.save()
@@ -281,7 +289,7 @@ def new_reply(self, reply):
self.last_modified = reply.created
self.last_reply = reply
self.save()
- self.forum.new_reply(reply)
+ self.forum.new_post(reply)
def subscribe(self, user, kind):
"""
@@ -309,6 +317,17 @@ def subscribed(self, user, kind):
def __unicode__(self):
return self.title
+
+ @property
+ def last_post(self):
+ if self.last_reply_id is None:
+ return self
+ else:
+ return self.last_reply
+
+ @property
+ def thread(self):
+ return self
class ForumReply(ForumPost):
@@ -331,7 +350,9 @@ class UserPostCount(models.Model):
@classmethod
def calculate(cls):
for user in User.objects.all():
- count = ForumReply.objects.filter(author=user).count()
+ thread_count = ForumThread.objects.filter(author=user).count()
+ reply_count = ForumReply.objects.filter(author=user).count()
+ count = thread_count + reply_count
upc, created = cls._default_manager.get_or_create(
user = user,
defaults = dict(
@@ -373,6 +394,18 @@ def _wrapped(func):
return _wrapped
+@signal(post_save, ForumThread)
+def forum_thread_save(sender, instance=None, created=False, **kwargs):
+ if instance and created:
+ forum = instance.forum
+ forum.new_post(instance)
+
+ # @@@ this next part could be manager method
+ post_count, created = UserPostCount.objects.get_or_create(user=instance.author)
+ post_count.count += 1
+ post_count.save()
+
+
@signal(post_save, ForumReply)
def forum_reply_save(sender, instance=None, created=False, **kwargs):
if instance and created:
View
@@ -25,7 +25,7 @@ def forums(request):
categories = ForumCategory.objects.filter(parent__isnull=True)
categories = categories.order_by("title")
- most_active_forums = Forum.objects.order_by("-reply_count")[:5]
+ most_active_forums = Forum.objects.order_by("-post_count")[:5]
most_viewed_forums = Forum.objects.order_by("-view_count")[:5]
most_active_members = UserPostCount.objects.order_by("-count")[:5]

0 comments on commit 8d2e935

Please sign in to comment.