def clientOrganisation(request, client_organisation_id=None): """Client creation or modification""" clientOrganisation = None try: if client_organisation_id: clientOrganisation = ClientOrganisation.objects.get(id=client_organisation_id) except ClientOrganisation.DoesNotExist: pass if request.method == "POST": if client: form = ClientOrganisationForm(request.POST, instance=clientOrganisation) else: form = ClientOrganisationForm(request.POST) if form.is_valid(): clientOrganisation = form.save() clientOrganisation.save() return HttpResponseRedirect(urlresolvers.reverse("crm.views.company_detail", args=[clientOrganisation.company.id])) else: if clientOrganisation: form = ClientOrganisationForm(instance=clientOrganisation) # A form that edit current client organisation else: form = ClientOrganisationForm() # An unbound form return render(request, "crm/client_organisation.html", {"client_organisation": clientOrganisation, "form": form, "user": request.user})
def client_organisation_company_popup(request): """Client, organisation and company creation in one popup""" template = get_template("crm/client-popup.html") result = {"success": False} if request.method == "POST": clientForm = ClientForm(request.POST, prefix="client") organisationForm = ClientOrganisationForm(request.POST, prefix="organisation") companyForm = CompanyForm(request.POST, prefix="company") contactForm = ContactForm(request.POST, prefix="contact") if contactForm.is_valid(): contact = contactForm.save() else: contact = None if companyForm.is_valid(): company = companyForm.save() else: company = None client = None if clientForm.is_valid(): client = clientForm.save(commit=False) else: # clientForm may be invalid because client organisation is a new one if organisationForm.is_valid(): organisation = organisationForm.save() clientForm.data = clientForm.data.copy() clientForm.data["client-organisation"] = organisation.id # Inject organisation in client form clientForm.full_clean() if clientForm.is_valid(): client = clientForm.save(commit=False) elif company: # organisationForm may be invalid because company is a new one organisationForm.data = organisationForm.data.copy() organisationForm.data["organisation-company"] = company.id # Inject company in organisation form organisationForm.full_clean() if organisationForm.is_valid(): organisation = organisationForm.save() clientForm.data = clientForm.data.copy() clientForm.data["client-organisation"] = organisation.id # Inject organisation in client form clientForm.full_clean() if clientForm.is_valid(): client = clientForm.save(commit=False) # If everything is allright, save the client and create response if client: # Add contact if defined if contact: client.contact = contact client.active = True client.save() result["success"] = True result["client_id"] = client.id result["client_name"] = unicode(client) else: # Unbound forms for GET requests clientForm = ClientForm(prefix="client") organisationForm = ClientOrganisationForm(prefix="organisation") companyForm = CompanyForm(prefix="company") contactForm = ContactForm(prefix="contact") # Render form result["form"] = template.render({ "clientForm": clientForm, "organisationForm": organisationForm, "companyForm": companyForm , "contactForm": contactForm}) return HttpResponse(json.dumps(result), content_type="application/json")