def update(request, id): client = Client.objects.get(id=id) form = ClientForm(request.POST, instance=client) if form.is_valid(): form.save(commit=True) return redirect("/clients") return render(request, 'edit.html', {'form': form, 'clientid': client.id})
def profile_edit(request, template_name="clients/clients_profile_edit.html"): client = get_object_or_404(Client, user__id=request.user.id) try: get_user_customer(request.user) # initiate customer except: pass if is_healer(client): return redirect('healer_edit') phones = ClientPhoneNumber.objects.filter(client=client) if request.method == 'POST': form = ClientForm(request.POST) if form.is_valid(): if phones: phones[0].number = form.cleaned_data['phone'] phones[0].save() else: ClientPhoneNumber.objects.create( client=client, number=form.cleaned_data['phone']) client.user.first_name = form.cleaned_data['first_name'] client.user.last_name = form.cleaned_data['last_name'] client.user.save() client.about = form.cleaned_data['about'] client.send_reminders = form.cleaned_data['send_reminders'] client.ghp_notification_frequency = form.cleaned_data[ 'ghp_notification_frequency'] # client.beta_tester = form.cleaned_data['beta_tester'] client.save() return redirect('clients_profile') else: form = ClientForm( initial={ 'first_name': client.user.first_name, 'last_name': client.user.last_name, 'about': client.about, 'phone': (phones[0].number if (len(phones) > 0) else ''), 'send_reminders': client.send_reminders, 'ghp_notification_frequency': client.ghp_notification_frequency # 'beta_tester' : client.beta_tester }) return render_to_response(template_name, { 'client': client, 'form': form, 'locations': client.get_locations_objects(), 'STRIPE_PUBLIC_KEY': settings.STRIPE_PUBLIC_KEY, }, context_instance=RequestContext(request))
def reg_page(request): c = get_common_context(request) if request.method == 'GET': client_form = ClientForm() profile_form = ProfileForm() user_form = UserForm() elif request.method == 'POST': client_form = ClientForm(request.POST) profile_form = ProfileForm(request.POST) user_form = UserForm(request.POST) if request.POST.get('confirm', None) is None: user_form.errors['confirm'] = ErrorList([u'Необходимо согласиться с договором.']) elif user_form.data['passwd1'] != user_form.data['passwd2']: user_form.errors['passwd1'] = ErrorList([u'Пароли не совпадают.']) elif not user_form.data['email']: user_form.errors['email'] = ErrorList([u'Обязательное поле.']) elif client_form.is_valid() and profile_form.is_valid() and user_form.is_valid(): try: u = auth.models.User(username=user_form.data['email'], email=user_form.data['email'], first_name=user_form.data['first_name'], ) u.save() u.set_password(user_form.data['passwd1']) u.save() except: u = None user_form.errors['email'] = ErrorList([u'Пользователь с таким email уже существует']) if u: # user saved, all is right p = u.get_profile() p.phone = profile_form.data['phone'] p.work_phone=profile_form.data['work_phone'] p.qiwi=profile_form.data['qiwi'] p.user = u p.type = 'c' p.save() Client.add(u, client_form.data['name']) user = auth.authenticate(username=user_form.data['email'], password=user_form.data['passwd1']) auth.login(request, user) messages.success(request, u'Вы успешно зарегистрировались в системе в качестве клиента.') return HttpResponseRedirect('/cabinet') c['client_form'] = client_form c['profile_form'] = profile_form c['user_form'] = user_form return render_to_response('client/reg.html', c, context_instance=RequestContext(request))
def add_client(request): if request.method == 'POST': form = ClientForm(request.POST) if form.is_valid(): client = form.save(commit=False) client.save() return HttpResponseRedirect( reverse(list_clients) + '?info=success') else: return render(request, 'clients/new_client.html', {'form': form}) else: context = {'form': ClientForm()} return render(request, 'clients/new_client.html', context)
def get(self, *args, **kwargs): """ Display the client form """ context = {} client = get_object_or_404(Client, id=kwargs['client_id']) context['form'] = ClientForm(instance=client) return render(self.request, self.template_name, context)
def post(self, *args, **kwargs): """Client add form """ client_form = ClientForm(self.request.POST) if client_form.is_valid() : client = client_form.save(commit=False) client.owner = self.request.user client.company = self.request.user.company client.save() messages.success(self.request, 'Client is successfully added') return redirect('index') else: context = super().get_context_data(**kwargs) context['client_form'] = client_form context['invoices'] = Invoice.objects.filter(company=self.request.user.company) context['clients'] = Client.objects.filter(company=self.request.user.company) return render(self.request, self.template_name, context=context)
def post(self, *args, **kwargs): """ Getting the filled form of client """ form = ClientForm(self.request.POST, self.request.FILES, company=self.request.user.company) if form.is_valid(): client = form.save(commit=False) client.owner = self.request.user client.company = self.request.user.company client.save() messages.success(self.request, 'Client is successfully added') return redirect('clients') else: context = {} context['form'] = form return render(self.request, self.template_name, context=context)
def post(self, request, *args, **kwargs): """ Getting the filled client form """ client = get_object_or_404(Client, id=kwargs['client_id']) form = ClientForm(self.request.POST, instance=client, company=self.request.user.company) if form.is_valid(): client = form.save(commit=False) client.owner = self.request.user if self.request.user.company: client.company = self.request.user.company client.save() messages.success(request, 'Client is successfully updated') return redirect('clients') else: context = {} context['form'] = form return render(self.request, self.template_name, context=context)
def create_client(request): if request.method == 'POST': form = ClientForm(request.POST) if form.is_valid(): cleaned_data = form.cleaned_data new_client = Client(**cleaned_data) new_client.save() return redirect('clients_list') else: context = { 'form': form, } return render(request, 'clients/clients_create.html', context) else: form = ClientForm() context = { 'form': form, } return render(request, 'clients/clients_create.html', context)
def edit_client(request, client_id): client = get_object_or_404(Client, pk=client_id) if request.method == 'POST': form = ClientForm(request.POST, instance=client) if form.is_valid(): c = form.save(commit=False) c.save() return HttpResponseRedirect( reverse(list_clients) + '?info=success-edit') else: return render(request, 'clients/edit_client.html', { 'form': form, 'client_name': client.client_name }) else: return render(request, 'clients/edit_client.html', { 'form': ClientForm(instance=client), 'client_name': client.client_name })
def create_client(request): if request.method == 'POST': form = ClientForm(data=request.POST) if form.is_valid(): form.save() messages.success( request, "You successfully add {} contact ".format( form['client_name'].value())) return redirect('all_clients') else: form = ClientForm() return render(request, 'create_client.html', {'form': form})
def update_client(request, pk): client = Client.objects.get(pk=pk) client_dict = model_to_dict(client) if request.method == 'POST': form = ClientForm(request.POST) if form.is_valid(): cleaned_data = form.cleaned_data client = Client(pk=pk, **cleaned_data) client.save(force_update=True) return get_client_details(request, pk) else: context = { 'client': client, 'form': form, } return render(request, 'clients/clients_update.html', context) else: form = ClientForm(client_dict) context = { 'client': client, 'form': form, } return render(request, 'clients/clients_update.html', context)
def edit_client(request, id_client): client = get_object_or_404(Client, id=id_client) form = ClientForm(request.POST or None, instance=client) if request.method == 'POST': if form.is_valid(): obj_client = form.save(commit=False) obj_client.save() messages.success( request, "You successfully updated {} contact information ".format( client.client_name)) return redirect('all_clients') else: return render( request, 'edit_client.html', { 'form': form, 'error': 'The form was not updated successfully. ' 'Please enter correct information' }) else: return render(request, 'edit_client.html', {'form': form})
def home(request): # client = Client if request.method == 'POST': form = ClientForm(request.POST) if form.is_valid(): form.save() return render(request, 'navigations/index.html', {'mensaje':'Información recibida, te contactaremos pronto'}) else: form = ClientForm() return render(request, 'navigations/index.html', {'form':form})
def get(self, *args, **kwargs): """ Display invoices, clients and client add form """ clients = Client.objects.filter(company=self.request.user.company) invoices = Invoice.objects.filter(company=self.request.user.company) context = {} context['invoices'] = Invoice.objects.filter(company=self.request.user.company) context['clients'] = Client.objects.filter(company=self.request.user.company) context['client_form'] = ClientForm() query = self.request.GET.get("q") if query: clients = clients.filter(display_name__icontains=query, owner=self.request.user) context['clients'] = clients return render(self.request, self.template_name, context)
def contact(request): # client = Client if request.method == 'POST': form = ClientForm(request.POST) if form.is_valid(): form.save() return render(request, 'navigations/contact.html', {'mensaje':'Informacion recibida'}) else: form = ClientForm() return render(request, 'navigations/contact.html', {'form':form})
def add(request): if request.method == "POST": form = ClientForm(request.POST) if form.is_valid(): try: form.save(commit=True) return redirect('/clients') except: print(form.errors) pass else: print(form.errors) else: form = ClientForm() return render(request, 'index.html', {'form': form})
def edit_client(request, id): try: id = int(id) if id > 0: client = Client.objects.get(id=id) if not request.user.has_perm('clients.change_client'): return HttpResponseForbidden() else: if not request.user.has_perm('clients.add_client'): return HttpResponseForbidden() client = False if request.POST: if client: form = ClientForm(request.POST, instance=client) else: form = ClientForm(request.POST) if form.is_valid(): form.save() return redirect('client-list') else: if client: form = ClientForm(instance=client) else: form = ClientForm() except: client = False form = False c = RequestContext(request, { 'client': client, 'form': form, 'id': id, }) return render_to_response("clients/edit.html", c)
def get(self, *args, **kwargs): """ Display the client form """ context = {} context['form'] = ClientForm() return render(self.request, self.template_name, context)
def edit(request, id): client = Client.objects.get(id=id) form = ClientForm(instance=client) return render(request, 'edit.html', {'form': form, 'clientid': client.id})