From b4539481e6d81c052f52634248ecf064487d5ad8 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..2d6ef13 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.ProfileView.as_view(), name="profile"), + path("accounts/profile/", views.ProfileView.as_view(), name="profile"), ] diff --git a/aircox/views/__init__.py b/aircox/views/__init__.py index 40b0213..f584d58 100644 --- a/aircox/views/__init__.py +++ b/aircox/views/__init__.py @@ -11,6 +11,7 @@ from .page import ( PageDetailView, PageListView, ) +from .profile import ProfileView from .program import ( ProgramDetailView, ProgramListView, @@ -38,6 +39,7 @@ __all__ = ( "BasePageListView", "PageDetailView", "PageListView", + "ProfileView", "ProgramDetailView", "ProgramListView", "ProgramPageDetailView", diff --git a/aircox/views/profile.py b/aircox/views/profile.py new file mode 100644 index 0000000..5d5a132 --- /dev/null +++ b/aircox/views/profile.py @@ -0,0 +1,15 @@ +from django.contrib.auth.mixins import LoginRequiredMixin +from django.views.generic.base import TemplateView + +from aircox.models import Program +from aircox.views import BaseView + + +class ProfileView(LoginRequiredMixin, BaseView, TemplateView): + template_name = "accounts/profile.html" + + def get_context_data(self, **kwargs): + groups = self.request.user.groups.all() + programs = Program.objects.filter(editors__in=groups) + kwargs.update({"user": self.request.user, "programs": programs}) + return super().get_context_data(**kwargs)