0003-toulouse_smart-move-make_id-into-utils-55230.patch
passerelle/contrib/toulouse_smart/utils.py | ||
---|---|---|
1 |
# passerelle - uniform access to multiple data sources and services |
|
2 |
# Copyright (C) 2021 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 | ||
17 |
import hashlib |
|
18 |
import uuid |
|
19 | ||
20 | ||
21 |
def make_id(s): |
|
22 |
return str(uuid.UUID(bytes=hashlib.md5(s.encode()).digest())) |
passerelle/contrib/toulouse_smart/views.py | ||
---|---|---|
18 | 18 |
import uuid |
19 | 19 |
import zipfile |
20 | 20 | |
21 | 21 |
from django.http import HttpResponse |
22 | 22 |
from django.template.loader import render_to_string |
23 | 23 |
from django.utils.text import slugify |
24 | 24 |
from django.views.generic import DetailView |
25 | 25 | |
26 |
from . import utils |
|
26 | 27 |
from .models import ToulouseSmartResource |
27 | 28 | |
28 | 29 | |
29 | 30 |
class TypeIntervention(DetailView): |
30 | 31 |
model = ToulouseSmartResource |
31 | 32 |
template_name = 'toulouse_smart/type-intervention.html' |
32 | 33 | |
33 | 34 | |
34 | 35 |
class TypeInterventionAsBlocks(DetailView): |
35 | 36 |
model = ToulouseSmartResource |
36 | 37 | |
37 | 38 |
def get(self, request, *args, **kwargs): |
38 | 39 |
self.object = self.get_object() |
39 | 40 | |
40 |
def make_id(s): |
|
41 |
return str(uuid.UUID(bytes=hashlib.md5(s.encode()).digest())) |
|
42 | ||
43 | 41 |
# generate file contents |
44 | 42 |
files = {} |
45 | 43 |
for intervention_type in self.object.get_intervention_types(): |
46 | 44 |
slug = slugify(intervention_type['name']) |
47 | 45 |
# only export intervention_type with properties |
48 | 46 |
if not intervention_type.get('properties'): |
49 | 47 |
continue |
50 | 48 |
for prop in intervention_type['properties']: |
51 | 49 |
# generate a natural id for fields |
52 |
prop['id'] = make_id(slug + slugify(prop['name'])) |
|
50 |
prop['id'] = utils.make_id(slug + slugify(prop['name']))
|
|
53 | 51 |
# adapt types |
54 | 52 |
prop.setdefault('type', 'string') |
55 | 53 |
if prop['type'] == 'boolean': |
56 | 54 |
prop['type'] = 'bool' |
57 | 55 |
if prop['type'] == 'int': |
58 | 56 |
prop['type'] = 'string' |
59 | 57 |
prop['validation'] = 'digits' |
60 | 58 |
filename = 'block-%s.wcs' % slug |
61 |
- |