Added 'extra_debug' field and a function to only log debug message if extra_debug is enabled
This commit is contained in:
parent
7020d3c2d0
commit
522634d564
|
@ -1,5 +1,5 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.11.15 on 2019-07-10 14:51
|
||||
# Generated by Django 1.11.15 on 2019-07-11 16:20
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
@ -26,6 +26,7 @@ class Migration(migrations.Migration):
|
|||
('openADS_API_login', models.CharField(default=b'', help_text='ex: user1234', max_length=255, verbose_name='openADS API login')),
|
||||
('openADS_API_password', models.CharField(default=b'', help_text='ex: ah9pGbKKHv5ToF3cPQuV', max_length=255, verbose_name='openADS API password')),
|
||||
('openADS_API_url', models.URLField(default=b'', help_text='ex: https://openads.your_domain.net/api/', max_length=255, verbose_name='openADS API URL')),
|
||||
('extra_debug', models.BooleanField(default=0, help_text='ex: True', verbose_name='Extra debug')),
|
||||
('users', models.ManyToManyField(blank=True, related_name='_atrealopenads_users_+', related_query_name='+', to='base.ApiUser')),
|
||||
],
|
||||
options={
|
||||
|
|
|
@ -119,6 +119,8 @@ class AtrealOpenads(BaseResource):
|
|||
help_text=_('ex: ah9pGbKKHv5ToF3cPQuV'), default='')
|
||||
openADS_API_url = models.URLField(_('openADS API URL'), max_length=255,
|
||||
help_text=_('ex: https://openads.your_domain.net/api/'), default='')
|
||||
extra_debug = models.BooleanField(_('Extra debug'),
|
||||
help_text=_('ex: True'), default=0)
|
||||
|
||||
category = _('Business Process Connectors')
|
||||
|
||||
|
@ -130,6 +132,18 @@ class AtrealOpenads(BaseResource):
|
|||
verbose_name = _('openADS')
|
||||
|
||||
|
||||
@property
|
||||
def extra_debug_enabled(self):
|
||||
"""Return True if 'extra debug' is enabled."""
|
||||
return bool(self.extra_debug)
|
||||
|
||||
|
||||
def debug(self, *args, **kwargs):
|
||||
"""Log a debug message only if 'extra debug' is enabled."""
|
||||
if self.extra_debug_enabled:
|
||||
self.logger.debug(*args, **kwargs)
|
||||
|
||||
|
||||
def check_status(self):
|
||||
"""Check avaibility of the openADS.API service."""
|
||||
url = urlparse.urljoin(self.openADS_API_url, '__api__')
|
||||
|
@ -147,7 +161,7 @@ class AtrealOpenads(BaseResource):
|
|||
'type_dossier': {'description': _("Type of 'dossier'"), 'example_value': 'DIA'}
|
||||
})
|
||||
def create_dossier(self, request, type_dossier, *args, **kwargs):
|
||||
self.logger.debug("----- request json (begining) -----")
|
||||
self.debug("----- request json (begining) -----")
|
||||
json_data = json.loads(request.body)
|
||||
debug_json = copy.deepcopy(json_data)
|
||||
file_keys = ['cerfa'] + ['plan_cadastral_%s' % i for i in range(1,5)]
|
||||
|
@ -157,8 +171,8 @@ class AtrealOpenads(BaseResource):
|
|||
and isinstance(debug_json['fields'][k], dict) \
|
||||
and 'content' in debug_json['fields'][k]:
|
||||
debug_json['fields'][k]['content'] = '<b64 content>'
|
||||
self.logger.debug(json.dumps(debug_json))
|
||||
self.logger.debug("----- request json (end) -----")
|
||||
self.debug(json.dumps(debug_json))
|
||||
self.debug("----- request json (end) -----")
|
||||
|
||||
payload = { "collectivite": self.collectivite }
|
||||
|
||||
|
@ -207,9 +221,9 @@ class AtrealOpenads(BaseResource):
|
|||
|
||||
payload["demandeurs"] = [demandeur]
|
||||
|
||||
self.logger.debug("----- payload (begining) -----")
|
||||
self.logger.debug(json.dumps(payload))
|
||||
self.logger.debug("----- payload (end) -----")
|
||||
self.debug("----- payload (begining) -----")
|
||||
self.debug(json.dumps(payload))
|
||||
self.debug("----- payload (end) -----")
|
||||
|
||||
files = []
|
||||
file_keys = ['cerfa'] + ['plan_cadastral_%s' % i for i in range(1,5)]
|
||||
|
@ -237,9 +251,9 @@ class AtrealOpenads(BaseResource):
|
|||
'upload_file' : upload_file
|
||||
})
|
||||
|
||||
self.logger.debug("----- files (begining) -----")
|
||||
self.logger.debug(files)
|
||||
self.logger.debug("----- files (end) -----")
|
||||
self.debug("----- files (begining) -----")
|
||||
self.debug(files)
|
||||
self.debug("----- files (end) -----")
|
||||
|
||||
url = urlparse.urljoin(self.openADS_API_url, '/dossiers/%s' % type_dossier)
|
||||
response = self.requests.post(url, json=payload, auth=(self.openADS_API_login, self.openADS_API_password))
|
||||
|
@ -252,7 +266,7 @@ class AtrealOpenads(BaseResource):
|
|||
except ValueError:
|
||||
raise APIError('No JSON content returned: %r' % response.content[:1000])
|
||||
|
||||
self.logger.debug("----- response json (begining) -----")
|
||||
self.debug("----- response json (begining) -----")
|
||||
debug_json = copy.deepcopy(result)
|
||||
if 'files' in debug_json \
|
||||
and debug_json['files'] \
|
||||
|
@ -261,21 +275,21 @@ class AtrealOpenads(BaseResource):
|
|||
and isinstance(debug_json['files'][0], dict) \
|
||||
and 'b64_content' in debug_json['files'][0]:
|
||||
debug_json['files'][0]['b64_content'] = '<b64 content>'
|
||||
self.logger.debug(json.dumps(debug_json))
|
||||
self.logger.debug("----- response json (end) -----")
|
||||
self.debug(json.dumps(debug_json))
|
||||
self.debug("----- response json (end) -----")
|
||||
|
||||
numero_dossier = result.get('numero_dossier')
|
||||
self.logger.debug("Numéro dossier: %s", str(numero_dossier))
|
||||
self.debug("Numéro dossier: %s", str(numero_dossier))
|
||||
|
||||
recepisse = result['files'][0]
|
||||
try:
|
||||
recepisse_content = base64.b64decode(recepisse['b64_content'])
|
||||
except TypeError:
|
||||
raise APIError('Invalid content for recepisse')
|
||||
self.logger.debug("Successfully decoded recepisse from base64")
|
||||
self.debug("Successfully decoded recepisse from base64")
|
||||
|
||||
if recepisse['content_type'] and recepisse['content_type'] != 'application/pdf':
|
||||
self.logger.warning(
|
||||
self.debug(
|
||||
"Forcing 'recepisse' content type to '%s' instead of '%s'.",
|
||||
'application/pdf',
|
||||
recepisse['content_type']
|
||||
|
@ -294,7 +308,7 @@ class AtrealOpenads(BaseResource):
|
|||
FF.upload_file.save(FF.orig_filename, f['upload_file'])
|
||||
FF.upload_status = 'pending'
|
||||
FF.save()
|
||||
self.logger.debug(
|
||||
self.debug(
|
||||
"Created ForwardFile '%s' for file '%s' (%s)",
|
||||
FF.id,
|
||||
FF.orig_filename,
|
||||
|
@ -307,7 +321,7 @@ class AtrealOpenads(BaseResource):
|
|||
type_dossier=type_dossier,
|
||||
numero_dossier=numero_dossier,
|
||||
file_ids=file_ids)
|
||||
self.logger.debug(
|
||||
self.debug(
|
||||
"Added a job '%s' for dossier '%s' (%s) with file ids '%s'",
|
||||
job.id,
|
||||
numero_dossier,
|
||||
|
@ -377,6 +391,7 @@ class AtrealOpenads(BaseResource):
|
|||
def get_fwd_files_status(self, request, numero_dossier, fichier_id=None, summary=None, *args, **kwargs):
|
||||
payload = []
|
||||
fwd_files = []
|
||||
|
||||
if not fichier_id:
|
||||
try:
|
||||
fwd_files = ForwardFile.objects.filter(numero_dossier=numero_dossier)
|
||||
|
@ -389,6 +404,7 @@ class AtrealOpenads(BaseResource):
|
|||
raise Http404("No file matches 'numero_dossier=%s' and 'id=%s'." % (numero_dossier, fichier_id))
|
||||
if fwd_file:
|
||||
fwd_files.append(fwd_file)
|
||||
|
||||
if fwd_files:
|
||||
|
||||
summary_enabled = summary and len(summary) and summary == '1'
|
||||
|
@ -485,38 +501,40 @@ class AtrealOpenads(BaseResource):
|
|||
"""A Job to forward user uploaded files to openADS."""
|
||||
payload = []
|
||||
fwd_files = []
|
||||
|
||||
for fid in file_ids:
|
||||
self.logger.debug("upload_user_files() ForwardFile file_id: %s", fid)
|
||||
self.debug("upload_user_files() ForwardFile file_id: %s", fid)
|
||||
fwd_file = ForwardFile.objects.get(id=fid)
|
||||
if fwd_file:
|
||||
self.logger.debug("upload_user_files() got ForwardFile")
|
||||
self.debug("upload_user_files() got ForwardFile")
|
||||
payload.append({
|
||||
'filename' : fwd_file.orig_filename + ('.pdf' if fwd_file.orig_filename[-4:] != '.pdf' else ''),
|
||||
'content_type' : fwd_file.content_type,
|
||||
'b64_content' : base64.b64encode(fwd_file.upload_file.read()),
|
||||
'file_type' : fwd_file.type_fichier
|
||||
})
|
||||
self.logger.debug("upload_user_files() payload added")
|
||||
self.debug("upload_user_files() payload added")
|
||||
fwd_file.upload_status = 'uploading'
|
||||
if fwd_file.upload_msg and re.search(r'^attempt \d+$', fwd_file.upload_msg):
|
||||
self.logger.debug("upload_user_files() upload_msg: '%s'", fwd_file.upload_msg)
|
||||
self.debug("upload_user_files() upload_msg: '%s'", fwd_file.upload_msg)
|
||||
attempt_num = fwd_file.upload_msg.replace('attempt ', '').strip()
|
||||
self.logger.debug("upload_user_files() attempt_num: '%s'", attempt_num)
|
||||
self.debug("upload_user_files() attempt_num: '%s'", attempt_num)
|
||||
fwd_file.upload_msg = 'attempt %s' % attempt_num
|
||||
else:
|
||||
fwd_file.upload_msg = 'attempt 1'
|
||||
self.logger.debug("upload_user_files() ForwardFile ready to be saved")
|
||||
self.debug("upload_user_files() ForwardFile ready to be saved")
|
||||
fwd_file.save()
|
||||
fwd_files.append(fwd_file)
|
||||
else:
|
||||
self.logger.warning("upload_user_files() failed to find ForwardFile file_id: %s", fid);
|
||||
|
||||
if payload:
|
||||
self.logger.debug("upload_user_files() payload is not empty")
|
||||
self.debug("upload_user_files() payload is not empty")
|
||||
debug_payload = copy.deepcopy(payload)
|
||||
for p in debug_payload:
|
||||
if 'b64_content' in p:
|
||||
p['b64_content'] = '<b64 content>'
|
||||
self.logger.debug("upload_user_files() payload is: %s", str(debug_payload))
|
||||
self.debug("upload_user_files() payload is: %s", str(debug_payload))
|
||||
url = urlparse.urljoin(self.openADS_API_url, '/dossier/%s/%s/files' % (type_dossier, numero_dossier))
|
||||
response = self.requests.post(url, json=payload, auth=(self.openADS_API_login, self.openADS_API_password))
|
||||
if response.status_code // 100 != 2:
|
||||
|
@ -552,7 +570,7 @@ class AtrealOpenads(BaseResource):
|
|||
fpath = fwd_file.upload_file.path
|
||||
fwd_file.upload_file.delete()
|
||||
fwd_file.save()
|
||||
self.logger.debug(
|
||||
self.debug(
|
||||
"upload_user_files() flaging file '%s' has transfered (deleted '%s')",
|
||||
fwd_file.id,
|
||||
fpath
|
||||
|
|
Reference in New Issue