def client(request, client_id=None): """Client creation or modification""" client = None try: if client_id: client = Client.objects.get(id=client_id) except Client.DoesNotExist: pass if request.method == "POST": if client: form = ClientForm(request.POST, instance=client) else: form = ClientForm(request.POST) if form.is_valid(): client = form.save() client.save() return HttpResponseRedirect(urlresolvers.reverse("crm.views.company_detail", args=[client.organisation.company.id])) else: if client: form = ClientForm(instance=client) # A form that edit current client else: form = ClientForm() # An unbound form return render(request, "crm/client.html", {"client": client, "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")
def add_new(self, form: ClientForm): event = self.get_object() client = form.save() client.signup_for_event(event)