def event_edit(request, event_ident): try: event = Event.get_by_ident(event_ident) tasks = event.task_set.order_by('role__name') except ObjectDoesNotExist: raise Http404("No event found matching the query.") if request.method == 'GET': event_form = EventForm(prefix='event', instance=event) task_form = TaskForm(prefix='task', initial={'event': event}) elif request.method == 'POST': event_form = EventForm(request.POST, prefix='event', instance=event) task_form = TaskForm(request.POST, prefix='task', initial={'event': event}) if "submit" in request.POST and event_form.is_valid(): event_form.save() return redirect(event) if "add" in request.POST and task_form.is_valid(): task_form.save() context = {'title': 'Edit Event {0}'.format(event.get_ident()), 'event_form': event_form, 'object': event, 'model': Event, 'tasks': tasks, 'task_form': task_form, 'form_helper': bootstrap_helper_without_form} return render(request, 'workshops/event_edit_form.html', context)
def test_open_TTT_applications_form_validation(self): """Ensure validation of `open_TTT_applications` field.""" data = { 'slug': '2018-09-02-open-applications', 'host': self.org_alpha.pk, 'tags': [Tag.objects.get(name='SWC').pk], 'invoice_status': 'unknown', 'open_TTT_applications': True, } form = EventForm(data) self.assertFalse(form.is_valid()) self.assertIn('open_TTT_applications', form.errors.keys()) data['tags'] = [Tag.objects.get(name='TTT').pk] form = EventForm(data) self.assertTrue(form.is_valid())
def test_slug_illegal_formats(self): """Disallow slugs with wrong formats. Slug format should follow: YYYY-MM-DD-location, where YYYY, MM, DD can be unspecified (== 'xx').""" data = { 'slug': '', 'host': Organization.objects.all()[0].pk, 'tags': [Tag.objects.first().pk], 'invoice_status': 'unknown', } # disallow invalid formats formats = [ '20166-06-30-Krakow', '2016-006-30-Krakow', '2016-06-300-Krakow', '201-06-30-Krakow', '2016-6-30-Krakow', '2016-06-3-Krakow', 'SWC-2016-06-300-Krakow', '', 'xxxxx-xx-xx-Krakow', 'xxxx-xxx-xx-Krakow', 'xxxx-xx-xxx-Krakow', 'xxx-xx-xx-Krakow', 'xxxx-x-xx-Krakow', 'xxxx-xx-x-Krakow', ] for slug in formats: with self.subTest(slug=slug): data['slug'] = slug f = EventForm(data) self.assertEqual(f.is_valid(), False) self.assertIn('slug', f.errors)
def test_negative_manual_attendance(self): """Ensure we disallow negative manual attendance. This is a regression test for https://github.com/swcarpentry/amy/issues/435. Warning: this used to test `admin_fee` for negative value, but since #1411 (https://github.com/swcarpentry/amy/pull/1411) we don't have `admin_fee` in the form nor on Event Details page.""" error_str = "Ensure this value is greater than or equal to 0." data = { 'host': self.test_host.id, 'tags': [self.test_tag.id], 'slug': '2016-06-30-test-event', 'manual_attendance': -36, 'invoice_status': 'unknown', } data['manual_attendance'] = -36 f = EventForm(data) self.assertIn('manual_attendance', f.errors) self.assertIn(error_str, f.errors['manual_attendance']) data['manual_attendance'] = 0 f = EventForm(data) self.assertNotIn('manual_attendance', f.errors) data['slug'] = '2016-06-30-test-event2' data['manual_attendance'] = 36 f = EventForm(data) self.assertTrue(f.is_valid())
def test_slug_illegal_characters(self): """Disallow slugs with wrong characters. Slug allows only: latin characters, numbers, dashes and underscores. Slug format should follow: YYYY-MM-DD-location, where YYYY, MM, DD can be unspecified (== 'xx').""" data = { 'slug': '', 'host': Organization.objects.all()[0].pk, 'tags': Tag.objects.all(), 'invoice_status': 'unknown', } # disallow illegal characters for slug_suffix in ['a/b', 'a b', 'a!b', 'a.b', 'a\\b', 'a?b', 'aób']: with self.subTest(slug_suffix=slug_suffix): data['slug'] = '2016-06-30-{}'.format(slug_suffix) f = EventForm(data) self.assertEqual(f.is_valid(), False) self.assertIn('slug', f.errors)
def test_slug_valid_formats(self): """Allow slugs with wrong formats. Slug format should follow: YYYY-MM-DD-location, where YYYY, MM, DD can be unspecified (== 'xx').""" data = { 'slug': '', 'host': Organization.objects.all()[0].pk, 'tags': [Tag.objects.first().pk], 'invoice_status': 'unknown', } # allow correct formats formats = [ '2016-06-30-Krakow', '2016-06-xx-Krakow', '2016-xx-30-Krakow', 'xxxx-06-30-Krakow', '2016-xx-xx-Krakow', 'xxxx-06-xx-Krakow', 'xxxx-xx-30-Krakow', 'xxxx-xx-xx-Krakow', '2016-06-30-Krakow-multiple-words', '2016-06-xx-Krakow-multiple-words', '2016-xx-30-Krakow-multiple-words', 'xxxx-06-30-Krakow-multiple-words', '2016-xx-xx-Krakow-multiple-words', 'xxxx-06-xx-Krakow-multiple-words', 'xxxx-xx-30-Krakow-multiple-words', 'xxxx-xx-xx-Krakow-multiple-words', ] for slug in formats: with self.subTest(slug=slug): data['slug'] = slug f = EventForm(data) self.assertEqual(f.is_valid(), True) self.assertNotIn('slug', f.errors)