def get_reports_for_year(user, start_year, end_year=None, allow_empty=False, permission=REPORTS_PERMISSION_LEVEL['anonymous']): """Return a list of reports for year.""" reports_list = {} tmp_date = datetime.today() up = user.userprofile today = datetime(year=tmp_date.year, month=tmp_date.month, day=1) date_joined = up.date_joined_program month_first_report = get_month_first_report(user) if not end_year: end_year = start_year for year in range(max(start_year, date_joined.year), end_year + 1): reports = user.reports.filter(month__year=year) reports_list[year] = [] for month in range(1, 13): month = datetime(year=year, month=month, day=1) month_details = { 'name': number2month(month.month, full_name=False), 'fullname': number2month(month.month, full_name=True) } if ((reports.filter(month=month).exists() and ((permission > 1 and month >= today) or (month < today)))): report = reports.get(month=month) month_details['report'] = report month_details['class'] = 'exists' month_details['link'] = get_report_view_url(report) else: month_details['report'] = None if ((permission < 3 or month_first_report > month or month > today)): month_details['class'] = 'unavailable' month_details['link'] = '#' else: month_details['class'] = 'editable' link = reverse('reports_edit_report', kwargs={ 'display_name': up.display_name, 'year': year, 'month': month_details['fullname'] }) month_details['link'] = link reports_list[year].append(month_details) return reports_list
def get_reports_for_year(user, start_year, end_year=None, allow_empty=False, private=True): """Return a list of reports for year.""" reports_list = {} tmp_date = datetime.today() up = user.userprofile today = datetime(year=tmp_date.year, month=tmp_date.month, day=1) date_joined = up.date_joined_program tmp_date = go_fwd_n_months(date_joined) month_first_report = datetime(year=tmp_date.year, day=1, month=tmp_date.month) if not end_year: end_year = start_year for year in range(max(start_year, date_joined.year), end_year+1): reports = user.reports.filter(month__year=year) if (reports.count() == 0 and private == True): reports_list[year] = None continue else: reports_list[year] = [] for month in range(1, 13): month_details = {'name': number2month(month, full_name=False), 'fullname': number2month(month, full_name=True)} if reports.filter(month__month=month).exists(): report = reports.get(month__month=month) month_details['report'] = report month_details['class'] = 'exists' month_details['link'] = get_report_view_url(report) else: month_details['report'] = None date = datetime(year=year, month=month, day=1) if (private or month_first_report > date or date >= today): month_details['class'] = 'unavailable' month_details['link'] = '#' else: month_details['class'] = 'editable' link = reverse('reports_edit_report', kwargs={'display_name': up.display_name, 'year': year, 'month': month_details['fullname']}) month_details['link'] = link reports_list[year].append(month_details) return reports_list
def get_reports_for_year(user, start_year, end_year=None, allow_empty=False, permission=REPORTS_PERMISSION_LEVEL['anonymous']): """Return a list of reports for year.""" reports_list = {} tmp_date = datetime.today() up = user.userprofile today = datetime(year=tmp_date.year, month=tmp_date.month, day=1) date_joined = up.date_joined_program tmp_date = go_fwd_n_months(date_joined) month_first_report = datetime(year=tmp_date.year, day=1, month=tmp_date.month) if not end_year: end_year = start_year for year in range(max(start_year, date_joined.year), end_year+1): reports = user.reports.filter(month__year=year) reports_list[year] = [] for month in range(1, 13): month = datetime(year=year, month=month, day=1) month_details = {'name': number2month(month.month, full_name=False), 'fullname': number2month(month.month, full_name=True)} if (reports.filter(month=month).exists() and ((permission > 1 and month == today) or (month < today))): report = reports.get(month=month) month_details['report'] = report month_details['class'] = 'exists' month_details['link'] = get_report_view_url(report) else: month_details['report'] = None if (permission < 3 or month_first_report > month or month > today): month_details['class'] = 'unavailable' month_details['link'] = '#' else: month_details['class'] = 'editable' link = reverse('reports_edit_report', kwargs={'display_name': up.display_name, 'year': year, 'month': month_details['fullname']}) month_details['link'] = link reports_list[year].append(month_details) return reports_list
def _get_url_args(self): args = [self.user.userprofile.display_name, self.report_date.year, utils.number2month(self.report_date.month), self.report_date.day, self.id] return args
def handle(self, *args, **options): """Prepares a list of reps to be notified and the required template variables. """ date = go_back_n_months(datetime.today(), 1) rep_group = Group.objects.get(name='Rep') reps = (rep_group.user_set .exclude(userprofile__registration_complete=False) .exclude(reports__month__year=date.year, reports__month__month=date.month)) reps_without_report = reps.values_list('id', flat=True) month = number2month(date.month) subject = self.SUBJECT % month data = {'year': date.year, 'month': month} if options['dry_run']: email_reps = reps.values_list('email', flat=True) for recipient in email_reps: msg = 'Second notification sent to %s' % recipient print(msg) else: send_remo_mail(reps_without_report, subject, self.EMAIL_TEMPLATE, data)
def handle(self, *args, **options): """Prepares a list of reps to be notified and the required template variables. """ rep_group = Group.objects.get(name='Rep') reps = rep_group.user_set.exclude( userprofile__registration_complete=False) date = go_back_n_months(datetime.today(), 2) reps_without_report = reps.exclude(reports__month__year=date.year, reports__month__month=date.month) mentors = [rep.userprofile.mentor.id for rep in reps_without_report] month = number2month(date.month) subject = self.SUBJECT % month data = {'year': date.year, 'month': month, 'reps_without_report': reps_without_report} if options['dry_run']: for mentor in mentors: msg = 'Email sent to mentor: %d' % mentor print(msg) else: send_remo_mail(mentors, subject, self.EMAIL_TEMPLATE, data)
def handle(self, *args, **options): """Prepares a list of reps to be notified and the required template variables. """ date = go_back_n_months(datetime.today(), 2) rep_group = Group.objects.get(name='Rep') reps = (rep_group.user_set .exclude(userprofile__registration_complete=False) .exclude(reports__month__year=date.year, reports__month__month=date.month)) reps_without_report = reps.values_list('id', flat=True) month = number2month(date.month) subject = self.SUBJECT % month data = {'year': date.year, 'month': month} if options['dry_run']: email_reps = reps.values_list('email', flat=True) for recipient in email_reps: msg = 'Third notification sent to %s' % recipient print(msg) else: send_remo_mail(reps_without_report, subject, self.EMAIL_TEMPLATE, data)
def get_comment_delete_url(obj): """Return the delete url of a comment.""" up = obj.report.user.userprofile month_name = number2month(obj.report.month.month) return reverse('reports_delete_report_comment', kwargs={'display_name': up.display_name, 'year': obj.report.month.year, 'month': month_name, 'comment_id': obj.id})
def get_comment_delete_url(obj): """Return the delete url of a comment.""" up = obj.user.userprofile month_name = number2month(obj.report.month.month) return reverse('reports_delete_report_comment', kwargs={'display_name': up.display_name, 'year': obj.report.month.year, 'month': month_name, 'comment_id': obj.id})
def handle(self, *args, **options): """Prepares a list of reps to be notified and the required template variables. """ rep_group = Group.objects.get(name="Rep") reps = rep_group.user_set.exclude(userprofile__registration_complete=False) date = go_back_n_months(datetime.datetime.today(), 1) data = {"year": date.year, "month": number2month(date.month)} send_remo_mail(reps, self.SUBJECT, self.EMAIL_TEMPLATE, data)
def get_comment_delete_url(obj): """Return the delete url of a comment.""" up = obj.user.userprofile month_name = number2month(obj.report.month.month) return reverse( "reports_delete_report_comment", kwargs={ "display_name": up.display_name, "year": obj.report.month.year, "month": month_name, "comment_id": obj.id, }, )
def test_invalid_date(self, mocked_now_date, mocked_waffle_switch): mocked_waffle_switch.return_value = False UserFactory.create(userprofile__is_rotm_nominee=True) UserFactory.create(userprofile__is_rotm_nominee=True) UserFactory.create(username='******') mocked_now_date.return_value = datetime(now().year, now().month, 5) poll_name = ('Rep of the month for {0}'.format( number2month(now().month))) create_rotm_poll() poll = Poll.objects.filter(name=poll_name) ok_(not poll.exists())
def handle(self, *args, **options): """Prepares a list of reps to be notified and the required template variables. """ rep_group = Group.objects.get(name='Rep') reps = rep_group.user_set.exclude( userprofile__registration_complete=False) date = go_back_n_months(datetime.datetime.today(), 1) month = number2month(date.month) subject = self.SUBJECT % month data = {'year': date.year, 'month': month} send_remo_mail(reps, subject, self.EMAIL_TEMPLATE, data)
def test_nominate_notification_base(self, mocked_date, mail_mock): UserFactory.create(groups=['Mentor']) subject = 'Nominate Rep of the month' mentor_alias = settings.REPS_MENTORS_LIST mocked_date.return_value = datetime(now().year, now().month, 1) send_rotm_nomination_reminder() ok_(mail_mock.called) eq_(mail_mock.call_count, 1) expected_call_list = [call(recipients_list=[mentor_alias], subject=subject, data={'month': number2month(now().month)}, email_template=ANY)] eq_(mail_mock.call_args_list, expected_call_list)
def handle(self, *args, **options): """Prepares a list of reps to be notified and the required template variables. """ rep_group = Group.objects.get(name='Rep') reps = (rep_group.user_set .exclude(userprofile__registration_complete=False) .values_list('id', flat=True)) date = go_back_n_months(datetime.datetime.today(), 1) month = number2month(date.month) subject = self.SUBJECT % month data = {'year': date.year, 'month': month} send_remo_mail(reps, subject, self.EMAIL_TEMPLATE, data)
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)
def handle(self, *args, **options): """Prepares a list of reps to be notified and the required template variables. """ rep_group = Group.objects.get(name='Rep') reps = rep_group.user_set.exclude( userprofile__registration_complete=False) date = go_back_n_months(datetime.datetime.today(), 1) reps_without_report = reps.exclude(reports__month__year=date.year, reports__month__month=date.month) data = {'year': date.year, 'month': number2month(date.month)} send_remo_mail(reps_without_report, self.SUBJECT, self.EMAIL_TEMPLATE, data)
def current_report(request, edit=False): display_name = request.user.userprofile.display_name previous_month = utils.go_back_n_months(datetime.date.today(), first_day=True) month_name = utils.number2month(previous_month.month) report = utils.get_object_or_none( Report, user__userprofile__display_name=display_name, month=previous_month) view = 'reports_view_report' if edit or not report: view = 'reports_edit_report' redirect_url = reverse(view, kwargs={'display_name': display_name, 'year': previous_month.year, 'month': month_name}) return redirect(redirect_url)
def handle(self, *args, **options): """Prepares a list of reps to be notified and the required template variables. """ rep_group = Group.objects.get(name="Rep") reps = rep_group.user_set.exclude(userprofile__registration_complete=False) date = go_back_n_months(datetime.datetime.today(), 2) reps_without_report = reps.exclude(reports__month__year=date.year, reports__month__month=date.month) mentors = [rep.userprofile.mentor.id for rep in reps_without_report] month = number2month(date.month) subject = self.SUBJECT % month data = {"year": date.year, "month": month, "reps_without_report": reps_without_report} send_remo_mail(mentors, subject, self.EMAIL_TEMPLATE, data)
def test_base(self, mocked_now_date): nominee_1 = UserFactory.create(userprofile__is_rotm_nominee=True) nominee_2 = UserFactory.create(userprofile__is_rotm_nominee=True) UserFactory.create(username='******') mocked_now_date.return_value = datetime(now().year, now().month, 11) poll_name = ('Rep of the month for {0}'.format( number2month(now().month))) create_rotm_poll() poll = Poll.objects.filter(name=poll_name) range_poll = RangePoll.objects.get(poll=poll) range_poll_choices = RangePollChoice.objects.filter( range_poll=range_poll) ok_(poll.exists()) eq_(poll.count(), 1) eq_(set([choice.nominee for choice in range_poll_choices]), set([nominee_1, nominee_2]))
def create_rotm_poll(): """Create a poll for the Rep of the month nominee. This task will create a range poll after the first days of the month during which mentors nominated mentees through their user profiles. The poll will last 14 days. """ # Avoid circular dependencies from remo.voting.models import Poll, RangePoll, RangePollChoice create_poll_flag = True poll_name = 'Rep of the month for {0}'.format(number2month(now().month)) start = ( datetime.combine(rotm_nomination_end_date(), datetime.min.time()) + timedelta(days=1)) end = start + timedelta(days=ROTM_VOTING_DAYS) rotm_poll = Poll.objects.filter(name=poll_name, start=start, end=end) if not now().date() > rotm_nomination_end_date() or rotm_poll.exists(): create_poll_flag = False nominees = User.objects.filter(userprofile__registration_complete=True, userprofile__is_rotm_nominee=True) if ((nominees and create_poll_flag) or waffle.switch_is_active('enable_rotm_tasks')): remobot = User.objects.get(username='******') description = 'Automated vote for the Rep of this month.' mentor_group = Group.objects.get(name='Mentor') with transaction.commit_on_success(): poll = Poll.objects.create(name=poll_name, description=description, valid_groups=mentor_group, start=start, end=end, created_by=remobot) range_poll = RangePoll.objects.create( poll=poll, name='Rep of the month nominees') for nominee in nominees: RangePollChoice.objects.create(range_poll=range_poll, nominee=nominee)
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 create_rotm_poll(): """Create a poll for the Rep of the month nominee. This task will create a range poll after the first days of the month during which mentors nominated mentees through their user profiles. The poll will last 14 days. """ # Avoid circular dependencies from remo.voting.models import Poll, RangePoll, RangePollChoice create_poll_flag = True poll_name = 'Rep of the month for {0}'.format(number2month(now().month)) start = (datetime.combine(rotm_nomination_end_date(), datetime.min.time()) + timedelta(days=1)) end = start + timedelta(days=ROTM_VOTING_DAYS) rotm_poll = Poll.objects.filter(name=poll_name, start=start, end=end) if not now().date() > rotm_nomination_end_date() or rotm_poll.exists(): create_poll_flag = False nominees = User.objects.filter(userprofile__registration_complete=True, userprofile__is_rotm_nominee=True) if ((nominees and create_poll_flag) or waffle.switch_is_active('enable_rotm_tasks')): remobot = User.objects.get(username='******') description = 'Automated vote for the Rep of this month.' mentor_group = Group.objects.get(name='Mentor') with transaction.commit_on_success(): poll = Poll.objects.create(name=poll_name, description=description, valid_groups=mentor_group, start=start, end=end, created_by=remobot) range_poll = RangePoll.objects.create( poll=poll, name='Rep of the month nominees') for nominee in nominees: RangePollChoice.objects.create(range_poll=range_poll, nominee=nominee)
def handle(self, *args, **options): """Prepares a list of reps to be notified and the required template variables. """ rep_group = Group.objects.get(name="Rep") reps = rep_group.user_set.exclude(userprofile__registration_complete=False) id_reps = reps.values_list("id", flat=True) date = go_back_n_months(datetime.datetime.today(), 1) month = number2month(date.month) subject = self.SUBJECT % month data = {"year": date.year, "month": month} if options["dry_run"]: email_reps = reps.values_list("email", flat=True) for recipient in email_reps: msg = "First notification sent to %s" % recipient print(msg) else: send_remo_mail(id_reps, subject, self.EMAIL_TEMPLATE, data)
def test_poll_already_exists(self, mocked_now_date, mocked_waffle_switch): mocked_waffle_switch.return_value = False UserFactory.create(userprofile__is_rotm_nominee=True) UserFactory.create(userprofile__is_rotm_nominee=True) UserFactory.create(username='******') # Nomination ends on the 10th of each month mocked_now_date.return_value = datetime(now().year, now().month, 10) poll_start = datetime(now().year, now().month, 1) poll_end = poll_start + timedelta(days=14) poll_name = ('Rep of the month for {0}'.format( number2month(now().month))) mentor_group = Group.objects.get(name='Mentor') poll = PollFactory.create(start=poll_start, end=poll_end, valid_groups=mentor_group, name=poll_name) create_rotm_poll() rotm_polls = Poll.objects.filter(name=poll_name) ok_(rotm_polls.exists()) eq_(rotm_polls.count(), 1) eq_(rotm_polls[0].pk, poll.pk)