Exemple #1
0
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
    })
Exemple #2
0
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")