From d9ec205c4eed422a2b4db2fa53dda52a06e8e65c Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 17 Oct 2023 15:49:56 +0200 Subject: [PATCH] add services --- idhub/admin/forms.py | 5 +- idhub/admin/views.py | 80 ++++++++++++------- idhub/migrations/0002_rol.py | 27 +++++++ idhub/migrations/0003_service.py | 37 +++++++++ idhub/models.py | 13 +++ .../idhub/admin_service_register.html | 34 ++++++++ idhub/templates/idhub/admin_services.html | 31 +++++++ idhub/urls.py | 6 ++ 8 files changed, 202 insertions(+), 31 deletions(-) create mode 100644 idhub/migrations/0002_rol.py create mode 100644 idhub/migrations/0003_service.py create mode 100644 idhub/templates/idhub/admin_service_register.html diff --git a/idhub/admin/forms.py b/idhub/admin/forms.py index d96dcbc..c6f90bc 100644 --- a/idhub/admin/forms.py +++ b/idhub/admin/forms.py @@ -1,6 +1,5 @@ from django import forms from django.contrib.auth.models import User -from idhub.models import Membership class ProfileForm(forms.ModelForm): @@ -18,3 +17,7 @@ class MembershipForm(forms.ModelForm): class RolForm(forms.ModelForm): MANDATORY_FIELDS = ['name'] + +class ServiceForm(forms.ModelForm): + MANDATORY_FIELDS = ['domain', 'rol'] + diff --git a/idhub/admin/views.py b/idhub/admin/views.py index 7e54d5d..780902d 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -7,9 +7,9 @@ from django.contrib.auth.models import User from django.shortcuts import get_object_or_404, redirect from django.urls import reverse_lazy from django.contrib import messages -from idhub.models import Membership, Rol +from idhub.models import Membership, Rol, Service from idhub.mixins import AdminView -from idhub.admin.forms import ProfileForm, MembershipForm, RolForm +from idhub.admin.forms import ProfileForm, MembershipForm, RolForm, ServiceForm class AdminDashboardView(AdminView, TemplateView): @@ -224,42 +224,18 @@ class AdminRolesView(AccessControl): class AdminRolRegisterView(AccessControl, CreateView): template_name = "idhub/admin_rol_register.html" subtitle = _('Add Rol') - icon = 'bi bi-person' + icon = '' model = Rol from_class = RolForm fields = ('name',) success_url = reverse_lazy('idhub:admin_roles') object = None -# class AdminPeopleMembershipRegisterView(People, CreateView): - # template_name = "idhub/admin_people_membership_register.html" - - # def get(self, request, *args, **kwargs): - # self.pk = kwargs['pk'] - # self.user = get_object_or_404(User, pk=self.pk) - # return super().get(request, *args, **kwargs) - - # def post(self, request, *args, **kwargs): - # self.pk = kwargs['pk'] - # self.user = get_object_or_404(User, pk=self.pk) - # return super().post(request, *args, **kwargs) - - # def get_form(self): - # form = super().get_form() - # form.fields['start_date'].widget.input_type = 'date' - # form.fields['end_date'].widget.input_type = 'date' - # return form - - # def get_form_kwargs(self): - # self.object = self.model(user=self.user) - # kwargs = super().get_form_kwargs() - # return kwargs - class AdminRolEditView(AccessControl, CreateView): template_name = "idhub/admin_rol_register.html" - subtitle = _('Add Rol') - icon = 'bi bi-person' + subtitle = _('Edit Rol') + icon = '' model = Rol from_class = RolForm fields = ('name',) @@ -284,12 +260,56 @@ class AdminRolDeleteView(AccessControl): return redirect('idhub:admin_roles') - class AdminServicesView(AccessControl): template_name = "idhub/admin_services.html" subtitle = _('Service Management') icon = '' + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context.update({ + 'services': Service.objects, + }) + return context + +class AdminServiceRegisterView(AccessControl, CreateView): + template_name = "idhub/admin_service_register.html" + subtitle = _('Add Service') + icon = '' + model = Service + from_class = ServiceForm + fields = ('domain', 'description', 'rol') + success_url = reverse_lazy('idhub:admin_services') + object = None + + +class AdminServiceEditView(AccessControl, CreateView): + template_name = "idhub/admin_service_register.html" + subtitle = _('Edit Service') + icon = '' + model = Service + from_class = ServiceForm + fields = ('domain', 'description', 'rol') + success_url = reverse_lazy('idhub:admin_services') + + def get_form_kwargs(self): + pk = self.kwargs.get('pk') + if pk: + self.object = get_object_or_404(self.model, pk=pk) + kwargs = super().get_form_kwargs() + return kwargs + + +class AdminServiceDeleteView(AccessControl): + model = Service + + def get(self, request, *args, **kwargs): + self.pk = kwargs['pk'] + self.object = get_object_or_404(self.model, pk=self.pk) + + self.object.delete() + return redirect('idhub:admin_services') + class AdminCredentialsView(Credentials): template_name = "idhub/admin_credentials.html" diff --git a/idhub/migrations/0002_rol.py b/idhub/migrations/0002_rol.py new file mode 100644 index 0000000..41f8aba --- /dev/null +++ b/idhub/migrations/0002_rol.py @@ -0,0 +1,27 @@ +# Generated by Django 4.2.5 on 2023-10-17 11:28 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("idhub", "0001_initial"), + ] + + operations = [ + migrations.CreateModel( + name="Rol", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("name", models.CharField(max_length=250)), + ], + ), + ] diff --git a/idhub/migrations/0003_service.py b/idhub/migrations/0003_service.py new file mode 100644 index 0000000..2c908de --- /dev/null +++ b/idhub/migrations/0003_service.py @@ -0,0 +1,37 @@ +# Generated by Django 4.2.5 on 2023-10-17 13:29 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + dependencies = [ + ("idhub", "0002_rol"), + ] + + operations = [ + migrations.CreateModel( + name="Service", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("domain", models.CharField(max_length=250)), + ("description", models.CharField(max_length=250)), + ( + "rol", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="services", + to="idhub.rol", + ), + ), + ], + ), + ] diff --git a/idhub/models.py b/idhub/models.py index 1c5091b..54c1953 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -84,3 +84,16 @@ class Membership(models.Model): class Rol(models.Model): name = models.CharField(max_length=250) + + def __str__(self): + return self.name + + +class Service(models.Model): + domain = models.CharField(max_length=250) + description = models.CharField(max_length=250) + rol = models.ForeignKey( + Rol, + on_delete=models.CASCADE, + related_name='services', + ) diff --git a/idhub/templates/idhub/admin_service_register.html b/idhub/templates/idhub/admin_service_register.html new file mode 100644 index 0000000..625999d --- /dev/null +++ b/idhub/templates/idhub/admin_service_register.html @@ -0,0 +1,34 @@ +{% extends "idhub/base_admin.html" %} +{% load i18n %} + +{% block content %} +

+ + {{ subtitle }} +

+{% load django_bootstrap5 %} +
+{% csrf_token %} +{% if form.errors %} + +{% endif %} +
+
+ {% bootstrap_form form %} +
+
+ + +
+{% endblock %} diff --git a/idhub/templates/idhub/admin_services.html b/idhub/templates/idhub/admin_services.html index f5849fd..11f7c0b 100644 --- a/idhub/templates/idhub/admin_services.html +++ b/idhub/templates/idhub/admin_services.html @@ -6,4 +6,35 @@ {{ subtitle }} +
+
+
+ + + + + + + + + + + + {% for service in services.all %} + + + + + + + + {% endfor %} + +
{{ service.domain }}{{ service.description }}{{ service.rol.name }}
+ +
+
+
{% endblock %} diff --git a/idhub/urls.py b/idhub/urls.py index 3089005..4c64041 100644 --- a/idhub/urls.py +++ b/idhub/urls.py @@ -80,6 +80,12 @@ urlpatterns = [ name='admin_rol_del'), path('admin/services/', views_admin.AdminServicesView.as_view(), name='admin_services'), + path('admin/services/new', views_admin.AdminServiceRegisterView.as_view(), + name='admin_service_new'), + path('admin/services/', views_admin.AdminServiceEditView.as_view(), + name='admin_service_edit'), + path('admin/services//del', views_admin.AdminServiceDeleteView.as_view(), + name='admin_service_del'), path('admin/credentials/', views_admin.AdminCredentialsView.as_view(), name='admin_credentials'), path('admin/credentials/new/', views_admin.AdminIssueCredentialsView.as_view(),