fix messages system

This commit is contained in:
Cayo Puigdefabregas 2023-10-13 21:04:44 +02:00
parent 9ad6be8db6
commit e3ac939afc
8 changed files with 32 additions and 19 deletions

View File

@ -3,7 +3,7 @@ from django.contrib.auth.models import User
class ProfileForm(forms.ModelForm): class ProfileForm(forms.ModelForm):
MANDATORY_FIELDS = ['first_name', 'last_name', 'email'] MANDATORY_FIELDS = ['first_name', 'last_name', 'email', 'username']
class Meta: class Meta:
model = User model = User

View File

@ -81,6 +81,10 @@ class AdminPeopleActivateView(AdminPeopleView):
self.pk = kwargs['pk'] self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk) self.object = get_object_or_404(self.model, pk=self.pk)
if self.object == self.request.user:
messages.error(self.request, _('Is not possible deactivate your account!'))
return redirect('idhub:admin_people', self.object.id)
if self.object.is_active: if self.object.is_active:
self.object.is_active = False self.object.is_active = False
else: else:
@ -96,14 +100,17 @@ class AdminPeopleDeleteView(AdminPeopleView):
self.pk = kwargs['pk'] self.pk = kwargs['pk']
self.object = get_object_or_404(self.model, pk=self.pk) self.object = get_object_or_404(self.model, pk=self.pk)
self.object.delete() if self.object != self.request.user:
self.object.delete()
else:
messages.error(self.request, _('Is not possible delete your account!'))
return redirect('idhub:admin_people_list') return redirect('idhub:admin_people_list')
class AdminPeopleEditView(AdminPeopleView, UpdateView): class AdminPeopleEditView(AdminPeopleView, UpdateView):
template_name = "idhub/admin_user_edit.html" template_name = "idhub/admin_user_edit.html"
from_class = ProfileForm from_class = ProfileForm
fields = ('first_name', 'last_name', 'email') fields = ('first_name', 'last_name', 'email', 'username')
success_url = reverse_lazy('idhub:admin_people_list') success_url = reverse_lazy('idhub:admin_people_list')
@ -113,7 +120,7 @@ class AdminPeopleRegisterView(People, CreateView):
icon = 'bi bi-person' icon = 'bi bi-person'
model = User model = User
from_class = ProfileForm from_class = ProfileForm
fields = ('first_name', 'last_name', 'email') fields = ('first_name', 'last_name', 'email', 'username')
success_url = reverse_lazy('idhub:admin_people_list') success_url = reverse_lazy('idhub:admin_people_list')

View File

@ -13,19 +13,17 @@
<div class="alert alert-danger alert-icon alert-icon-border alert-dismissible" role="alert"> <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="icon"><span class="mdi mdi-close-circle-o"></span></div>
<div class="message"> <div class="message">
<button class="close" type="button" data-dismiss="alert" aria-label="Close">
<span class="mdi mdi-close" aria-hidden="true"></span>
</button>
{% for field, error in form.errors.items %} {% for field, error in form.errors.items %}
{{ error }} {{ error }}<br />
{% endfor %} {% endfor %}
<button class="btn-close" type="button" data-dismiss="alert" aria-label="Close"></button>
</div> </div>
</div> </div>
{% endif %} {% endif %}
{% bootstrap_form form %} {% bootstrap_form form %}
<div class="form-actions-no-box"> <div class="form-actions-no-box">
<a class="btn btn-secondary" href="{% url 'idhub:user_profile' %}">{% translate "Cancel" %}</a> <a class="btn btn-grey" href="{% url 'idhub:admin_people_list' %}">{% translate "Cancel" %}</a>
<input class="btn btn-success" type="submit" name="submit" value="{% translate 'Save' %}" /> <input class="btn btn-green-admin" type="submit" name="submit" value="{% translate 'Save' %}" />
</div> </div>
</form> </form>

View File

@ -4,7 +4,7 @@
{% block content %} {% block content %}
<div class="row"> <div class="row">
<div class="col"> <div class="col">
<h3 class="{% if not object.is_active %}bg-danger text-white{% endif %}"> <h3 class="{% if not object.is_active %}alert alert-danger{% endif %}">
<i class="{{ icon }}"></i> <i class="{{ icon }}"></i>
{{ subtitle }} {{ subtitle }}
</h3> </h3>

View File

@ -24,8 +24,8 @@
{% endif %} {% endif %}
{% bootstrap_form form %} {% bootstrap_form form %}
<div class="form-actions-no-box"> <div class="form-actions-no-box">
<a class="btn btn-secondary" href="{% url 'idhub:user_profile' %}">{% translate "Cancel" %}</a> <a class="btn btn-grey" href="{% url 'idhub:admin_people' object.id %}">{% translate "Cancel" %}</a>
<input class="btn btn-success" type="submit" name="submit" value="{% translate 'Save' %}" /> <input class="btn btn-green-admin" type="submit" name="submit" value="{% translate 'Save' %}" />
</div> </div>
</form> </form>

View File

@ -128,10 +128,9 @@
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4"> <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
{% block messages %} {% block messages %}
{% for message in messages %} {% for message in messages %}
<div class="alert alert-{{ message.tags|default:'info' }} alert-dismissible fade show" role="alert"> <div class="alert {{ message.tags|default:'info' }} alert-dismissible fade show mt-3" role="alert">
{{ message }} {{ message }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close"> <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button> </button>
</div> </div>
{% endfor %} {% endfor %}

View File

@ -199,10 +199,9 @@
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4"> <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
{% block messages %} {% block messages %}
{% for message in messages %} {% for message in messages %}
<div class="alert alert-{{ message.tags|default:'info' }} alert-dismissible fade show" role="alert"> <div class="alert {{ message.tags|default:'info' }} alert-dismissible fade show mt-3" role="alert">
{{ message }} {{ message }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close"> <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button> </button>
</div> </div>
{% endfor %} {% endfor %}

View File

@ -11,6 +11,8 @@ https://docs.djangoproject.com/en/4.2/ref/settings/
""" """
from pathlib import Path from pathlib import Path
from django.contrib.messages import constants as messages
# Build paths inside the project like this: BASE_DIR / 'subdir'. # Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent BASE_DIR = Path(__file__).resolve().parent.parent
@ -125,3 +127,11 @@ STATIC_URL = 'static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
LOGOUT_REDIRECT_URL = 'idhub:login' LOGOUT_REDIRECT_URL = 'idhub:login'
MESSAGE_TAGS = {
messages.DEBUG: 'alert-secondary',
messages.INFO: 'alert-info',
messages.SUCCESS: 'alert-success',
messages.WARNING: 'alert-warning',
messages.ERROR: 'alert-danger',
}