def add_contact(request, company): # add a new contact c = get_object_or_404(Company, url_name=company) # check permissions: needs to be manager if not has_permission(request.user, c, 'contact', 'edit'): return no_permission_view(request, c, _("You have no permission to add contacts.")) t = request.GET.get('type') if not t: t = 'Individual' # the default context = { 'add': True, 'type': t, 'company': c, 'title': _("Add contact"), 'site_title': g.MISC['site_title'], 'date_format_js': get_date_format(request.user, c, 'js') } if request.method == 'POST': # submit data form = ContactForm(request.POST) form.user = request.user form.company = c if form.is_valid(): # create a new Contact contact = Contact( type=form.cleaned_data.get('type'), company_name=form.cleaned_data.get('company_name'), first_name=form.cleaned_data.get('first_name'), last_name=form.cleaned_data.get('last_name'), sex=form.cleaned_data.get('sex'), street_address=form.cleaned_data.get('street_address'), postcode=form.cleaned_data.get('postcode'), city=form.cleaned_data.get('city'), state=form.cleaned_data.get('state'), country=form.cleaned_data.get('country'), email=form.cleaned_data.get('email'), phone=form.cleaned_data.get('phone'), vat=form.cleaned_data.get('vat'), date_of_birth=form.cleaned_data.get('date_of_birth'), created_by=request.user, company=c ) contact.save() return redirect('pos:list_contacts', company=c.url_name) else: form = ContactForm(initial={'type': t, 'country': c.country}) form.user = request.user form.company = c context['form'] = form return render(request, 'pos/manage/contact.html', context)
def quick_contacts(request, company): """ creates contact on the fly (while creating bill on the terminal) contact data is in request.POST """ try: c = Company.objects.get(url_name=company) except Company.DoesNotExist: return JsonError(_("Company does not exist.")) # permissions if not has_permission(request.user, c, 'contact', 'edit'): return JsonError(_("You have no permission to add contacts")) contact = JsonParse(request.POST.get('data')) if not contact: return JsonError(_("No data in request")) r = validate_contact(request.user, c, contact) if not r['status']: return JsonError(r['message']) contact = r['data'] if int(contact.get('id')) == -1: # it's a new contact: use .get() and forget about different types try: obj = Contact( created_by=request.user, company=c, type=contact.get('type'), company_name=contact.get('company_name'), first_name=contact.get('first_name'), last_name=contact.get('last_name'), sex=contact.get('sex'), date_of_birth=contact.get('date_of_birth'), street_address=contact.get('street_address'), postcode=contact.get('postcode'), city=contact.get('city'), state=contact.get('state'), country=contact.get('country'), email=contact.get('email'), phone=contact.get('phone'), vat=contact.get('vat') ) obj.save() except Exception, e: return JsonResponse(_("Saving contact failed") + ": " + str(e))