Skip to content

Commit

Permalink
Merge pull request #438 from ox-it/audience-options-1916
Browse files Browse the repository at this point in the history
Allow specifying an 'other' option for who can attend.
  • Loading branch information
markdoub committed May 3, 2016
2 parents 1029465 + ce68ad9 commit 4c3976d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
45 changes: 43 additions & 2 deletions talks/contributors/forms.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from django import forms
from django.contrib.auth.models import User
from django.db.models.query_utils import Q
from django.forms.widgets import Select
from django.forms.widgets import Select, RadioSelect
from django.utils.safestring import mark_safe
from django.contrib.contenttypes.models import ContentType

from talks.events import models, typeahead, datasources
from talks.events.models import EventGroup
from talks.events.models import EventGroup, AUDIENCE_CHOICES, AUDIENCE_PUBLIC, AUDIENCE_OXFORD, AUDIENCE_OTHER
from talks.users.authentication import GROUP_EDIT_EVENTS


Expand Down Expand Up @@ -57,6 +57,19 @@ def __init__(self, *args, **kwargs):
# Amend help text if no groups to choose from
if (not self.fields['group'].queryset) or self.fields['group'].queryset.count <= 0:
self.fields['group'].empty_label = "-- There are no series which you can add this talk to --"

# set preliminary values for audience_choices and audience_other
if self.instance.audience == AUDIENCE_PUBLIC:
self.fields['audience_choices'].initial = 'public'

elif self.instance.audience == AUDIENCE_OXFORD:
self.fields['audience_choices'].initial = 'oxonly'

else:
self.fields['audience_choices'].initial = 'other'
self.fields['audience_other'].initial = self.instance.audience



speakers = forms.ModelMultipleChoiceField(
queryset=models.Person.objects.all(),
Expand Down Expand Up @@ -115,6 +128,23 @@ def __init__(self, *args, **kwargs):
required=False,
widget=typeahead.MultipleTypeahead(datasources.USERS_DATA_SOURCE),
)

audience = forms.CharField(
required=False
)

audience_other = forms.CharField(
label="",
required=False,
help_text="If other, please specify"
)

audience_choices = forms.ChoiceField(
label="Who can attend",
required=False,
choices=AUDIENCE_CHOICES,
widget=RadioSelect()
)

class Meta:
exclude = ('slug', 'embargo')
Expand Down Expand Up @@ -179,6 +209,17 @@ def clean(self):
#don't allow this user to clear the group
raise forms.ValidationError("You do not have permission to move this talk from its current series")

# fill in the 'audience' field used by the model, based on the form values filled in
if 'audience_choices' in self.cleaned_data and self.cleaned_data['audience_choices'] == 'other':
if not self.cleaned_data['audience_other'] or self.cleaned_data['audience_other'] == '':
raise forms.ValidationError("You must specify who can attend")
else:
print "setting to value of audience_other field"
self.cleaned_data['audience'] = self.cleaned_data['audience_other']
else:
if 'audience_choices' in self.cleaned_data and not self.cleaned_data['audience_choices'] == '':
self.cleaned_data['audience'] = self.cleaned_data['audience_choices']

return self.cleaned_data

def _update_people(self, field, event, role):
Expand Down
7 changes: 2 additions & 5 deletions talks/contributors/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ def test_empty(self):
self.assertEquals(form.is_valid(), False, "empty form should not validate")
errors = form.errors.as_data()
logging.info("form errors: %s", errors)
self.assertEquals(len(errors), 6)
self.assertEquals(len(errors), 5)
self.assertIn('booking_type', errors)
self.assertIn('audience', errors)
self.assertIn('start', errors)
self.assertIn('end', errors)
self.assertIn('status', errors)
Expand All @@ -49,9 +48,8 @@ def test_all_fields_blanked(self):
self.assertEquals(form.is_valid(), False, "blanked form should not validate")
errors = form.errors.as_data()
logging.info("form errors: %s", errors)
self.assertEquals(len(errors), 6)
self.assertEquals(len(errors), 5)
self.assertIn('booking_type', errors)
self.assertIn('audience', errors)
self.assertIn('status', errors)
self.assertIn('start', errors)
self.assertIn('end', errors)
Expand Down Expand Up @@ -824,7 +822,6 @@ def test_edit_event_post_invalid(self):
self.assertEquals(response.status_code, 200)
logging.info("form errors: %s", response.context['event_form'].errors.as_data())
self.assertFormError(response, 'event_form', 'booking_type', ['This field is required.'])
self.assertFormError(response, 'event_form', 'audience', ['This field is required.'])
self.assertEquals(saved_event.title, old_title)
self.assertEquals(saved_event.description, old_description)
self.assertTemplateUsed(response, "contributors/event_form.html")
Expand Down
17 changes: 15 additions & 2 deletions talks/events/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@

AUDIENCE_PUBLIC = 'public'
AUDIENCE_OXFORD = 'oxonly'
AUDIENCE_OTHER = 'other'

AUDIENCE_CHOICES = (
(AUDIENCE_OXFORD, 'Members of the University only'),
(AUDIENCE_PUBLIC, 'Public'),
(AUDIENCE_OTHER, 'Other')
)

EVENT_PUBLISHED = 'published'
Expand Down Expand Up @@ -240,7 +243,7 @@ class Event(models.Model):
description = models.TextField(blank=True)
person_set = models.ManyToManyField(Person, through=PersonEvent, blank=True)
editor_set = models.ManyToManyField(User, blank=True)
audience = models.TextField(verbose_name="Who can attend", choices=AUDIENCE_CHOICES, default=AUDIENCE_OXFORD)
audience = models.TextField(verbose_name="Who can attend", default=AUDIENCE_OXFORD)
booking_type = models.TextField(verbose_name="Booking required",
choices=BOOKING_CHOICES,
default=BOOKING_NOT_REQUIRED)
Expand Down Expand Up @@ -447,7 +450,17 @@ def public_collections_containing_this_event(self):

return publicCollectionsContainingThisEvent


def get_audience_display(self):
# look up if it is one of the standard choices, else use the field value verbatim

audience_choices = dict(AUDIENCE_CHOICES)

try:
audience = audience_choices[self.audience]
except KeyError:
audience = self.audience

return audience

reversion.register(Event)
reversion.register(EventGroup)
Expand Down
4 changes: 3 additions & 1 deletion talks/templates/contributors/event_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ <h3>New Talk</h3>
{% include 'contributors/person_quick_add.html' %}
{% endwith %}

{{ event_form.audience|bootstrap_horizontal }}
{{ event_form.audience_choices|bootstrap_horizontal }}
{{ event_form.audience_other|bootstrap_horizontal }}

{{ event_form.booking_type|bootstrap_horizontal }}
{{ event_form.booking_url|bootstrap_horizontal }}
{{ event_form.booking_email|bootstrap_horizontal }}
Expand Down

0 comments on commit 4c3976d

Please sign in to comment.