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 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(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 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 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_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)