Projet

Général

Profil

0001-sms-add-an-SMSConnectorForm-class-42627.patch

Nicolas Roche, 07 mai 2020 15:11

Télécharger (7,43 ko)

Voir les différences:

Subject: [PATCH] sms: add an SMSConnectorForm class (#42627)

 passerelle/base/__init__.py | 13 +++++++++++++
 passerelle/sms/forms.py     | 23 +++++++++++++++++++++++
 passerelle/views.py         |  4 ++--
 tests/test_sms.py           | 12 ++++++++++++
 4 files changed, 50 insertions(+), 2 deletions(-)
 create mode 100644 passerelle/sms/forms.py
passerelle/base/__init__.py
13 13
#
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17 17
import django.apps
18 18
from django.apps import apps
19 19
from django.utils.module_loading import import_string
20 20

  
21
from passerelle.forms import GenericConnectorForm
22

  
23

  
21 24
class ConnectorAppMixin(object):
22 25
    def get_connector_model(self):
23 26
        return self._connector_model
24 27

  
28
    def get_connector_form(self):
29
        return self._connector_form
30

  
25 31
    def get_urls(self):
26 32
        try:
27 33
            return import_string('%s.urls.urlpatterns' % self.name)
28 34
        except ImportError:
29 35
            return None
30 36

  
31 37
    def get_management_urls(self):
32 38
        try:
......
38 44
class ConnectorAppConfig(ConnectorAppMixin, django.apps.AppConfig):
39 45
    pass
40 46

  
41 47

  
42 48
class AppConfig(django.apps.AppConfig):
43 49
    name = 'passerelle.base'
44 50

  
45 51
    def ready(self):
52
        from passerelle.sms.models import SMSResource
53
        from passerelle.sms.forms import SMSConnectorForm
54

  
46 55
        # once all applications are ready, go through them and mark them as
47 56
        # connectors if they have a get_connector_model() method or a model
48 57
        # that inherits from BaseResource.
49 58
        from .models import BaseResource
50 59
        for app in apps.get_app_configs():
51 60
            connector_model = None
61
            connector_form = GenericConnectorForm
52 62
            if hasattr(app, 'get_connector_model'):
53 63
                connector_model = app.get_connector_model()
54 64
            else:
55 65
                for model in app.get_models():
66
                    if issubclass(model, SMSResource):
67
                        connector_form = SMSConnectorForm
56 68
                    if issubclass(model, BaseResource):
57 69
                        connector_model = model
58 70
                        app._connector_model = model
59 71
                        break
60 72
            if not connector_model:
61 73
                continue
74
            app._connector_form = connector_form
62 75
            if app.__class__ is django.apps.AppConfig:
63 76
                # switch class if it's an application without a custom
64 77
                # appconfig.
65 78
                app.__class__ = ConnectorAppConfig
66 79
            else:
67 80
                # add mixin to base classes if it's an application with a
68 81
                # custom appconfig.
69 82
                app.__class__.__bases__ = (ConnectorAppMixin,) + app.__class__.__bases__
passerelle/sms/forms.py
1
# passerelle - uniform access to multiple data sources and services
2
# Copyright (C) 2029 Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
from passerelle.forms import GenericConnectorForm
17
from passerelle.sms.models import SMSResource
18

  
19

  
20
class SMSConnectorForm(GenericConnectorForm):
21
    class Meta:
22
        model = SMSResource
23
        fields = '__all__'
passerelle/views.py
49 49
from jsonschema import validate, ValidationError
50 50

  
51 51
from passerelle.base.models import BaseResource, ResourceLog
52 52
from passerelle.compat import json_loads
53 53
from passerelle.utils.jsonresponse import APIError
54 54
from passerelle.utils.json import unflatten
55 55

  
56 56
from .utils import to_json, is_authorized
57
from .forms import GenericConnectorForm
58 57
from .forms import ResourceLogSearchForm
59 58

  
60 59
if 'mellon' in settings.INSTALLED_APPS:
61 60
    from mellon.utils import get_idps
62 61
else:
63 62
    def get_idps():
64 63
        return []
65 64

  
......
151 150
            if not hasattr(app, 'get_connector_model'):
152 151
                continue
153 152
            if app.get_connector_model().get_connector_slug() == connector:
154 153
                break
155 154
        else:
156 155
            raise Http404()
157 156

  
158 157
        self.model = app.get_connector_model()
158
        self.form = app.get_connector_form()
159 159
        if hasattr(app, 'get_form_class'):
160 160
            self.form_class = app.get_form_class()
161 161
        else:
162 162
            self.form_class = modelform_factory(
163 163
                self.model,
164
                form=GenericConnectorForm,
164
                form=self.form,
165 165
                exclude=self.exclude_fields)
166 166
            for field in self.form_class.base_fields.values():
167 167
                if isinstance(field.widget, ClearableFileInput):
168 168
                    field.widget.template_with_initial = ''\
169 169
                        '%(initial_text)s: %(initial)s '\
170 170
                        '%(clear_template)s<br />%(input_text)s: %(input)s'
171 171

  
172 172
    def dispatch(self, request, *args, **kwargs):
tests/test_sms.py
1 1
import mock
2 2
import pytest
3 3

  
4 4
from django.contrib.contenttypes.models import ContentType
5
from django.utils.translation import ugettext as _
5 6

  
7
from passerelle.apps.choosit.models import ChoositSMSGateway
6 8
from passerelle.apps.ovh.models import OVHSMSGateway
7 9
from passerelle.base.models import ApiUser, AccessRight
8 10
from passerelle.sms.models import SMSResource, SMSLog
9 11
from passerelle.utils.jsonresponse import APIError
10 12

  
11 13
from test_manager import login, admin_user
12 14

  
13 15
import utils
......
107 109
        'message': 'plop',
108 110
        'from': '+33699999999',
109 111
        'to': ['+33688888888'],
110 112
    }
111 113
    with mock.patch.object(OVHSMSGateway, 'send_msg') as send_function:
112 114
        send_function.return_value = {}
113 115
        result = app.post_json(path, params=payload)
114 116
        assert SMSLog.objects.filter(appname=connector.get_connector_slug(), slug=connector.slug).exists()
117

  
118

  
119
@pytest.mark.parametrize('connector', [ChoositSMSGateway], indirect=True)
120
def test_manager(admin_user, app, connector):
121
    app = login(app)
122
    path = '/%s/%s/' % (connector.get_connector_slug(), connector.slug)
123
    resp = app.get(path)
124
    assert '33' in [
125
        x.text for x in resp.html.find('div', {'id': 'description'}).find_all('p')
126
        if x.text.startswith(_('Default country code'))][0]
115
-