def register_wholesale(request): """Регистрация оптового покупателя""" if request.POST: form = RegisterForm(request.POST) company_form = CompanyForm(request.POST) if form.is_valid() and company_form.is_valid(): instance = form.save(commit=False) instance.set_password(form.cleaned_data["password"]) instance.status = User.STATUS_WHOLESALE instance.save() instance = authenticate(email=instance.email, password=form.cleaned_data["password"]) user_login(request, instance) company = company_form.save(commit=False) company.user = request.user company.save() return redirect(reverse("users.auth.register.completed")) else: form = RegisterForm() company_form = CompanyForm() context = {"form": form, "company_form": company_form} return render(request, "users/auth/register_wholesale.html", context)
def get(self, *args, **kwargs): """Display signup form """ context = { 'company_form': CompanyForm(), 'signup_form': SignupForm(), } return render(self.request, self.template_name, context)
def post(self, *args, **kwargs): company_form = CompanyForm(self.request.POST, self.request.FILES) signup_form = SignupForm(self.request.POST, self.request.FILES) if signup_form.is_valid() and company_form.is_valid(): company = company_form.save(commit=False) user = signup_form.save(commit=False) company.save() user.company = company user.save() messages.error( self.request, 'Account successfully created. Activate your account from the admin.' ) return redirect('index') else: context = { 'company_form': CompanyForm(self.request.POST), 'signup_form': SignupForm(self.request.POST), } return render(self.request, self.template_name, context)
def update(request): try: company = request.user.company except Company.DoesNotExist: # Для старых компаний company = Company() if request.POST: form = CompanyForm(request.POST, instance=company) if form.is_valid(): form.save() return HttpResponseRedirect('.') else: form = CompanyForm(instance=company) context = { 'form': form, } return render(request, 'users/company/update.html', context)
def post(self, request, *args, **kwargs): """Update a user """ user = get_object_or_404(User, pk=kwargs['user_id']) user_form = UserUpdateForm(self.request.POST, self.request.FILES, instance=user) company_form = CompanyForm(self.request.POST, self.request.FILES, instance=user.company) if user_form.is_valid() and company_form.is_valid(): company_form.save() user_form.save() messages.success(self.request, 'User is successfully updated') return redirect('index') else: context = { 'company_form': company_form, 'user_form': user_form, } return render(self.request, self.template_name, context=context)
def get(self, *args, **kwargs): """Display form """ user = get_object_or_404(User, pk=kwargs['user_id']) if self.request.user == user: context = { 'company_form': CompanyForm(instance=user.company), 'user_form': UserUpdateForm(instance=user), } return render(self.request, self.template_name, context=context) else: raise Http404("Does not exist")