From 124395a313c6e086e19e69bcfa30094dcfbb81ca Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 7 Oct 2024 16:56:24 +0200 Subject: [PATCH] edit and delete user --- admin/templates/admin_users.html | 27 +++++++++++--- admin/templates/delete_user.html | 38 ++++++++++++++++++++ admin/templates/user.html | 32 +++++++++++++++++ admin/urls.py | 3 ++ admin/views.py | 62 ++++++++++++++++++++++++++++++++ 5 files changed, 158 insertions(+), 4 deletions(-) create mode 100644 admin/templates/delete_user.html create mode 100644 admin/templates/user.html diff --git a/admin/templates/admin_users.html b/admin/templates/admin_users.html index 9f3cd32..37fbf96 100644 --- a/admin/templates/admin_users.html +++ b/admin/templates/admin_users.html @@ -6,14 +6,33 @@

{{ subtitle }}

+
+ {% translate "Add new user" %} +
- {% for u in users %} - {{ u.email }} - {{ u.is_admin }} - {% endfor %} + + + + + + + + + + + + {% for u in users %} + + + + + + {% endfor %} + +
Emailis Admin
{{ u.email }}{{ u.is_admin }}
diff --git a/admin/templates/delete_user.html b/admin/templates/delete_user.html new file mode 100644 index 0000000..5aa402d --- /dev/null +++ b/admin/templates/delete_user.html @@ -0,0 +1,38 @@ +{% extends "base.html" %} +{% load i18n %} + +{% block content %} +
+
+

{{ subtitle }}

+
+
+ +{% load django_bootstrap5 %} +
+
+ Are you sure than want remove the lot {{ object.name }} with {{ object.devices.count }} devices. +
+
+ +
+{% csrf_token %} +{% if form.errors %} + +{% endif %} +{% bootstrap_form form %} + + +
+{% endblock %} diff --git a/admin/templates/user.html b/admin/templates/user.html new file mode 100644 index 0000000..1ea00d8 --- /dev/null +++ b/admin/templates/user.html @@ -0,0 +1,32 @@ +{% extends "base.html" %} +{% load i18n %} + +{% block content %} +
+
+

{{ subtitle }}

+
+
+ +{% load django_bootstrap5 %} +
+{% csrf_token %} +{% if form.errors %} + +{% endif %} +{% bootstrap_form form %} + + +
+{% endblock %} diff --git a/admin/urls.py b/admin/urls.py index 6a9d492..cd79c3f 100644 --- a/admin/urls.py +++ b/admin/urls.py @@ -6,4 +6,7 @@ app_name = 'admin' urlpatterns = [ path("panel/", views.PanelView.as_view(), name="panel"), path("users/", views.UsersView.as_view(), name="users"), + path("users/new", views.CreateUserView.as_view(), name="new_user"), + path("users/edit/", views.EditUserView.as_view(), name="edit_user"), + path("users/delete/", views.DeleteUserView.as_view(), name="delete_user"), ] diff --git a/admin/views.py b/admin/views.py index e95c8c2..bcc8889 100644 --- a/admin/views.py +++ b/admin/views.py @@ -1,5 +1,12 @@ +from django.urls import reverse_lazy +from django.shortcuts import get_object_or_404 from django.utils.translation import gettext_lazy as _ from django.views.generic.base import TemplateView +from django.views.generic.edit import ( + CreateView, + UpdateView, + DeleteView, +) from dashboard.mixins import DashboardView from user.models import User @@ -25,3 +32,58 @@ class UsersView(DashboardView, TemplateView): "users": User.objects.filter() }) return context + + +class CreateUserView(DashboardView, CreateView): + template_name = "user.html" + title = _("User") + breadcrumb = _("admin / User") + " /" + success_url = reverse_lazy('admin:users') + model = User + fields = ( + "email", + "password", + "is_admin", + ) + + def form_valid(self, form): + form.instance.institution = self.request.user.institution + form.instance.set_password(form.instance.password) + response = super().form_valid(form) + return response + + +class DeleteUserView(DashboardView, DeleteView): + template_name = "delete_user.html" + title = _("Delete user") + breadcrumb = "admin / Delete user" + success_url = reverse_lazy('admin:users') + model = User + fields = ( + "email", + "password", + "is_admin", + ) + + def form_valid(self, form): + response = super().form_valid(form) + return response + + +class EditUserView(DashboardView, UpdateView): + template_name = "user.html" + title = _("Edit user") + breadcrumb = "admin / Edit user" + success_url = reverse_lazy('admin:users') + model = User + fields = ( + "email", + "is_admin", + ) + + def get_form_kwargs(self): + pk = self.kwargs.get('pk') + self.object = get_object_or_404(self.model, pk=pk) + #self.object.set_password(self.object.password) + kwargs = super().get_form_kwargs() + return kwargs