def opening_exception_view(request, openingid, year, month, day, time): """ View an opening exception, or add by POSTing """ """ This view is odd because we don't actually want the user to see a form they can edit, the exception is created by clicking a link from another page """ exception = None opening = None form = None if request.method == "POST": # We're adding a new exception form = OpeningExceptionForm(request.POST) if form.is_valid(): opening = ClientOpening.objects.get( id=form.cleaned_data['clientOpening']) date = timezone.make_aware(form.cleaned_data['date'], timezone.UTC()) exception, created = ClientOpeningException.objects.get_or_create( clientOpening=opening, date=date) if opening is None: opening = get_object_or_404(ClientOpening, id=openingid) if exception is None: date = timezone.make_aware( datetime.datetime(int(year), int(month), int(day), int(time[0:2]), int(time[2:4])), timezone.UTC()) exception = get_object_or_404(ClientOpeningException, clientOpening=opening, date=date) if form is None: form = OpeningExceptionForm({ 'clientOpening': exception.clientOpening.id, 'date': exception.date }) return render(request, 'timeslots/opening/opening_exception.html', { 'opening': opening, 'exception': exception, 'form': form })
def opening_exception_delete(request, openingid, year, month, day, time): """ Delete an opening exception """ # opening = get_object_or_404(ClientOpening, id=openingid) # exceptiondate = timezone.make_aware(datetime.datetime(int(year), int(month), int(day), int(time[0:2]), int(time[2:4])), timezone.UTC()) # exception = ClientOpeningException.objects.get(clientOpening=opening, date=exceptiondate) if request.method == "POST": # We're adding a new exception form = OpeningExceptionForm(request.POST) if form.is_valid(): opening = ClientOpening.objects.get( id=form.cleaned_data['clientOpening']) date = form.cleaned_data['date'] exception = ClientOpeningException.objects.get( clientOpening=opening, date=date) exception.delete() return HttpResponseRedirect( reverse('timeslots_opening_instance_view', kwargs={ 'clientid': opening.client.user.id, 'year': exception.date.year, 'month': exception.date.month, 'day': exception.date.day, 'time': exception.date.strftime('%H%M') })) raise Exception("Error while trying to delete an Opening Exception")
def opening_instance_view(request, clientid, year, month, day, time): """ Show a single opening instance """ volunteer = get_volunteer(request) client = get_object_or_404(Client, user__id=clientid) opening = None openings = client.openings.all() instance_date = timezone.make_aware( datetime.datetime(int(year), int(month), int(day), int(time[0:2]), int(time[2:4])), timezone.UTC()) for o in openings: instance = o.get_instance(instance_date) if instance: opening = o break if opening: exception_form = OpeningExceptionForm({ 'clientOpening': opening.id, 'date': instance_date }) return render( request, 'timeslots/opening/opening_instance_view.html', { "instance_date": instance_date, "opening": opening, "client": client, "instance": instance, "exception_form": exception_form })