feat: register page
This commit is contained in:
+55
-6
@@ -1,14 +1,19 @@
|
||||
from django.contrib.auth import login as login_user
|
||||
from django.contrib.auth import login as login_user, authenticate
|
||||
from django.contrib.auth import logout as logout_user
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.db import IntegrityError
|
||||
from django.shortcuts import redirect, render, reverse
|
||||
from django.utils.text import gettext_lazy as _
|
||||
from django.views.generic import TemplateView, View
|
||||
|
||||
from shop.models import CustomerAddress
|
||||
from users.forms import LoginForm
|
||||
from users.forms import LoginForm, RegisterForm
|
||||
from web.models import WebSettings
|
||||
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
class RedirectionMixin:
|
||||
redirect_to = ""
|
||||
|
||||
@@ -55,6 +60,54 @@ class LoginView(View, RedirectionMixin):
|
||||
return render(request, self.template_name, {"form": login_form}, status=400)
|
||||
|
||||
|
||||
class RegisterView(TemplateView, RedirectionMixin):
|
||||
template_name = "users/register.html"
|
||||
redirect_to = "web:index"
|
||||
form_class = RegisterForm
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
settings = WebSettings.load()
|
||||
|
||||
return {
|
||||
"form": self.form_class(),
|
||||
"title": settings.web_title,
|
||||
"description": _("crear cuenta"),
|
||||
}
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
settings = WebSettings.load()
|
||||
form = self.form_class(request.POST)
|
||||
errors = []
|
||||
|
||||
try:
|
||||
if form.is_valid():
|
||||
user = form.save()
|
||||
|
||||
user.username = user.email
|
||||
user.first_name = form.cleaned_data.get("first_name")
|
||||
user.last_name = form.cleaned_data.get("last_name")
|
||||
user.save()
|
||||
|
||||
login(request, user)
|
||||
return redirect(self.get_redirection())
|
||||
else:
|
||||
errors = form.errors
|
||||
except IntegrityError as e:
|
||||
errors = [_("Ya existe un usuario con ese correo electrónico"), ]
|
||||
|
||||
return render(
|
||||
request,
|
||||
self.template_name,
|
||||
{
|
||||
"form": form,
|
||||
"title": settings.web_title,
|
||||
"description": _("Crear cuenta"),
|
||||
"errors": errors,
|
||||
},
|
||||
status=400,
|
||||
)
|
||||
|
||||
|
||||
class LogoutView(View, RedirectionMixin):
|
||||
redirect_to = "web:index"
|
||||
|
||||
@@ -67,10 +120,6 @@ class LogoutView(View, RedirectionMixin):
|
||||
return redirect(self.get_redirection())
|
||||
|
||||
|
||||
class RegisterView(TemplateView):
|
||||
template_name = "web/register.html"
|
||||
|
||||
|
||||
class ResetPasswordView(TemplateView):
|
||||
template_name = "web/reset-password.html"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user