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 %} +