Example #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))
Example #2
0
def edit_shift_view(request, semester, pk, profile=None):
    """
    View for a manager to edit the details of a particular RegularWorkshift.
    """
    shift = get_object_or_404(RegularWorkshift, pk=pk)
    managers = shift.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())

    edit_form = RegularWorkshiftForm(
        request.POST if "edit" in request.POST else None,
        instance=shift,
        semester=semester,
        )

    if "delete" in request.POST:
        # Open instances are deleted automatically
        shift.delete()
        return HttpResponseRedirect(wurl('workshift:manage',
                                         sem_url=semester.sem_url))
    elif edit_form.is_valid():
        shift = edit_form.save()
        return HttpResponseRedirect(shift.get_view_url())

    page_name = "Edit {0}".format(shift)

    return render_to_response("edit_shift.html", {
        "page_name": page_name,
        "shift": shift,
        "edit_form": edit_form,
    }, context_instance=RequestContext(request))
Example #3
0
def edit_shift_view(request, semester, pk, profile=None):
    """
    View for a manager to edit the details of a particular RegularWorkshift.
    """
    shift = get_object_or_404(RegularWorkshift, pk=pk)
    managers = shift.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(wurl('workshift:view_semester',
                                         sem_url=semester.sem_url))

    edit_form = RegularWorkshiftForm(
        request.POST if "edit" in request.POST else None,
        instance=shift,
        semester=semester,
        )

    if "delete" in request.POST:
        instances = WorkshiftInstance.objects.filter(weekly_workshift=shift)
        info = InstanceInfo(
            title=shift.title,
            description=shift.workshift_type.description,
            pool=shift.pool,
            start_time=shift.start_time,
            end_time=shift.end_time,
            )
        info.save()
        for instance in instances:
            instance.weekly_workshift = None
            instance.info = info
            instance.closed = True
            instance.save()
        shift.delete()
        return HttpResponseRedirect(wurl('workshift:manage',
                                         sem_url=semester.sem_url))
    elif edit_form.is_valid():
        shift = edit_form.save()
        return HttpResponseRedirect(wurl('workshift:view_shift',
                                         sem_url=semester.sem_url,
                                         pk=shift.pk))

    page_name = "Edit " + shift.title

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