def commitment_exception_view(request, commitmentid, year, month, day, time): """ View a commitment 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 """ commitment = get_object_or_404(VolunteerCommitment, id=commitmentid) if not (request.user.is_staff or request.user.id == commitment.volunteer.user.id): # only a staff member or the committed volunteer can create a commitment exception return HttpResponseForbidden exception = None form = None if request.method == "POST": # We're adding a new exception form = CommitmentExceptionForm(request.POST) if form.is_valid(): commitment = VolunteerCommitment.objects.get( id=form.cleaned_data['commitment']) date = timezone.make_aware(form.cleaned_data['date'], timezone.UTC()) exception, created = VolunteerCommitmentException.objects.get_or_create( volunteerCommitment=commitment, date=date) #also create a new one-off opening opening = ClientOpening.objects.create( client=commitment.clientOpening.client, type="One-Off", startDate=date) ometadata = ClientOpeningMetadata.objects.create( clientOpening=opening, metadata=date.strftime('%Y-%m-%d')) 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(VolunteerCommitmentException, volunteerCommitment=commitment, date=date) if form is None: form = CommitmentExceptionForm({ 'commitment': exception.volunteerCommitment.id, 'date': exception.date }) return render(request, 'timeslots/commitment/commitment_exception.html', { 'commitment': commitment, 'exception': exception, 'form': form })
def commitment_instance_view(request, clientid, year, month, day, time): """ Show a single commitment instance (i.e., a single date) """ client = get_object_or_404(Client, user__id=clientid) commitment = None commitments = client.get_commitments() instance_date = timezone.make_aware( datetime.datetime(int(year), int(month), int(day), int(time[0:2]), int(time[2:4])), timezone.UTC()) is_my_commitment = False for c in commitments: instance = c.get_instance(instance_date) if instance: commitment = c is_my_commitment = True if commitment.volunteer.user == request.user else False break if commitment: exception_form = CommitmentExceptionForm({ 'commitment': commitment.id, 'date': instance_date }) return render( request, 'timeslots/commitment/commitment_instance_view.html', { "instance_date": instance_date, "commitment": commitment, "client": client, "instance": instance, "is_my_commitment": is_my_commitment, "exception_form": exception_form })
def commitment_exception_delete(request, commitmentid, year, month, day, time): """ Delete a commitment exception """ if request.method == "POST": # We're deleting the exception form = CommitmentExceptionForm(request.POST) if form.is_valid(): commitment = VolunteerCommitment.objects.get( id=form.cleaned_data['commitment']) if not (request.user.is_staff or request.user.id == commitment.volunteer.user.id): # only a staff member or the committed volunteer can create a commitment exception return HttpResponseForbidden date = form.cleaned_data['date'] exception = VolunteerCommitmentException.objects.get( volunteerCommitment=commitment, date=date) exception.delete() #also delete the one-off opening (metadata will be deleted by cascade) opening = ClientOpening.objects.filter( client=commitment.clientOpening.client, type="One-Off", metadata__metadata=date.strftime('%Y-%m-%d')).order_by( "-id")[0] opening.delete() return HttpResponseRedirect( reverse('timeslots_commitment_instance_view', kwargs={ 'clientid': commitment.clientOpening.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 a Commitment Exception")