trivial: apply black, isort; add pre-commit system

This commit is contained in:
Thomas NOËL 2022-09-13 11:15:46 +02:00
parent 541ad11836
commit eb0baa5b59
19 changed files with 329 additions and 228 deletions

17
.pre-commit-config.yaml Normal file
View File

@ -0,0 +1,17 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
args: ['--target-version', 'py37', '--skip-string-normalization', '--line-length', '110']
- repo: https://github.com/PyCQA/isort
rev: 5.7.0
hooks:
- id: isort
args: ['--profile', 'black', '--line-length', '110']
- repo: https://git.entrouvert.org/pre-commit-debian.git
rev: v0.1
hooks:
- id: pre-commit-debian

View File

@ -17,12 +17,20 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='Brexit',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
(
'id',
models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
('title', models.CharField(max_length=50, verbose_name='Title')),
('slug', models.SlugField(unique=True, verbose_name='Identifier')),
('description', models.TextField(verbose_name='Description')),
('url', models.URLField(default=b'IP_INES/action', verbose_name=b'URL')),
('users', models.ManyToManyField(blank=True, related_name='_brexit_users_+', related_query_name='+', to='base.ApiUser')),
(
'users',
models.ManyToManyField(
blank=True, related_name='_brexit_users_+', related_query_name='+', to='base.ApiUser'
),
),
],
options={
'verbose_name': 'BREXIT - API REST vers MAARCH',

View File

@ -1,10 +1,8 @@
# coding=utf-8
import requests
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils.http import urlquote
from django.utils.translation import ugettext_lazy as _
from passerelle.base.models import BaseResource
from passerelle.utils.api import endpoint
from passerelle.utils.jsonresponse import APIError, exception_to_text
@ -23,10 +21,15 @@ class Brexit(BaseResource):
def info(self, request):
return {'hello': 'world'}
@endpoint(parameters={
'num': {'description': _('Num dossier'), 'example_value': '12345'},
'email': {'description': _('Email associe au dossier'), 'example_value': 'franck@interieur.gouv.fr'},
})
@endpoint(
parameters={
'num': {'description': _('Num dossier'), 'example_value': '12345'},
'email': {
'description': _('Email associe au dossier'),
'example_value': 'franck@interieur.gouv.fr',
},
}
)
def find_folder(self, request, num, email):
self.logger.info('MININT BREXIT - find_folder num: %s ; email : %s', num, email)
@ -34,20 +37,19 @@ class Brexit(BaseResource):
try:
response = self.requests.get(self.url + '/%s/%s' % (num, email), verify=False)
except requests.RequestException as e:
raise APIError(u'API-maarch connection error: %s' %
exception_to_text(e), data=[])
raise APIError(u'API-maarch connection error: %s' % exception_to_text(e), data=[])
try:
data = response.json()
except ValueError as e:
content = repr(response.content[:1000])
raise APIError(
u'API-maarch returned non-JSON content with status %s: %s' %
(response.status_code, content),
u'API-maarch returned non-JSON content with status %s: %s' % (response.status_code, content),
data={
'status_code': response.status_code,
'exception': exception_to_text(e),
'content': content,
})
},
)
if response.status_code != 200:
if data.get('error') == 'not_found':
return {
@ -55,12 +57,12 @@ class Brexit(BaseResource):
'err_desc': data.get('message', 'not-found'),
}
raise APIError(
u'API-maarch returned a non 200 status %s: %s' %
(response.status_code, data),
u'API-maarch returned a non 200 status %s: %s' % (response.status_code, data),
data={
'status_code': response.status_code,
'content': data,
})
},
)
document_id = data.get('res_id')
if document_id is not None:
data_response = {'res_id': document_id}
@ -70,4 +72,3 @@ class Brexit(BaseResource):
'err': 0,
'data': data_response,
}

View File

@ -17,16 +17,42 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='MinIntMaarch',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
(
'id',
models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
('title', models.CharField(max_length=50, verbose_name='Title')),
('slug', models.SlugField(unique=True, verbose_name='Identifier')),
('description', models.TextField(verbose_name='Description')),
('wsdl_url', models.CharField(help_text='Maarch WSDL URL', max_length=128, verbose_name='WSDL URL')),
('verify_cert', models.BooleanField(default=True, verbose_name='Check HTTPS Certificate validity')),
(
'wsdl_url',
models.CharField(help_text='Maarch WSDL URL', max_length=128, verbose_name='WSDL URL'),
),
(
'verify_cert',
models.BooleanField(default=True, verbose_name='Check HTTPS Certificate validity'),
),
('username', models.CharField(blank=True, max_length=128, verbose_name='Username')),
('password', models.CharField(blank=True, max_length=128, verbose_name='Password')),
('keystore', models.FileField(blank=True, help_text='Certificate and private key in PEM format', null=True, upload_to=b'maarch', verbose_name='Keystore')),
('users', models.ManyToManyField(blank=True, related_name='_minintmaarch_users_+', related_query_name='+', to='base.ApiUser')),
(
'keystore',
models.FileField(
blank=True,
help_text='Certificate and private key in PEM format',
null=True,
upload_to=b'maarch',
verbose_name='Keystore',
),
),
(
'users',
models.ManyToManyField(
blank=True,
related_name='_minintmaarch_users_+',
related_query_name='+',
to='base.ApiUser',
),
),
],
options={
'verbose_name': 'Maarch',

View File

@ -16,22 +16,23 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _
from passerelle.base.models import BaseResource
class MinIntMaarch(BaseResource):
wsdl_url = models.CharField(max_length=128, blank=False,
verbose_name=_('WSDL URL'),
help_text=_('Maarch WSDL URL'))
verify_cert = models.BooleanField(default=True,
verbose_name=_('Check HTTPS Certificate validity'))
username = models.CharField(max_length=128, blank=True,
verbose_name=_('Username'))
password = models.CharField(max_length=128, blank=True,
verbose_name=_('Password'))
keystore = models.FileField(upload_to='maarch', null=True, blank=True,
verbose_name=_('Keystore'),
help_text=_('Certificate and private key in PEM format'))
wsdl_url = models.CharField(
max_length=128, blank=False, verbose_name=_('WSDL URL'), help_text=_('Maarch WSDL URL')
)
verify_cert = models.BooleanField(default=True, verbose_name=_('Check HTTPS Certificate validity'))
username = models.CharField(max_length=128, blank=True, verbose_name=_('Username'))
password = models.CharField(max_length=128, blank=True, verbose_name=_('Password'))
keystore = models.FileField(
upload_to='maarch',
null=True,
blank=True,
verbose_name=_('Keystore'),
help_text=_('Certificate and private key in PEM format'),
)
category = _('MinInt')

View File

@ -18,18 +18,18 @@
# and https://docs.oracle.com/cd/E50245_01/E50253/html/vmprg-soap-example-authentication-python.html
from io import BytesIO
import requests
import requests
from django.conf import settings
from suds.transport.http import HttpAuthenticated
from suds.transport import Reply
from suds.client import Client
from suds.transport import Reply
from suds.transport.http import HttpAuthenticated
class Transport(HttpAuthenticated):
def __init__(self, model, **kwargs):
self.model = model
HttpAuthenticated.__init__(self, **kwargs) # oldstyle class...
HttpAuthenticated.__init__(self, **kwargs) # oldstyle class...
def get_requests_kwargs(self):
kwargs = {}
@ -44,14 +44,14 @@ class Transport(HttpAuthenticated):
return kwargs
def open(self, request):
resp = requests.get(request.url, headers=request.headers,
**self.get_requests_kwargs())
resp = requests.get(request.url, headers=request.headers, **self.get_requests_kwargs())
return BytesIO(resp.content)
def send(self, request):
self.addcredentials(request)
resp = requests.post(request.url, data=request.message,
headers=request.headers, **self.get_requests_kwargs())
resp = requests.post(
request.url, data=request.message, headers=request.headers, **self.get_requests_kwargs()
)
result = Reply(resp.status_code, resp.headers, resp.content)
return result

View File

@ -19,14 +19,9 @@ from django.conf.urls import include, url
from .views import *
urlpatterns = [
url(r'^(?P<slug>[\w,-]+)/$', MaarchDetailView.as_view(),
name='minint-maarch-view'),
url(r'^(?P<slug>[\w,-]+)/ping/$', PingView.as_view(),
name='minint-maarch-ping'),
url(r'^(?P<slug>[\w,-]+)/resource/$', ResourceView.as_view(),
name='minint-maarch-resource'),
url(r'^(?P<slug>[\w,-]+)/attachment/$', AttachmentView.as_view(),
name='minint-maarch-attachment'),
url(r'^(?P<slug>[\w,-]+)/debug/$', DebugView.as_view(),
name='minint-maarch-debug'),
url(r'^(?P<slug>[\w,-]+)/$', MaarchDetailView.as_view(), name='minint-maarch-view'),
url(r'^(?P<slug>[\w,-]+)/ping/$', PingView.as_view(), name='minint-maarch-ping'),
url(r'^(?P<slug>[\w,-]+)/resource/$', ResourceView.as_view(), name='minint-maarch-resource'),
url(r'^(?P<slug>[\w,-]+)/attachment/$', AttachmentView.as_view(), name='minint-maarch-attachment'),
url(r'^(?P<slug>[\w,-]+)/debug/$', DebugView.as_view(), name='minint-maarch-debug'),
]

View File

@ -15,30 +15,31 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import base64
from datetime import datetime
import json
import logging
import re
import requests
import urllib.parse
from datetime import datetime
from django.views.generic import DetailView as GenericDetailView
import passerelle.utils as utils
import requests
from django.utils.decorators import method_decorator
from django.utils.encoding import force_text
from django.views.decorators.csrf import csrf_exempt
import passerelle.utils as utils
from passerelle.soap import sudsobject_to_dict, client_to_jsondict
from django.views.generic import DetailView as GenericDetailView
from passerelle.soap import client_to_jsondict, sudsobject_to_dict
from suds import TypeNotFound
from .soap import get_client
from .models import MinIntMaarch
from .soap import get_client
logger = logging.getLogger('minint_maarch')
class MaarchException(Exception):
pass
class MaarchDetailView(GenericDetailView):
model = MinIntMaarch
template_name = 'minint_maarch/detail.html'
@ -69,6 +70,7 @@ class DetailView(GenericDetailView):
def translate(self, key, default):
return self.translations[key] if self.translations and key in self.translations else default
class PingView(DetailView):
def get_data(self, request, *args, **kwargs):
client = self.get_client()
@ -96,8 +98,7 @@ class ResourceView(DetailView):
# get formdef schema from wcs API
url = formdata['url']
p = urllib.parse.urlsplit(url)
scheme, netloc, path, query, fragment = \
p.scheme, p.netloc, p.path, p.query, p.fragment
scheme, netloc, path, query, fragment = p.scheme, p.netloc, p.path, p.query, p.fragment
schema_path = path.rsplit('/', 2)[0] + '/schema'
schema_url = urllib.parse.urlunsplit((scheme, netloc, schema_path, query, fragment))
schema = requests.get(schema_url).json()
@ -106,8 +107,10 @@ class ResourceView(DetailView):
attachments = []
# regexp to rename attachments
regexp_filename_search_pattern_in_label, filename_result_pattern = self.prepare_regexp_filename_replacement(
extras)
(
regexp_filename_search_pattern_in_label,
filename_result_pattern,
) = self.prepare_regexp_filename_replacement(extras)
#
# build document (encodedFile and fileFormat)
@ -143,14 +146,18 @@ class ResourceView(DetailView):
continue
if field['type'] == 'file':
# field is a file: add it to attachments list
value['fileFormat'] = value['content_type'].split('/')[1] # FIXME (how ?)
value['fileFormat'] = value['content_type'].split('/')[1] # FIXME (how ?)
if regexp_filename_search_pattern_in_label:
m = regexp_filename_search_pattern_in_label.match(field['label'])
if m:
value['filename_original'] = value['filename']
value['filename'] = filename_result_pattern.replace('searchedPatternInLabel', m.group(1))
value['filename'] = filename_result_pattern.replace(
'searchedPatternInLabel', m.group(1)
)
if 'filename' in filename_result_pattern:
value['filename'] = value['filename'].replace('filename', value['filename_original'])
value['filename'] = value['filename'].replace(
'filename', value['filename_original']
)
attachments.append(value)
value = '%s' % value['filename']
elif field['type'] == 'date':
@ -164,7 +171,7 @@ class ResourceView(DetailView):
page += '<dd>%s</dd>' % value
page += '</dl>'
empty_page = False
if page and not empty_page: # add last page, if it contains values
if page and not empty_page: # add last page, if it contains values
document += page
document += '</body></html>'
encodedFile = force_text(base64.b64encode(document.encode('utf-8')))
@ -265,7 +272,6 @@ class ResourceView(DetailView):
debug_output['contact'] = '%r' % contact
debug_output['metadata'] = '%r' % metadata
#
# get other Maarch variables (letterbox configuration by default)
#
@ -281,8 +287,6 @@ class ResourceView(DetailView):
debug_output['status'] = status
debug_output['ext_table'] = ext_table
if debug:
debug_output['maarch_id'] = maarch_id
@ -316,20 +320,22 @@ class ResourceView(DetailView):
debug_output['ext_metadata'] = '%r' % ext_metadata
# store the resource (storeResource)
logger.debug('storeResource: encodedFile(size):%r fileFormat:%r '
'collId:%r table:%r status:%r', len(encodedFile),
fileFormat, collId, table, status)
logger.debug(
'storeResource: encodedFile(size):%r fileFormat:%r ' 'collId:%r table:%r status:%r',
len(encodedFile),
fileFormat,
collId,
table,
status,
)
logger.debug('storeResource: metadata: %r', metadata)
if maarch_id:
logger.debug('storeResource: INES maarch_id: %r', maarch_id)
results = client.service.storeResource(
maarch_id,
encodedFile, metadata, collId,
table, fileFormat, status)
maarch_id, encodedFile, metadata, collId, table, fileFormat, status
)
else:
results = client.service.storeResource(
encodedFile, metadata, collId,
table, fileFormat, status)
results = client.service.storeResource(encodedFile, metadata, collId, table, fileFormat, status)
data = sudsobject_to_dict(results)
logger.debug('storeResource result: %r', data)
@ -338,35 +344,39 @@ class ResourceView(DetailView):
raise MaarchException('no resId after storeResource')
logger.debug('storeResource result: resId:%r', resId)
logger.debug('storeExtResource: resId:%r ext_table:%r', resId,
ext_table)
logger.debug('storeExtResource: resId:%r ext_table:%r', resId, ext_table)
# logger.debug('storeExtResource: ext_metadata: %r', ext_metadata)
# store external metadata (storeExtResource)
if maarch_id:
logger.debug('storeExtResource: INES maarch_id: %r', maarch_id)
results = client.service.storeExtResource(
maarch_id,
resId, ext_metadata, ext_table)
results = client.service.storeExtResource(maarch_id, resId, ext_metadata, ext_table)
else:
results = client.service.storeExtResource(
resId, ext_metadata, ext_table)
results = client.service.storeExtResource(resId, ext_metadata, ext_table)
# store attachments
for attachment in attachments:
logger.debug('storeAttachmentResource: resId:%r collId:%r '
'content(size):%r fileFormat:%r filename:%r', resId,
collId, len(attachment['content']),
attachment['fileFormat'], attachment['filename'])
logger.debug(
'storeAttachmentResource: resId:%r collId:%r ' 'content(size):%r fileFormat:%r filename:%r',
resId,
collId,
len(attachment['content']),
attachment['fileFormat'],
attachment['filename'],
)
if maarch_id:
logger.debug('storeAttachmentResource: INES maarch_id: %r', maarch_id)
client.service.storeAttachmentResource(
maarch_id,
resId, collId, attachment['content'],
attachment['fileFormat'], attachment['filename'])
maarch_id,
resId,
collId,
attachment['content'],
attachment['fileFormat'],
attachment['filename'],
)
else:
client.service.storeAttachmentResource(
resId, collId, attachment['content'],
attachment['fileFormat'], attachment['filename'])
resId, collId, attachment['content'], attachment['fileFormat'], attachment['filename']
)
if debug:
data['debug'] = debug_output
@ -393,13 +403,15 @@ class AttachmentView(DetailView):
# get formdef schema from wcs API
url = formdata['url']
p = urllib.parse.urlsplit(url)
scheme, netloc, path, query, fragment = \
p.scheme, p.netloc, p.path, p.query, p.fragment
scheme, netloc, path, query, fragment = p.scheme, p.netloc, p.path, p.query, p.fragment
schema_path = path.rsplit('/', 2)[0] + '/schema'
schema_url = urllib.parse.urlunsplit((scheme, netloc, schema_path, query, fragment))
schema = requests.get(schema_url).json()
regexp_filename_search_pattern_in_label, filename_result_pattern = self.prepare_regexp_filename_replacement(extras)
(
regexp_filename_search_pattern_in_label,
filename_result_pattern,
) = self.prepare_regexp_filename_replacement(extras)
# storeAttachmentResource attachments: list, build from formdata file fields
attachments = []
@ -411,14 +423,18 @@ class AttachmentView(DetailView):
continue
if field['type'] == 'file':
# field is a file: add it to attachments list
value['fileFormat'] = value['content_type'].split('/')[1] # FIXME (how ?)
value['fileFormat'] = value['content_type'].split('/')[1] # FIXME (how ?)
if regexp_filename_search_pattern_in_label:
m = regexp_filename_search_pattern_in_label.match(field['label'])
if m:
value['filename_original'] = value['filename']
value['filename'] = filename_result_pattern.replace('searchedPatternInLabel', m.group(1))
value['filename'] = filename_result_pattern.replace(
'searchedPatternInLabel', m.group(1)
)
if 'filename' in filename_result_pattern:
value['filename'] = value['filename'].replace('filename', value['filename_original'])
value['filename'] = value['filename'].replace(
'filename', value['filename_original']
)
attachments.append(value)
#
# get other Maarch variables (letterbox configuration by default)
@ -449,23 +465,37 @@ class AttachmentView(DetailView):
nbAttachments = len(attachments)
countAttachment = 0
for attachment in attachments:
logger.debug('storeAttachmentResource: resId:%r collId:%r '
'content(size):%r fileFormat:%r filename:%r', resId,
collId, len(attachment['content']),
attachment['fileFormat'], attachment['filename'])
logger.debug(
'storeAttachmentResource: resId:%r collId:%r ' 'content(size):%r fileFormat:%r filename:%r',
resId,
collId,
len(attachment['content']),
attachment['fileFormat'],
attachment['filename'],
)
countAttachment += 1
if maarch_id:
email_to = notification_email if notification_email and countAttachment == nbAttachments else ''
modele_ae = notification_model if notification_model and countAttachment == nbAttachments else ''
email_to = (
notification_email if notification_email and countAttachment == nbAttachments else ''
)
modele_ae = (
notification_model if notification_model and countAttachment == nbAttachments else ''
)
logger.debug('storeAdditionalAttachmentResource: INES maarch_id: %r', maarch_id)
client.service.storeAdditionalAttachmentResource(
maarch_id,
resId, collId, attachment['content'],
attachment['fileFormat'], attachment['filename'], email_to, modele_ae)
maarch_id,
resId,
collId,
attachment['content'],
attachment['fileFormat'],
attachment['filename'],
email_to,
modele_ae,
)
else:
results = client.service.storeAttachmentResource(
resId, collId, attachment['content'],
attachment['fileFormat'], attachment['filename'])
resId, collId, attachment['content'], attachment['fileFormat'], attachment['filename']
)
res = sudsobject_to_dict(results)
debug_output['res'].append(res)
@ -487,15 +517,14 @@ class DebugView(DetailView):
formdata = json.loads(request.body)
extras = formdata.get('extra', {})
logger.debug('formdata %r: ', formdata)
debug = False # 'debug' in request.GET
debug = False # 'debug' in request.GET
if debug:
debug_output = {}
# get formdef schema from wcs API
url = formdata['url']
p = urllib.parse.urlsplit(url)
scheme, netloc, path, query, fragment = \
p.scheme, p.netloc, p.path, p.query, p.fragment
scheme, netloc, path, query, fragment = p.scheme, p.netloc, p.path, p.query, p.fragment
schema_path = path.rsplit('/', 2)[0] + '/schema'
schema_url = urllib.parse.urlunsplit((scheme, netloc, schema_path, query, fragment))
schema = requests.get(schema_url).json()
@ -504,8 +533,10 @@ class DebugView(DetailView):
attachments = []
# regexp to rename attachments
regexp_filename_search_pattern_in_label, filename_result_pattern = self.prepare_regexp_filename_replacement(
extras)
(
regexp_filename_search_pattern_in_label,
filename_result_pattern,
) = self.prepare_regexp_filename_replacement(extras)
#
# build document (encodedFile and fileFormat)
@ -549,11 +580,13 @@ class DebugView(DetailView):
m = regexp_filename_search_pattern_in_label.match(field['label'])
if m:
value['filename_original'] = value['filename']
value['filename'] = filename_result_pattern.replace('searchedPatternInLabel',
m.group(1))
value['filename'] = filename_result_pattern.replace(
'searchedPatternInLabel', m.group(1)
)
if 'filename' in filename_result_pattern:
value['filename'] = value['filename'].replace('filename',
value['filename_original'])
value['filename'] = value['filename'].replace(
'filename', value['filename_original']
)
attachments.append(value)
value = '%s' % value['filename']
elif field['type'] == 'date':
@ -718,9 +751,14 @@ class DebugView(DetailView):
debug_output['ext_metadata'] = '%r' % ext_metadata
# store the resource (storeResource)
logger.debug('storeResource: encodedFile(size):%r fileFormat:%r '
'collId:%r table:%r status:%r', len(encodedFile),
fileFormat, collId, table, status)
logger.debug(
'storeResource: encodedFile(size):%r fileFormat:%r ' 'collId:%r table:%r status:%r',
len(encodedFile),
fileFormat,
collId,
table,
status,
)
# logger.debug('storeResource: metadata: %r', metadata)
if maarch_id:
logger.debug('storeResource: INES maarch_id: %r', maarch_id)
@ -729,16 +767,16 @@ class DebugView(DetailView):
# encodedFile, metadata, collId,
# table, fileFormat, status)
# else:
# results = client.service.storeResource(
# encodedFile, metadata, collId,
# table, fileFormat, status)
# results = client.service.storeResource(
# encodedFile, metadata, collId,
# table, fileFormat, status)
# results = {'err': 0, 'data': {'returnCode': 0, 'resId': 1000}}
# data = sudsobject_to_dict(results)
# logger.debug('storeResource result: %r', data)
resId = 1000 # data.get('resId')
resId = 1000 # data.get('resId')
if not resId:
raise MaarchException('no resId after storeResource')
logger.debug('storeResource result: resId:%r', resId)
@ -753,14 +791,20 @@ class DebugView(DetailView):
# maarch_id,
# resId, ext_metadata, ext_table)
# else:
# results = client.service.storeExtResource(
# resId, ext_metadata, ext_table)
# results = client.service.storeExtResource(
# resId, ext_metadata, ext_table)
results = {'returnCode': 0, 'resId': 1000}
# store attachments
for attachment in attachments:
logger.debug('storeAttachmentResource: resId:%r collId:%r content(size):%r fileFormat:%r filename:%r',
resId, collId, len(attachment['content']), attachment['fileFormat'], attachment['filename'])
logger.debug(
'storeAttachmentResource: resId:%r collId:%r content(size):%r fileFormat:%r filename:%r',
resId,
collId,
len(attachment['content']),
attachment['fileFormat'],
attachment['filename'],
)
if maarch_id:
logger.debug('storeAttachmentResource: INES maarch_id: %r', maarch_id)
# client.service.storeAttachmentResource(
@ -768,9 +812,9 @@ class DebugView(DetailView):
# resId, collId, attachment['content'],
# attachment['fileFormat'], attachment['filename'])
# else:
# client.service.storeAttachmentResource(
# resId, collId, attachment['content'],
# attachment['fileFormat'], attachment['filename'])
# client.service.storeAttachmentResource(
# resId, collId, attachment['content'],
# attachment['fileFormat'], attachment['filename'])
data = {}
data['resId'] = 1000

View File

@ -17,16 +17,44 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='SeisinManagement',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
(
'id',
models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
('title', models.CharField(max_length=50, verbose_name='Title')),
('slug', models.SlugField(unique=True, verbose_name='Identifier')),
('description', models.TextField(verbose_name='Description')),
('wsdl_url', models.CharField(help_text='Seisin-by-Email WSDL URL', max_length=128, verbose_name='WSDL URL')),
('verify_cert', models.BooleanField(default=True, verbose_name='Check HTTPS Certificate validity')),
(
'wsdl_url',
models.CharField(
help_text='Seisin-by-Email WSDL URL', max_length=128, verbose_name='WSDL URL'
),
),
(
'verify_cert',
models.BooleanField(default=True, verbose_name='Check HTTPS Certificate validity'),
),
('username', models.CharField(blank=True, max_length=128, verbose_name='Username')),
('password', models.CharField(blank=True, max_length=128, verbose_name='Password')),
('keystore', models.FileField(blank=True, help_text='Certificate and private key in PEM format', null=True, upload_to=b'seisin-by-email', verbose_name='Keystore')),
('users', models.ManyToManyField(blank=True, related_name='_seisinmanagement_users_+', related_query_name='+', to='base.ApiUser')),
(
'keystore',
models.FileField(
blank=True,
help_text='Certificate and private key in PEM format',
null=True,
upload_to=b'seisin-by-email',
verbose_name='Keystore',
),
),
(
'users',
models.ManyToManyField(
blank=True,
related_name='_seisinmanagement_users_+',
related_query_name='+',
to='base.ApiUser',
),
),
],
options={
'verbose_name': 'Seisin by Email',

View File

@ -16,22 +16,23 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _
from passerelle.base.models import BaseResource
class SeisinManagement(BaseResource):
wsdl_url = models.CharField(max_length=128, blank=False,
verbose_name=_('WSDL URL'),
help_text=_('Seisin-by-Email WSDL URL'))
verify_cert = models.BooleanField(default=True,
verbose_name=_('Check HTTPS Certificate validity'))
username = models.CharField(max_length=128, blank=True,
verbose_name=_('Username'))
password = models.CharField(max_length=128, blank=True,
verbose_name=_('Password'))
keystore = models.FileField(upload_to='seisin-by-email', null=True, blank=True,
verbose_name=_('Keystore'),
help_text=_('Certificate and private key in PEM format'))
wsdl_url = models.CharField(
max_length=128, blank=False, verbose_name=_('WSDL URL'), help_text=_('Seisin-by-Email WSDL URL')
)
verify_cert = models.BooleanField(default=True, verbose_name=_('Check HTTPS Certificate validity'))
username = models.CharField(max_length=128, blank=True, verbose_name=_('Username'))
password = models.CharField(max_length=128, blank=True, verbose_name=_('Password'))
keystore = models.FileField(
upload_to='seisin-by-email',
null=True,
blank=True,
verbose_name=_('Keystore'),
help_text=_('Certificate and private key in PEM format'),
)
category = _('MinInt')

View File

@ -18,18 +18,18 @@
# and https://docs.oracle.com/cd/E50245_01/E50253/html/vmprg-soap-example-authentication-python.html
from io import BytesIO
import requests
import requests
from django.conf import settings
from suds.transport.http import HttpAuthenticated
from suds.transport import Reply
from suds.client import Client
from suds.transport import Reply
from suds.transport.http import HttpAuthenticated
class Transport(HttpAuthenticated):
def __init__(self, model, **kwargs):
self.model = model
HttpAuthenticated.__init__(self, **kwargs) # oldstyle class...
HttpAuthenticated.__init__(self, **kwargs) # oldstyle class...
def get_requests_kwargs(self):
kwargs = {}
@ -44,14 +44,14 @@ class Transport(HttpAuthenticated):
return kwargs
def open(self, request):
resp = requests.get(request.url, headers=request.headers,
**self.get_requests_kwargs())
resp = requests.get(request.url, headers=request.headers, **self.get_requests_kwargs())
return BytesIO(resp.content)
def send(self, request):
self.addcredentials(request)
resp = requests.post(request.url, data=request.message,
headers=request.headers, **self.get_requests_kwargs())
resp = requests.post(
request.url, data=request.message, headers=request.headers, **self.get_requests_kwargs()
)
result = Reply(resp.status_code, resp.headers, resp.content)
return result

View File

@ -19,10 +19,7 @@ from django.conf.urls import include, url
from .views import *
urlpatterns = [
url(r'^(?P<slug>[\w,-]+)/$', SeisinManagementDetailView.as_view(),
name='minint-seisin-by-email-view'),
url(r'^(?P<slug>[\w,-]+)/ping/$', PingView.as_view(),
name='minint-seisin-by-email-ping'),
url(r'^(?P<slug>[\w,-]+)/resource/$', ResourceView.as_view(),
name='minint-seisin-by-email-resource'),
url(r'^(?P<slug>[\w,-]+)/$', SeisinManagementDetailView.as_view(), name='minint-seisin-by-email-view'),
url(r'^(?P<slug>[\w,-]+)/ping/$', PingView.as_view(), name='minint-seisin-by-email-ping'),
url(r'^(?P<slug>[\w,-]+)/resource/$', ResourceView.as_view(), name='minint-seisin-by-email-resource'),
]

View File

@ -16,15 +16,14 @@
import json
from django.views.generic import DetailView as GenericDetailView
import passerelle.utils as utils
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import DetailView as GenericDetailView
from passerelle.soap import client_to_jsondict, sudsobject_to_dict
import passerelle.utils as utils
from passerelle.soap import sudsobject_to_dict, client_to_jsondict
from .soap import get_client
from .models import SeisinManagement
from .soap import get_client
class SeisinManagementDetailView(GenericDetailView):
@ -129,26 +128,18 @@ class ResourceView(DetailView):
# call sendSeisinByEmail web service
if departement_number:
results = client.service.sendSeisinByEmail(departement_number,
target_entity,
theme,
userType,
request_object,
data,
attachmentData)
results = client.service.sendSeisinByEmail(
departement_number, target_entity, theme, userType, request_object, data, attachmentData
)
data = sudsobject_to_dict(results)
elif departement_numbers:
results = []
for dep in departement_numbers:
# dep may be empty
if dep:
result = client.service.sendSeisinByEmail(dep,
target_entity,
theme,
userType,
request_object,
data,
attachmentData)
result = client.service.sendSeisinByEmail(
dep, target_entity, theme, userType, request_object, data, attachmentData
)
results.append(sudsobject_to_dict(result))
data = {}
data['results'] = results

View File

@ -3,13 +3,13 @@
import os
import subprocess
from setuptools import setup, find_packages
from setuptools import find_packages, setup
from setuptools.command.sdist import sdist
def get_version():
'''Use the VERSION, if absent generates a version with git describe, if not
tag exists, take 0.0- and add the length of the commit log.
tag exists, take 0.0- and add the length of the commit log.
'''
if os.path.exists('VERSION'):
version_file = open('VERSION', 'r')
@ -37,7 +37,6 @@ def get_version():
class eo_sdist(sdist):
def run(self):
if os.path.exists('VERSION'):
os.remove('VERSION')
@ -68,11 +67,9 @@ setup(
'Programming Language :: Python',
'Programming Language :: Python :: 2',
],
install_requires=[
'suds'
],
install_requires=['suds'],
zip_safe=False,
cmdclass={
'sdist': eo_sdist,
}
},
)

View File

@ -1,12 +1,10 @@
import pytest
from httmock import urlmatch, HTTMock, response, remember_called
import django_webtest
from django.core.files import File
from django.core.cache import cache
from io import BytesIO
import django_webtest
import pytest
from django.core.cache import cache
from django.core.files import File
from httmock import HTTMock, remember_called, response, urlmatch
from utils import make_resource
@ -26,10 +24,10 @@ def app(request):
@pytest.fixture
def endpoint_dummy_cache(monkeypatch):
from django.core.cache import caches
import passerelle.views
monkeypatch.setattr(
passerelle.views, 'cache', caches['dummy'])
from django.core.cache import caches
monkeypatch.setattr(passerelle.views, 'cache', caches['dummy'])
@urlmatch()

View File

@ -1,16 +1,16 @@
import os
INSTALLED_APPS += (
'passerelle_minint.minint_maarch',
'passerelle_minint.minint_seisin',
'passerelle_minint.minint_brexit',
)
'passerelle_minint.minint_maarch',
'passerelle_minint.minint_seisin',
'passerelle_minint.minint_brexit',
)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'TEST': {
'NAME': 'passerelle-minint-test-%s' % os.environ.get("BRANCH_NAME", "").replace('/', '-')[:63],
}
},
}
}

View File

@ -4,5 +4,6 @@ from passerelle_minint.minint_maarch.models import MinIntMaarch
def test_creation(app, db):
resource = utils.make_resource(MinIntMaarch, wsdl_url='https://example.com/',
slug='slug', verify_cert=True)
resource = utils.make_resource(
MinIntMaarch, wsdl_url='https://example.com/', slug='slug', verify_cert=True
)

View File

@ -4,5 +4,6 @@ from passerelle_minint.minint_seisin.models import SeisinManagement
def test_creation(app, db):
resource = utils.make_resource(SeisinManagement, wsdl_url='https://example.com/',
slug='slug', verify_cert=True)
resource = utils.make_resource(
SeisinManagement, wsdl_url='https://example.com/', slug='slug', verify_cert=True
)

View File

@ -1,26 +1,21 @@
import json
import mock
import httmock
import urllib.parse
import httmock
import mock
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from passerelle.base.models import ApiUser, AccessRight
from passerelle.base.models import AccessRight, ApiUser
def generic_endpoint_url(connector, endpoint, slug='test'):
return reverse('generic-endpoint', kwargs={
'connector': connector, 'slug': slug, 'endpoint': endpoint})
return reverse('generic-endpoint', kwargs={'connector': connector, 'slug': slug, 'endpoint': endpoint})
def setup_access_rights(obj):
api = ApiUser.objects.create(username='all',
keytype='', key='')
api = ApiUser.objects.create(username='all', keytype='', key='')
obj_type = ContentType.objects.get_for_model(obj)
AccessRight.objects.create(codename='can_access', apiuser=api,
resource_type=obj_type, resource_pk=obj.pk)
AccessRight.objects.create(codename='can_access', apiuser=api, resource_type=obj_type, resource_pk=obj.pk)
return obj
@ -46,6 +41,7 @@ def mock_url(url=None, response='', status_code=200, headers=None):
@httmock.urlmatch(**urlmatch_kwargs)
def mocked(url, request):
return httmock.response(status_code, response, headers, request=request)
return httmock.HTTMock(mocked)
@ -57,8 +53,7 @@ def make_resource(model_class, **kwargs):
def endpoint_get(expected_url, app, resource, endpoint, **kwargs):
url = generic_endpoint_url(
connector=resource.__class__.get_connector_slug(),
endpoint=endpoint,
slug=resource.slug)
connector=resource.__class__.get_connector_slug(), endpoint=endpoint, slug=resource.slug
)
assert url == expected_url, 'endpoint URL has changed'
return app.get(url, **kwargs)