From: hernani Date: Tue, 8 Jun 2010 11:13:42 +0000 (+0000) Subject: Tried to fix OSQA 266, Emails sent from the cron job are not using the website locale. X-Git-Tag: live~741 X-Git-Url: https://git.openstreetmap.org./osqa.git/commitdiff_plain/3538034a6d2cb9f8377c38aee83d48a7ae37ba13 Tried to fix OSQA 266, Emails sent from the cron job are not using the website locale. git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@391 0cfe37f9-358a-4d5e-be75-b63607b5c754 --- diff --git a/forum/management/commands/send_email_alerts.py b/forum/management/commands/send_email_alerts.py index 729ebaa..fa9d442 100644 --- a/forum/management/commands/send_email_alerts.py +++ b/forum/management/commands/send_email_alerts.py @@ -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