Exemplo n.º 1
0
def edit(request, id):
    provider = request.user.service_provider
    employee = get_object_or_404(Employee, employer=request.user.service_provider, id=id)
    hours = EmployeeWorkingHours.objects.filter(employee=employee)
    services = Service.objects.filter(service_provider=provider, employees__in=[employee.id])
    if request.method == 'POST':
        if request.POST.get('action') == 'delete':
            EmployeeWorkingHours.objects.get(id=request.POST.get('workinghours')).delete()
            return HttpResponseRedirect('/myemployees/edit/' + id)
        form = EmployeeForm(request.POST, request.FILES, instance=employee)
        qs = EmployeeWorkingHours.objects.filter(employee=employee)
        EmployeeWorkingHoursFormSet.form = staticmethod(curry(EmployeeWorkingHoursForm, employee=employee))
        forms = EmployeeWorkingHoursFormSet(request.POST)
        form_s = EmployeeServicesForm(request.POST, service_provider=provider, employee=employee, data=services)
        form_valid = form.is_valid()
        form_s_valid = form_s.is_valid()
        forms_valid = True
        for f in forms:
            if not f.is_valid():
                forms_valid = False
        if form_valid and form_s_valid and forms_valid:
            form.save()
            for f in forms:
                f.save()
            form_s.save()
            return HttpResponseRedirect(reverse(myemployees))
    else:
        form = EmployeeForm(instance=employee)
        qs = EmployeeWorkingHours.objects.filter(employee=employee)
        EmployeeWorkingHoursFormSet.form = staticmethod(curry(EmployeeWorkingHoursForm, employee=employee))
        forms = EmployeeWorkingHoursFormSet(queryset=qs)
        form_s = EmployeeServicesForm(service_provider=provider, employee=employee, data=services)
    return render_to_response('employees/edit.html', locals(), context_instance=RequestContext(request))
Exemplo n.º 2
0
def add(request):
    if request.method == 'POST':
        # set current user as employer implicitly
        form = EmployeeForm(request.POST)
        form_valid = form.is_valid()
        if form_valid:
            employee = form.save(commit=False)
            employee.employer = request.user.service_provider
            employee.save()

            h = EmployeeWorkingHours()
            h.employee = employee

            spwh = WorkingHours.objects.filter(service_provider=request.user.service_provider.id)[0]
            if spwh:
                h.time_from = spwh.time_from
                h.time_to = spwh.time_to
                h.week_days = [1, 2, 3, 4, 5]
            else:
                h.time_from = datetime.time(9, 0)
                h.time_to = datetime.time(21, 0)
                h.week_days = [1, 2, 3, 4, 5]

            h.save()
            return HttpResponseRedirect(reverse(myemployees))
    else:
        form = EmployeeForm()
        # render form - new (get request) or invalid with error messages (post request)
    return render_to_response('employees/add.html', locals(), context_instance=RequestContext(request))
Exemplo n.º 3
0
Arquivo: views.py Projeto: mfrlin/TPO
def add(request):
    if request.method == 'POST':
        # set current user as employer implicitly
        form = EmployeeForm(request.POST)
        form_valid = form.is_valid()
        if form_valid:
            employee = form.save(commit=False)
            employee.employer = request.user.service_provider
            employee.save()

            h = EmployeeWorkingHours()
            h.employee = employee

            spwh = WorkingHours.objects.filter(
                service_provider=request.user.service_provider.id)[0]
            if spwh:
                h.time_from = spwh.time_from
                h.time_to = spwh.time_to
                h.week_days = [1, 2, 3, 4, 5]
            else:
                h.time_from = datetime.time(9, 0)
                h.time_to = datetime.time(21, 0)
                h.week_days = [1, 2, 3, 4, 5]

            h.save()
            return HttpResponseRedirect(reverse(myemployees))
    else:
        form = EmployeeForm()
        # render form - new (get request) or invalid with error messages (post request)
    return render_to_response('employees/add.html',
                              locals(),
                              context_instance=RequestContext(request))
Exemplo n.º 4
0
Arquivo: views.py Projeto: mfrlin/TPO
def edit(request, id):
    provider = request.user.service_provider
    employee = get_object_or_404(Employee,
                                 employer=request.user.service_provider,
                                 id=id)
    hours = EmployeeWorkingHours.objects.filter(employee=employee)
    services = Service.objects.filter(service_provider=provider,
                                      employees__in=[employee.id])
    if request.method == 'POST':
        if request.POST.get('action') == 'delete':
            EmployeeWorkingHours.objects.get(
                id=request.POST.get('workinghours')).delete()
            return HttpResponseRedirect('/myemployees/edit/' + id)
        form = EmployeeForm(request.POST, request.FILES, instance=employee)
        qs = EmployeeWorkingHours.objects.filter(employee=employee)
        EmployeeWorkingHoursFormSet.form = staticmethod(
            curry(EmployeeWorkingHoursForm, employee=employee))
        forms = EmployeeWorkingHoursFormSet(request.POST)
        form_s = EmployeeServicesForm(request.POST,
                                      service_provider=provider,
                                      employee=employee,
                                      data=services)
        form_valid = form.is_valid()
        form_s_valid = form_s.is_valid()
        forms_valid = True
        for f in forms:
            if not f.is_valid():
                forms_valid = False
        if form_valid and form_s_valid and forms_valid:
            form.save()
            for f in forms:
                f.save()
            form_s.save()
            return HttpResponseRedirect(reverse(myemployees))
    else:
        form = EmployeeForm(instance=employee)
        qs = EmployeeWorkingHours.objects.filter(employee=employee)
        EmployeeWorkingHoursFormSet.form = staticmethod(
            curry(EmployeeWorkingHoursForm, employee=employee))
        forms = EmployeeWorkingHoursFormSet(queryset=qs)
        form_s = EmployeeServicesForm(service_provider=provider,
                                      employee=employee,
                                      data=services)
    return render_to_response('employees/edit.html',
                              locals(),
                              context_instance=RequestContext(request))