diff --git a/hobo/profile/forms.py b/hobo/profile/forms.py new file mode 100644 index 0000000..e41a15a --- /dev/null +++ b/hobo/profile/forms.py @@ -0,0 +1,26 @@ +# hobo - portal to configure and deploy applications +# Copyright (C) 2015-2023 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django import forms +from django.utils.translation import ugettext_lazy as _ + + +class EditFullNameTemplateForm(forms.Form): + user_full_name_template = forms.CharField( + label=_('User full name template (Django)'), + widget=forms.Textarea, + required=False, + ) diff --git a/hobo/profile/templates/profile/attributedefinition_list.html b/hobo/profile/templates/profile/attributedefinition_list.html index 6c1444b..0152f2c 100644 --- a/hobo/profile/templates/profile/attributedefinition_list.html +++ b/hobo/profile/templates/profile/attributedefinition_list.html @@ -9,6 +9,7 @@ {% block appbar %}

{% trans 'User Profile' %}

+ {% trans 'User full name template' %} {% trans 'New attribute' %} {% endblock %} diff --git a/hobo/profile/templates/profile/edit_full_name_template.html b/hobo/profile/templates/profile/edit_full_name_template.html new file mode 100644 index 0000000..61f64ec --- /dev/null +++ b/hobo/profile/templates/profile/edit_full_name_template.html @@ -0,0 +1,19 @@ +{% extends "hobo/base.html" %} +{% load i18n %} + +{% block content %} + +
+
+ {% csrf_token %} + {{ form.as_p }} +
+ {% block buttons %} +
+ + {% trans 'Cancel' %} +
+ {% endblock %} +
+ +{% endblock %} diff --git a/hobo/profile/urls.py b/hobo/profile/urls.py index 4793cff..6a87021 100644 --- a/hobo/profile/urls.py +++ b/hobo/profile/urls.py @@ -23,4 +23,9 @@ urlpatterns = [ re_path(r'(?P[\w-]+)/options', views.options, name='profile-attribute-options'), path('reorder', views.reorder, name='profile-reorder'), path('add-attribute', views.add_attribute, name='profile-add-attribute'), + path( + 'edit-user-full-name-template', + views.edit_user_full_name_template, + name='profile-edit-user-full-name-template', + ), ] diff --git a/hobo/profile/views.py b/hobo/profile/views.py index 5891577..386a8e4 100644 --- a/hobo/profile/views.py +++ b/hobo/profile/views.py @@ -16,10 +16,13 @@ from django.shortcuts import redirect from django.urls import reverse, reverse_lazy -from django.views.generic import CreateView, ListView, RedirectView, UpdateView +from django.utils.translation import ugettext as _ +from django.views.generic import CreateView, ListView, RedirectView, TemplateView, UpdateView from hobo.deploy.signals import notify_agents +from hobo.environment.forms import VariablesFormMixin +from .forms import EditFullNameTemplateForm from .models import AttributeDefinition @@ -72,6 +75,16 @@ class OptionsView(UpdateView): options = OptionsView.as_view() +class EditFullNameTemplateView(VariablesFormMixin, TemplateView): + template_name = 'profile/edit_full_name_template.html' + form_class = EditFullNameTemplateForm + variables = ['user_full_name_template'] + success_message = _('User full name template has been updated.') + + +edit_user_full_name_template = EditFullNameTemplateView.as_view() + + def reorder(request): new_order_list = [int(x) for x in request.GET['new-order'].split(',')] for attribute in AttributeDefinition.objects.all(): diff --git a/hobo/user_name/apps.py b/hobo/user_name/apps.py index 016287e..b80f4a6 100644 --- a/hobo/user_name/apps.py +++ b/hobo/user_name/apps.py @@ -17,7 +17,7 @@ def get_full_name(user): context = {} context['user'] = user template_vars = getattr(settings, 'TEMPLATE_VARS', {}) - if 'user_full_name_template' in template_vars: + if template_vars.get('user_full_name_template'): try: template = engines['django'].from_string(template_vars['user_full_name_template']) return template.render(context) diff --git a/tests/test_manager.py b/tests/test_manager.py index 3e4815c..26b207a 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -17,7 +17,7 @@ import pytest -from hobo.environment.models import Authentic +from hobo.environment.models import Authentic, Variable from hobo.environment.utils import get_variable from hobo.profile import models from hobo.profile.models import AttributeDefinition @@ -79,13 +79,39 @@ def test_add_attribute(logged_app, admin_user, kind): assert models.AttributeDefinition.objects.filter(kind=kind).filter(name='test').count() == 1 +def test_edit_user_full_name_template(logged_app, admin_user, settings): + app = logged_app + value = '{{ user.first_name }}' + assert not Variable.objects.filter(name='user_full_name_template') + page = app.get('/profile/edit-user-full-name-template', status=200) + page.form['user_full_name_template'] = value + page.form.submit() + assert Variable.objects.get(name='user_full_name_template').value == value + + value = '{{ user.last_name }} etc.' + page = app.get('/profile/edit-user-full-name-template', status=200) + page.form['user_full_name_template'] = value + page.form.submit() + assert Variable.objects.get(name='user_full_name_template').value == value + + page = app.get('/profile/edit-user-full-name-template', status=200) + page.form['user_full_name_template'] = 'whatever' + page.click('Cancel') + assert Variable.objects.get(name='user_full_name_template').value == value + + page = app.get('/profile/edit-user-full-name-template', status=200) + page.form['user_full_name_template'] = '' + page.form.submit() + assert Variable.objects.get(name='user_full_name_template').value == '' + + def test_attribute_kind_not_restricted_at_model_level(db): assert models.AttributeDefinition.objects.create(label='test', kind='somestring') def test_profile_home_view(logged_app): resp = logged_app.get('/profile/', status=200) - assert [x['href'] for x in resp.html.findAll('a', {'rel': 'popup'})][1:4] == [ + assert [x['href'] for x in resp.html.findAll('a', {'rel': 'popup'})][2:5] == [ '/profile/title/options', '/profile/first_name/options', '/profile/last_name/options', @@ -99,7 +125,7 @@ def test_reorder_view(logged_app): assert resp.location == '/profile/' assert AttributeDefinition.objects.filter(name='last_name')[0].order == 1 resp = resp.follow() - assert [x['href'] for x in resp.html.findAll('a', {'rel': 'popup'})][1:4] == [ + assert [x['href'] for x in resp.html.findAll('a', {'rel': 'popup'})][2:5] == [ '/profile/last_name/options', '/profile/first_name/options', '/profile/title/options', diff --git a/tests/test_user_name.py b/tests/test_user_name.py index af7c409..21a4c46 100644 --- a/tests/test_user_name.py +++ b/tests/test_user_name.py @@ -36,9 +36,15 @@ def test_user_get_full_name_from_template(user): ): assert get_full_name(user) == 'Jane bar' + with override_settings(TEMPLATE_VARS={'user_full_name_template': ''}): + assert get_full_name(user) == 'Jane Doe' + def test_user_get_full_name(user): with override_settings( TEMPLATE_VARS={'user_full_name_template': '{{ user.first_name }} {{ user.attributes.foo }}'} ): assert user.get_full_name() == 'Jane bar' + + with override_settings(TEMPLATE_VARS={'user_full_name_template': ''}): + assert user.get_full_name() == 'Jane Doe' diff --git a/tests_authentic/test_user_name.py b/tests_authentic/test_user_name.py index 9089231..0fd9a5f 100644 --- a/tests_authentic/test_user_name.py +++ b/tests_authentic/test_user_name.py @@ -49,6 +49,9 @@ def test_get_full_name_from_template_utils_from_multiple_attrs(db, tenant, setti ): assert get_full_name(user) == 'Jane Milly Minnie' + with override_settings(TEMPLATE_VARS={'user_full_name_template': ''}): + assert get_full_name(user) == 'Jane Doe' + def test_get_full_name_from_template_accessor_from_multiple_attrs(db, tenant, settings): with tenant_context(tenant): @@ -90,3 +93,6 @@ def test_get_full_name_from_template_accessor_from_multiple_attrs(db, tenant, se } ): assert user.get_full_name() == 'Jane Milly Minnie' + + with override_settings(TEMPLATE_VARS={'user_full_name_template': ''}): + assert user.get_full_name() == 'Jane Doe'