summaryrefslogtreecommitdiffstats
path: root/forum/management/commands/send_email_alerts.py
blob: 283d56832c299e8c194b667de77c24e53bcc1b34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
from django.core.management.base import NoArgsCommand
from django.db import connection
from django.db.models import Q, F
from forum.models import *
<<<<<<< HEAD:forum/management/commands/send_email_alerts.py
=======
from forum import const 
>>>>>>> 82d35490db90878f013523c4d1a5ec3af2df8b23:forum/management/commands/send_email_alerts.py
from django.core.mail import EmailMessage
from django.utils.translation import ugettext as _
from django.utils.translation import ungettext
import datetime
import settings
<<<<<<< HEAD:forum/management/commands/send_email_alerts.py
=======
import logging
from utils.odict import OrderedDict
>>>>>>> 82d35490db90878f013523c4d1a5ec3af2df8b23:forum/management/commands/send_email_alerts.py

class Command(NoArgsCommand):
    def handle_noargs(self,**options):
        try:
            self.send_email_alerts()
        except Exception, e:
            print e
        finally:
            connection.close()

    def get_updated_questions_for_user(self,user):
<<<<<<< HEAD:forum/management/commands/send_email_alerts.py
        q_sel = []
        q_ask = []
        q_ans = []
        q_all = []
=======
        q_sel = None 
        q_ask = None 
        q_ans = None 
        q_all = None 
>>>>>>> 82d35490db90878f013523c4d1a5ec3af2df8b23:forum/management/commands/send_email_alerts.py
        now = datetime.datetime.now()
        Q_set1 = Question.objects.exclude(
                                        last_activity_by=user,
                                  ).exclude(
                                        last_activity_at__lt=user.date_joined
                                  ).filter(
                                        Q(viewed__who=user,viewed__when__lt=F('last_activity_at')) | \
                                        ~Q(viewed__who=user)
                                  ).exclude(
                                        deleted=True
                                  ).exclude(
                                        closed=True
                                  )
<<<<<<< HEAD:forum/management/commands/send_email_alerts.py
=======
            
>>>>>>> 82d35490db90878f013523c4d1a5ec3af2df8b23:forum/management/commands/send_email_alerts.py
        user_feeds = EmailFeedSetting.objects.filter(subscriber=user).exclude(frequency='n')
        for feed in user_feeds:
            cutoff_time = now - EmailFeedSetting.DELTA_TABLE[feed.frequency]
            if feed.reported_at == None or feed.reported_at <= cutoff_time:
<<<<<<< HEAD:forum/management/commands/send_email_alerts.py
                Q_set = Q_set1.exclude(last_activity_at__gt=cutoff_time)
=======
                Q_set = Q_set1.exclude(last_activity_at__gt=cutoff_time)#report these excluded later
>>>>>>> 82d35490db90878f013523c4d1a5ec3af2df8b23:forum/management/commands/send_email_alerts.py
                feed.reported_at = now
                feed.save()#may not actually report anything, depending on filters below
                if feed.feed_type == 'q_sel':
                    q_sel = Q_set.filter(followed_by=user)
<<<<<<< HEAD:forum/management/commands/send_email_alerts.py
                    q_sel.cutoff_time = cutoff_time
=======
                    q_sel.cutoff_time = cutoff_time #store cutoff time per query set
>>>>>>> 82d35490db90878f013523c4d1a5ec3af2df8b23:forum/management/commands/send_email_alerts.py
                elif feed.feed_type == 'q_ask':
                    q_ask = Q_set.filter(author=user)
                    q_ask.cutoff_time = cutoff_time
                elif feed.feed_type == 'q_ans':
                    q_ans = Q_set.filter(answers__author=user)
                    q_ans.cutoff_time = cutoff_time
                elif feed.feed_type == 'q_all':
<<<<<<< HEAD:forum/management/commands/send_email_alerts.py
                    q_all = Q_set
                    q_all.cutoff_time = cutoff_time
        #build list in this order
        q_tbl = {} 
        def extend_question_list(src, dst):
            if isinstance(src,list):
                return
            cutoff_time = src.cutoff_time
            for q in src:
                if q in dst:
                    if cutoff_time < dst[q]:
                        dst[q] = cutoff_time
                else:
                    dst[q] = cutoff_time

        extend_question_list(q_sel, q_tbl)
        extend_question_list(q_ask, q_tbl)
        extend_question_list(q_ans, q_tbl)
        extend_question_list(q_all, q_tbl)

        ctype = ContentType.objects.get_for_model(Question)
        out = {}
        for q, cutoff_time in q_tbl.items():
            #todo use Activity, but first start keeping more Activity records
            #act = Activity.objects.filter(content_type=ctype, object_id=q.id)
            #get info on question edits, answer edits, comments
            out[q] = {}
            q_rev = QuestionRevision.objects.filter(question=q,revised_at__lt=cutoff_time)
            q_rev = q_rev.exclude(author=user)
            out[q]['q_rev'] = len(q_rev)
            if len(q_rev) > 0 and q.added_at == q_rev[0].revised_at:
                out[q]['q_rev'] = 0
                out[q]['new_q'] = True
            else:
                out[q]['new_q'] = False
                
            new_ans = Answer.objects.filter(question=q,added_at__lt=cutoff_time)
            new_ans = new_ans.exclude(author=user)
            out[q]['new_ans'] = len(new_ans)
            ans_rev = AnswerRevision.objects.filter(answer__question=q,revised_at__lt=cutoff_time)
            ans_rev = ans_rev.exclude(author=user)
            out[q]['ans_rev'] = len(ans_rev)
        return out 

    def __act_count(self,string,number,output):
=======
                    if user.tag_filter_setting == 'ignored':
                        ignored_tags = Tag.objects.filter(user_selections___reason='bad',user_selections__user=user)
                        q_all = Q_set.exclude( tags__in=ignored_tags )
                    else:
                        selected_tags = Tag.objects.filter(user_selections___reason='good',user_selections__user=user)
                        q_all = Q_set.filter( tags__in=selected_tags )
                    q_all.cutoff_time = cutoff_time
        #build list in this order
        q_list = OrderedDict()
        def extend_question_list(src, dst):
            """src is a query set with questions
               or an empty list
                dst - is an ordered dictionary
            """
            if src is None:
                return #will not do anything if subscription of this type is not used
            cutoff_time = src.cutoff_time
            for q in src:
                if q in dst:
                    if cutoff_time < dst[q]['cutoff_time']:
                        dst[q]['cutoff_time'] = cutoff_time
                else:
                    #initialise a questions metadata dictionary to use for email reporting
                    dst[q] = {'cutoff_time':cutoff_time}

        extend_question_list(q_sel, q_list)
        extend_question_list(q_ask, q_list)
        extend_question_list(q_ans, q_list)
        extend_question_list(q_all, q_list)

        ctype = ContentType.objects.get_for_model(Question)
        EMAIL_UPDATE_ACTIVITY = const.TYPE_ACTIVITY_QUESTION_EMAIL_UPDATE_SENT
        for q, meta_data in q_list.items():
            #todo use Activity, but first start keeping more Activity records
            #act = Activity.objects.filter(content_type=ctype, object_id=q.id)
            #because currently activity is not fully recorded to through
            #revision records to see what kind modifications were done on
            #the questions and answers
            try:
                update_info = Activity.objects.get(content_type=ctype, 
                                                    object_id=q.id,
                                                    activity_type=EMAIL_UPDATE_ACTIVITY)
                emailed_at = update_info.active_at
            except Activity.DoesNotExist:
                update_info = Activity(user=user, content_object=q, activity_type=EMAIL_UPDATE_ACTIVITY)
                emailed_at = datetime.datetime(1970,1,1)#long time ago
            except Activity.MultipleObjectsReturned:
                raise Exception('server error - multiple question email activities found per user-question pair')

            q_rev = QuestionRevision.objects.filter(question=q,\
                                                    revised_at__lt=cutoff_time,\
                                                    revised_at__gt=emailed_at)
            q_rev = q_rev.exclude(author=user)
            meta_data['q_rev'] = len(q_rev)
            if len(q_rev) > 0 and q.added_at == q_rev[0].revised_at:
                meta_data['q_rev'] = 0
                meta_data['new_q'] = True
            else:
                meta_data['new_q'] = False
                
            new_ans = Answer.objects.filter(question=q,\
                                            added_at__lt=cutoff_time,\
                                            added_at__gt=emailed_at)
            new_ans = new_ans.exclude(author=user)
            meta_data['new_ans'] = len(new_ans)
            ans_rev = AnswerRevision.objects.filter(answer__question=q,\
                                            revised_at__lt=cutoff_time,\
                                            revised_at__gt=emailed_at)
            ans_rev = ans_rev.exclude(author=user)
            meta_data['ans_rev'] = len(ans_rev)
            if len(q_rev) == 0 and len(new_ans) == 0 and len(ans_rev) == 0:
                meta_data['nothing_new'] = True
            else:
                meta_data['nothing_new'] = False
                update_info.active_at = now
                update_info.save() #save question email update activity 
        return q_list 

    def __action_count(self,string,number,output):
>>>>>>> 82d35490db90878f013523c4d1a5ec3af2df8b23:forum/management/commands/send_email_alerts.py
        if number > 0:
            output.append(_(string) % {'num':number})

    def send_email_alerts(self):

<<<<<<< HEAD:forum/management/commands/send_email_alerts.py
        for user in User.objects.all():
            q_list = self.get_updated_questions_for_user(user)
            num_q = len(q_list)
=======
        #todo: move this to template
        for user in User.objects.all():
            q_list = self.get_updated_questions_for_user(user)
            num_q = 0
            num_moot = 0
            for meta_data in q_list.values():
                if meta_data['nothing_new'] == False:
                    num_q += 1
                else:
                    num_moot += 1
>>>>>>> 82d35490db90878f013523c4d1a5ec3af2df8b23:forum/management/commands/send_email_alerts.py
            if num_q > 0:
                url_prefix = settings.APP_URL
                subject = _('email update message subject')
                text = ungettext('%(name)s, this is an update message header for a question', 
                            '%(name)s, this is an update message header for %(num)d questions',num_q) \
                                % {'num':num_q, 'name':user.username}

                text += '<ul>'
<<<<<<< HEAD:forum/management/commands/send_email_alerts.py
                for q, act in q_list.items():
                    act_list = []
                    if act['new_q']:
                        act_list.append(_('new question'))
                    self.__act_count('%(num)d rev', act['q_rev'],act_list)
                    self.__act_count('%(num)d ans', act['new_ans'],act_list)
                    self.__act_count('%(num)d ans rev',act['ans_rev'],act_list)
                    act_token = ', '.join(act_list)
                    text += '<li><a href="%s?sort=latest">%s</a> <font color="#777777">(%s)</font></li>' \
                                % (url_prefix + q.get_absolute_url(), q.title, act_token)
                text += '</ul>'
=======
                for q, meta_data in q_list.items():
                    act_list = []
                    if meta_data['nothing_new']:
                        continue
                    else:
                        if meta_data['new_q']:
                            act_list.append(_('new question'))
                        self.__action_count('%(num)d rev', meta_data['q_rev'],act_list)
                        self.__action_count('%(num)d ans', meta_data['new_ans'],act_list)
                        self.__action_count('%(num)d ans rev',meta_data['ans_rev'],act_list)
                        act_token = ', '.join(act_list)
                        text += '<li><a href="%s?sort=latest">%s</a> <font color="#777777">(%s)</font></li>' \
                                    % (url_prefix + q.get_absolute_url(), q.title, act_token)
                text += '</ul>'
                if num_moot > 0:
                    text += '<p></p>'
                    text += ungettext('There is also one question which was recently '\
                                +'updated but you might not have seen its latest version.',
                            'There are also %(num)d more questions which were recently updated '\
                            +'but you might not have seen their latest version.',num_moot) \
                                % {'num':num_moot,}
                    text += _('Perhaps you could look up previously sent forum reminders in your mailbox.')
                    text += '</p>'

>>>>>>> 82d35490db90878f013523c4d1a5ec3af2df8b23:forum/management/commands/send_email_alerts.py
                link = url_prefix + user.get_profile_url() + '?sort=email_subscriptions'
                text += _('go to %(link)s to change frequency of email updates or %(email)s administrator') \
                                % {'link':link, 'email':settings.ADMINS[0][1]}
                msg = EmailMessage(subject, text, settings.DEFAULT_FROM_EMAIL, [user.email])
                msg.content_subtype = 'html'
                msg.send()