From e774033ff1382c859afba53c868c24b3cf6953a9 Mon Sep 17 00:00:00 2001 From: Evgeny Fadeev Date: Fri, 11 Jul 2014 19:23:48 -0300 Subject: when answers are deleted or restored and when users are blocked, last thread update info is recalculated --- .../management/commands/load_stackexchange.py | 2 +- askbot/models/__init__.py | 6 ++++++ askbot/models/post.py | 15 ++++++++++++--- askbot/models/question.py | 19 ++++++++++++++++++- askbot/templates/user_profile/user_moderate.html | 2 ++ 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/askbot/importers/stackexchange/management/commands/load_stackexchange.py b/askbot/importers/stackexchange/management/commands/load_stackexchange.py index 902bc988..5cefd93f 100644 --- a/askbot/importers/stackexchange/management/commands/load_stackexchange.py +++ b/askbot/importers/stackexchange/management/commands/load_stackexchange.py @@ -573,7 +573,7 @@ it may be helpful to split this procedure in two:\n def mark_activity(self,p,u,t): """p,u,t - post, user, timestamp """ - p.thread.set_last_activity(last_activity_by=u, last_activity_at=t) + p.thread.set_last_activity_info(last_activity_by=u, last_activity_at=t) def _process_post_rollback_revision_group(self, rev_group): #todo: don't know what to do here as there were no diff --git a/askbot/models/__init__.py b/askbot/models/__init__.py index beff6621..8b85e42d 100644 --- a/askbot/models/__init__.py +++ b/askbot/models/__init__.py @@ -1514,6 +1514,7 @@ def user_delete_answer( answer.save() answer.thread.update_answer_count() + answer.thread.update_last_activity_info() answer.thread.invalidate_cached_data() logging.debug('updated answer count to %d' % answer.thread.answer_count) @@ -1581,6 +1582,10 @@ def user_delete_all_content_authored_by_user(self, author, timestamp=None): questions = Post.objects.get_questions().filter(author=author) count += questions.update(deleted_at=timestamp, deleted_by=self, deleted=True) + threads = Thread.objects.filter(last_activity_by=author) + for thread in threads: + thread.update_last_activity_info() + #delete threads thread_ids = questions.values_list('thread_id', flat=True) #load second time b/c threads above are not quite real @@ -1654,6 +1659,7 @@ def user_restore_post( post.thread.invalidate_cached_data() if post.post_type == 'answer': post.thread.update_answer_count() + post.thread.update_last_activity_info() else: #todo: make sure that these tags actually exist #some may have since been deleted for good diff --git a/askbot/models/post.py b/askbot/models/post.py index 31396d67..4e9e5d6f 100644 --- a/askbot/models/post.py +++ b/askbot/models/post.py @@ -292,7 +292,10 @@ class PostManager(BaseQuerySetManager): if answer.is_approved(): thread.answer_count += 1 thread.save() - thread.set_last_activity(last_activity_at=added_at, last_activity_by=author) # this should be here because it regenerates cached thread summary html + thread.set_last_activity_info( + last_activity_at=added_at, + last_activity_by=author + ) # this should be here because it regenerates cached thread summary html return answer @@ -1831,7 +1834,10 @@ class Post(models.Model): if edited_at is None: edited_at = datetime.datetime.now() - self.thread.set_last_activity(last_activity_at=edited_at, last_activity_by=edited_by) + self.thread.set_last_activity_info( + last_activity_at=edited_at, + last_activity_by=edited_by + ) def _question__apply_edit( self, @@ -1891,7 +1897,10 @@ class Post(models.Model): ip_addr=ip_addr ) - self.thread.set_last_activity(last_activity_at=edited_at, last_activity_by=edited_by) + self.thread.set_last_activity_info( + last_activity_at=edited_at, + last_activity_by=edited_by + ) def apply_edit(self, *args, **kwargs): #todo: unify this, here we have unnecessary indirection diff --git a/askbot/models/question.py b/askbot/models/question.py index 8336ac5d..2cdcdf71 100644 --- a/askbot/models/question.py +++ b/askbot/models/question.py @@ -898,7 +898,7 @@ class Thread(models.Model): self.answer_accepted_at = timestamp self.save() - def set_last_activity(self, last_activity_at, last_activity_by): + def set_last_activity_info(self, last_activity_at, last_activity_by): self.last_activity_at = last_activity_at self.last_activity_by = last_activity_by self.save() @@ -906,6 +906,23 @@ class Thread(models.Model): self.update_summary_html() # regenerate question/thread summary html #################################################################### + def get_last_activity_info(self): + post_ids = self.get_answers().values_list('id', flat=True) + question = self._question_post() + post_ids = list(post_ids) + post_ids.append(question.id) + from askbot.models import PostRevision + revs = PostRevision.objects.filter( + post__id__in=post_ids, + revision__gt=0 + ).order_by('-id') + rev = revs[0] + return rev.revised_at, rev.author + + def update_last_activity_info(self): + timestamp, user = self.get_last_activity_info() + self.set_last_activity_info(timestamp, user) + def get_tag_names(self): "Creates a list of Tag names from the ``tagnames`` attribute." if self.tagnames.strip() == '': diff --git a/askbot/templates/user_profile/user_moderate.html b/askbot/templates/user_profile/user_moderate.html index 1e33f4d7..b944b84a 100644 --- a/askbot/templates/user_profile/user_moderate.html +++ b/askbot/templates/user_profile/user_moderate.html @@ -18,7 +18,9 @@

+ {% if not view_user.is_blocked() %} + {% endif %}

{% endif %} -- cgit v1.2.3-1-g7c22