- digest_control = KeyValue(key='DIGEST_CONTROL', value={
- 'LAST_DAILY': datetime.now() - timedelta(days=1),
- 'LAST_WEEKLY': datetime.now() - timedelta(days=1),
- })
-
- return digest_control
-
- def prepare_activity(self, since):
- all_activity = Activity.objects.filter(active_at__gt=since, activity_type__in=(
- const.TYPE_ACTIVITY_ASK_QUESTION, const.TYPE_ACTIVITY_ANSWER,
- const.TYPE_ACTIVITY_COMMENT_QUESTION, const.TYPE_ACTIVITY_COMMENT_ANSWER,
- const.TYPE_ACTIVITY_MARK_ANSWER
- )).order_by('active_at')
-
- question_records = {}
- new_questions = []
-
-
- for activity in all_activity:
- try:
- question = self.get_question_for_activity(activity)
-
- if not question.id in question_records:
- question_records[question.id] = QuestionRecord(question)
-
- question_records[question.id].log_activity(activity)
-
- if activity.activity_type == const.TYPE_ACTIVITY_ASK_QUESTION:
- new_questions.append(question)
- except:
- pass
-
- return new_questions, question_records
-
- def get_question_for_activity(self, activity):
- if activity.activity_type == const.TYPE_ACTIVITY_ASK_QUESTION:
- question = activity.content_object
- elif activity.activity_type == const.TYPE_ACTIVITY_ANSWER:
- question = activity.content_object.question
- elif activity.activity_type == const.TYPE_ACTIVITY_COMMENT_QUESTION:
- question = activity.content_object.content_object
- elif activity.activity_type == const.TYPE_ACTIVITY_COMMENT_ANSWER:
- question = activity.content_object.content_object.question
- elif activity.activity_type == const.TYPE_ACTIVITY_MARK_ANSWER:
- question = activity.content_object.question
+ logging.error("Unable to set the locale in the send emails cron job")
+
+ digest_control = EMAIL_DIGEST_FLAG.value
+
+ if digest_control is None:
+ digest_control = {
+ 'LAST_DAILY': datetime.datetime.now() - datetime.timedelta(days=1),
+ 'LAST_WEEKLY': datetime.datetime.now() - datetime.timedelta(days=1),
+ }
+
+ from_date = digest_control['LAST_DAILY']
+ digest_control['LAST_DAILY'] = datetime.datetime.now()
+
+ EMAIL_DIGEST_FLAG.set_value(digest_control)
+
+ 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()
+
+ # 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