diff options
-rw-r--r-- | askbot/management/__init__.py | 1 | ||||
-rw-r--r-- | askbot/management/commands/fix_inbox_counts.py | 31 | ||||
-rw-r--r-- | askbot/models/__init__.py | 50 | ||||
-rw-r--r-- | askbot/models/meta.py | 16 | ||||
-rw-r--r-- | askbot/models/signals.py | 2 | ||||
-rw-r--r-- | askbot/models/user.py | 3 | ||||
-rw-r--r-- | askbot/tasks.py | 5 | ||||
-rw-r--r-- | askbot/views/commands.py | 22 |
8 files changed, 45 insertions, 85 deletions
diff --git a/askbot/management/__init__.py b/askbot/management/__init__.py index 1e2b2aaf..6dc50083 100644 --- a/askbot/management/__init__.py +++ b/askbot/management/__init__.py @@ -61,6 +61,7 @@ class NoArgsJob(NoArgsCommand): total_count = batch['query_set'].count() if total_count == 0: + transaction.commit() return for item in batch['query_set'].all(): diff --git a/askbot/management/commands/fix_inbox_counts.py b/askbot/management/commands/fix_inbox_counts.py index c2ffffdc..fc1a37d1 100644 --- a/askbot/management/commands/fix_inbox_counts.py +++ b/askbot/management/commands/fix_inbox_counts.py @@ -1,35 +1,22 @@ from askbot.management import NoArgsJob from askbot import models -from askbot import const - -ACTIVITY_TYPES = const.RESPONSE_ACTIVITY_TYPES_FOR_DISPLAY -ACTIVITY_TYPES += (const.TYPE_ACTIVITY_MENTION,) def fix_inbox_counts(user): + """a unit of job - returns True if change was made + and False otherwise + """ old_new_count = user.new_response_count old_seen_count = user.seen_response_count - new_new_count = models.ActivityAuditStatus.objects.filter( - user = user, - status = models.ActivityAuditStatus.STATUS_NEW, - activity__activity_type__in = ACTIVITY_TYPES - ).count() - new_seen_count = models.ActivityAuditStatus.objects.filter( - user = user, - status = models.ActivityAuditStatus.STATUS_SEEN, - activity__activity_type__in = ACTIVITY_TYPES - ).count() + + user.update_response_counts() (changed1, changed2) = (False, False) - if new_new_count != old_new_count: - user.new_response_count = new_new_count + if user.new_response_count != old_new_count: changed1 = True - if new_seen_count != old_seen_count: - user.seen_response_count = new_seen_count + if user.seen_response_count != old_seen_count: changed2 = True - if changed1 or changed2: - user.save() - return True - return False + + return (changed1 or changed2) class Command(NoArgsJob): """definition of the job that fixes response counts diff --git a/askbot/models/__init__.py b/askbot/models/__init__.py index 983188a6..24ec6418 100644 --- a/askbot/models/__init__.py +++ b/askbot/models/__init__.py @@ -1297,8 +1297,7 @@ def user_visit_question(self, question = None, timestamp = None): status=ActivityAuditStatus.STATUS_SEEN ) if cleared_record_count > 0: - self.decrement_response_count(cleared_record_count) - self.save() + self.update_response_counts() #finally, mark admin memo objects if applicable #the admin response counts are not denormalized b/c they are easy to obtain @@ -1774,35 +1773,24 @@ def user_get_flags_for_post(self, post): flags = self.get_flags() return flags.filter(content_type = post_content_type, object_id=post.id) -def user_increment_response_count(user): - """increment response counter for user - by one +def user_update_response_counts(user): + """Recount number of responses to the user. """ - user.new_response_count += 1 + ACTIVITY_TYPES = const.RESPONSE_ACTIVITY_TYPES_FOR_DISPLAY + ACTIVITY_TYPES += (const.TYPE_ACTIVITY_MENTION,) + + user.new_response_count = ActivityAuditStatus.objects.filter( + user = user, + status = ActivityAuditStatus.STATUS_NEW, + activity__activity_type__in = ACTIVITY_TYPES + ).count() + user.seen_response_count = ActivityAuditStatus.objects.filter( + user = user, + status = ActivityAuditStatus.STATUS_SEEN, + activity__activity_type__in = ACTIVITY_TYPES + ).count() + user.save() -def user_decrement_response_count(user, amount=1): - """decrement response count for the user - by one, log critical error if count would go below zero - but limit decrementation at zero exactly - """ - assert(amount > 0) - user.seen_response_count += amount - if user.new_response_count >= amount: - user.new_response_count -= amount - user.clean_response_counts() - -def user_clean_response_counts(user): - "" - if user.new_response_count < 0: - user.new_response_count = 0 - logging.critical( - 'new response count wanted to go below zero for %s' % user.username - ) - if user.seen_response_count < 0: - user.seen_response_count = 0 - logging.critical( - 'seen response count wanted to go below zero form %s' % user.username - ) def user_receive_reputation(self, num_points): new_points = self.reputation + num_points @@ -1886,9 +1874,7 @@ User.add_to_class('follow_question', user_follow_question) User.add_to_class('unfollow_question', user_unfollow_question) User.add_to_class('mark_tags', user_mark_tags) User.add_to_class('is_following', user_is_following) -User.add_to_class('decrement_response_count', user_decrement_response_count) -User.add_to_class('increment_response_count', user_increment_response_count) -User.add_to_class('clean_response_counts', user_clean_response_counts) +User.add_to_class('update_response_counts', user_update_response_counts) User.add_to_class('can_have_strong_url', user_can_have_strong_url) User.add_to_class('is_administrator', user_is_administrator) User.add_to_class('set_admin_status', user_set_admin_status) diff --git a/askbot/models/meta.py b/askbot/models/meta.py index 290f5ef0..db92b4cd 100644 --- a/askbot/models/meta.py +++ b/askbot/models/meta.py @@ -260,7 +260,6 @@ class Comment(base.MetaContent, base.UserContent): comment_content_type = ContentType.objects.get_for_model(self) comment_id = self.id - #on these activities decrement response counter #todo: implement a custom delete method on these #all this should pack into Activity.responses.filter( somehow ).delete() activity_types = const.RESPONSE_ACTIVITY_TYPES_FOR_DISPLAY @@ -273,16 +272,19 @@ class Comment(base.MetaContent, base.UserContent): object_id = comment_id, activity_type__in = activity_types ) + + recipients = set() for activity in activities: for user in activity.recipients.all(): - user.decrement_response_count() - user.save() + recipients.add(user) + + #activities need to be deleted before the response + #counts are updated activities.delete() - #mentions - simply delete - mentions = Activity.objects.get_mentions(mentioned_in = self) - mentions.delete() - + for user in recipients: + user.update_response_counts() + super(Comment,self).delete(**kwargs) def get_absolute_url(self): diff --git a/askbot/models/signals.py b/askbot/models/signals.py index d28cd4a5..8802fcf7 100644 --- a/askbot/models/signals.py +++ b/askbot/models/signals.py @@ -73,7 +73,7 @@ def pop_all_db_signal_receivers(): post_syncdb, ) if 'm2m_changed' in globals(): - signals += m2m_changed + signals += (m2m_changed, ) receiver_data = dict() for signal in signals: diff --git a/askbot/models/user.py b/askbot/models/user.py index 09e7ebee..621a2bb8 100644 --- a/askbot/models/user.py +++ b/askbot/models/user.py @@ -77,8 +77,7 @@ class ActivityManager(models.Manager): if mentioned_whom: assert(isinstance(mentioned_whom, User)) mention_activity.add_recipients([mentioned_whom]) - mentioned_whom.increment_response_count() - mentioned_whom.save() + mentioned_whom.update_response_counts() return mention_activity diff --git a/askbot/tasks.py b/askbot/tasks.py index 8dfea5b4..caa33c64 100644 --- a/askbot/tasks.py +++ b/askbot/tasks.py @@ -93,9 +93,8 @@ def record_post_update( assert(updated_by not in recipients) - for user in set(recipients) | set(newly_mentioned_users): - user.increment_response_count() - user.save() + for user in (set(recipients) | set(newly_mentioned_users)): + user.update_response_counts() #todo: weird thing is that only comments need the recipients #todo: debug these calls and then uncomment in the repo diff --git a/askbot/views/commands.py b/askbot/views/commands.py index 809d4249..e2293262 100644 --- a/askbot/views/commands.py +++ b/askbot/views/commands.py @@ -99,35 +99,21 @@ def manage_inbox(request): activity__activity_type__in = activity_types, user = user ) + action_type = post_data['action_type'] - seen_memos = memo_set.filter( - status=models.ActivityAuditStatus.STATUS_SEEN - ) - new_memos = memo_set.filter( - status=models.ActivityAuditStatus.STATUS_NEW - ) if action_type == 'delete': - user.new_response_count -= new_memos.count() - user.seen_response_count -= seen_memos.count() - user.clean_response_counts() - user.save() memo_set.delete() elif action_type == 'mark_new': - user.new_response_count += seen_memos.count() - user.seen_response_count -= seen_memos.count() - user.clean_response_counts() - user.save() memo_set.update(status = models.ActivityAuditStatus.STATUS_NEW) elif action_type == 'mark_seen': - user.new_response_count -= new_memos.count() - user.seen_response_count += new_memos.count() - user.clean_response_counts() - user.save() memo_set.update(status = models.ActivityAuditStatus.STATUS_SEEN) else: raise exceptions.PermissionDenied( _('Oops, apologies - there was some error') ) + + user.update_response_counts() + response_data['success'] = True data = simplejson.dumps(response_data) return HttpResponse(data, mimetype="application/json") |