diff --git a/core/templatetags/profiles.py b/core/templatetags/profiles.py new file mode 100644 index 0000000..9433393 --- /dev/null +++ b/core/templatetags/profiles.py @@ -0,0 +1,11 @@ +from django import template + +from core.models.profiles import ClientProfile + +register = template.Library() + +@register.simple_tag() +def is_client(user): + if isinstance(user.profile, ClientProfile): + return True + return False diff --git a/core/views/backend/profiles.py b/core/views/backend/profiles.py index 6de4ba5..f44bc86 100644 --- a/core/views/backend/profiles.py +++ b/core/views/backend/profiles.py @@ -4,7 +4,7 @@ from django.contrib.auth import get_user_model from django.contrib import messages from django.utils import timezone -from core.models import AdminProfile, ClientProfile, ClientGroup +from core.models import AdminProfile, ClientProfile, ClientGroup, Profile from core.forms.profiles import AdminEditForm, AdminCreateForm, ClientEditForm, ClientCreateForm from core.views.backend.generic import BackendFormView, BackendListView, BackendDeleteView, BackendUpdateView, BackendCreateView from core.helpers.auth import request_password @@ -13,7 +13,7 @@ from core.helpers.auth import request_password class AdminListView(BackendListView): template_name = f"{settings.EXPEPHALON_BACKEND}/profiles/index.html" - model = AdminProfile + model = get_user_model() def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) @@ -76,7 +76,11 @@ class AdminDeleteView(BackendDeleteView): def get_object(self, queryset=None): admin = super().get_object(queryset=queryset) - assert type(admin.profile) == AdminProfile + try: + assert type(admin.profile) == AdminProfile + except Profile.DoesNotExist: + pass + return admin class AdminCreateView(BackendFormView): @@ -108,7 +112,7 @@ class AdminCreateView(BackendFormView): request_password(admin) - messages.success(self.request, f"User {admin.get_full_name} was successfully created. They should receive an email to set their password shortly.") + messages.success(self.request, f"User {admin.get_full_name()} was successfully created. They should receive an email to set their password shortly.") return super().form_valid(form) @@ -166,6 +170,8 @@ class ClientEditView(BackendFormView): client.last_name = form.cleaned_data["last_name"] client.username = form.cleaned_data["email"] client.email = form.cleaned_data["email"] + + client.profile.company = form.cleaned_data["company"] client.profile.mobile = form.cleaned_data["mobile"] client.profile.address1 = form.cleaned_data["address1"] client.profile.address2 = form.cleaned_data["address2"] @@ -219,6 +225,7 @@ class ClientCreateView(BackendFormView): profile = ClientProfile() profile.user = client + profile.company = form.cleaned_data["company"] profile.mobile = form.cleaned_data["mobile"] profile.address1 = form.cleaned_data["address1"] profile.address2 = form.cleaned_data["address2"] @@ -242,7 +249,7 @@ class ClientCreateView(BackendFormView): if form.cleaned_data["send_password"]: request_password(client) - messages.success(self.request, f"Client {client.get_full_name} was successfully created. They should receive an email to set their password shortly.") + messages.success(self.request, f"Client {client.get_full_name()} was successfully created." + (" They should receive an email to set their password shortly." if form.cleaned_data["send_password"] else "")) return super().form_valid(form) diff --git a/templates/backend/profiles/index.html b/templates/backend/profiles/index.html index 8343c12..48ae422 100644 --- a/templates/backend/profiles/index.html +++ b/templates/backend/profiles/index.html @@ -1,4 +1,5 @@ {% extends "backend/base.html" %} +{% load profiles %} {% block content %}
@@ -45,15 +46,18 @@ - {% for admin in object_list %} + {% for user in object_list %} + {% is_client user as client %} + {% if not client %} - {{ admin.user.first_name }} - {{ admin.user.last_name }} - {{ admin.role }} - {{ admin.user.username }} - {{ admin.mobile }} - + {{ user.first_name }} + {{ user.last_name }} + {{ user.profile.role }} + {{ user.username }} + {{ user.profile.mobile }} + + {% endif %} {% endfor %}