]> git.openstreetmap.org Git - osqa.git/blob - forum/management/commands/send_email_alerts.py
80159e7988554b2ccbaba2188670836816f4333d
[osqa.git] / forum / management / commands / send_email_alerts.py
1 import datetime
2 from forum.models import *
3 from forum import settings
4 from django.db import models
5 from forum.utils.mail import send_template_email
6 from django.core.management.base import NoArgsCommand
7 from forum.settings.email import EMAIL_DIGEST_FLAG
8 from django.utils import translation
9 import logging
10
11 SHOW_N_MORE_ACTIVE_NEW_MEMBERS = 5
12 SUB_QUESTION_LIST_LENGTH = 5
13 TRY_N_USER_TAGS = 5
14
15 class DigestQuestionsIndex(object):
16     def __init__(self, from_date):
17         self.from_date = from_date
18
19         new_questions = Question.objects.filter_state(deleted=False).\
20             filter(added_at__gt=from_date).\
21             annotate(n_actions=models.Count('actions')).\
22             annotate(child_count=models.Count('all_children'))
23
24         hotness = lambda q: 3*q.child_count + q.n_actions
25
26         for q in new_questions:
27             q.hotness=hotness(q)
28
29         self.questions = sorted(new_questions, lambda q1, q2: q2.hotness - q1.hotness)
30         self.count = len(self.questions)
31
32     def unseen_question(self, user, question):
33         try:
34             subscription = QuestionSubscription.objects.get(question=q, user=user)
35         except:
36             subscription = None
37
38         return (not subscription) or subscription.last_view < q.last_activity_at
39
40     def get_for_user(self, user):
41         user_tags = list(user.marked_tags.filter(user_selections__reason='good'))
42
43         if len(user_tags) < TRY_N_USER_TAGS:
44             user_tags += list(Tag.objects.filter(models.Q(nodes__author=user) | models.Q(nodes__children__author=user)) \
45                 .annotate(user_tag_usage_count=models.Count('name')).order_by('-user_tag_usage_count')[:TRY_N_USER_TAGS - len(user_tags)])
46
47         user_tag_names = set([t.name for t in user_tags])
48
49
50         subscriptions = user.subscriptions.filter(added_at__lt=self.from_date, last_activity_at__gt=models.F('questionsubscription__last_view')
51                                                   ).order_by('-questionsubscription__last_view')[:SUB_QUESTION_LIST_LENGTH]
52
53         unseen_questions = [q for q in self.questions if self.unseen_question(user, q)]
54
55         interesting = []
56
57         for q in unseen_questions:
58             if len(set(q.tagname_list()) & user_tag_names): interesting.append(q)
59
60
61         may_help = []
62         if len(interesting):
63             if len(interesting) > SUB_QUESTION_LIST_LENGTH:
64                 may_help = interesting[SUB_QUESTION_LIST_LENGTH:][-SUB_QUESTION_LIST_LENGTH:]
65                 interesting = interesting[:SUB_QUESTION_LIST_LENGTH]
66         else:
67             interesting = unseen_questions[:SUB_QUESTION_LIST_LENGTH]
68
69         return {'interesting': interesting, 'may_help': may_help, 'subscriptions': subscriptions}
70
71
72
73
74 class Command(NoArgsCommand):
75     def handle_noargs(self, **options):
76         try:
77             translation.activate(settings.LANGUAGE_CODE)
78         except:
79             logging.error("Unable to set the locale in the send emails cron job")
80
81         digest_control = EMAIL_DIGEST_FLAG.value
82
83         if digest_control is None:
84             digest_control = {
85             'LAST_DAILY': datetime.datetime.now() - datetime.timedelta(days=1),
86             'LAST_WEEKLY': datetime.datetime.now() - datetime.timedelta(days=1),
87             }
88
89         from_date = digest_control['LAST_DAILY']
90         digest_control['LAST_DAILY'] = datetime.datetime.now()
91
92         EMAIL_DIGEST_FLAG.set_value(digest_control)
93
94         users = User.objects.filter(subscription_settings__enable_notifications=True, subscription_settings__send_digest=True)
95         new_members = User.objects.filter(is_active=True, date_joined__gt=from_date).annotate(n_actions=models.Count('actions')).order_by('-n_actions')
96
97         new_member_count = new_members.count()
98
99         # The number of the flagged content for the day
100         flagged_count = Flag.objects.filter(flagged_at__gt=datetime.datetime.today()-datetime.timedelta(days=1)).count()
101
102         if new_member_count >= SHOW_N_MORE_ACTIVE_NEW_MEMBERS:
103             new_members = new_members[:SHOW_N_MORE_ACTIVE_NEW_MEMBERS]
104             show_all_users = True
105         else:
106             show_all_users = False
107
108         digest = DigestQuestionsIndex(from_date)
109
110         if (not new_member_count) and (not digest.count):
111             return
112
113         send_template_email(users, "notifications/digest.html", locals())
114
115