Projet

Général

Profil

0001-environment-coding-style-29240.patch

Benjamin Dauvergne, 18 mars 2019 16:07

Télécharger (21,3 ko)

Voir les différences:

Subject: [PATCH 1/9] 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, 164 insertions(+), 68 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 re
2 18
import datetime
3 19
import json
......
13 29
from django.utils.six.moves.urllib.parse import urlparse
14 30
from django.utils.timezone import now
15 31
from django.utils.translation import ugettext_lazy as _
32
from django.utils import six
16 33
from django.core.exceptions import ValidationError
17 34

  
18 35
from django.contrib.contenttypes.models import ContentType
......
22 39
from .mandayejs_app_settings import APP_SETTINGS_CLASSES, DEFAULT_APP_SETTINGS
23 40

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

  
26 42
FLOAT_RE = re.compile(r'^\s*[0-9]+\.[0-9]+\s*')
27 43

  
28 44

  
29 45
class Variable(models.Model):
30 46
    name = models.CharField(max_length=100, verbose_name=_('name'))
31 47
    label = models.CharField(max_length=100, blank=True, verbose_name=_('label'))
32
    value = models.TextField(verbose_name=_('value'),
33
            blank=True,
34
            help_text=_('start with [ or { for a JSON document'))
48
    value = models.TextField(
49
        verbose_name=_('value'),
50
        blank=True,
51
        help_text=_('start with [ or { for a JSON document'))
35 52
    auto = models.BooleanField(default=False)
36 53
    service_type = models.ForeignKey(ContentType, null=True)
37 54
    service_pk = models.PositiveIntegerField(null=True)
......
80 97
    last_operational_success_timestamp = models.DateTimeField(null=True)
81 98
    last_update_timestamp = models.DateTimeField(auto_now=True, null=True)
82 99

  
83
    variables = GenericRelation(Variable,
84
            content_type_field='service_type', object_id_field='service_pk')
100
    variables = GenericRelation(
101
        Variable,
102
        content_type_field='service_type',
103
        object_id_field='service_pk')
85 104

  
86 105
    @classmethod
87 106
    def is_enabled(cls):
88 107
        return True
89 108

  
90 109
    def is_operational(self):
91
        return (self.last_operational_success_timestamp is not None and
92
                self.last_operational_success_timestamp == self.last_operational_check_timestamp)
110
        return (self.last_operational_success_timestamp is not None
111
                and self.last_operational_success_timestamp == self.last_operational_check_timestamp)
93 112

  
94 113
    def check_operational(self):
95 114
        once_now = now()
......
99 118
            response = requests.get(zone.href, timeout=10, allow_redirects=False)
100 119
            response.raise_for_status()
101 120
            self.last_operational_success_timestamp = once_now
102
        except requests.RequestException as e:
121
        except requests.RequestException:
103 122
            pass
104 123
        self.save(update_fields=('last_operational_check_timestamp', 'last_operational_success_timestamp'))
105 124

  
......
119 138

  
120 139
    def as_dict(self):
121 140
        as_dict = dict([(x, y) for (x, y) in self.__dict__.items()
122
                        if type(y) in (int, str, unicode)])
141
                        if isinstance(y, six.integer_types + six.string_types)])
123 142
        as_dict['base_url'] = self.get_base_url_path()
124 143
        as_dict['service-id'] = self.Extra.service_id
125 144
        as_dict['service-label'] = force_text(self.Extra.service_label)
......
224 243

  
225 244
class Authentic(ServiceBase):
226 245
    use_as_idp_for_self = models.BooleanField(
227
            verbose_name=_('Use as IdP'),
228
            default=False)
246
        verbose_name=_('Use as IdP'),
247
        default=False)
229 248

  
230 249
    class Meta:
231 250
        verbose_name = _('Authentic Identity Provider')
......
239 258

  
240 259
    def get_admin_zones(self):
241 260
        return [
242
                Zone(_('User Management'), 'users', self.get_base_url_path() + 'manage/users/'),
243
                Zone(_('Role Management'), 'roles', self.get_base_url_path() + 'manage/roles/'),
244
                ]
261
            Zone(_('User Management'), 'users', self.get_base_url_path() + 'manage/users/'),
262
            Zone(_('Role Management'), 'roles', self.get_base_url_path() + 'manage/roles/'),
263
        ]
245 264

  
246 265
    def get_saml_idp_metadata_url(self):
247 266
        return self.get_base_url_path() + 'idp/saml2/metadata'
......
266 285

  
267 286
    def get_admin_zones(self):
268 287
        return [
269
                Zone(self.title, 'webforms', self.get_base_url_path() + 'admin/'),
270
                ]
288
            Zone(self.title, 'webforms', self.get_base_url_path() + 'admin/'),
289
        ]
271 290

  
272 291
    def get_saml_sp_metadata_url(self):
273 292
        return self.get_base_url_path() + 'saml/metadata'
......
286 305

  
287 306
    def get_admin_zones(self):
288 307
        return [
289
                Zone(self.title, 'webservices', self.get_base_url_path() + 'manage/')
290
                ]
308
            Zone(self.title, 'webservices', self.get_base_url_path() + 'manage/')
309
        ]
291 310

  
292 311
    def get_saml_sp_metadata_url(self):
293 312
        return self.get_base_url_path() + 'accounts/mellon/metadata/'
......
309 328

  
310 329
    def get_admin_zones(self):
311 330
        return [
312
                Zone(self.title, 'portal', self.get_base_url_path() + 'manage/')
313
                ]
331
            Zone(self.title, 'portal', self.get_base_url_path() + 'manage/')
332
        ]
314 333

  
315 334
    def get_saml_sp_metadata_url(self):
316 335
        return self.get_base_url_path() + 'accounts/mellon/metadata/'
......
332 351

  
333 352
    def get_admin_zones(self):
334 353
        return [
335
                Zone(self.title, 'document-box', self.get_base_url_path() + 'admin/')
336
                ]
354
            Zone(self.title, 'document-box', self.get_base_url_path() + 'admin/')
355
        ]
337 356

  
338 357
    def get_saml_sp_metadata_url(self):
339 358
        return self.get_base_url_path() + 'accounts/mellon/metadata/'
......
351 370

  
352 371
    def get_admin_zones(self):
353 372
        return [
354
                Zone(self.title, 'multichannel-guichet', self.get_base_url_path() + 'admin/')
355
                ]
373
            Zone(self.title, 'multichannel-guichet', self.get_base_url_path() + 'admin/')
374
        ]
356 375

  
357 376
    def get_saml_sp_metadata_url(self):
358 377
        return self.get_base_url_path() + 'accounts/mellon/metadata/'
......
362 381

  
363 382

  
364 383
class MandayeJS(ServiceBase):
365
    site_app = models.CharField(_('Site Application'), max_length=128,
366
        choices = APP_SETTINGS_CLASSES,
367
        default = DEFAULT_APP_SETTINGS
368
    )
384
    site_app = models.CharField(
385
        _('Site Application'),
386
        max_length=128,
387
        choices=APP_SETTINGS_CLASSES,
388
        default=DEFAULT_APP_SETTINGS)
389

  
369 390
    class Meta:
370 391
        verbose_name = _('Authentication Reverse Proxy')
371 392
        ordering = ['title']
......
381 402

  
382 403
    def get_admin_zones(self):
383 404
        return [
384
                Zone(self.title, 'mandayejs', self.get_base_url_path() + '_mandaye/admin/')
385
                ]
405
            Zone(self.title, 'mandayejs', self.get_base_url_path() + '_mandaye/admin/')
406
        ]
386 407

  
387 408
    def get_saml_sp_metadata_url(self):
388
        return self.get_base_url_path()+ '_mandaye/accounts/mellon/metadata/'
409
        return self.get_base_url_path() + '_mandaye/accounts/mellon/metadata/'
389 410

  
390 411

  
391 412
class Chrono(ServiceBase):
......
401 422

  
402 423
    def get_admin_zones(self):
403 424
        return [
404
                Zone(self.title, 'calendar', self.get_base_url_path() + 'manage/')
405
                ]
425
            Zone(self.title, 'calendar', self.get_base_url_path() + 'manage/')
426
        ]
406 427

  
407 428
    def get_saml_sp_metadata_url(self):
408 429
        return self.get_base_url_path() + 'accounts/mellon/metadata/'
......
424 445

  
425 446
    def get_admin_zones(self):
426 447
        return [
427
                Zone(self.title, 'hobo', self.get_base_url_path())
428
                ]
448
            Zone(self.title, 'hobo', self.get_base_url_path())
449
        ]
429 450

  
430 451
    def get_saml_sp_metadata_url(self):
431 452
        return self.get_base_url_path() + 'accounts/mellon/metadata/'
......
451 472

  
452 473
    def get_admin_zones(self):
453 474
        return [
454
                Zone(self.title, 'corbo', self.get_base_url_path() + 'admin/')
455
                ]
475
            Zone(self.title, 'corbo', self.get_base_url_path() + 'admin/')
476
        ]
456 477

  
457 478
    def get_saml_sp_metadata_url(self):
458
        return self.get_base_url_path()+ 'accounts/mellon/metadata/'
479
        return self.get_base_url_path() + 'accounts/mellon/metadata/'
459 480

  
460 481
    def get_backoffice_menu_url(self):
461 482
        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
-