Projet

Général

Profil

0001-environment-coding-style-29240.patch

Benjamin Dauvergne, 08 mars 2019 03:15

Télécharger (21,2 ko)

Voir les différences:

Subject: [PATCH 1/8] environment: coding style (#29240)

 hobo/environment/forms.py  | 26 ++++++++--
 hobo/environment/models.py | 99 +++++++++++++++++++++++---------------
 hobo/environment/urls.py   | 40 ++++++++++-----
 hobo/environment/utils.py  | 33 ++++++++++---
 hobo/environment/views.py  | 34 ++++++++++---
 5 files changed, 165 insertions(+), 67 deletions(-)
hobo/environment/forms.py
1
# hobo - portal to configure and deploy applications
2
# Copyright (C) 2015-2019  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

  
1 17
from django import forms
2 18
from django.conf import settings
3 19
from django.contrib import messages
4 20
from django.http import HttpResponseRedirect
5 21
from django.template.defaultfilters import slugify
6 22
from django.utils.translation import ugettext_lazy as _
7
from django.core.validators import validate_email
8 23

  
9 24

  
10 25
from .models import (Authentic, Wcs, Passerelle, Variable, Combo, Fargo, Welco,
......
12 27
from .utils import get_setting_variable
13 28

  
14 29
EXCLUDED_FIELDS = ('last_operational_check_timestamp',
15
        'last_operational_success_timestamp', 'secret_key', 'secondary')
30
                   'last_operational_success_timestamp', 'secret_key',
31
                   'secondary')
16 32

  
17 33

  
18 34
class BaseForm(forms.ModelForm):
......
29 45
        # choice that was selected as an additional, disabled, <select> widget.
30 46
        if self.instance.id and len(choices) > 1:
31 47
            self.fields['template_name_readonly'] = forms.fields.CharField(
32
                    label=_('Template'), required=False,
33
                    initial=self.instance.template_name)
48
                label=_('Template'), required=False,
49
                initial=self.instance.template_name)
34 50
            self.fields['template_name_readonly'].widget = forms.Select(choices=choices)
35 51
            self.fields['template_name_readonly'].widget.attrs['disabled'] = 'disabled'
36 52

  
......
116 132
        model = MandayeJS
117 133
        exclude = EXCLUDED_FIELDS
118 134

  
135

  
119 136
class ChronoForm(BaseForm):
120 137
    class Meta:
121 138
        model = Chrono
122 139
        exclude = EXCLUDED_FIELDS
123 140

  
141

  
124 142
class CorboForm(BaseForm):
125 143
    class Meta:
126 144
        model = Corbo
hobo/environment/models.py
1
# hobo - portal to configure and deploy applications
2
# Copyright (C) 2015-2019  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

  
1 17
import datetime
2 18
import json
3 19
import random
......
12 28
from django.utils.six.moves.urllib.parse import urlparse
13 29
from django.utils.timezone import now
14 30
from django.utils.translation import ugettext_lazy as _
31
from django.utils import six
15 32
from django.core.exceptions import ValidationError
16 33

  
17 34
from django.contrib.contenttypes.models import ContentType
......
22 39

  
23 40
SECRET_CHARS = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
24 41

  
42

  
25 43
class Variable(models.Model):
26 44
    name = models.CharField(max_length=100, verbose_name=_('name'))
27 45
    label = models.CharField(max_length=100, blank=True, verbose_name=_('label'))
28
    value = models.TextField(verbose_name=_('value'),
29
            blank=True,
30
            help_text=_('start with [ or { for a JSON document'))
46
    value = models.TextField(
47
        verbose_name=_('value'),
48
        blank=True,
49
        help_text=_('start with [ or { for a JSON document'))
31 50
    auto = models.BooleanField(default=False)
32 51
    service_type = models.ForeignKey(ContentType, null=True)
33 52
    service_pk = models.PositiveIntegerField(null=True)
......
71 90
    last_operational_success_timestamp = models.DateTimeField(null=True)
72 91
    last_update_timestamp = models.DateTimeField(auto_now=True, null=True)
73 92

  
74
    variables = GenericRelation(Variable,
75
            content_type_field='service_type', object_id_field='service_pk')
93
    variables = GenericRelation(
94
        Variable,
95
        content_type_field='service_type',
96
        object_id_field='service_pk')
76 97

  
77 98
    @classmethod
78 99
    def is_enabled(cls):
79 100
        return True
80 101

  
81 102
    def is_operational(self):
82
        return (self.last_operational_success_timestamp is not None and
83
                self.last_operational_success_timestamp == self.last_operational_check_timestamp)
103
        return (self.last_operational_success_timestamp is not None
104
                and self.last_operational_success_timestamp == self.last_operational_check_timestamp)
84 105

  
85 106
    def check_operational(self):
86 107
        once_now = now()
......
90 111
            response = requests.get(zone.href, timeout=10, allow_redirects=False)
91 112
            response.raise_for_status()
92 113
            self.last_operational_success_timestamp = once_now
93
        except requests.RequestException as e:
114
        except requests.RequestException:
94 115
            pass
95 116
        self.save(update_fields=('last_operational_check_timestamp', 'last_operational_success_timestamp'))
96 117

  
......
110 131

  
111 132
    def as_dict(self):
112 133
        as_dict = dict([(x, y) for (x, y) in self.__dict__.items()
113
                        if type(y) in (int, str, unicode)])
134
                        if type(y) in (int, six.string_types)])
114 135
        as_dict['base_url'] = self.get_base_url_path()
115 136
        as_dict['service-id'] = self.Extra.service_id
116 137
        as_dict['service-label'] = force_text(self.Extra.service_label)
......
215 236

  
216 237
class Authentic(ServiceBase):
217 238
    use_as_idp_for_self = models.BooleanField(
218
            verbose_name=_('Use as IdP'),
219
            default=False)
239
        verbose_name=_('Use as IdP'),
240
        default=False)
220 241

  
221 242
    class Meta:
222 243
        verbose_name = _('Authentic Identity Provider')
......
230 251

  
231 252
    def get_admin_zones(self):
232 253
        return [
233
                Zone(_('User Management'), 'users', self.get_base_url_path() + 'manage/users/'),
234
                Zone(_('Role Management'), 'roles', self.get_base_url_path() + 'manage/roles/'),
235
                ]
254
            Zone(_('User Management'), 'users', self.get_base_url_path() + 'manage/users/'),
255
            Zone(_('Role Management'), 'roles', self.get_base_url_path() + 'manage/roles/'),
256
        ]
236 257

  
237 258
    def get_saml_idp_metadata_url(self):
238 259
        return self.get_base_url_path() + 'idp/saml2/metadata'
......
257 278

  
258 279
    def get_admin_zones(self):
259 280
        return [
260
                Zone(self.title, 'webforms', self.get_base_url_path() + 'admin/'),
261
                ]
281
            Zone(self.title, 'webforms', self.get_base_url_path() + 'admin/'),
282
        ]
262 283

  
263 284
    def get_saml_sp_metadata_url(self):
264 285
        return self.get_base_url_path() + 'saml/metadata'
......
277 298

  
278 299
    def get_admin_zones(self):
279 300
        return [
280
                Zone(self.title, 'webservices', self.get_base_url_path() + 'manage/')
281
                ]
301
            Zone(self.title, 'webservices', self.get_base_url_path() + 'manage/')
302
        ]
282 303

  
283 304
    def get_saml_sp_metadata_url(self):
284 305
        return self.get_base_url_path() + 'accounts/mellon/metadata/'
......
300 321

  
301 322
    def get_admin_zones(self):
302 323
        return [
303
                Zone(self.title, 'portal', self.get_base_url_path() + 'manage/')
304
                ]
324
            Zone(self.title, 'portal', self.get_base_url_path() + 'manage/')
325
        ]
305 326

  
306 327
    def get_saml_sp_metadata_url(self):
307 328
        return self.get_base_url_path() + 'accounts/mellon/metadata/'
......
323 344

  
324 345
    def get_admin_zones(self):
325 346
        return [
326
                Zone(self.title, 'document-box', self.get_base_url_path() + 'admin/')
327
                ]
347
            Zone(self.title, 'document-box', self.get_base_url_path() + 'admin/')
348
        ]
328 349

  
329 350
    def get_saml_sp_metadata_url(self):
330 351
        return self.get_base_url_path() + 'accounts/mellon/metadata/'
......
342 363

  
343 364
    def get_admin_zones(self):
344 365
        return [
345
                Zone(self.title, 'multichannel-guichet', self.get_base_url_path() + 'admin/')
346
                ]
366
            Zone(self.title, 'multichannel-guichet', self.get_base_url_path() + 'admin/')
367
        ]
347 368

  
348 369
    def get_saml_sp_metadata_url(self):
349 370
        return self.get_base_url_path() + 'accounts/mellon/metadata/'
......
353 374

  
354 375

  
355 376
class MandayeJS(ServiceBase):
356
    site_app = models.CharField(_('Site Application'), max_length=128,
357
        choices = APP_SETTINGS_CLASSES,
358
        default = DEFAULT_APP_SETTINGS
359
    )
377
    site_app = models.CharField(
378
        _('Site Application'),
379
        max_length=128,
380
        choices=APP_SETTINGS_CLASSES,
381
        default=DEFAULT_APP_SETTINGS)
382

  
360 383
    class Meta:
361 384
        verbose_name = _('Authentication Reverse Proxy')
362 385
        ordering = ['title']
......
372 395

  
373 396
    def get_admin_zones(self):
374 397
        return [
375
                Zone(self.title, 'mandayejs', self.get_base_url_path() + '_mandaye/admin/')
376
                ]
398
            Zone(self.title, 'mandayejs', self.get_base_url_path() + '_mandaye/admin/')
399
        ]
377 400

  
378 401
    def get_saml_sp_metadata_url(self):
379
        return self.get_base_url_path()+ '_mandaye/accounts/mellon/metadata/'
402
        return self.get_base_url_path() + '_mandaye/accounts/mellon/metadata/'
380 403

  
381 404

  
382 405
class Chrono(ServiceBase):
......
392 415

  
393 416
    def get_admin_zones(self):
394 417
        return [
395
                Zone(self.title, 'calendar', self.get_base_url_path() + 'manage/')
396
                ]
418
            Zone(self.title, 'calendar', self.get_base_url_path() + 'manage/')
419
        ]
397 420

  
398 421
    def get_saml_sp_metadata_url(self):
399 422
        return self.get_base_url_path() + 'accounts/mellon/metadata/'
......
415 438

  
416 439
    def get_admin_zones(self):
417 440
        return [
418
                Zone(self.title, 'hobo', self.get_base_url_path())
419
                ]
441
            Zone(self.title, 'hobo', self.get_base_url_path())
442
        ]
420 443

  
421 444
    def get_saml_sp_metadata_url(self):
422 445
        return self.get_base_url_path() + 'accounts/mellon/metadata/'
......
442 465

  
443 466
    def get_admin_zones(self):
444 467
        return [
445
                Zone(self.title, 'corbo', self.get_base_url_path() + 'admin/')
446
                ]
468
            Zone(self.title, 'corbo', self.get_base_url_path() + 'admin/')
469
        ]
447 470

  
448 471
    def get_saml_sp_metadata_url(self):
449
        return self.get_base_url_path()+ 'accounts/mellon/metadata/'
472
        return self.get_base_url_path() + 'accounts/mellon/metadata/'
450 473

  
451 474
    def get_backoffice_menu_url(self):
452 475
        return self.get_base_url_path() + 'manage/menu.json'
hobo/environment/urls.py
1
# hobo - portal to configure and deploy applications
2
# Copyright (C) 2015-2019  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

  
1 17
from django.conf.urls import url
2 18

  
3
from .views import *
19
from . import views
4 20

  
5 21
urlpatterns = [
6
    url(r'^$', HomeView.as_view(), name='environment-home'),
7
    url(r'^variables$', VariablesView.as_view(), name='environment-variables'),
8
    url(r'^new-variable$', VariableCreateView.as_view(), name='new-variable',),
9
    url(r'^update-variable/(?P<pk>\w+)$', VariableUpdateView.as_view(),
22
    url(r'^$', views.HomeView.as_view(), name='environment-home'),
23
    url(r'^variables$', views.VariablesView.as_view(), name='environment-variables'),
24
    url(r'^new-variable$', views.VariableCreateView.as_view(), name='new-variable',),
25
    url(r'^update-variable/(?P<pk>\w+)$', views.VariableUpdateView.as_view(),
10 26
        name='update-variable'),
11
    url(r'^delete-variable/(?P<pk>\w+)$', VariableDeleteView.as_view(),
27
    url(r'^delete-variable/(?P<pk>\w+)$', views.VariableDeleteView.as_view(),
12 28
        name='delete-variable'),
13 29
    url(r'^check_operational/(?P<service>\w+)/(?P<slug>[\w-]+)$',
14
        operational_check_view, name='operational-check'),
15
    url(r'^new-(?P<service>\w+)$', ServiceCreateView.as_view(), name='create-service'),
16
    url(r'^save-(?P<service>\w+)/(?P<slug>[\w-]+)$', ServiceUpdateView.as_view(), name='save-service'),
17
    url(r'^delete-(?P<service>\w+)/(?P<slug>[\w-]+)$', ServiceDeleteView.as_view(), name='delete-service'),
30
        views.operational_check_view, name='operational-check'),
31
    url(r'^new-(?P<service>\w+)$', views.ServiceCreateView.as_view(), name='create-service'),
32
    url(r'^save-(?P<service>\w+)/(?P<slug>[\w-]+)$', views.ServiceUpdateView.as_view(), name='save-service'),
33
    url(r'^delete-(?P<service>\w+)/(?P<slug>[\w-]+)$', views.ServiceDeleteView.as_view(), name='delete-service'),
18 34

  
19 35
    url(r'^new-variable-(?P<service>\w+)/(?P<slug>[\w-]+)$',
20
        VariableCreateView.as_view(), name='new-variable-service',),
21
    url(r'^debug.json$', debug_json, name='debug-json'),
36
        views.VariableCreateView.as_view(), name='new-variable-service',),
37
    url(r'^debug.json$', views.debug_json, name='debug-json'),
22 38
]
hobo/environment/utils.py
1
# hobo - portal to configure and deploy applications
2
# Copyright (C) 2015-2019  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

  
1 17
from django.conf import settings
2 18
from django.core.urlresolvers import reverse
3 19
from django.db import connection
......
12 28
        installed_services.extend(available_service.objects.all())
13 29
    return installed_services
14 30

  
31

  
15 32
def get_operational_services():
16 33
    return [x for x in get_installed_services() if x.is_operational()]
17 34

  
35

  
18 36
def get_installed_services_dict():
19
    from .models import AVAILABLE_SERVICES, Variable
37
    from .models import Variable
20 38
    hobo_service = []
21 39
    build_absolute_uri = None
22 40
    if hasattr(connection, 'get_tenant') and hasattr(connection.get_tenant(), 'build_absolute_uri'):
......
37 55
        }]
38 56
    return {
39 57
        'services': hobo_service + [x.as_dict() for x in get_installed_services()],
40
        'variables': dict(((v.name, v.json)
41
            for v in Variable.objects.filter(service_pk__isnull=True))),
42
        }
58
        'variables': {v.name: v.json for v in Variable.objects.filter(service_pk__isnull=True)}
59
    }
43 60

  
44 61

  
45 62
class Zone:
......
55 72

  
56 73
def get_setting_variable(name):
57 74
    from .models import Variable
58
    variable, created = Variable.objects.get_or_create(name=name,
59
            defaults={'auto': True, 'value': settings.VARIABLE_SETTINGS_DEFAULTS.get(name) or ''})
75
    variable, created = Variable.objects.get_or_create(
76
        name=name,
77
        defaults={
78
            'auto': True,
79
            'value': settings.VARIABLE_SETTINGS_DEFAULTS.get(name) or ''
80
        })
60 81
    return variable
hobo/environment/views.py
1
# hobo - portal to configure and deploy applications
2
# Copyright (C) 2015-2019  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

  
1 17
import json
2 18
import string
3 19

  
......
26 42
        context = super(HomeView, self).get_context_data(**kwargs)
27 43
        context['url_template'] = settings.SERVICE_URL_TEMPLATE
28 44
        context['available_services'] = [
29
                AvailableService(x) for x in AVAILABLE_SERVICES if x.is_enabled()]
45
            AvailableService(x) for x in AVAILABLE_SERVICES if x.is_enabled()
46
        ]
30 47
        context['installed_services'] = [x for x in utils.get_installed_services() if not x.secondary]
31 48
        return context
32 49

  
......
36 53

  
37 54
    def get_context_data(self, **kwargs):
38 55
        context = super(VariablesView, self).get_context_data(**kwargs)
39
        context['variables'] = Variable.objects.filter(auto=False,
40
                service_pk__isnull=True).order_by('label')
56
        context['variables'] = Variable.objects.filter(auto=False, service_pk__isnull=True).order_by('label')
41 57
        return context
42 58

  
43 59

  
......
68 84
            }
69 85
        try:
70 86
            self.object = Variable.objects.get(
71
                    name=form.instance.name,
72
                    **service_kwargs)
87
                name=form.instance.name,
88
                **service_kwargs)
73 89
        except Variable.DoesNotExist:
74 90
            self.object = form.save()
75 91
        else:
......
114 130

  
115 131
    def get_initial(self):
116 132
        initial = super(ServiceCreateView, self).get_initial()
117
        initial['base_url'] = string.Template(settings.SERVICE_URL_TEMPLATE
118
                ).substitute({'app': self.model.Extra.service_id})
133
        initial['base_url'] = (
134
            string.Template(settings.SERVICE_URL_TEMPLATE)
135
            .substitute({'app': self.model.Extra.service_id})
136
        )
119 137
        initial['slug'] = self.model.Extra.service_default_slug
120 138
        return initial
121 139

  
......
168 186
                return form_class
169 187
        return None
170 188

  
189

  
171 190
class ServiceDeleteView(DeleteView):
172 191
    success_url = reverse_lazy('environment-home')
173 192
    template_name = 'environment/generic_confirm_delete.html'
......
181 200
                return service.objects.get(slug=service_slug)
182 201
        return None
183 202

  
203

  
184 204
def operational_check_view(request, service, slug, **kwargs):
185 205

  
186 206
    for klass in AVAILABLE_SERVICES:
187
-