feat: added user info form
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
{% load i18n %}
|
||||
{% load static %}
|
||||
<form hx-post="{% url 'web:user_info_component' %}" hx-swap="none">
|
||||
{% csrf_token %}
|
||||
{% for field in form %}
|
||||
<div class="mb-4">
|
||||
|
||||
<label for="{{ field.id_for_label }}"
|
||||
class="inline-block text-sm font-medium text-gray-900 dark:text-white ml-2">
|
||||
{{ field.label }}
|
||||
</label>
|
||||
{{ field }}
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
{% for k, error in form.errors.items %}
|
||||
<span class="flex justify-center mb-2 text-sm font-medium text-red-900 dark:text-red-400">{{ error }}</span>
|
||||
{% endfor %}
|
||||
|
||||
<button class="w-full text-white bg-primary-600 hover:bg-primary-700 focus:ring-4 focus:outline-none
|
||||
focus:ring-primary-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-primary-600
|
||||
dark:hover:bg-primary-700 dark:focus:ring-primary-800"
|
||||
>
|
||||
{% translate 'Actualizar datos' %}
|
||||
</button>
|
||||
|
||||
|
||||
</form>
|
||||
@@ -17,7 +17,7 @@
|
||||
<h1 class="text-xl font-bold leading-tight tracking-tight text-gray-900 md:text-2xl dark:text-white">
|
||||
{% translate 'Inicia sesión en tu cuenta' %}
|
||||
</h1>
|
||||
<form action="{% url 'web:login' %}" method="post">
|
||||
<form action="{% url 'web:login' %}{% querystring %}" method="post">
|
||||
{% csrf_token %}
|
||||
{% for field in form %}
|
||||
{% if field.id_for_label != 'id_remember_me' %}
|
||||
|
||||
@@ -9,12 +9,11 @@
|
||||
>
|
||||
{% translate 'Mi cuenta' %}
|
||||
</h2>
|
||||
<div
|
||||
class="mt-4 p-6 border border-gray-100 rounded-md dark:border-gray-600"
|
||||
hx-get="{% url 'web:user_info_component' %}" hx-trigger="load"
|
||||
>
|
||||
|
||||
<div class="flex space-x-4">
|
||||
<div>
|
||||
<h2 class="flex items-center text-lg font-bold leading-none text-gray-900 dark:text-white sm:text-xl">
|
||||
{{ user.first_name }} {{ user.last_name }}</h2>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-4 p-6 border border-gray-100 rounded-md dark:border-gray-600">
|
||||
|
||||
+13
-3
@@ -9,6 +9,7 @@ from web.views.components import (
|
||||
list_products,
|
||||
list_wishlisted_products,
|
||||
wishlist_button,
|
||||
user_info_component,
|
||||
)
|
||||
from web.views.users import (
|
||||
login,
|
||||
@@ -38,11 +39,11 @@ app_name = "web"
|
||||
|
||||
i18n_resolvers = i18n_patterns(
|
||||
path("", index, name="index"),
|
||||
path(_("wishlist"), login_required(wishlist, login_url="/login/"), name="wishlist"),
|
||||
path(_("wishlist"), login_required(wishlist, login_url=_("/login/")), name="wishlist"),
|
||||
path(_("categories/<str:slug>/"), category_view, name="category_view"),
|
||||
path(_("products/<int:pk>/<str:slug>/"), product_detail, name="product_detail"),
|
||||
path(_("cart/"), cart_detail, name="cart_detail"),
|
||||
path(_("orders/"), login_required(orders, login_url="/login/"), name="orders"),
|
||||
path(_("orders/"), login_required(orders, login_url=_("/login/")), name="orders"),
|
||||
path(_("order/<str:uuid>/"), order_detail, name="order"),
|
||||
# users
|
||||
path(_("login/"), login, name="login"),
|
||||
@@ -59,7 +60,11 @@ i18n_resolvers = i18n_patterns(
|
||||
reset_password_email_sent,
|
||||
name="reset_password_email_sent",
|
||||
),
|
||||
path(_("my-account/"), my_account, name="my_account"),
|
||||
path(
|
||||
_("my-account/"),
|
||||
login_required(my_account, login_url=_("/login/")),
|
||||
name="my_account",
|
||||
),
|
||||
)
|
||||
|
||||
urlpatterns = [
|
||||
@@ -77,6 +82,11 @@ urlpatterns = [
|
||||
wishlist_button,
|
||||
name="wishlist_button",
|
||||
),
|
||||
path(
|
||||
"web/components/user-info/",
|
||||
login_required(user_info_component, login_url=_("/login/")),
|
||||
name="user_info_component",
|
||||
),
|
||||
# api
|
||||
path("web/add-cart-item/", add_cart_item, name="add_cart_item"),
|
||||
path("web/delete-cart-item/<int:pk>/", delete_cart_item, name="delete_cart_item"),
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from decimal import Decimal
|
||||
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
@@ -12,7 +13,9 @@ from shop.models import (
|
||||
ShippingMethod,
|
||||
WishlistedProduct,
|
||||
)
|
||||
from users.forms.info import UserInfoForm
|
||||
from web.mixins import FilteredQuerysetMixin, PaginatedQuerysetMixin
|
||||
from web.models import WebSettings
|
||||
from web.settings import ANONYMOUS_CART_ID_COOKIE_NAME
|
||||
from web.utils import get_or_create_cart
|
||||
|
||||
@@ -142,6 +145,41 @@ class WishlistButton(TemplateView):
|
||||
}
|
||||
|
||||
|
||||
class UserInfoComponentView(TemplateView):
|
||||
template_name = "components/users/info.html"
|
||||
form_class = UserInfoForm
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
web_settings = WebSettings.load()
|
||||
user = self.request.user
|
||||
form = self.form_class(
|
||||
initial={
|
||||
"email": user.email,
|
||||
"first_name": user.first_name,
|
||||
"last_name": user.last_name,
|
||||
}
|
||||
)
|
||||
|
||||
return {
|
||||
"form": form,
|
||||
}
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
user = self.request.user
|
||||
form = self.form_class(request.POST)
|
||||
|
||||
if form.is_valid():
|
||||
user.email = form.cleaned_data.get("email")
|
||||
user.first_name = form.cleaned_data.get("first_name")
|
||||
user.last_name = form.cleaned_data.get("last_name")
|
||||
user.save()
|
||||
return HttpResponse(status=200)
|
||||
|
||||
return render(request, self.template_name, {"form": form})
|
||||
|
||||
|
||||
list_products = ListProducts.as_view()
|
||||
list_wishlisted_products = ListWishlistedProducts.as_view()
|
||||
wishlist_button = WishlistButton.as_view()
|
||||
|
||||
user_info_component = UserInfoComponentView.as_view()
|
||||
|
||||
+7
-1
@@ -1,3 +1,5 @@
|
||||
import urllib
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth import login as login_user
|
||||
@@ -6,7 +8,7 @@ from django.contrib.auth.tokens import default_token_generator
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import IntegrityError
|
||||
from django.shortcuts import redirect, render, reverse
|
||||
from django.utils.http import url_has_allowed_host_and_scheme, urlsafe_base64_decode
|
||||
from django.utils.http import url_has_allowed_host_and_scheme, urlsafe_base64_decode, urlencode
|
||||
from django.utils.text import gettext_lazy as _
|
||||
from django.views.generic import TemplateView, View
|
||||
|
||||
@@ -29,6 +31,10 @@ class LoginView(View, RedirectionMixin):
|
||||
redirect_to = "web:index"
|
||||
login_form_class = LoginForm
|
||||
|
||||
def get_redirection(self):
|
||||
query_param_string = urlencode(self.request.GET)
|
||||
return f'{reverse(self.redirect_to)}?{query_param_string.join("")}'
|
||||
|
||||
def get_login_form(self, request):
|
||||
return self.login_form_class(data=request.POST)
|
||||
|
||||
|
||||
+2
-2
@@ -187,7 +187,7 @@ class WishlistView(TemplateView):
|
||||
def get_context_data(self, **kwargs):
|
||||
settings = WebSettings.load()
|
||||
return {
|
||||
"title": settings.web_title,
|
||||
"title": _(f"{settings.web_title} - Lista de deseados"),
|
||||
"description": settings.web_description,
|
||||
}
|
||||
|
||||
@@ -206,7 +206,7 @@ class OrdersView(TemplateView, FilteredQuerysetMixin, PaginatedQuerysetMixin):
|
||||
page = self.get_paginated_queryset(qs)
|
||||
|
||||
return {
|
||||
"title": settings.web_title,
|
||||
"title": _(f"{settings.web_title} - Mis pedidos"),
|
||||
"page": page,
|
||||
"has_next_page": page.has_next(),
|
||||
"has_previous_page": page.has_previous(),
|
||||
|
||||
Reference in New Issue
Block a user