]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/subscriptions.py
Modified page titles (mostly) according to Oscar's suggestions in http://meta.osqa...
[osqa.git] / forum / subscriptions.py
index 4c5725650994f45c2dbc9d8579fc5db812c2884f..e943d2d34de098f583a63465b76e4a0060c54100 100644 (file)
@@ -2,15 +2,11 @@ import os
 import re\r
 import datetime\r
 from forum.models import User, Question, Comment, QuestionSubscription, SubscriptionSettings, Answer\r
-from forum.models.user import activity_record\r
 from forum.utils.mail import send_email\r
-from forum.views.readers import question_view\r
 from django.utils.translation import ugettext as _\r
+from forum.actions import AskAction, AnswerAction, CommentAction, AcceptAnswerAction, UserJoinsAction, QuestionViewAction\r
 from django.conf import settings\r
 from django.db.models import Q, F\r
-from django.db.models.signals import post_save\r
-from django.contrib.contenttypes.models import ContentType\r
-import const\r
 \r
 def create_subscription_if_not_exists(question, user):\r
     try:\r
@@ -25,10 +21,8 @@ def apply_default_filters(queryset, excluded_id):
 def create_recipients_dict(usr_list):\r
     return [(s['username'], s['email'], {'username': s['username']}) for s in usr_list]\r
 \r
-def question_posted(instance, created, **kwargs):\r
-    if not created: return\r
-\r
-    question = instance\r
+def question_posted(action, new):\r
+    question = action.node\r
 \r
     subscribers = User.objects.values('email', 'username').filter(\r
             Q(subscription_settings__enable_notifications=True, subscription_settings__new_question='i') |\r
@@ -57,13 +51,11 @@ def question_posted(instance, created, **kwargs):
     for user in new_subscribers:\r
         create_subscription_if_not_exists(question, user)\r
 \r
-#post_save.connect(question_posted, sender=Question)\r
-\r
+AskAction.hook(question_posted)\r
 \r
-def answer_posted(instance, created, **kwargs):\r
-    if not created: return\r
 \r
-    answer = instance\r
+def answer_posted(action, new):\r
+    answer = action.node\r
     question = answer.question\r
 \r
     subscribers = question.subscribers.values('email', 'username').filter(\r
@@ -77,16 +69,16 @@ def answer_posted(instance, created, **kwargs):
                recipients, "notifications/newanswer.html", {\r
         'question': question,\r
         'answer': answer\r
-    })\r
+    }, threaded=False)\r
 \r
     if answer.author.subscription_settings.questions_answered:\r
         create_subscription_if_not_exists(question, answer.author)\r
 \r
-post_save.connect(answer_posted, sender=Answer)\r
+AnswerAction.hook(answer_posted)\r
 \r
 \r
-def comment_posted(sender, instance, **kwargs):\r
-    comment = instance.content_object\r
+def comment_posted(action, new):\r
+    comment = action.node\r
     post = comment.content_object\r
 \r
     if post.__class__ == Question:\r
@@ -98,16 +90,16 @@ def comment_posted(sender, instance, **kwargs):
 \r
     q_filter = Q(subscription_settings__notify_comments=True) | Q(subscription_settings__notify_comments_own_post=True, id=post.author.id)\r
 \r
-    inreply = re.search('@\w+', comment.comment)\r
-    if inreply is not None:\r
-        q_filter = q_filter | Q(subscription_settings__notify_reply_to_comments=True,\r
-                                username__istartswith=inreply.group(0)[1:],\r
-                                comments__object_id=post.id,\r
-                                comments__content_type=ContentType.objects.get_for_model(post.__class__)\r
-                                )\r
+    #inreply = re.search('@\w+', comment.comment)\r
+    #if inreply is not None:\r
+    #    q_filter = q_filter | Q(subscription_settings__notify_reply_to_comments=True,\r
+    #                            username__istartswith=inreply.group(0)[1:],\r
+    ##                            comments__object_id=post.id,\r
+    #                            comments__content_type=ContentType.objects.get_for_model(post.__class__)\r
+    #                            )\r
 \r
     subscribers = subscribers.filter(\r
-            q_filter, subscription_settings__subscribed_questions='i', subscription_settings__enable_notifications=True \r
+            q_filter, subscription_settings__subscribed_questions='i', subscription_settings__enable_notifications=True\r
     ).exclude(id=comment.user.id).distinct()\r
 \r
     recipients = create_recipients_dict(subscribers)\r
@@ -117,111 +109,89 @@ def comment_posted(sender, instance, **kwargs):
                 'comment': comment,\r
                 'post': post,\r
                 'question': question,\r
-    })\r
+    }, threaded=False)\r
 \r
     if comment.user.subscription_settings.questions_commented:\r
         create_subscription_if_not_exists(question, comment.user)\r
 \r
-activity_record.connect(comment_posted, sender=const.TYPE_ACTIVITY_COMMENT_QUESTION, weak=False)\r
-activity_record.connect(comment_posted, sender=const.TYPE_ACTIVITY_COMMENT_ANSWER, weak=False)\r
+CommentAction.hook(comment_posted)\r
 \r
 \r
-def answer_accepted(instance, created, **kwargs):\r
-    if not created and 'accepted' in instance.get_dirty_fields() and instance.accepted:\r
-        question = instance.question\r
+def answer_accepted(action, new):\r
+    question = action.node.question\r
 \r
-        subscribers = question.subscribers.values('email', 'username').filter(\r
-                subscription_settings__enable_notifications=True,\r
-                subscription_settings__notify_accepted=True,\r
-                subscription_settings__subscribed_questions='i'\r
-        ).exclude(id=instance.accepted_by.id).distinct()\r
-        recipients = create_recipients_dict(subscribers)\r
+    subscribers = question.subscribers.values('email', 'username').filter(\r
+            subscription_settings__enable_notifications=True,\r
+            subscription_settings__notify_accepted=True,\r
+            subscription_settings__subscribed_questions='i'\r
+    ).exclude(id=instance.accepted_by.id).distinct()\r
+    recipients = create_recipients_dict(subscribers)\r
 \r
-        send_email(settings.EMAIL_SUBJECT_PREFIX + _("An answer to '%(question_title)s' was accepted") % dict(question_title=question.title),\r
-                   recipients, "notifications/answeraccepted.html", {\r
-            'question': question,\r
-            'answer': instance\r
-        })\r
+    send_email(settings.EMAIL_SUBJECT_PREFIX + _("An answer to '%(question_title)s' was accepted") % dict(question_title=question.title),\r
+               recipients, "notifications/answeraccepted.html", {\r
+        'question': question,\r
+        'answer': action.node\r
+    }, threaded=False)\r
 \r
-post_save.connect(answer_accepted, sender=Answer)\r
+AcceptAnswerAction.hook(answer_accepted)\r
 \r
 \r
-def member_joined(sender, instance, created, **kwargs):\r
-    if not created:\r
-        return\r
-        \r
+def member_joined(action, new):\r
     subscribers = User.objects.values('email', 'username').filter(\r
             subscription_settings__enable_notifications=True,\r
             subscription_settings__member_joins='i'\r
-    ).exclude(id=instance.id).distinct()\r
+    ).exclude(id=action.user.id).distinct()\r
 \r
     recipients = create_recipients_dict(subscribers)\r
 \r
     send_email(settings.EMAIL_SUBJECT_PREFIX + _("%(username)s is a new member on %(app_name)s") % dict(username=instance.username, app_name=settings.APP_SHORT_NAME),\r
                recipients, "notifications/newmember.html", {\r
-        'newmember': instance,\r
-    })\r
+        'newmember': action.user,\r
+    }, threaded=False)\r
 \r
-    sub_settings = SubscriptionSettings(user=instance)\r
-    sub_settings.save()\r
+UserJoinsAction.hook(member_joined)\r
 \r
-post_save.connect(member_joined, sender=User, weak=False)\r
-\r
-def question_viewed(instance, user, **kwargs):\r
-    if not user.is_authenticated():\r
+def question_viewed(action, new):\r
+    if not action.viewuser.is_authenticated():\r
         return\r
-        \r
+\r
     try:\r
-        subscription = QuestionSubscription.objects.get(question=instance, user=user)\r
+        subscription = QuestionSubscription.objects.get(question=action.question, user=action.viewuser)\r
         subscription.last_view = datetime.datetime.now()\r
         subscription.save()\r
     except:\r
-        if user.subscription_settings.questions_viewed:\r
-            subscription = QuestionSubscription(question=instance, user=user)\r
+        if action.viewuser.subscription_settings.questions_viewed:\r
+            subscription = QuestionSubscription(question=action.question, user=action.viewuser)\r
             subscription.save()\r
 \r
-question_view.connect(question_viewed)\r
-\r
-#todo: this stuff goes temporarily here\r
-from forum.models import Award, Answer\r
-\r
-def notify_award_message(instance, created, **kwargs):\r
-    if created:\r
-        user = instance.user\r
-\r
-        msg = (u"Congratulations, you have received a badge '%s'. " \\r
-                + u"Check out <a href=\"%s\">your profile</a>.") \\r
-                % (instance.badge.name, user.get_profile_url())\r
-\r
-        user.message_set.create(message=msg)\r
+QuestionViewAction.hook(question_viewed)\r
 \r
-post_save.connect(notify_award_message, sender=Award)\r
 \r
 #todo: translate this\r
-record_answer_event_re = re.compile("You have received (a|\d+) .*new response.*")\r
-def record_answer_event(instance, created, **kwargs):\r
-    if created:\r
-        q_author = instance.question.author\r
-        found_match = False\r
-        #print 'going through %d messages' % q_author.message_set.all().count()\r
-        for m in q_author.message_set.all():\r
-            #print m.message\r
-            match = record_answer_event_re.search(m.message)\r
-            if match:\r
-                found_match = True\r
-                try:\r
-                    cnt = int(match.group(1))\r
-                except:\r
-                    cnt = 1\r
-                m.message = u"You have received %d <a href=\"%s?sort=responses\">new responses</a>."\\r
-                            % (cnt+1, q_author.get_profile_url())\r
-\r
-                m.save()\r
-                break\r
-        if not found_match:\r
-            msg = u"You have received a <a href=\"%s?sort=responses\">new response</a>."\\r
-                    % q_author.get_profile_url()\r
-\r
-            q_author.message_set.create(message=msg)\r
-\r
-post_save.connect(record_answer_event, sender=Answer)
\ No newline at end of file
+#record_answer_event_re = re.compile("You have received (a|\d+) .*new response.*")\r
+#def record_answer_event(instance, created, **kwargs):\r
+#    if created:\r
+#        q_author = instance.question.author\r
+#        found_match = False\r
+#        #print 'going through %d messages' % q_author.message_set.all().count()\r
+#        for m in q_author.message_set.all():\r
+##            #print m.message\r
+# #           match = record_answer_event_re.search(m.message)\r
+#            if match:\r
+#                found_match = True\r
+#                try:\r
+#                    cnt = int(match.group(1))\r
+#                except:\r
+#                    cnt = 1\r
+##                m.message = u"You have received %d <a href=\"%s?sort=responses\">new responses</a>."\\r
+# #                           % (cnt+1, q_author.get_profile_url())\r
+#\r
+#                m.save()\r
+#                break\r
+#        if not found_match:\r
+#            msg = u"You have received a <a href=\"%s?sort=responses\">new response</a>."\\r
+#                    % q_author.get_profile_url()\r
+#\r
+#            q_author.message_set.create(message=msg)\r
+#\r
+#post_save.connect(record_answer_event, sender=Answer)
\ No newline at end of file