]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/subscriptions.py
Reintegrate merge cacheimp -> trunk.
[osqa.git] / forum / subscriptions.py
index e943d2d34de098f583a63465b76e4a0060c54100..e897e4eebb486e917a75c776b004156a661bf1a8 100644 (file)
@@ -1,11 +1,12 @@
 import os\r
 import re\r
 import datetime\r
 import os\r
 import re\r
 import datetime\r
+import logging\r
 from forum.models import User, Question, Comment, QuestionSubscription, SubscriptionSettings, Answer\r
 from forum.models import User, Question, Comment, QuestionSubscription, SubscriptionSettings, Answer\r
-from forum.utils.mail import send_email\r
+from forum.utils.mail import send_template_email\r
 from django.utils.translation import ugettext as _\r
 from forum.actions import AskAction, AnswerAction, CommentAction, AcceptAnswerAction, UserJoinsAction, QuestionViewAction\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 forum import settings\r
 from django.db.models import Q, F\r
 \r
 def create_subscription_if_not_exists(question, user):\r
 from django.db.models import Q, F\r
 \r
 def create_subscription_if_not_exists(question, user):\r
@@ -15,32 +16,30 @@ def create_subscription_if_not_exists(question, user):
         subscription = QuestionSubscription(question=question, user=user)\r
         subscription.save()\r
 \r
         subscription = QuestionSubscription(question=question, user=user)\r
         subscription.save()\r
 \r
-def apply_default_filters(queryset, excluded_id):\r
-    return queryset.values('email', 'username').exclude(id=excluded_id)\r
+def filter_subscribers(subscribers):\r
+    subscribers = subscribers.exclude(is_active=False)\r
 \r
 \r
-def create_recipients_dict(usr_list):\r
-    return [(s['username'], s['email'], {'username': s['username']}) for s in usr_list]\r
+    if settings.DONT_NOTIFY_UNVALIDATED:\r
+        return subscribers.exclude(email_isvalid=False)\r
+    else:\r
+        return subscribers\r
 \r
 def question_posted(action, new):\r
     question = action.node\r
 \r
 \r
 def question_posted(action, new):\r
     question = action.node\r
 \r
-    subscribers = User.objects.values('email', 'username').filter(\r
+    subscribers = User.objects.filter(\r
             Q(subscription_settings__enable_notifications=True, subscription_settings__new_question='i') |\r
             (Q(subscription_settings__new_question_watched_tags='i') &\r
               Q(marked_tags__name__in=question.tagnames.split(' ')) &\r
               Q(tag_selections__reason='good'))\r
     ).exclude(id=question.author.id).distinct()\r
 \r
             Q(subscription_settings__enable_notifications=True, subscription_settings__new_question='i') |\r
             (Q(subscription_settings__new_question_watched_tags='i') &\r
               Q(marked_tags__name__in=question.tagnames.split(' ')) &\r
               Q(tag_selections__reason='good'))\r
     ).exclude(id=question.author.id).distinct()\r
 \r
-    recipients = create_recipients_dict(subscribers)\r
+    subscribers = filter_subscribers(subscribers)\r
 \r
 \r
-    send_email(settings.EMAIL_SUBJECT_PREFIX + _("New question on %(app_name)s") % dict(app_name=settings.APP_SHORT_NAME),\r
-               recipients, "notifications/newquestion.html", {\r
-        'question': question,\r
-    })\r
+    send_template_email(subscribers, "notifications/newquestion.html", {'question': question})\r
 \r
 \r
-    if question.author.subscription_settings.questions_asked:\r
-        subscription = QuestionSubscription(question=question, user=question.author)\r
-        subscription.save()\r
+    subscription = QuestionSubscription(question=question, user=question.author)\r
+    subscription.save()\r
 \r
     new_subscribers = User.objects.filter(\r
             Q(subscription_settings__all_questions=True) |\r
 \r
     new_subscribers = User.objects.filter(\r
             Q(subscription_settings__all_questions=True) |\r
@@ -58,61 +57,48 @@ def answer_posted(action, new):
     answer = action.node\r
     question = answer.question\r
 \r
     answer = action.node\r
     question = answer.question\r
 \r
-    subscribers = question.subscribers.values('email', 'username').filter(\r
+    subscribers = question.subscribers.filter(\r
             subscription_settings__enable_notifications=True,\r
             subscription_settings__notify_answers=True,\r
             subscription_settings__subscribed_questions='i'\r
     ).exclude(id=answer.author.id).distinct()\r
             subscription_settings__enable_notifications=True,\r
             subscription_settings__notify_answers=True,\r
             subscription_settings__subscribed_questions='i'\r
     ).exclude(id=answer.author.id).distinct()\r
-    recipients = create_recipients_dict(subscribers)\r
 \r
 \r
-    send_email(settings.EMAIL_SUBJECT_PREFIX + _("New answer to '%(question_title)s'") % dict(question_title=question.title),\r
-               recipients, "notifications/newanswer.html", {\r
-        'question': question,\r
-        'answer': answer\r
-    }, threaded=False)\r
+    subscribers = filter_subscribers(subscribers)\r
+\r
+    send_template_email(subscribers, "notifications/newanswer.html", {'answer': answer})\r
 \r
 \r
-    if answer.author.subscription_settings.questions_answered:\r
-        create_subscription_if_not_exists(question, answer.author)\r
+    create_subscription_if_not_exists(question, answer.author)\r
 \r
 AnswerAction.hook(answer_posted)\r
 \r
 \r
 def comment_posted(action, new):\r
     comment = action.node\r
 \r
 AnswerAction.hook(answer_posted)\r
 \r
 \r
 def comment_posted(action, new):\r
     comment = action.node\r
-    post = comment.content_object\r
+    post = comment.parent\r
 \r
     if post.__class__ == Question:\r
         question = post\r
     else:\r
         question = post.question\r
 \r
 \r
     if post.__class__ == Question:\r
         question = post\r
     else:\r
         question = post.question\r
 \r
-    subscribers = question.subscribers.values('email', 'username')\r
-\r
     q_filter = Q(subscription_settings__notify_comments=True) | Q(subscription_settings__notify_comments_own_post=True, id=post.author.id)\r
 \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
+                                nodes__parent=post, nodes__node_type="comment")\r
 \r
 \r
-    subscribers = subscribers.filter(\r
+    subscribers = question.subscribers.filter(\r
             q_filter, subscription_settings__subscribed_questions='i', subscription_settings__enable_notifications=True\r
     ).exclude(id=comment.user.id).distinct()\r
 \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
+    subscribers = filter_subscribers(subscribers)\r
+\r
 \r
 \r
-    send_email(settings.EMAIL_SUBJECT_PREFIX + _("New comment on %(question_title)s") % dict(question_title=question.title),\r
-               recipients, "notifications/newcomment.html", {\r
-                'comment': comment,\r
-                'post': post,\r
-                'question': question,\r
-    }, threaded=False)\r
+    send_template_email(subscribers, "notifications/newcomment.html", {'comment': comment})\r
 \r
 \r
-    if comment.user.subscription_settings.questions_commented:\r
-        create_subscription_if_not_exists(question, comment.user)\r
+    create_subscription_if_not_exists(question, comment.user)\r
 \r
 CommentAction.hook(comment_posted)\r
 \r
 \r
 CommentAction.hook(comment_posted)\r
 \r
@@ -120,34 +106,27 @@ CommentAction.hook(comment_posted)
 def answer_accepted(action, new):\r
     question = action.node.question\r
 \r
 def answer_accepted(action, new):\r
     question = action.node.question\r
 \r
-    subscribers = question.subscribers.values('email', 'username').filter(\r
+    subscribers = question.subscribers.filter(\r
             subscription_settings__enable_notifications=True,\r
             subscription_settings__enable_notifications=True,\r
-            subscription_settings__notify_accepted=True,\r
             subscription_settings__subscribed_questions='i'\r
             subscription_settings__subscribed_questions='i'\r
-    ).exclude(id=instance.accepted_by.id).distinct()\r
-    recipients = create_recipients_dict(subscribers)\r
+    ).exclude(id=action.node.nstate.accepted.by.id).distinct()\r
+    \r
+    subscribers = filter_subscribers(subscribers)\r
 \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
+    send_template_email(subscribers, "notifications/answeraccepted.html", {'answer': action.node})\r
 \r
 AcceptAnswerAction.hook(answer_accepted)\r
 \r
 \r
 def member_joined(action, new):\r
 \r
 AcceptAnswerAction.hook(answer_accepted)\r
 \r
 \r
 def member_joined(action, new):\r
-    subscribers = User.objects.values('email', 'username').filter(\r
+    subscribers = User.objects.filter(\r
             subscription_settings__enable_notifications=True,\r
             subscription_settings__member_joins='i'\r
     ).exclude(id=action.user.id).distinct()\r
 \r
             subscription_settings__enable_notifications=True,\r
             subscription_settings__member_joins='i'\r
     ).exclude(id=action.user.id).distinct()\r
 \r
-    recipients = create_recipients_dict(subscribers)\r
+    subscribers = filter_subscribers(subscribers)\r
 \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': action.user,\r
-    }, threaded=False)\r
+    send_template_email(subscribers, "notifications/newmember.html", {'newmember': action.user})\r
 \r
 UserJoinsAction.hook(member_joined)\r
 \r
 \r
 UserJoinsAction.hook(member_joined)\r
 \r
@@ -156,12 +135,12 @@ def question_viewed(action, new):
         return\r
 \r
     try:\r
         return\r
 \r
     try:\r
-        subscription = QuestionSubscription.objects.get(question=action.question, user=action.viewuser)\r
+        subscription = QuestionSubscription.objects.get(question=action.node, user=action.viewuser)\r
         subscription.last_view = datetime.datetime.now()\r
         subscription.save()\r
     except:\r
         if action.viewuser.subscription_settings.questions_viewed:\r
         subscription.last_view = datetime.datetime.now()\r
         subscription.save()\r
     except:\r
         if action.viewuser.subscription_settings.questions_viewed:\r
-            subscription = QuestionSubscription(question=action.question, user=action.viewuser)\r
+            subscription = QuestionSubscription(question=action.node, user=action.viewuser)\r
             subscription.save()\r
 \r
 QuestionViewAction.hook(question_viewed)\r
             subscription.save()\r
 \r
 QuestionViewAction.hook(question_viewed)\r
@@ -194,4 +173,4 @@ QuestionViewAction.hook(question_viewed)
 #\r
 #            q_author.message_set.create(message=msg)\r
 #\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
+#post_save.connect(record_answer_event, sender=Answer)\r