5 from forum.models import User, Question, Comment, QuestionSubscription, SubscriptionSettings, Answer
6 from forum.utils.mail import send_template_email
7 from django.utils.translation import ugettext as _
8 from forum.actions import AskAction, AnswerAction, CommentAction, AcceptAnswerAction, UserJoinsAction, QuestionViewAction
9 from forum import settings
10 from django.db.models import Q, F
12 def create_subscription_if_not_exists(question, user):
14 subscription = QuestionSubscription.objects.get(question=question, user=user)
16 except QuestionSubscription.MultipleObjectsReturned:
18 except QuestionSubscription.DoesNotExist:
19 subscription = QuestionSubscription(question=question, user=user)
27 def filter_subscribers(subscribers):
28 subscribers = subscribers.exclude(is_active=False)
30 if settings.DONT_NOTIFY_UNVALIDATED:
31 return subscribers.exclude(email_isvalid=False)
35 def question_posted(action, new):
36 question = action.node
38 if not question.is_notifiable:
41 subscribers = User.objects.filter(
42 Q(subscription_settings__enable_notifications=True, subscription_settings__new_question='i') |
43 (Q(subscription_settings__new_question_watched_tags='i') &
44 Q(marked_tags__name__in=question.tagnames.split(' ')) &
45 Q(tag_selections__reason='good'))
46 ).exclude(id=question.author.id).distinct()
48 subscribers = filter_subscribers(subscribers)
50 send_template_email(subscribers, "notifications/newquestion.html", {'question': question})
52 subscription = QuestionSubscription(question=question, user=question.author)
55 new_subscribers = User.objects.filter(
56 Q(subscription_settings__all_questions=True) |
57 Q(subscription_settings__all_questions_watched_tags=True,
58 marked_tags__name__in=question.tagnames.split(' '),
59 tag_selections__reason='good'))
61 for user in new_subscribers:
62 create_subscription_if_not_exists(question, user)
64 AskAction.hook(question_posted)
67 def answer_posted(action, new):
69 question = answer.question
71 logging.error("Answer posted: %s" % str(answer.is_notifiable))
73 if not answer.is_notifiable or not question.is_notifiable:
76 subscribers = question.subscribers.filter(
77 subscription_settings__enable_notifications=True,
78 subscription_settings__notify_answers=True,
79 subscription_settings__subscribed_questions='i'
80 ).exclude(id=answer.author.id).distinct()
82 subscribers = filter_subscribers(subscribers)
84 send_template_email(subscribers, "notifications/newanswer.html", {'answer': answer})
86 create_subscription_if_not_exists(question, answer.author)
88 AnswerAction.hook(answer_posted)
91 def comment_posted(action, new):
95 if not comment.is_notifiable or not post.is_notifiable:
98 if post.__class__ == Question:
101 question = post.question
103 q_filter = Q(subscription_settings__notify_comments=True) | Q(subscription_settings__notify_comments_own_post=True, id=post.author.id)
105 inreply = re.search('@\w+', comment.comment)
106 if inreply is not None:
107 q_filter = q_filter | Q(subscription_settings__notify_reply_to_comments=True,
108 username__istartswith=inreply.group(0)[1:],
109 nodes__parent=post, nodes__node_type="comment")
111 subscribers = question.subscribers.filter(
112 q_filter, subscription_settings__subscribed_questions='i', subscription_settings__enable_notifications=True
113 ).exclude(id=comment.user.id).distinct()
115 subscribers = filter_subscribers(subscribers)
118 send_template_email(subscribers, "notifications/newcomment.html", {'comment': comment})
120 create_subscription_if_not_exists(question, comment.user)
122 CommentAction.hook(comment_posted)
125 def answer_accepted(action, new):
126 question = action.node.question
128 if not question.is_notifiable:
131 subscribers = question.subscribers.filter(
132 subscription_settings__enable_notifications=True,
133 subscription_settings__subscribed_questions='i'
134 ).exclude(id=action.node.nstate.accepted.by.id).distinct()
136 subscribers = filter_subscribers(subscribers)
138 send_template_email(subscribers, "notifications/answeraccepted.html", {'answer': action.node})
140 AcceptAnswerAction.hook(answer_accepted)
143 def member_joined(action, new):
144 subscribers = User.objects.filter(
145 subscription_settings__enable_notifications=True,
146 subscription_settings__member_joins='i'
147 ).exclude(id=action.user.id).distinct()
149 subscribers = filter_subscribers(subscribers)
151 send_template_email(subscribers, "notifications/newmember.html", {'newmember': action.user})
153 UserJoinsAction.hook(member_joined)
155 def question_viewed(action, new):
156 if not action.viewuser.is_authenticated():
160 subscription = QuestionSubscription.objects.get(question=action.node, user=action.viewuser)
161 subscription.last_view = datetime.datetime.now()
164 if action.viewuser.subscription_settings.questions_viewed:
165 subscription = QuestionSubscription(question=action.node, user=action.viewuser)
168 QuestionViewAction.hook(question_viewed)
171 #todo: translate this
172 #record_answer_event_re = re.compile("You have received (a|\d+) .*new response.*")
173 #def record_answer_event(instance, created, **kwargs):
175 # q_author = instance.question.author
176 # found_match = False
177 # #print 'going through %d messages' % q_author.message_set.all().count()
178 # for m in q_author.message_set.all():
180 # # match = record_answer_event_re.search(m.message)
184 # cnt = int(match.group(1))
187 ## m.message = u"You have received %d <a href=\"%s?sort=responses\">new responses</a>."\
188 # # % (cnt+1, q_author.get_profile_url())
192 # if not found_match:
193 # msg = u"You have received a <a href=\"%s?sort=responses\">new response</a>."\
194 # % q_author.get_profile_url()
196 # q_author.message_set.create(message=msg)
198 #post_save.connect(record_answer_event, sender=Answer)