From 0812f3a0a138e87f69030a415129f285872bb0e9 Mon Sep 17 00:00:00 2001 From: Christophe Siraut Date: Mon, 20 Nov 2023 14:19:44 +0100 Subject: [PATCH] misc: add a profile view for authenticated users --- aircox/templates/accounts/profile.html | 34 ++++++++++++++++++++++++++ aircox/templates/aircox/base.html | 14 ++++++++--- aircox/tests/test_profile.py | 10 ++++++++ aircox/urls.py | 2 ++ aircox/views/__init__.py | 2 ++ aircox/views/profile.py | 15 ++++++++++++ 6 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 aircox/templates/accounts/profile.html create mode 100644 aircox/views/profile.py diff --git a/aircox/templates/accounts/profile.html b/aircox/templates/accounts/profile.html new file mode 100644 index 0000000..33f8b2b --- /dev/null +++ b/aircox/templates/accounts/profile.html @@ -0,0 +1,34 @@ +{% extends "aircox/base.html" %} +{% load i18n aircox %} + +{% block head_title %} + {% block title %}{{ user.username }}{% endblock %} +{% endblock %} + +{% block content-container %} +
+

Mon Profil

+ {% translate "Username" %} : {{ user.username|title }}
+ + +

Mes émissions

+ {% if programs|length %} + + {% else %} + {% trans 'You are not listed as a program editor yet' %} + {% endif %} +
+{% endblock %} diff --git a/aircox/templates/aircox/base.html b/aircox/templates/aircox/base.html index 2456c40..a828f3f 100644 --- a/aircox/templates/aircox/base.html +++ b/aircox/templates/aircox/base.html @@ -72,16 +72,20 @@ Usefull context: {% endif %} {% if user.is_authenticated %} - + + {% translate "Profile" %} + + + + {% endif %} {% endblock %} {% endblock %} - {% endblock %} + {% block secondary-nav %}{% endblock %} + {% endblock %} {% block main-container %} @@ -95,6 +99,8 @@ Usefull context: {% endblock %} {% endspaceless %} + + {% block header-container %} {% if page or cover or title %}
diff --git a/aircox/tests/test_profile.py b/aircox/tests/test_profile.py index 90ace45..11129c7 100644 --- a/aircox/tests/test_profile.py +++ b/aircox/tests/test_profile.py @@ -10,3 +10,13 @@ def test_authenticate(user, client, program): r = client.post(reverse("login"), kwargs={"username": "foo", "password": "bar"}) assert b"errorlist" in r.content assert client.login(username="user1", password="bar") + + +@pytest.mark.django_db() +def test_profile_programs(user, client, program): + client.force_login(user) + r = client.get(reverse("profile")) + assert program.title not in r.content.decode("utf-8") + user.groups.add(program.editors) + r = client.get(reverse("profile")) + assert program.title in r.content.decode("utf-8") diff --git a/aircox/urls.py b/aircox/urls.py index 2783cdb..c7085b1 100755 --- a/aircox/urls.py +++ b/aircox/urls.py @@ -127,4 +127,6 @@ urls = [ views.errors.NoStationErrorView.as_view(), name="errors-no-station", ), + path("gestion/", views.profile, name="profile"), + path("accounts/profile/", views.profile, name="profile"), ] diff --git a/aircox/views/__init__.py b/aircox/views/__init__.py index 40b0213..d9c2010 100644 --- a/aircox/views/__init__.py +++ b/aircox/views/__init__.py @@ -11,6 +11,7 @@ from .page import ( PageDetailView, PageListView, ) +from .profile import profile from .program import ( ProgramDetailView, ProgramListView, @@ -38,6 +39,7 @@ __all__ = ( "BasePageListView", "PageDetailView", "PageListView", + "profile", "ProgramDetailView", "ProgramListView", "ProgramPageDetailView", diff --git a/aircox/views/profile.py b/aircox/views/profile.py new file mode 100644 index 0000000..4e002fa --- /dev/null +++ b/aircox/views/profile.py @@ -0,0 +1,15 @@ +from django.contrib.auth.decorators import login_required +from django.template.response import TemplateResponse + +from aircox.models import Program + + +@login_required +def profile(request): + programs = [] + ugroups = request.user.groups.all() + for p in Program.objects.all(): + if p.editors in ugroups: + programs.append(p) + context = {"user": request.user, "programs": programs} + return TemplateResponse(request, "accounts/profile.html", context)