add services

This commit is contained in:
Cayo Puigdefabregas 2023-10-17 15:49:56 +02:00
parent b4074005b4
commit d9ec205c4e
8 changed files with 202 additions and 31 deletions

View File

@ -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']

View File

@ -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"

View File

@ -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)),
],
),
]

View File

@ -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",
),
),
],
),
]

View File

@ -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',
)

View File

@ -0,0 +1,34 @@
{% extends "idhub/base_admin.html" %}
{% load i18n %}
{% block content %}
<h3>
<i class="{{ icon }}"></i>
{{ subtitle }}
</h3>
{% load django_bootstrap5 %}
<form role="form" method="post">
{% csrf_token %}
{% if form.errors %}
<div class="alert alert-danger alert-icon alert-icon-border alert-dismissible" role="alert">
<div class="icon"><span class="mdi mdi-close-circle-o"></span></div>
<div class="message">
{% for field, error in form.errors.items %}
{{ error }}<br />
{% endfor %}
<button class="btn-close" type="button" data-dismiss="alert" aria-label="Close"></button>
</div>
</div>
{% endif %}
<div class="row">
<div class="col-sm-4">
{% bootstrap_form form %}
</div>
</div>
<div class="form-actions-no-box">
<a class="btn btn-grey" href="{% url 'idhub:admin_services' %}">{% translate "Cancel" %}</a>
<input class="btn btn-green-admin" type="submit" name="submit" value="{% translate 'Save' %}" />
</div>
</form>
{% endblock %}

View File

@ -6,4 +6,35 @@
<i class="{{ icon }}"></i>
{{ subtitle }}
</h3>
<div class="row mt-5">
<div class="col">
<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Service' %}</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Description' %}</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Rol' %}</button></th>
<th scope="col"></th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{% for service in services.all %}
<tr>
<td>{{ service.domain }}</td>
<td>{{ service.description }}</td>
<td>{{ service.rol.name }}</td>
<td><a href="{% url 'idhub:admin_service_edit' service.id %}" title="{% trans 'Edit' %}"><i class="bi bi-pencil-square"></i></a></td>
<td><a href="{% url 'idhub:admin_service_del' service.id %}" title="{% trans 'Delete' %}"><i class="bi bi-trash"></i></a></td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="form-actions-no-box">
<a class="btn btn-green-admin" href="{% url 'idhub:admin_service_new' %}">{% translate "Add Service" %} <i class="bi bi-plus"></i></a>
</div>
</div>
</div>
</div>
{% endblock %}

View File

@ -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/<int:pk>', views_admin.AdminServiceEditView.as_view(),
name='admin_service_edit'),
path('admin/services/<int:pk>/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(),