Пример #1
0
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})
Пример #2
0
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),
    )
Пример #3
0
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),
    )
Пример #4
0
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/')
Пример #5
0
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),
    )
Пример #6
0
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),
    )
Пример #7
0
 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")
Пример #8
0
    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)