def create(request): if request.method == 'POST': contacts = ContactList.post_dict(request.POST) form = CompanyForm(request.POST) if form.is_valid(): company = form.save() msg = company.contact_list.update_contacts(contacts) if msg: messages.warning(request, msg) messages.success(request, 'Company created') return redirect('companies:update', company.pk) else: contacts = {} form = CompanyForm() data = { 'contacts': contacts, 'form': form, } return render_to_response( 'companies/create.html', data, context_instance=RequestContext(request), )
def update(request, company_id): company = get_object_or_404(Company, pk=company_id) if request.method == 'POST': contacts = customer.contact_list.post_dict(request.POST) form = CompanyForm(request.POST, instance=company) if form.is_valid(): updated_company = form.save() msg = updated_company.contact_list.update_contacts(contacts) if msg: messages.warning(request, msg) messages.success(request, 'Company updated.') else: try: contacts = company.contact_list.get_dict() except AttributeError: company.save() contacts = {} form = CompanyForm(instance=company) data = { 'contacts': contacts, 'company': company, 'form': form, } return render_to_response( 'companies/update.html', data, context_instance=RequestContext(request), )
def create_company(request): context = {'pagetitle': 'Cadastrar compania'} if request.method == "GET": form = CompanyForm() context['form'] = form return render(request, 'form_create.html', context) if request.method == "POST": form = CompanyForm(request.POST) if form.is_valid(): company = form.save(commit=False) # Extra rule (check if company already exists) company_already_exists = Company.objects.filter( name__icontains=company.name).first() if company_already_exists: context['form'] = form messages.error("Compania com esse nome já existe") return render(request, 'form_create.html', context) company.save() messages.success(request, 'Compania criada com sucesso!') return redirect('/company/create/') else: context['form'] = form messages.error(request, "Erro ao cadastrar compania.") return render(request, 'create_company.html', context) messages.error("Algo deu errado") return redirect('/company/create/')
def create_company(request): if request.method == 'GET': form = CompanyForm() return render(request, 'companies.html', context={'form': form}) elif request.method == 'POST': form = CompanyForm(request.POST) if form.is_valid(): form.save() return render(request, 'congratulation.html') else: return render(request, 'companies.html', context={"form": form})
def put(self, *args, **kwargs): # Update company company = get_object_or_404(Company, pk=self.kwargs.get('pk')) form = CompanyForm(data=json.loads(self.request.body), instance=company) if form.is_valid(): company = form.save() data = {'company': company.serialize()} else: data = {'errors': form.errors} return HttpResponse(json.dumps(data), content_type="application/json")
def test_unvalid_form(self): data = { "company_name": "", "company_adress": "", "company_zip_code": "", "company_city": "", "company_email": "", "company_phone": "", "company_notes": "" } form = CompanyForm(data=data) self.assertFalse(form.is_valid())
def test_valid_form(self): data = { "company_name": "Super Company", "company_adress": "6 rue du paradis", "company_zip_code": "75001", "company_city": "Paris", "company_email": "*****@*****.**", "company_phone": "+33687129463", "company_notes": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi id porta felis. Vivamus felis urna, feugiat a erat nec, molestie viverra urna. Nam consequat nisl sem, et fringilla urna sagittis a." } form = CompanyForm(data=data) self.assertTrue(form.is_valid())
def test_valid_create_company(self): form = CompanyForm(initial={ 'user': self.user_agent, }, data={ 'name': 'agent2', 'domain': 'agent2.com', 'city': 'agent2', 'country': 'PH', }) self.assertTrue(form.is_valid()) company = form.save() self.assertTrue(company)
def test_invalid_create_company(self): form = CompanyForm(initial={ 'user': self.user_agent, }, data={}) self.assertFalse(form.is_valid())
def test_company_form_valid(self): form_data = {'name': 'ACME', 'subdomain': 'Acmebob'} form = CompanyForm(data=form_data) self.assertTrue(form.is_valid()) self.assertEqual(form.cleaned_data["subdomain"], "acmebob")
def test_company_form_invalid_subdomain(self): form_data = {'name': 'Bob Company Ltd', 'subdomain': 'google.acme.job'} form = CompanyForm(data=form_data) self.assertFalse(form.is_valid())
def test_company_form_invalid_name(self): form_data = {'name': '', 'subdomain': 'acmebob'} form = CompanyForm(data=form_data) self.assertFalse(form.is_valid())