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):
MANDATORY_FIELDS = ['first_name', 'last_name', 'email']
MANDATORY_FIELDS = ['first_name', 'last_name', 'email', 'username']
class Meta:
model = User

View File

@ -81,6 +81,10 @@ class AdminPeopleActivateView(AdminPeopleView):
self.pk = kwargs['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:
self.object.is_active = False
else:
@ -96,14 +100,17 @@ class AdminPeopleDeleteView(AdminPeopleView):
self.pk = kwargs['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')
class AdminPeopleEditView(AdminPeopleView, UpdateView):
template_name = "idhub/admin_user_edit.html"
from_class = ProfileForm
fields = ('first_name', 'last_name', 'email')
fields = ('first_name', 'last_name', 'email', 'username')
success_url = reverse_lazy('idhub:admin_people_list')
@ -113,7 +120,7 @@ class AdminPeopleRegisterView(People, CreateView):
icon = 'bi bi-person'
model = User
from_class = ProfileForm
fields = ('first_name', 'last_name', 'email')
fields = ('first_name', 'last_name', 'email', 'username')
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="icon"><span class="mdi mdi-close-circle-o"></span></div>
<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 %}
{{ error }}
{{ error }}<br />
{% endfor %}
<button class="btn-close" type="button" data-dismiss="alert" aria-label="Close"></button>
</div>
</div>
{% endif %}
{% bootstrap_form form %}
<div class="form-actions-no-box">
<a class="btn btn-secondary" href="{% url 'idhub:user_profile' %}">{% translate "Cancel" %}</a>
<input class="btn btn-success" type="submit" name="submit" value="{% translate 'Save' %}" />
<a class="btn btn-grey" href="{% url 'idhub:admin_people_list' %}">{% translate "Cancel" %}</a>
<input class="btn btn-green-admin" type="submit" name="submit" value="{% translate 'Save' %}" />
</div>
</form>

View File

@ -4,7 +4,7 @@
{% block content %}
<div class="row">
<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>
{{ subtitle }}
</h3>

View File

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

View File

@ -128,10 +128,9 @@
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
{% block 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 }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close">
</button>
</div>
{% endfor %}

View File

@ -199,10 +199,9 @@
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
{% block 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 }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close">
</button>
</div>
{% endfor %}

View File

@ -11,6 +11,8 @@ https://docs.djangoproject.com/en/4.2/ref/settings/
"""
from pathlib import Path
from django.contrib.messages import constants as messages
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
@ -125,3 +127,11 @@ STATIC_URL = 'static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
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',
}