def test_invalid_timespan(self): mentor = UserFactory.create() user = UserFactory.create(userprofile__mentor=mentor) date = get_date(weeks=15) data = {'expected_date': date} form = UserStatusForm(data, instance=UserStatus(user=user)) ok_(not form.is_valid()) ok_('expected_date' in form.errors)
def test_expected_date_before_start_date(self): mentor = UserFactory.create() user = UserFactory.create(userprofile__mentor=mentor) start_date = get_date(4) expected_date = get_date(days=2) data = {'start_date': start_date, 'expected_date': expected_date} form = UserStatusForm(data, instance=UserStatus(user=user)) ok_(not form.is_valid()) ok_('expected_date' in form.errors)
def test_base(self): mentor = UserFactory.create() user = UserFactory.create(userprofile__mentor=mentor) date = get_date(days=1) data = {'expected_date': date} form = UserStatusForm(data, instance=UserStatus(user=user)) ok_(form.is_valid()) db_obj = form.save() eq_(db_obj.expected_date, get_date(days=1)) eq_(db_obj.user.get_full_name(), user.get_full_name())
def edit_availability(request, display_name): """Edit availability settings.""" user = request.user args = {} created = False if user.groups.filter(Q(name='Mozillians') | Q(name='Alumni')).exists(): raise Http404() try: status = (UserStatus.objects.filter(user=user).filter( is_unavailable=True).latest('created_on')) except UserStatus.DoesNotExist: status = UserStatus(user=user) created = True initial_data = {'is_replaced': False} if not created: initial_data['expected_date'] = ( status.expected_date.strftime('%d %B %Y')) status_form = UserStatusForm(request.POST or None, instance=status, initial=initial_data) email_mentor_form = EmailMentorForm(request.POST or None) if status_form.is_valid(): status_form.save() if created and email_mentor_form.is_valid(): expected_date = ( status_form.cleaned_data['expected_date'].strftime('%d %B %Y')) start_date = ( status_form.cleaned_data['start_date'].strftime('%d %B %Y')) msg = email_mentor_form.cleaned_data['body'] mentee = request.user.get_full_name() template = 'emails/mentor_unavailability_notification.jinja' subject = ('[Mentee {0}] Mentee will be unavailable starting ' 'on {1} until {2}'.format(mentee, start_date, expected_date)) email_mentor_form.send_email(request, subject, msg, template, {'user_status': status}) messages.success(request, 'Request submitted successfully.') return redirect('dashboard') args['status_form'] = status_form args['email_form'] = email_mentor_form args['created'] = created return render(request, 'edit_availability.jinja', args)
def edit_availability(request, display_name): """Edit availability settings.""" user = request.user args = {} created = False if user.groups.filter(name='Mozillians').exists(): raise Http404() if user.userprofile.is_unavailable: status = UserStatus.objects.filter(user=user).latest('created_on') else: status = UserStatus(user=user) created = True initial_data = {'is_replaced': False} if not created: initial_data['expected_date'] = ( status.expected_date.strftime('%d %B %Y')) status_form = UserStatusForm(request.POST or None, instance=status, initial=initial_data) email_mentor_form = EmailMentorForm(request.POST or None) if status_form.is_valid(): status_form.save() if created and email_mentor_form.is_valid(): expected_date = status_form.cleaned_data['expected_date'] msg = email_mentor_form.cleaned_data['body'] subject = ('[Rep unavailable] Mentee %s will be unavailable ' 'from %s' % (request.user.get_full_name(), expected_date.strftime('%d %B %Y'))) template = 'emails/mentor_unavailability_notification.txt' subject = ('[Mentee %s] Mentee will be unavailable ' 'until %s' % (request.user.get_full_name(), expected_date.strftime('%d %B %Y'))) email_mentor_form.send_email(request, subject, msg, template, {'user_status': status}) messages.success(request, 'Request submitted successfully.') return redirect('dashboard') args['status_form'] = status_form args['email_form'] = email_mentor_form args['created'] = created return render(request, 'edit_availability.html', args)