Refactoring: remove 'extra_debug' field and replaced all 'self.debug' calls by 'self.logger.debug'

This commit is contained in:
Michael Bideau 2019-07-18 22:54:50 +02:00
parent 02f4a071fc
commit 62dae769d6
2 changed files with 20 additions and 29 deletions

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.15 on 2019-07-18 15:52
# Generated by Django 1.11.15 on 2019-07-18 20:51
from __future__ import unicode_literals
import atreal_openads.models
@ -30,7 +30,6 @@ class Migration(migrations.Migration):
('http_proxy', models.CharField(blank=True, max_length=128, verbose_name='HTTP and HTTPS proxy')),
('collectivite', models.CharField(blank=True, default=b'', help_text='ex: Marseille, or ex: 3', max_length=255, verbose_name='Collectivity (identifier)')),
('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={

View File

@ -192,8 +192,6 @@ class AtrealOpenads(BaseResource, HTTPResource):
help_text=_('ex: Marseille, or ex: 3'), default='', blank=True)
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)
openADS_API_timeout = 3600
@ -211,25 +209,19 @@ class AtrealOpenads(BaseResource, HTTPResource):
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 log_json_payload(self, payload, title='payload', paths_to_replace=[]):
"""Log a json paylod surrounded by dashes and with file content filtered."""
self.debug("----- %s (begining) -----", title)
self.logger.debug("----- %s (begining) -----", title)
if paths_to_replace:
self.debug("%s", LogJsonPayloadWithFileContent(
self.logger.debug("%s", LogJsonPayloadWithFileContent(
payload,
paths_to_replace = paths_to_replace,
path_separator = '.',
replace_by = '<b64 content>'
))
else:
self.debug("%s", LogJsonPayloadWithFileContent(payload))
self.debug("----- %s (end) -----", title)
self.logger.debug("%s", LogJsonPayloadWithFileContent(payload))
self.logger.debug("----- %s (end) -----", title)
def get_files_from_json_payload(self, payload, title='payload'):
@ -455,9 +447,9 @@ class AtrealOpenads(BaseResource, HTTPResource):
})
# log files to be forwarded
self.debug("----- files (begining) -----")
self.debug(files)
self.debug("----- files (end) -----")
self.logger.debug("----- files (begining) -----")
self.logger.debug(files)
self.logger.debug("----- files (end) -----")
# make a request to openADS.API (with the payload)
url = urlparse.urljoin(self.openADS_API_url, '/dossiers/%s' % type_dossier)
@ -488,7 +480,7 @@ class AtrealOpenads(BaseResource, HTTPResource):
and recepisse['content_type']
and recepisse['content_type'] != 'application/pdf'
):
self.debug(
self.logger.debug(
"Forcing 'recepisse' content type to '%s' instead of '%s'.",
'application/pdf',
recepisse['content_type']
@ -500,7 +492,7 @@ class AtrealOpenads(BaseResource, HTTPResource):
recepisse_content = base64.b64decode(recepisse['b64_content'])
except TypeError:
raise APIError('Failed to decode recepisse content from base 64')
self.debug("Successfully decoded recepisse from base 64")
self.logger.debug("Successfully decoded recepisse from base 64")
# check/get the 'numero_dossier'
if 'numero_dossier' not in result:
@ -513,7 +505,7 @@ class AtrealOpenads(BaseResource, HTTPResource):
"Expecting '%s' value in JSON response to be a %s (not a %s)" %
('numero_dossier', 'string', str(type(numero_dossier))))
self.debug("Numéro dossier: %s", str(numero_dossier))
self.logger.debug("Numéro dossier: %s", str(numero_dossier))
# save files to be forwarded to openADS.API
if files:
@ -528,7 +520,7 @@ class AtrealOpenads(BaseResource, HTTPResource):
FF.upload_file.save(FF.orig_filename, f['upload_file'])
FF.upload_status = 'pending'
FF.save()
self.debug(
self.logger.debug(
"Created ForwardFile '%s' for file '%s' (%s)",
FF.id,
FF.orig_filename,
@ -541,7 +533,7 @@ class AtrealOpenads(BaseResource, HTTPResource):
type_dossier=type_dossier,
numero_dossier=numero_dossier,
file_ids=file_ids)
self.debug(
self.logger.debug(
"Added a job '%s' for dossier '%s' (%s) with file ids '%s'",
job.id,
numero_dossier,
@ -810,14 +802,14 @@ class AtrealOpenads(BaseResource, HTTPResource):
# for every file ids specified (in parameters of this job)
for fid in file_ids:
self.debug("upload_user_files() ForwardFile file_id: %s", fid)
self.logger.debug("upload_user_files() ForwardFile file_id: %s", fid)
# get the matching forward file
fwd_file = ForwardFile.objects.get(id=fid)
# found one
if fwd_file:
self.debug("upload_user_files() got ForwardFile")
self.logger.debug("upload_user_files() got ForwardFile")
# add the file content and data to the payload
payload.append({
@ -826,15 +818,15 @@ class AtrealOpenads(BaseResource, HTTPResource):
'b64_content' : base64.b64encode(fwd_file.upload_file.read()),
'file_type' : fwd_file.type_fichier
})
self.debug("upload_user_files() payload added")
self.logger.debug("upload_user_files() payload added")
# update the file upload data (status and attempts)
fwd_file.upload_status = 'uploading'
fwd_file.upload_attempt += 1
fwd_file.upload_msg = 'attempt %s' % fwd_file.upload_attempt
self.debug("upload_user_files() upload_msg: '%s'", fwd_file.upload_msg)
self.logger.debug("upload_user_files() upload_msg: '%s'", fwd_file.upload_msg)
fwd_file.save()
self.debug("upload_user_files() ForwardFile saved")
self.logger.debug("upload_user_files() ForwardFile saved")
# append the forwarded file to the list
fwd_files.append(fwd_file)
@ -846,7 +838,7 @@ class AtrealOpenads(BaseResource, HTTPResource):
# if files need to be forwarded
if payload:
self.debug("upload_user_files() payload is not empty")
self.logger.debug("upload_user_files() payload is not empty")
# log the payload
self.log_json_payload(
@ -919,7 +911,7 @@ class AtrealOpenads(BaseResource, HTTPResource):
fwd_file.save()
# log the success message
self.debug(
self.logger.debug(
"upload_user_files() flaging file '%s' has transfered (deleted '%s')",
fwd_file.id,
fpath