diff --git a/RIGS/forms.py b/RIGS/forms.py index e1e95012..c19ac5b8 100644 --- a/RIGS/forms.py +++ b/RIGS/forms.py @@ -141,3 +141,37 @@ class EventForm(forms.ModelForm): 'end_time', 'meet_at', 'access_at', 'description', 'notes', 'mic', 'person', 'organisation', 'dry_hire', 'checked_in_by', 'status', 'collector', 'purchase_order'] + + +class BaseClientEventAuthorisationForm(forms.ModelForm): + tos = forms.BooleanField(required=True, label="Terms of hire") + name = forms.CharField(label="Your Name") + + def clean(self): + if self.cleaned_data.get('amount') != self.instance.event.total: + self.add_error('amount', 'The amount authorised must equal the total for the event.') + return super(BaseClientEventAuthorisationForm, self).clean() + + class Meta: + abstract = True + + +class InternalClientEventAuthorisationForm(BaseClientEventAuthorisationForm): + def __init__(self, **kwargs): + super(InternalClientEventAuthorisationForm, self).__init__(**kwargs) + self.fields['uni_id'].required = True + self.fields['account_code'].required = True + + class Meta: + model = models.EventAuthorisation + fields = ('tos', 'name', 'amount', 'uni_id', 'account_code') + + +class ExternalClientEventAuthorisationForm(BaseClientEventAuthorisationForm): + def __init__(self, **kwargs): + super(ExternalClientEventAuthorisationForm, self).__init__(**kwargs) + self.fields['po'].required = True + + class Meta: + model = models.EventAuthorisation + fields = ('tos', 'name', 'amount', 'po') diff --git a/RIGS/migrations/0026_remove_eventauthorisation_created_at.py b/RIGS/migrations/0026_remove_eventauthorisation_created_at.py new file mode 100644 index 00000000..c5ddc143 --- /dev/null +++ b/RIGS/migrations/0026_remove_eventauthorisation_created_at.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('RIGS', '0025_eventauthorisation'), + ] + + operations = [ + migrations.RemoveField( + model_name='eventauthorisation', + name='created_at', + ), + ] diff --git a/RIGS/migrations/0027_eventauthorisation_event_singular.py b/RIGS/migrations/0027_eventauthorisation_event_singular.py new file mode 100644 index 00000000..d7796895 --- /dev/null +++ b/RIGS/migrations/0027_eventauthorisation_event_singular.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('RIGS', '0026_remove_eventauthorisation_created_at'), + ] + + operations = [ + migrations.AlterField( + model_name='eventauthorisation', + name='event', + field=models.OneToOneField(related_name='authorisation', to='RIGS.Event'), + ), + ] diff --git a/RIGS/models.py b/RIGS/models.py index 350f406a..37225697 100644 --- a/RIGS/models.py +++ b/RIGS/models.py @@ -387,7 +387,7 @@ class Event(models.Model, RevisionMixin): @property def authorised(self): - return self.authroisations.latest('created_at').amount >= self.total + return self.authorisation.amount == self.total @property def has_start_time(self): @@ -505,28 +505,15 @@ class EventCrew(models.Model): notes = models.TextField(blank=True, null=True) +@reversion.register class EventAuthorisation(models.Model): - event = models.ForeignKey('Event', related_name='authroisations') + event = models.OneToOneField('Event', related_name='authorisation') email = models.EmailField() name = models.CharField(max_length=255) uni_id = models.CharField(max_length=10, blank=True, null=True, verbose_name="University ID") account_code = models.CharField(max_length=50, blank=True, null=True) po = models.CharField(max_length=255, blank=True, null=True, verbose_name="purchase order") amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="authorisation amount") - created_at = models.DateTimeField(auto_now_add=True) - - def clean(self): - if self.amount != self.event.total: - raise ValidationError("The amount authorised must equal the total for the event") - if self.event.organisation and self.event.organisation.union_account: - # Is a union account, requires username and account number - if self.uni_id is None or self.uni_id == "" or self.account_code is None or self.account_code == "": - raise ValidationError("Internal clients require a University ID number and an account code") - else: - # Is an external client, only requires PO - if self.po is None or self.po == "": - raise ValidationError("External clients require a Purchase Order number") - return super(EventAuthorisation, self).clean() @python_2_unicode_compatible diff --git a/RIGS/rigboard.py b/RIGS/rigboard.py index 81cf564e..d602d66b 100644 --- a/RIGS/rigboard.py +++ b/RIGS/rigboard.py @@ -10,7 +10,9 @@ from django.template import RequestContext from django.template.loader import get_template from django.conf import settings from django.core.urlresolvers import reverse +from django.core import signing from django.http import HttpResponse +from django.core.exceptions import SuspiciousOperation from django.db.models import Q from django.contrib import messages from z3c.rml import rml2pdf @@ -36,15 +38,17 @@ class RigboardIndex(generic.TemplateView): context['events'] = models.Event.objects.current_events() return context + class WebCalendar(generic.TemplateView): template_name = 'RIGS/calendar.html' def get_context_data(self, **kwargs): context = super(WebCalendar, self).get_context_data(**kwargs) - context['view'] = kwargs.get('view','') - context['date'] = kwargs.get('date','') + context['view'] = kwargs.get('view', '') + context['date'] = kwargs.get('date', '') return context + class EventDetail(generic.DetailView): model = models.Event @@ -53,7 +57,6 @@ class EventOembed(generic.View): model = models.Event def get(self, request, pk=None): - embed_url = reverse('event_embed', args=[pk]) full_url = "{0}://{1}{2}".format(request.scheme, request.META['HTTP_HOST'], embed_url) @@ -85,7 +88,6 @@ class EventCreate(generic.CreateView): if re.search('"-\d+"', form['items_json'].value()): messages.info(self.request, "Your item changes have been saved. Please fix the errors and save the event.") - # Get some other objects to include in the form. Used when there are errors but also nice and quick. for field, model in form.related_models.iteritems(): value = form[field].value() @@ -117,15 +119,17 @@ class EventUpdate(generic.UpdateView): def get_success_url(self): return reverse_lazy('event_detail', kwargs={'pk': self.object.pk}) + class EventDuplicate(EventUpdate): def get_object(self, queryset=None): - old = super(EventDuplicate, self).get_object(queryset) # Get the object (the event you're duplicating) - new = copy.copy(old) # Make a copy of the object in memory - new.based_on = old # Make the new event based on the old event + old = super(EventDuplicate, self).get_object(queryset) # Get the object (the event you're duplicating) + new = copy.copy(old) # Make a copy of the object in memory + new.based_on = old # Make the new event based on the old event new.purchase_order = None - if self.request.method in ('POST', 'PUT'): # This only happens on save (otherwise items won't display in editor) - new.pk = None # This means a new event will be created on save, and all items will be re-created + if self.request.method in ( + 'POST', 'PUT'): # This only happens on save (otherwise items won't display in editor) + new.pk = None # This means a new event will be created on save, and all items will be re-created else: messages.info(self.request, 'Event data duplicated but not yet saved. Click save to complete operation.') @@ -136,6 +140,7 @@ class EventDuplicate(EventUpdate): context["duplicate"] = True return context + class EventPrint(generic.View): def get(self, request, pk): object = get_object_or_404(models.Event, pk=pk) @@ -145,8 +150,7 @@ class EventPrint(generic.View): merger = PdfFileMerger() for copy in copies: - - context = RequestContext(request, { # this should be outside the loop, but bug in 1.8.2 prevents this + context = RequestContext(request, { # this should be outside the loop, but bug in 1.8.2 prevents this 'object': object, 'fonts': { 'opensans': { @@ -154,8 +158,8 @@ class EventPrint(generic.View): 'bold': 'RIGS/static/fonts/OPENSANS-BOLD.TTF', } }, - 'copy':copy, - 'current_user':request.user, + 'copy': copy, + 'current_user': request.user, }) # context['copy'] = copy # this is the way to do it once we upgrade to Django 1.8.3 @@ -183,6 +187,7 @@ class EventPrint(generic.View): response.write(merged.getvalue()) return response + class EventArchive(generic.ArchiveIndexView): model = models.Event date_field = "start_date" @@ -219,3 +224,68 @@ class EventArchive(generic.ArchiveIndexView): messages.add_message(self.request, messages.WARNING, "No events have been found matching those criteria.") return qs + + +class EventAuthorise(generic.UpdateView): + template_name = 'RIGS/eventauthorisation_form.html' + success_template = 'RIGS/eventauthorisation_success.html' + + def form_valid(self, form): + # TODO: send email confirmation + self.template_name = self.success_template + messages.add_message(self.request, messages.SUCCESS, + 'Success! Your event has been authorised. You will also receive email confirmation.') + return self.render_to_response(self.get_context_data()) + + @property + def event(self): + return models.Event.objects.select_related('organisation', 'person', 'venue').get(pk=self.kwargs['pk']) + + def get_object(self, queryset=None): + return self.event.authorisation + + def get_form_class(self): + if self.event.organisation is not None and self.event.organisation.union_account: + return forms.InternalClientEventAuthorisationForm + else: + return forms.ExternalClientEventAuthorisationForm + + def get_context_data(self, **kwargs): + context = super(EventAuthorise, self).get_context_data(**kwargs) + context['event'] = self.event + + if self.get_form_class() is forms.InternalClientEventAuthorisationForm: + context['internal'] = True + else: + context['internal'] = False + + context['tos_url'] = settings.TERMS_OF_HIRE_URL + return context + + def get(self, request, *args, **kwargs): + if self.get_object() is not None and self.get_object().pk is not None: + if self.event.authorised: + messages.add_message(self.request, messages.WARNING, + "This event has already been authorised. Please confirm you wish to reauthorise") + else: + messages.add_message(self.request, messages.WARNING, + "This event has already been authorised, but the amount has changed." + + "Please check the amount and reauthorise.") + return super(EventAuthorise, self).get(request, *args, **kwargs) + + def get_form(self, **kwargs): + form = super(EventAuthorise, self).get_form(**kwargs) + form.instance.event = self.event + form.instance.email = self.request.email + return form + + def dispatch(self, request, *args, **kwargs): + # Verify our signature matches up and all is well with the integrity of the URL + try: + data = signing.loads(kwargs.get('hmac')) + assert int(kwargs.get('pk')) == int(data.get('pk')) + request.email = data['email'] + except (signing.BadSignature, AssertionError, KeyError): + raise SuspiciousOperation( + "The security integrity of that URL is invalid. Please contact your event MIC to obtain a new URL") + return super(EventAuthorise, self).dispatch(request, *args, **kwargs) diff --git a/RIGS/templates/RIGS/client_eventdetails.html b/RIGS/templates/RIGS/client_eventdetails.html new file mode 100644 index 00000000..4f3810f8 --- /dev/null +++ b/RIGS/templates/RIGS/client_eventdetails.html @@ -0,0 +1,78 @@ +