]> git.openstreetmap.org Git - osqa.git/commitdiff
Tried to fix OSQA 266, Emails sent from the cron job are not using the website locale.
authorhernani <hernani@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Tue, 8 Jun 2010 11:13:42 +0000 (11:13 +0000)
committerhernani <hernani@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Tue, 8 Jun 2010 11:13:42 +0000 (11:13 +0000)
git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@391 0cfe37f9-358a-4d5e-be75-b63607b5c754

forum/management/commands/send_email_alerts.py

index 729ebaa3e7f590460e18b353af008d9ee1585dd7..fa9d442be15ee380296d5ab0aae74f086d7d59b0 100644 (file)
@@ -3,11 +3,14 @@ from django.core.management.base import NoArgsCommand
 from django.utils.translation import ugettext as _
 from django.template import loader, Context, Template
 from django.core.mail import EmailMultiAlternatives
+from django.utils import translation
+from django.conf import settings
 from forum import settings
 from forum.settings.email import EMAIL_DIGEST_CONTROL
 from forum import actions
 from forum.models import KeyValue, Action, User, QuestionSubscription
 from forum.utils.mail import send_email
+import logging
 
 class QuestionRecord:
     def __init__(self, question):
@@ -30,20 +33,25 @@ class QuestionRecord:
             accepted = None
 
         return {
-            'answers': answers,
-            'comments': comments,
-            'accepted': accepted,
+        'answers': answers,
+        'comments': comments,
+        'accepted': accepted,
         }
 
 
 class Command(NoArgsCommand):
     def handle_noargs(self, **options):
+        try:
+            translation.activate(settings.LANGUAGE_CODE)
+        except:
+            logging.error("Unable to set the locale in the send emails cron job")
+
         digest_control = EMAIL_DIGEST_CONTROL.value
 
         if digest_control is None:
             digest_control = KeyValue(key='DIGEST_CONTROL', value={
-                'LAST_DAILY': datetime.now() - timedelta(days=1),
-                'LAST_WEEKLY': datetime.now() - timedelta(days=1),
+            'LAST_DAILY': datetime.now() - timedelta(days=1),
+            'LAST_WEEKLY': datetime.now() - timedelta(days=1),
             })
 
         self.send_digest('daily', 'd', digest_control.value['LAST_DAILY'])
@@ -54,10 +62,9 @@ class Command(NoArgsCommand):
             digest_control.value['LAST_WEEKLY'] = datetime.now()
 
         EMAIL_DIGEST_CONTROL.set_value(digest_control)
-            
+
 
     def send_digest(self, name, char_in_db, control_date):
-        
         new_questions, question_records = self.prepare_activity(control_date)
         new_users = User.objects.filter(date_joined__gt=control_date)
 
@@ -69,8 +76,8 @@ class Command(NoArgsCommand):
 
         for u in users:
             context = {
-                'user': u,
-                'digest_type': name,
+            'user': u,
+            'digest_type': name,
             }
 
             if u.subscription_settings.member_joins == char_in_db:
@@ -100,7 +107,8 @@ class Command(NoArgsCommand):
                         if not u.subscription_settings.notify_accepted:
                             del record['accepted']
 
-                        if record.get('answers', False) or record.get('comments', False) or record.get('accepted', False):
+                        if record.get('answers', False) or record.get('comments', False) or record.get('accepted', False
+                                                                                                       ):
                             activity_in_subscriptions.append({'question': r.question, 'activity': record})
                     except:
                         pass
@@ -109,31 +117,31 @@ class Command(NoArgsCommand):
             else:
                 context['activity_in_subscriptions'] = False
 
-
             if u.subscription_settings.new_question == char_in_db:
                 context['new_questions'] = new_questions
                 context['watched_tags_only'] = False
             elif u.subscription_settings.new_question_watched_tags == char_in_db:
                 context['new_questions'] = [q for q in new_questions if
-                                            q.tags.filter(id__in=u.marked_tags.filter(user_selections__reason='good')).count() > 0]
+                                            q.tags.filter(id__in=u.marked_tags.filter(user_selections__reason='good')
+                                                          ).count() > 0]
                 context['watched_tags_only'] = True
             else:
                 context['new_questions'] = False
 
             if context['new_users'] or context['activity_in_subscriptions'] or context['new_questions']:
-                send_email(digest_subject, [(u.username, u.email)], "notifications/digest.html", context, threaded=False)
+                send_email(digest_subject, [(u.username, u.email)], "notifications/digest.html", context, threaded=False
+                           )
 
 
     def prepare_activity(self, since):
         all_activity = Action.objects.filter(canceled=False, action_date__gt=since, action_type__in=(
-            actions.AskAction.get_type(),actions.AnswerAction.get_type(),
-            actions.CommentAction.get_type(), actions.AcceptAnswerAction.get_type()
+        actions.AskAction.get_type(), actions.AnswerAction.get_type(),
+        actions.CommentAction.get_type(), actions.AcceptAnswerAction.get_type()
         )).order_by('action_date')
 
         question_records = {}
         new_questions = []
 
-
         for activity in all_activity:
             try:
                 question = activity.node.abs_parent