def save(self, *args, **kwargs): if not self.pk: self.slug = uuslug(self.name, instance=self) else: # Update action items current_poll = Poll.objects.get(id=self.pk) action_model = ContentType.objects.get_for_model(self) action_items = ActionItem.objects.filter(content_type=action_model, object_id=self.pk) if current_poll.end != self.end: action_items.update(due_date=self.end.date()) if current_poll.valid_groups != self.valid_groups: action_items.delete() ActionItem.create(self) if not settings.CELERY_ALWAYS_EAGER: if self.is_current_voting: celery_control.revoke(self.task_end_id) elif self.is_future_voting: celery_control.revoke(self.task_start_id) celery_control.revoke(self.task_end_id) if not self.is_future_voting: obj = Poll.objects.get(pk=self.id) if self.end > obj.end: self.is_extended = True super(Poll, self).save()
def notify_event_owners_to_input_metrics(): """Send an email to event creators. After an event has finished event creators are notified that they should input the actual metrics for the event. """ start = datetime.combine(get_date(days=-1), datetime.min.time()) end = datetime.combine(get_date(days=-1), datetime.max.time()) events = Event.objects.filter(end__range=[start, end], has_new_metrics=True, eventmetricoutcome__outcome__isnull=True) events = events.distinct() event_model = ContentType.objects.get_for_model(Event) for event in events: # Before sending an email check that an action item already exists. # If it does, then we have already sent this email. action_item = ActionItem.objects.filter(content_type=event_model, object_id=event.id) if not action_item.exists(): subject = u'[Reminder] Please add the actual metrics for event {0}'.format( event.name) template = 'email/event_creator_notification_to_input_metrics.jinja' data = {'event': event} send_remo_mail.delay(subject=subject, email_template=template, recipients_list=[event.owner.id], data=data) ActionItem.create(instance=event)
def notify_event_owners_to_input_metrics(): """Send an email to event creators. After an event has finished event creators are notified that they should input the actual metrics for the event. """ start = datetime.combine(get_date(days=-1), datetime.min.time()) end = datetime.combine(get_date(days=-1), datetime.max.time()) events = Event.objects.filter( end__range=[start, end], has_new_metrics=True, eventmetricoutcome__outcome__isnull=True ) events = events.distinct() event_model = ContentType.objects.get_for_model(Event) for event in events: # Before sending an email check that an action item already exists. # If it does, then we have already sent this email. action_item = ActionItem.objects.filter(content_type=event_model, object_id=event.id) if not action_item.exists(): subject = "[Reminder] Please add the actual metrics for event {0}".format(event.name) template = "email/event_creator_notification_to_input_metrics.txt" data = {"event": event} send_remo_mail(subject=subject, email_template=template, recipients_list=[event.owner.id], data=data) ActionItem.create(instance=event)
def create_poll_action_items(): """Create action items for current polls.""" # avoid circular dependencies from remo.voting.models import Poll start = now() - timedelta(hours=4) polls = Poll.objects.filter(start__range=[start, now()]) for poll in polls: ActionItem.create(poll)
def test_resolve_action_item_invalid_date(self, mocked_date): model = ContentType.objects.get_for_model(UserProfile) user = UserFactory.create(groups=['Mentor']) mocked_date.return_value = datetime(now().year, now().month, 1) ActionItem.create(user.userprofile) items = ActionItem.objects.filter(content_type=model) eq_(items.count(), 1) eq_(items[0].resolved, False) mocked_date.return_value = datetime(now().year, now().month, 11) resolve_nomination_action_items() eq_(items.count(), 1) eq_(items[0].resolved, False)
def update_budget_needinfo_action_items(sender, instance, action, pk_set, **kwargs): """Update ActionItem objects on needinfo change.""" name = '{0} {1}'.format(NEEDINFO_ACTION, instance.summary) if action == 'post_remove': for pk in pk_set: ActionItem.resolve(instance=instance, user=User.objects.get(pk=pk), name=name) if action == 'pre_clear': for user in instance.budget_needinfo.all(): ActionItem.resolve(instance=instance, user=user, name=name) if action == 'post_add': ActionItem.create(instance=instance)
def send_rotm_nomination_reminder(): """ Send an email reminder to all mentors. The first day of each month, the mentor group receives an email reminder in order to nominate Reps for the Rep of the month voting. """ now_date = now().date() if now_date.day == ROTM_REMINDER_DAY: data = {'month': number2month(now_date.month)} subject = 'Nominate Rep of the month' template = 'emails/mentors_rotm_reminder.txt' send_remo_mail(subject=subject, email_template=template, recipients_list=[settings.REPS_MENTORS_LIST], data=data) mentors = User.objects.filter(groups__name='Mentor') for mentor in mentors: ActionItem.create(mentor.userprofile)
def send_rotm_nomination_reminder(): """ Send an email reminder to all mentors. The first day of each month, the mentor group receives an email reminder in order to nominate Reps for the Rep of the month voting. """ now_date = now().date() if (now_date.day == ROTM_REMINDER_DAY or waffle.switch_is_active('enable_rotm_tasks')): data = {'month': number2month(now_date.month)} subject = 'Nominate Rep of the month' template = 'emails/mentors_rotm_reminder.jinja' send_remo_mail(subject=subject, email_template=template, recipients_list=[settings.REPS_MENTORS_LIST], data=data) mentors = User.objects.filter(groups__name='Mentor') for mentor in mentors: ActionItem.create(mentor.userprofile)
def notify_event_owners_to_input_metrics(): """Send an email to event creators. After an event has finished event creators are notified that they should input the actual metrics for the event. """ start = datetime.combine(get_date(days=-1), datetime.min.time()) end = datetime.combine(get_date(days=-1), datetime.max.time()) events = Event.objects.filter(end__range=[start, end], has_new_metrics=True, eventmetricoutcome__outcome__isnull=True) events = events.distinct() for event in events: subject = ('[Reminder] Please add the actual metrics for event {0}' .format(event.name)) template = 'email/event_creator_notification_to_input_metrics.txt' data = {'event': event} send_remo_mail(subject=subject, email_template=template, recipients_list=[event.owner.id], data=data) ActionItem.create(instance=event)
def notify_event_owners_to_input_metrics(): """Send an email to event creators. After an event has finished event creators are notified that they should input the actual metrics for the event. """ start = datetime.combine(get_date(days=-1), datetime.min.time()) end = datetime.combine(get_date(days=-1), datetime.max.time()) events = Event.objects.filter(end__range=[start, end], has_new_metrics=True, eventmetricoutcome__outcome__isnull=True) events = events.distinct() for event in events: subject = ( '[Reminder] Please add the actual metrics for event {0}'.format( event.name)) template = 'email/event_creator_notification_to_input_metrics.txt' data = {'event': event} send_remo_mail(subject=subject, email_template=template, recipients_list=[event.owner.id], data=data) ActionItem.create(instance=event)
def save(self, *args, **kwargs): """Override save for custom functionality.""" # Make last report notificaton date NULL on new report today = get_date() if self.pk is None and self.user.userprofile.first_report_notification: start = today - datetime.timedelta(weeks=4) end = today + datetime.timedelta(weeks=4) if self.report_date in daterange(start, end): self.user.userprofile.first_report_notification = None self.user.userprofile.second_report_notification = None self.user.userprofile.save() # User streaks functionality one_week = datetime.timedelta(7) current_start = self.user.userprofile.current_streak_start or None longest_start = self.user.userprofile.longest_streak_start or None longest_end = self.user.userprofile.longest_streak_end or None # Save the mentor of the user if no mentor is defined. if not self.mentor: self.mentor = self.user.userprofile.mentor # Save the country if possible saved_report = get_object_or_none(NGReport, id=self.id) if (saved_report and (saved_report.latitude != self.latitude or saved_report.longitude != self.longitude)): country = None try: country = self.location.split(',')[-1].strip() except IndexError: pass if country in COUNTRIES_LIST: self.country = country super(NGReport, self).save() # Resolve the verified action items. action_item_name = u'{0} {1}'.format(VERIFY_ACTION, self.user.get_full_name()) if self.verified_activity: ActionItem.resolve(instance=self, user=self.mentor, name=action_item_name) else: # Create all the action items, if any ActionItem.create(self) if self.is_future_report: return # If there is already a running streak and the report date # is within this streak, update the current streak counter. if (current_start and self.report_date < current_start and self.report_date in daterange((current_start - one_week), current_start)): current_start = self.report_date # If there isn't any current streak, and the report date # is within the current week, let's start the counting. elif not current_start and self.report_date in daterange(get_date(-7), today): current_start = self.report_date # Longest streak section # If longest streak already exists, let's update it. if longest_start and longest_end: # Compare the number of reports registered during # the current streak and the number of reports # during the longest streak. If current streak is bigger # than the previous longest streak, update the longest streak. longest_streak_count = NGReport.objects.filter( report_date__range=(longest_start, longest_end), user=self.user).count() current_streak_count = NGReport.objects.filter( report_date__range=(current_start, today), user=self.user).count() if current_start and current_streak_count > longest_streak_count: longest_start = current_start longest_end = today # This happens only when a user appends a report, dated in the # range of longest streak counters and it's out of the range # of current streak counter. elif self.report_date in daterange(longest_start - one_week, longest_end + one_week): if self.report_date < longest_start: longest_start = self.report_date elif self.report_date > longest_end: longest_end = self.report_date else: # Longest streak counters are empty, let's setup their value longest_start = self.report_date longest_end = self.report_date # Assign the calculated values, to user's profile. self.user.userprofile.current_streak_start = current_start self.user.userprofile.longest_streak_start = longest_start self.user.userprofile.longest_streak_end = longest_end self.user.userprofile.save()
def save(self, *args, **kwargs): """Override save for custom functionality.""" # Make last report notificaton date NULL on new report today = get_date() if self.pk is None and self.user.userprofile.first_report_notification: start = today - datetime.timedelta(weeks=4) end = today + datetime.timedelta(weeks=4) if self.report_date in daterange(start, end): self.user.userprofile.first_report_notification = None self.user.userprofile.second_report_notification = None self.user.userprofile.save() # User streaks functionality one_week = datetime.timedelta(7) current_start = self.user.userprofile.current_streak_start or None longest_start = self.user.userprofile.longest_streak_start or None longest_end = self.user.userprofile.longest_streak_end or None # Save the mentor of the user if no mentor is defined. if not self.mentor: self.mentor = self.user.userprofile.mentor # Save the country if possible saved_report = get_object_or_none(NGReport, id=self.id) if (saved_report and (saved_report.latitude != self.latitude or saved_report.longitude != self.longitude)): country = None try: country = self.location.split(',')[-1].strip() except IndexError: pass if country in COUNTRIES_LIST: self.country = country super(NGReport, self).save() # Resolve the verified action items. action_item_name = u'{0} {1}'.format(VERIFY_ACTION, self.user.get_full_name()) if self.verified_activity: ActionItem.resolve(instance=self, user=self.mentor, name=action_item_name) else: # Create all the action items, if any ActionItem.create(self) if self.is_future_report: return # If there is already a running streak and the report date # is within this streak, update the current streak counter. if (current_start and self.report_date < current_start and self.report_date in daterange( (current_start - one_week), current_start)): current_start = self.report_date # If there isn't any current streak, and the report date # is within the current week, let's start the counting. elif not current_start and self.report_date in daterange( get_date(-7), today): current_start = self.report_date # Longest streak section # If longest streak already exists, let's update it. if longest_start and longest_end: # Compare the number of reports registered during # the current streak and the number of reports # during the longest streak. If current streak is bigger # than the previous longest streak, update the longest streak. longest_streak_count = NGReport.objects.filter( report_date__range=(longest_start, longest_end), user=self.user).count() current_streak_count = NGReport.objects.filter( report_date__range=(current_start, today), user=self.user).count() if current_start and current_streak_count > longest_streak_count: longest_start = current_start longest_end = today # This happens only when a user appends a report, dated in the # range of longest streak counters and it's out of the range # of current streak counter. elif self.report_date in daterange(longest_start - one_week, longest_end + one_week): if self.report_date < longest_start: longest_start = self.report_date elif self.report_date > longest_end: longest_end = self.report_date else: # Longest streak counters are empty, let's setup their value longest_start = self.report_date longest_end = self.report_date # Assign the calculated values, to user's profile. self.user.userprofile.current_streak_start = current_start self.user.userprofile.longest_streak_start = longest_start self.user.userprofile.longest_streak_end = longest_end self.user.userprofile.save()