def add(request): if request.method == 'POST': form = WorkingHoursForm(request.POST, provider=request.user.service_provider) form_valid = form.is_valid() formset = WorkingHoursFormSet(request.POST) # formset forms need to know working hours for validation for fs_form in formset: fs_form.wh_time_from = form.cleaned_data.get('time_from') fs_form.wh_time_to = form.cleaned_data.get('time_to') formset_valid = formset.is_valid() # even if form fails validaton, formset should be still validated formset_valid = formset.is_valid() if form_valid and formset_valid: service = form.save(commit=False) service.service_provider = request.user.service_provider service.save() formset.instance = service formset.save() return HttpResponseRedirect(reverse(myworkinghours)) else: initial = {} if not request.user.service_provider.working_hours.exists(): initial['week_days'] = '1,2,3,4,5' form = WorkingHoursForm(initial=initial, provider=request.user.service_provider) formset = WorkingHoursFormSet() return render_to_response('workinghours/add.html', locals(), context_instance=RequestContext(request))
def edit(request, id): workinghours = get_object_or_404( WorkingHours, service_provider=request.user.service_provider, id=id) if request.method == 'POST': form = WorkingHoursForm(request.POST, instance=workinghours, provider=request.user.service_provider) form_valid = form.is_valid() formset = WorkingHoursFormSet(request.POST, instance=workinghours) # formset forms need to know working hours for validation for fs_form in formset: fs_form.wh_time_from = form.cleaned_data.get('time_from') fs_form.wh_time_to = form.cleaned_data.get('time_to') formset_valid = formset.is_valid() # even if form fails validaton, formset should be still validated if form_valid and formset_valid: form.save() formset.save() return HttpResponseRedirect(reverse(myworkinghours)) else: form = WorkingHoursForm(instance=workinghours, provider=request.user.service_provider) formset = WorkingHoursFormSet(instance=workinghours) return render_to_response('workinghours/edit.html', locals(), context_instance=RequestContext(request))
def edit(request, id): workinghours = get_object_or_404(WorkingHours, service_provider=request.user.service_provider, id=id) if request.method == 'POST': form = WorkingHoursForm(request.POST, instance=workinghours, provider=request.user.service_provider) form_valid = form.is_valid() formset = WorkingHoursFormSet(request.POST, instance=workinghours) # formset forms need to know working hours for validation for fs_form in formset: fs_form.wh_time_from = form.cleaned_data.get('time_from') fs_form.wh_time_to = form.cleaned_data.get('time_to') formset_valid = formset.is_valid() # even if form fails validaton, formset should be still validated if form_valid and formset_valid: form.save() formset.save() return HttpResponseRedirect(reverse(myworkinghours)) else: form = WorkingHoursForm(instance=workinghours, provider=request.user.service_provider) formset = WorkingHoursFormSet(instance=workinghours) return render_to_response('workinghours/edit.html', locals(), context_instance=RequestContext(request))