def create_client(request): client_form = ClientForm(request.POST, request.FILES) address_form = AddressForm(request.POST) if request.method == 'POST': if address_form.is_valid() and client_form.is_valid(): client = client_form.save(commit=False) address = address_form.save(commit=False) # Verifica se usuário já existe if not User.objects.filter(username=client.email).exists(): # Cria usuário para acessar o sistema User.objects.create_user(username=client.email, email=client.email, password=request.POST.get('password'), first_name=client.first_name) # Insere o cliente e endereço no banco client.save() address.client = client address.save() messages.success(request, _(u'Usuário criado com sucesso !')) return redirect('home_page') messages.add_message(request, messages.ERROR, _(u'Já existe usuário cadastrado com esse e-mail !'), extra_tags='danger') else: client_form = ClientForm() address_form = AddressForm() return render(request, 'client/client_form.html', { 'client_form': client_form, 'address_form': address_form, 'login_form': LoginForm() })
def address(request): if request.method == 'POST': form = AddressForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/client/') else: form = AddressForm() return {'form': form, 'button': 'Criar'}
def edit_address(request, id): a = Address.objects.get(id=id) if request.method == 'POST': form = AddressForm(request.POST, instance=a) if form.is_valid(): form.save() return HttpResponseRedirect('/client/') else: form = AddressForm(instance=a) return {'form': form, 'action': '/address/edit/'+id+'/', 'button': 'Editar'}
def get_context_data(self, **kwargs): context = super(OrderSummaryView, self).get_context_data(**kwargs) context['fees'] = Site.objects.get_current().fees context['delivery_date'] = default_date().strftime('%Y-%m-%d') context['delivery_date_max'] = (default_date() + timedelta(days=7)).strftime('%Y-%m-%d') context['address_form'] = AddressForm( initial=model_to_dict(self.request.user.userprofile)) return context
def add_staff(request): context_vars = dict() context_vars['header'] = capfirst(_('add new staff')) staff_form = StaffForm() address_form = AddressForm() if request.method == "POST": staff_form = StaffForm(request.POST, request.FILES) address_form = AddressForm(request.POST, request.FILES) try: if staff_form.is_valid() and address_form.is_valid(): staff = staff_form.save() address_form = AddressForm(request.POST, request.FILES, instance=staff.address) if not address_form.is_valid(): raise InvalidForm() address_form.save() if settings.WORKFLOWMAX_APIKEY and settings.WORKFLOWMAX_ACCOUNTKEY: staff.wm_sync() return HttpResponseRedirect(reverse('staff-view', args=[staff.id])) except InvalidForm: pass context_vars['staff_form'] = staff_form context_vars['address_form'] = address_form return direct_to_template(request, template='staff/form.html', extra_context=context_vars)
def add_supplier(request): context_vars = dict() context_vars['header'] = capfirst(_('add new supplier')) supplier_form = SupplierForm() address_form = AddressForm(prefix='address') postal_address_form = AddressForm(prefix='post_address') if request.method == "POST": supplier_form = SupplierForm(request.POST, request.FILES) address_form = AddressForm(request.POST, request.FILES, prefix='address') postal_address_form = AddressForm(request.POST, request.FILES, prefix='post_address') try: if supplier_form.is_valid() and address_form.is_valid() and postal_address_form.is_valid(): supplier = supplier_form.save() address_form = AddressForm(request.POST, request.FILES, prefix='address', instance=supplier.address) if not address_form.is_valid(): raise InvalidForm() address_form.save() postal_address_form = AddressForm(request.POST, request.FILES, prefix='post_address', instance=supplier.postal_address) if postal_address_form.is_valid(): postal_address_form.save() else: supplier.postal_address.postcode = supplier.address.postcode supplier.postal_address.address = supplier.address.address supplier.postal_address.city = supplier.address.city supplier.postal_address.county = supplier.address.county supplier.postal_address.country = supplier.address.country supplier.postal_address.latitude = supplier.address.latitude supplier.postal_address.longitude = supplier.address.longitude supplier.postal_address.save() if settings.WORKFLOWMAX_APIKEY and settings.WORKFLOWMAX_ACCOUNTKEY: supplier.wm_sync() return HttpResponseRedirect(reverse('supplier-view', args=[supplier.id])) except InvalidForm: pass context_vars['supplier_form'] = supplier_form context_vars['address_form'] = address_form context_vars['postal_address_form'] = postal_address_form return direct_to_template(request, template='supplier/form.html', extra_context=context_vars)
context_vars['address_form'] = address_form context_vars['postal_address_form'] = postal_address_form return direct_to_template(request, template='supplier/form.html', extra_context=context_vars) @login_required def edit_supplier(request, object_id): context_vars = dict() try: object_id = int(object_id) supplier = Supplier.objects.get(id=object_id) except ValueError, ObjectDoesNotExist: return HttpResponseRedirect(reverse('supplier-list')) context_vars['header'] = capfirst(_('edit supplier %s') % supplier.name) supplier_form = SupplierForm(instance=supplier) address_form = AddressForm(prefix='address', instance=supplier.address) postal_address_form = AddressForm(prefix='post_address', instance=supplier.postal_address) if request.method == "POST": supplier_form = SupplierForm(request.POST, request.FILES, instance=supplier) address_form = AddressForm(request.POST, request.FILES, prefix='address', instance=supplier.address) postal_address_form = AddressForm(request.POST, request.FILES, prefix='post_address', instance=supplier.postal_address) if supplier_form.is_valid() and address_form.is_valid() and postal_address_form.is_valid(): supplier = supplier_form.save() address_form.save() postal_address_form.save() if settings.WORKFLOWMAX_APIKEY and settings.WORKFLOWMAX_ACCOUNTKEY: supplier.wm_sync() return HttpResponseRedirect(reverse('supplier-view', args=[supplier.id])) context_vars['supplier_form'] = supplier_form context_vars['address_form'] = address_form
def add_client(request): return_to = request.GET.get('return_to', '') context_vars = dict() context_vars['header'] = capfirst(_('add new client')) client_form = ClientForm(prefix='client') # contact_form = ContactForm(prefix='contact') address_form = AddressForm(prefix='address') postal_address_form = AddressForm(prefix='post_address') if request.method == "POST": client_form = ClientForm(request.POST, request.FILES, prefix='client') address_form = AddressForm(request.POST, request.FILES, prefix='address') postal_address_form = AddressForm(request.POST, request.FILES, prefix='post_address') try: if client_form.is_valid() and address_form.is_valid(): client = client_form.save() address_form = AddressForm(request.POST, request.FILES, prefix='address', instance=client.address) if not address_form.is_valid(): raise InvalidForm() address_form.save() postal_address_form = AddressForm(request.POST, request.FILES, prefix='post_address', instance=client.postal_address) if postal_address_form.is_valid(): postal_address_form.save() else: client.postal_address.postcode = client.address.postcode client.postal_address.address = client.address.address client.postal_address.city = client.address.city client.postal_address.county = client.address.county client.postal_address.country = client.address.country client.postal_address.latitude = client.address.latitude client.postal_address.longitude = client.address.longitude client.postal_address.save() if settings.WORKFLOWMAX_APIKEY and settings.WORKFLOWMAX_ACCOUNTKEY: client.wm_sync() if not return_to: return_to = reverse('client-view', args=[client.id]) messages.success(request, capfirst(_('client was created successfully')), fail_silently=True) return HttpResponseRedirect(return_to) except InvalidForm: pass context_vars['client_form'] = client_form context_vars['address_form'] = address_form context_vars['postal_address_form'] = postal_address_form return direct_to_template(request, template='client/form.html', extra_context=context_vars)
return direct_to_template(request, template='client/form.html', extra_context=context_vars) @login_required def edit_client(request, object_id): return_to = request.GET.get('return_to', '') context_vars = dict() try: object_id = int(object_id) client = Client.objects.get(id=object_id) except ValueError, ObjectDoesNotExist: return HttpResponseRedirect(reverse('client-list')) context_vars['header'] = capfirst(_('edit client %s') % client.name) client_form = ClientForm(prefix='client', instance=client) address_form = AddressForm(prefix='address', instance=client.address) postal_address_form = AddressForm(prefix='post_address', instance=client.postal_address) if request.method == "POST": client_form = ClientForm(request.POST, request.FILES, prefix='client', instance=client) address_form = AddressForm(request.POST, request.FILES, prefix='address', instance=client.address) postal_address_form = AddressForm(request.POST, request.FILES, prefix='post_address', instance=client.postal_address) if client_form.is_valid() and address_form.is_valid() and postal_address_form.is_valid(): client = client_form.save() address_form.save() postal_address_form.save() if settings.WORKFLOWMAX_APIKEY and settings.WORKFLOWMAX_ACCOUNTKEY: client.wm_sync() if not return_to: return_to = reverse('client-view', args=[client.id]) messages.success(request, capfirst(_('client was modified successfully')), fail_silently=True) return HttpResponseRedirect(return_to)
context_vars['staff_form'] = staff_form context_vars['address_form'] = address_form return direct_to_template(request, template='staff/form.html', extra_context=context_vars) @login_required def edit_staff(request, object_id): context_vars = dict() try: object_id = int(object_id) staff = Staff.objects.get(id=object_id) except ValueError, ObjectDoesNotExist: return HttpResponseRedirect(reverse('staff-list')) context_vars['header'] = capfirst(_('edit staff %s') % staff.username) staff_form = StaffForm(instance=staff) address_form = AddressForm(instance=staff.address) if request.method == "POST": staff_form = StaffForm(request.POST, request.FILES, instance=staff) address_form = AddressForm(request.POST, request.FILES, instance=staff.address) if staff_form.is_valid() and address_form.is_valid(): staff = staff_form.save() address_form.save() if settings.WORKFLOWMAX_APIKEY and settings.WORKFLOWMAX_ACCOUNTKEY: staff.wm_sync() return HttpResponseRedirect(reverse('staff-view', args=[staff.id])) context_vars['staff_form'] = staff_form context_vars['address_form'] = address_form return direct_to_template(request, template='staff/form.html', extra_context=context_vars) @login_required