]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/management/commands/send_email_alerts.py
Resolves Jira OSQA-682, do not use the MAX_VOTES_PER_DAT setting, but the can_vote_co...
[osqa.git] / forum / management / commands / send_email_alerts.py
index 4cdb2e1d35636fa27a5061539d83095db69a067c..80159e7988554b2ccbaba2188670836816f4333d 100644 (file)
@@ -1,9 +1,10 @@
 import datetime
 from forum.models import *
 import datetime
 from forum.models import *
+from forum import settings
 from django.db import models
 from forum.utils.mail import send_template_email
 from django.core.management.base import NoArgsCommand
 from django.db import models
 from forum.utils.mail import send_template_email
 from django.core.management.base import NoArgsCommand
-from forum.settings.email import EMAIL_DIGEST_CONTROL
+from forum.settings.email import EMAIL_DIGEST_FLAG
 from django.utils import translation
 import logging
 
 from django.utils import translation
 import logging
 
@@ -11,8 +12,6 @@ SHOW_N_MORE_ACTIVE_NEW_MEMBERS = 5
 SUB_QUESTION_LIST_LENGTH = 5
 TRY_N_USER_TAGS = 5
 
 SUB_QUESTION_LIST_LENGTH = 5
 TRY_N_USER_TAGS = 5
 
-
-
 class DigestQuestionsIndex(object):
     def __init__(self, from_date):
         self.from_date = from_date
 class DigestQuestionsIndex(object):
     def __init__(self, from_date):
         self.from_date = from_date
@@ -79,24 +78,27 @@ class Command(NoArgsCommand):
         except:
             logging.error("Unable to set the locale in the send emails cron job")
 
         except:
             logging.error("Unable to set the locale in the send emails cron job")
 
-        digest_control = EMAIL_DIGEST_CONTROL.value
+        digest_control = EMAIL_DIGEST_FLAG.value
 
         if digest_control is None:
 
         if digest_control is None:
-            digest_control = KeyValue(key='DIGEST_CONTROL', value={
+            digest_control = {
             'LAST_DAILY': datetime.datetime.now() - datetime.timedelta(days=1),
             'LAST_WEEKLY': datetime.datetime.now() - datetime.timedelta(days=1),
             'LAST_DAILY': datetime.datetime.now() - datetime.timedelta(days=1),
             'LAST_WEEKLY': datetime.datetime.now() - datetime.timedelta(days=1),
-            })
+            }
 
 
-        from_date = digest_control.value['LAST_DAILY']
-        digest_control.value['LAST_DAILY'] = datetime.datetime.now()
+        from_date = digest_control['LAST_DAILY']
+        digest_control['LAST_DAILY'] = datetime.datetime.now()
 
 
-        EMAIL_DIGEST_CONTROL.set_value(digest_control)
+        EMAIL_DIGEST_FLAG.set_value(digest_control)
 
 
-        users = User.objects.filter(subscription_settings__enable_notifications=True)
+        users = User.objects.filter(subscription_settings__enable_notifications=True, subscription_settings__send_digest=True)
         new_members = User.objects.filter(is_active=True, date_joined__gt=from_date).annotate(n_actions=models.Count('actions')).order_by('-n_actions')
 
         new_member_count = new_members.count()
 
         new_members = User.objects.filter(is_active=True, date_joined__gt=from_date).annotate(n_actions=models.Count('actions')).order_by('-n_actions')
 
         new_member_count = new_members.count()
 
+        # The number of the flagged content for the day
+        flagged_count = Flag.objects.filter(flagged_at__gt=datetime.datetime.today()-datetime.timedelta(days=1)).count()
+
         if new_member_count >= SHOW_N_MORE_ACTIVE_NEW_MEMBERS:
             new_members = new_members[:SHOW_N_MORE_ACTIVE_NEW_MEMBERS]
             show_all_users = True
         if new_member_count >= SHOW_N_MORE_ACTIVE_NEW_MEMBERS:
             new_members = new_members[:SHOW_N_MORE_ACTIVE_NEW_MEMBERS]
             show_all_users = True
@@ -105,6 +107,9 @@ class Command(NoArgsCommand):
 
         digest = DigestQuestionsIndex(from_date)
 
 
         digest = DigestQuestionsIndex(from_date)
 
+        if (not new_member_count) and (not digest.count):
+            return
+
         send_template_email(users, "notifications/digest.html", locals())
 
 
         send_template_email(users, "notifications/digest.html", locals())