def test_wrong_postal_code_raise_exception(self): """Test clean_postal_code() method. Testing Company.clean_postal_code() method with wrong postal code raise ValidationError exception """ form_data = dict( name='Example Company', city='City', street='Example Street 6', postal_code='111', number='123', ) form = CompanyForm(data=form_data) form.is_valid() self.assertRaises(ValidationError, form.clean_postal_code)
def company(request, company_id=None): """Client creation or modification""" company = None try: if company_id: company = Company.objects.get(id=company_id) except Company.DoesNotExist: pass if request.method == "POST": if company: form = CompanyForm(request.POST, instance=company) else: form = CompanyForm(request.POST) if form.is_valid(): company = form.save() company.save() return HttpResponseRedirect(urlresolvers.reverse("crm.views.company_detail", args=[company.id])) else: if company: form = CompanyForm(instance=company) # A form that edit current company else: form = CompanyForm() # An unbound form return render(request, "crm/clientcompany.html", {"company": company, "form": form, "user": request.user})
def test_form_is_not_valid(self): """Test is_validate() method. Testing CompanyForm.is_valid() method with empty fields return invalid form """ form_data = dict() form = CompanyForm(data=form_data) self.assertFalse(form.is_valid())
def company_new(request): if request.method == "POST": form = CompanyForm(request.POST) if form.is_valid(): company = form.save(commit=False) company.save() return redirect('company_details', pk=company.pk) else: form = CompanyForm() return render(request, 'company/company_add.html', {'form': form})
def company_edit(request, pk): company = get_object_or_404(Company, pk=pk) if request.method == "POST": form = CompanyForm(request.POST, instance=company) if form.is_valid(): company = form.save(commit=False) company.save() return redirect('company_details', pk=company.pk) else: form = CompanyForm(instance=company) return render(request, 'company/company_edit.html', {'form': form})
def test_form_is_valid(self): """Test is_validate() method. Testing CompanyForm.is_valid() method with correct fields return valid form """ form_data = dict( name='Example Company', city='City', street='Example Street 6', postal_code='11-111', number='586-22-50-789', ) form = CompanyForm(data=form_data) self.assertTrue(form.is_valid())
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")