From 9d647580e2f0c2834e743e4b5309f2e2df13871e Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 6 Oct 2022 21:30:33 +0200 Subject: [PATCH 2/3] misc: move AppConfing in apps module (#69739) --- setup.py | 2 +- src/authentic2_auth_fedict/__init__.py | 129 +---------------------- src/authentic2_auth_fedict/apps.py | 140 +++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 128 deletions(-) create mode 100644 src/authentic2_auth_fedict/apps.py diff --git a/setup.py b/setup.py index 5e4251b..6f707e9 100755 --- a/setup.py +++ b/setup.py @@ -100,7 +100,7 @@ setup( ], entry_points={ 'authentic2.plugin': [ - 'authentic2-auth-fedict = authentic2_auth_fedict:Plugin', + 'authentic2-auth-fedict = authentic2_auth_fedict.apps:Plugin', ], }, cmdclass={ diff --git a/src/authentic2_auth_fedict/__init__.py b/src/authentic2_auth_fedict/__init__.py index 9186c8a..42b5f0b 100644 --- a/src/authentic2_auth_fedict/__init__.py +++ b/src/authentic2_auth_fedict/__init__.py @@ -1,5 +1,5 @@ # authentic2_auth_fedict - Fedict authentication for Authentic -# Copyright (C) 2016 Entr'ouvert +# Copyright (C) 2016-2022 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 @@ -14,130 +14,5 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -import json +default_app_config = '%s.apps.AppConfig' % __name__ -import django.apps -from django.contrib.auth.signals import user_logged_in -from django.utils.translation import ugettext_lazy as _ - - -class AppConfig(django.apps.AppConfig): - name = 'authentic2_auth_fedict' - - def ready(self): - from . import signals - - user_logged_in.connect(signals.on_user_logged_in) - - def a2_hook_event(self, name, **kwargs): - if name == 'registration': - if kwargs.get('authentication_method') == 'fedict': - user = kwargs.get('user') - user.backend = 'authentic2_auth_fedict.backends.FedictBackend' - - -default_app_config = 'authentic2_auth_fedict.AppConfig' - - -class Plugin: - def get_before_urls(self): - from . import urls - - return urls.urlpatterns - - def get_apps(self): - return ['mellon', __name__] - - def get_authentication_backends(self): - return ['authentic2_auth_fedict.backends.FedictBackend'] - - def redirect_logout_list(self, request, next_url=None): - from mellon.views import logout - - if 'mellon_session' in request.session: - response = logout(request) - if 'Location' in response: - return [response['Location']] - return [] - - def registration_form_prefill(self, request): - if request.token.get('first_name'): - return [ - { - 'first_name': [request.token.get('first_name')], - 'last_name': [request.token.get('last_name')], - } - ] - else: - return [{'first_name': [], 'last_name': []}] - - def attribute_kinds(self): - from . import fields - - def attribute_json_loads(x): - if not x: - return x - try: - return json.loads(x) - except json.JSONDecodeError: - # "compatibility" with native date/phone kinds - return x - - return [ - { - 'label': _('National Register Number'), - 'name': 'nrn', - 'serialize': json.dumps, - 'deserialize': attribute_json_loads, - 'field_class': fields.NrnField, - }, - { - 'label': _('Date'), - 'serialize': json.dumps, - 'deserialize': attribute_json_loads, - 'name': 'date', - 'field_class': fields.DateField, - }, - { - 'label': _('Date'), - 'serialize': json.dumps, - 'deserialize': attribute_json_loads, - 'name': 'fedict_date', - 'field_class': fields.DateField, - }, - { - 'label': _('Street'), - 'serialize': json.dumps, - 'deserialize': attribute_json_loads, - 'name': 'street', - 'field_class': fields.StreetField, - }, - { - 'label': _('House number'), - 'serialize': json.dumps, - 'deserialize': attribute_json_loads, - 'name': 'num_house', - 'field_class': fields.NumHouseField, - }, - { - 'label': _('Phone number'), - 'serialize': json.dumps, - 'deserialize': attribute_json_loads, - 'name': 'phone', - 'field_class': fields.NumPhoneField, - }, - { - 'label': _('Phone number'), - 'serialize': json.dumps, - 'deserialize': attribute_json_loads, - 'name': 'fedict_phone', - 'field_class': fields.NumPhoneField, - }, - { - 'label': _('Country'), - 'serialize': json.dumps, - 'deserialize': attribute_json_loads, - 'name': 'country', - 'field_class': fields.CountryField, - }, - ] diff --git a/src/authentic2_auth_fedict/apps.py b/src/authentic2_auth_fedict/apps.py new file mode 100644 index 0000000..4efaf4c --- /dev/null +++ b/src/authentic2_auth_fedict/apps.py @@ -0,0 +1,140 @@ +# authentic2_auth_fedict - Fedict authentication for Authentic +# Copyright (C) 2016-2022 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 . + +import json + +import django.apps +from django.contrib.auth.signals import user_logged_in +from django.utils.translation import ugettext_lazy as _ + + +class AppConfig(django.apps.AppConfig): + name = 'authentic2_auth_fedict' + + def ready(self): + from . import signals + + user_logged_in.connect(signals.on_user_logged_in) + + def a2_hook_event(self, name, **kwargs): + if name == 'registration': + if kwargs.get('authentication_method') == 'fedict': + user = kwargs.get('user') + user.backend = 'authentic2_auth_fedict.backends.FedictBackend' + + +class Plugin: + def get_before_urls(self): + from . import urls + + return urls.urlpatterns + + def get_apps(self): + return ['mellon', 'authentic2_auth_fedict'] + + def get_authentication_backends(self): + return ['authentic2_auth_fedict.backends.FedictBackend'] + + def redirect_logout_list(self, request, next_url=None): + from mellon.views import logout + + if 'mellon_session' in request.session: + response = logout(request) + if 'Location' in response: + return [response['Location']] + return [] + + def registration_form_prefill(self, request): + if request.token.get('first_name'): + return [ + { + 'first_name': [request.token.get('first_name')], + 'last_name': [request.token.get('last_name')], + } + ] + else: + return [{'first_name': [], 'last_name': []}] + + def attribute_kinds(self): + from . import fields + + def attribute_json_loads(x): + if not x: + return x + try: + return json.loads(x) + except json.JSONDecodeError: + # "compatibility" with native date/phone kinds + return x + + return [ + { + 'label': _('National Register Number'), + 'name': 'nrn', + 'serialize': json.dumps, + 'deserialize': attribute_json_loads, + 'field_class': fields.NrnField, + }, + { + 'label': _('Date'), + 'serialize': json.dumps, + 'deserialize': attribute_json_loads, + 'name': 'date', + 'field_class': fields.DateField, + }, + { + 'label': _('Date'), + 'serialize': json.dumps, + 'deserialize': attribute_json_loads, + 'name': 'fedict_date', + 'field_class': fields.DateField, + }, + { + 'label': _('Street'), + 'serialize': json.dumps, + 'deserialize': attribute_json_loads, + 'name': 'street', + 'field_class': fields.StreetField, + }, + { + 'label': _('House number'), + 'serialize': json.dumps, + 'deserialize': attribute_json_loads, + 'name': 'num_house', + 'field_class': fields.NumHouseField, + }, + { + 'label': _('Phone number'), + 'serialize': json.dumps, + 'deserialize': attribute_json_loads, + 'name': 'phone', + 'field_class': fields.NumPhoneField, + }, + { + 'label': _('Phone number'), + 'serialize': json.dumps, + 'deserialize': attribute_json_loads, + 'name': 'fedict_phone', + 'field_class': fields.NumPhoneField, + }, + { + 'label': _('Country'), + 'serialize': json.dumps, + 'deserialize': attribute_json_loads, + 'name': 'country', + 'field_class': fields.CountryField, + }, + ] -- 2.37.2