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):
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'])
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)
for u in users:
context = {
- 'user': u,
- 'digest_type': name,
+ 'user': u,
+ 'digest_type': name,
}
if u.subscription_settings.member_joins == char_in_db:
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
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