diff --git a/idhub/admin/forms.py b/idhub/admin/forms.py index 74046df..fc66f14 100644 --- a/idhub/admin/forms.py +++ b/idhub/admin/forms.py @@ -1,5 +1,6 @@ from django import forms from django.contrib.auth.models import User +from idhub.models import Rol class ProfileForm(forms.ModelForm): @@ -21,5 +22,6 @@ class RolForm(forms.ModelForm): class ServiceForm(forms.ModelForm): MANDATORY_FIELDS = ['domain', 'rol'] + class UserRolForm(forms.ModelForm): MANDATORY_FIELDS = ['service'] diff --git a/idhub/admin/views.py b/idhub/admin/views.py index 0e6f122..13031b0 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -1,4 +1,5 @@ import logging +from smtplib import SMTPException from django.utils.translation import gettext_lazy as _ from django.views.generic.base import TemplateView @@ -9,6 +10,7 @@ from django.urls import reverse_lazy from django.contrib import messages from idhub.models import Membership, Rol, Service, UserRol from idhub.mixins import AdminView +from idhub.email.views import NotifyActivateUserByEmail from idhub.admin.forms import ( ProfileForm, MembershipForm, @@ -121,7 +123,7 @@ class AdminPeopleEditView(AdminPeopleView, UpdateView): success_url = reverse_lazy('idhub:admin_people_list') -class AdminPeopleRegisterView(People, CreateView): +class AdminPeopleRegisterView(NotifyActivateUserByEmail, People, CreateView): template_name = "idhub/admin/people_register.html" subtitle = _('People Register') icon = 'bi bi-person' @@ -137,6 +139,16 @@ class AdminPeopleRegisterView(People, CreateView): ) return self.success_url + def form_valid(self, form): + user = form.save() + messages.success(self.request, _('The account is created successfully')) + if user.is_active: + try: + self.send_email(user) + except SMTPException as e: + messages.error(self.request, e) + return super().form_valid(form) + class AdminPeopleMembershipRegisterView(People, CreateView): template_name = "idhub/admin/people_membership_register.html" diff --git a/idhub/email/__init__.py b/idhub/email/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/idhub/email/views.py b/idhub/email/views.py new file mode 100644 index 0000000..ae73ebf --- /dev/null +++ b/idhub/email/views.py @@ -0,0 +1,55 @@ +from django.conf import settings +from django.template import loader +from django.core.mail import EmailMultiAlternatives +from django.contrib.auth.tokens import default_token_generator +from django.contrib.sites.shortcuts import get_current_site +from django.utils.encoding import force_bytes +from django.utils.http import urlsafe_base64_encode + + +class NotifyActivateUserByEmail: + def get_email_context(self, user): + """ + Define a new context with a token for put in a email + when send a email for add a new password + """ + protocol = 'https' if self.request.is_secure() else 'http' + current_site = get_current_site(self.request) + site_name = current_site.name + domain = current_site.domain + context = { + 'email': user.email, + 'domain': domain, + 'site_name': site_name, + 'uid': urlsafe_base64_encode(force_bytes(user.pk)), + 'user': user, + 'token': default_token_generator.make_token(user), + 'protocol': protocol, + } + return context + + def send_email(self, user): + """ + Send a email when a user is activated. + """ + context = self.get_email_context(user) + subject_template_name = 'idhub/admin/registration/activate_user_subject.txt' + email_template_name = 'idhub/admin/registration/activate_user_email.txt' + html_email_template_name = 'idhub/admin/registration/activate_user_email.html' + subject = loader.render_to_string(subject_template_name, context) + # Email subject *must not* contain newlines + subject = ''.join(subject.splitlines()) + body = loader.render_to_string(email_template_name, context) + from_email = settings.DEFAULT_FROM_EMAIL + to_email = user.email + + email_message = EmailMultiAlternatives( + subject, body, from_email, [to_email]) + html_email = loader.render_to_string(html_email_template_name, context) + email_message.attach_alternative(html_email, 'text/html') + if settings.DEVELOPMENT: + print(to_email) + print(body) + return + + email_message.send() diff --git a/idhub/migrations/0005_remove_service_rol_service_rol.py b/idhub/migrations/0005_remove_service_rol_service_rol.py new file mode 100644 index 0000000..a383ca4 --- /dev/null +++ b/idhub/migrations/0005_remove_service_rol_service_rol.py @@ -0,0 +1,21 @@ +# Generated by Django 4.2.5 on 2023-10-19 13:01 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("idhub", "0004_userrol"), + ] + + operations = [ + migrations.RemoveField( + model_name="service", + name="rol", + ), + migrations.AddField( + model_name="service", + name="rol", + field=models.ManyToManyField(to="idhub.rol"), + ), + ] diff --git a/idhub/models.py b/idhub/models.py index 6c143b7..3c187b3 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -92,12 +92,13 @@ class Rol(models.Model): class Service(models.Model): domain = models.CharField(max_length=250) description = models.CharField(max_length=250) - rol = models.ForeignKey( + rol = models.ManyToManyField( Rol, - on_delete=models.CASCADE, - related_name='services', ) + def get_roles(self): + return ", ".join([x.name for x in self.rol.all()]) + def __str__(self): return "{} -> {}".format(self.domain, self.rol.name) diff --git a/idhub/templates/auth/login.html b/idhub/templates/auth/login.html index 6f43260..28c199e 100644 --- a/idhub/templates/auth/login.html +++ b/idhub/templates/auth/login.html @@ -1,162 +1,54 @@ - +{% extends "auth/login_base.html" %} {% load i18n static %} - - -
- {% block head %} - {% block meta %} - - - - - - {% endblock %} -