Esempio n. 1
0
def add_shift_view(request, semester):
    """
    View for the workshift manager to create new types of workshifts.
    """
    page_name = "Add Workshift"
    pools = WorkshiftPool.objects.filter(semester=semester)
    full_management = utils.can_manage(request.user, semester=semester)
    if not full_management:
        pools = pools.filter(managers__incumbent__user=request.user)
        if not pools.count():
            messages.add_message(request, messages.ERROR,
                                 MESSAGES['ADMINS_ONLY'])
            return HttpResponseRedirect(wurl('workshift:view_semester',
                                             sem_url=semester.sem_url))

    if full_management:
        add_type_form = WorkshiftTypeForm(
            request.POST if "add_type" in request.POST else None,
            )
    else:
        add_type_form = None

    add_instance_form = WorkshiftInstanceForm(
        request.POST if "add_instance" in request.POST else None,
        pools=pools,
        semester=semester,
        )
    if WorkshiftType.objects.count():
        add_shift_form = RegularWorkshiftForm(
            request.POST if "add_shift" in request.POST else None,
            pools=pools,
            semester=semester,
            )
    else:
        add_shift_form = None

    if add_type_form and add_type_form.is_valid():
        add_type_form.save()
        return HttpResponseRedirect(wurl("workshift:manage",
                                         sem_url=semester.sem_url))
    elif add_instance_form and add_instance_form.is_valid():
        add_instance_form.save()
        return HttpResponseRedirect(wurl("workshift:manage",
                                         sem_url=semester.sem_url))
    elif add_shift_form and add_shift_form.is_valid():
        add_shift_form.save()
        return HttpResponseRedirect(wurl("workshift:manage",
                                         sem_url=semester.sem_url))
    return render_to_response("add_shift.html", {
        "page_name": page_name,
        "add_type_form": add_type_form,
        "add_instance_form": add_instance_form,
        "add_shift_form": add_shift_form,
    }, context_instance=RequestContext(request))
Esempio n. 2
0
def edit_instance_view(request, semester, pk, profile=None):
    """
    View for a manager to edit the details of a particular WorkshiftInstance.
    """
    instance = get_object_or_404(WorkshiftInstance, pk=pk)
    managers = instance.pool.managers.filter(incumbent__user=request.user)

    if not request.user.is_superuser and not managers.count():
        messages.add_message(request, messages.ERROR,
                             MESSAGES['ADMINS_ONLY'])
        return HttpResponseRedirect(semester.get_view_url())

    page_name = "Edit " + instance.title

    edit_form = WorkshiftInstanceForm(
        request.POST if "edit" in request.POST else None,
        instance=instance,
        semester=semester,
        edit_hours=False,
        )

    if "delete" in request.POST:
        instance.delete()
        return HttpResponseRedirect(wurl('workshift:manage',
                                         sem_url=semester.sem_url))
    elif edit_form.is_valid():
        instance = edit_form.save()
        return HttpResponseRedirect(instance.get_view_url())

    return render_to_response("edit_instance.html", {
        "page_name": page_name,
        "instance": instance,
        "edit_form": edit_form,
    }, context_instance=RequestContext(request))