trivial: apply black

This commit is contained in:
Frédéric Péters 2021-03-14 18:17:40 +01:00
parent 02f92afd2d
commit 5481a73269
4 changed files with 71 additions and 58 deletions

View File

@ -22,12 +22,15 @@ class Plugin(object):
def get_apps(self):
return [__name__]
class AppConfig(django.apps.AppConfig):
name = __name__
verbose_name = _('Nanterre extension')
def get_after_urls(self):
from . import urls
return urls.urlpatterns
default_app_config = __name__ + '.AppConfig'

View File

@ -16,16 +16,23 @@
from django.conf.urls import url
from .views import (saga_transaction, saga_retour_asynchrone,
saga_retour_synchrone, qf_carte_famille)
from .views import saga_transaction, saga_retour_asynchrone, saga_retour_synchrone, qf_carte_famille
urlpatterns = [
url('^_plugin/nanterre/saga-transaction/*$', saga_transaction,
name='nanterre-saga-transaction'),
url('^_plugin/nanterre/saga-retour-asynchrone/*$', saga_retour_asynchrone,
name='nanterre-saga-retour-asynchrone'),
url('^_plugin/nanterre/saga-retour-synchrone/*$', saga_retour_synchrone,
name='nanterre-saga-retour-synchrone'),
url('^_plugin/nanterre/qf-carte-famille/(?P<qf_id>\w+)/$', qf_carte_famille,
name='nanterre-qf-carte-famille'),
url('^_plugin/nanterre/saga-transaction/*$', saga_transaction, name='nanterre-saga-transaction'),
url(
'^_plugin/nanterre/saga-retour-asynchrone/*$',
saga_retour_asynchrone,
name='nanterre-saga-retour-asynchrone',
),
url(
'^_plugin/nanterre/saga-retour-synchrone/*$',
saga_retour_synchrone,
name='nanterre-saga-retour-synchrone',
),
url(
'^_plugin/nanterre/qf-carte-famille/(?P<qf_id>\w+)/$',
qf_carte_famille,
name='nanterre-qf-carte-famille',
),
]

View File

@ -29,8 +29,7 @@ from django.views.decorators.csrf import csrf_exempt
from combo.utils import requests, get_templated_url
ERROR_MESSAGE = ("Le système de paiement n'est pas disponible, "
"veuillez essayer ultérieurement.")
ERROR_MESSAGE = "Le système de paiement n'est pas disponible, " "veuillez essayer ultérieurement."
MESSAGE_BY_STATE = {
'paye': (messages.SUCCESS, "Le paiement a bien été effectué."),
'abandon': (messages.WARNING, "Le paiement a été annulé."),
@ -57,10 +56,8 @@ def saga_transaction(request):
if 'saga_retour_synchrone' in request.session:
del request.session['saga_retour_synchrone']
urlretour_asynchrone = request.build_absolute_uri(
reverse('nanterre-saga-retour-asynchrone')).rstrip('/')
urlretour_synchrone = request.build_absolute_uri(
reverse('nanterre-saga-retour-synchrone')).rstrip('/')
urlretour_asynchrone = request.build_absolute_uri(reverse('nanterre-saga-retour-asynchrone')).rstrip('/')
urlretour_synchrone = request.build_absolute_uri(reverse('nanterre-saga-retour-synchrone')).rstrip('/')
payload = {
'num_factures': num_factures,
@ -71,40 +68,49 @@ def saga_transaction(request):
try:
saga = rsu_post(request, 'saga/[user_nameid]/transaction/', payload)
except:
logger.exception('[rsu/saga] failed to create transaction '
'for num_factures=%s', num_factures)
logger.exception('[rsu/saga] failed to create transaction ' 'for num_factures=%s', num_factures)
messages.error(request, ERROR_MESSAGE)
return HttpResponseRedirect(error_url)
if not isinstance(saga, dict):
logger.error('[rsu/saga] failed to create transaction '
'for num_factures=%s, received bad response=%r',
num_factures, saga)
logger.error(
'[rsu/saga] failed to create transaction ' 'for num_factures=%s, received bad response=%r',
num_factures,
saga,
)
messages.error(request, ERROR_MESSAGE)
return HttpResponseRedirect(error_url)
if saga.get('errors'):
logger.warning('[rsu/saga] failed to create transaction '
'for num_factures=%s, errors=%r',
num_factures, saga['errors'])
logger.warning(
'[rsu/saga] failed to create transaction ' 'for num_factures=%s, errors=%r',
num_factures,
saga['errors'],
)
for error in saga['errors']:
messages.error(request, error)
return HttpResponseRedirect(error_url)
if saga.get('err') != 0:
logger.warning('[rsu/saga] failed to create transaction '
'for num_factures=%s, unknown error, code=%r',
num_factures, saga['err'])
logger.warning(
'[rsu/saga] failed to create transaction ' 'for num_factures=%s, unknown error, code=%r',
num_factures,
saga['err'],
)
messages.error(request, ERROR_MESSAGE)
return HttpResponseRedirect(error_url)
if not saga.get('data', {}).get('url'):
logger.error('[rsu/saga] failed to create transaction '
'for num_factures=%s, response without url: %r',
num_factures, saga)
logger.error(
'[rsu/saga] failed to create transaction ' 'for num_factures=%s, response without url: %r',
num_factures,
saga,
)
messages.error(request, ERROR_MESSAGE)
return HttpResponseRedirect(error_url)
# finally, response seems good! redirect to payment system URL
logger.info('[rsu/saga] new transaction created '
'for num_factures=%s, redirect to %s',
num_factures, saga['data']['url'])
logger.info(
'[rsu/saga] new transaction created ' 'for num_factures=%s, redirect to %s',
num_factures,
saga['data']['url'],
)
return HttpResponseRedirect(saga['data']['url'])
@ -123,8 +129,7 @@ def saga_retour_synchrone(request):
return HttpResponseRedirect(next_url)
# add a result message and redirect
if (isinstance(saga, dict) and saga.get('err') == 0 and
saga.get('data', {}).get('etat')):
if isinstance(saga, dict) and saga.get('err') == 0 and saga.get('data', {}).get('etat'):
etat = saga['data']['etat']
if etat == 'paye':
logger.info('[rsu/saga] retour-synchrone: idop=%s etat=%s', idop, etat)
@ -137,12 +142,10 @@ def saga_retour_synchrone(request):
logger.info('[rsu/saga] retour-synchrone: idop=%s etat=%s', idop, etat)
messages.add_message(request, *MESSAGE_BY_STATE[etat])
else:
logger.error('[rsu/saga] retour-synchrone: idop=%s '
'receive unknown etat=%s', idop, etat)
logger.error('[rsu/saga] retour-synchrone: idop=%s ' 'receive unknown etat=%s', idop, etat)
messages.error(request, ERROR_MESSAGE)
else:
logger.error('[rsu/saga] retour-synchrone: idop=%s '
'receive bad response=%r', idop, saga)
logger.error('[rsu/saga] retour-synchrone: idop=%s ' 'receive bad response=%r', idop, saga)
messages.error(request, ERROR_MESSAGE)
return HttpResponseRedirect(next_url)
@ -162,20 +165,16 @@ def saga_retour_asynchrone(request):
err = 1
logger.error('[rsu/saga] retour-asynchrone: cannot post idop=%s', idop)
else:
if (isinstance(saga, dict) and saga.get('err') == 0 and
saga.get('data', {}).get('etat')):
if isinstance(saga, dict) and saga.get('err') == 0 and saga.get('data', {}).get('etat'):
etat = saga['data']['etat']
if etat in MESSAGE_BY_STATE:
logger.info('[rsu/saga] retour-asynchrone: idop=%s etat=%s',
idop, etat)
logger.info('[rsu/saga] retour-asynchrone: idop=%s etat=%s', idop, etat)
else:
err = 1
logger.error('[rsu/saga] retour-asynchrone: idop=%s '
'receive unknown etat=%s', idop, etat)
logger.error('[rsu/saga] retour-asynchrone: idop=%s ' 'receive unknown etat=%s', idop, etat)
else:
err = 1
logger.error('[rsu/saga] retour-asynchrone: idop=%s '
'receive bad response=%r', idop, saga)
logger.error('[rsu/saga] retour-asynchrone: idop=%s ' 'receive bad response=%r', idop, saga)
response = HttpResponse(content_type='application/json')
response.write(json.dumps({'err': err}))
return response
@ -209,8 +208,7 @@ def qf_carte_famille(request, qf_id):
logger.warning('fail to get PDF on %s, got JSON: %r', url, carte.content)
raise Http404
if content_type != 'application/pdf':
logger.warning('fail to get PDF on %s, got %s: %r', url, content_type,
carte.content[200:])
logger.warning('fail to get PDF on %s, got %s: %r', url, content_type, carte.content[200:])
raise Http404
filename = 'carte-famille-%s.pdf' % qf_id
logger.debug('return %s obtained from %s', filename, url)

View File

@ -13,6 +13,7 @@ from distutils.command.sdist import sdist
from distutils.cmd import Command
from setuptools import setup, find_packages
class eo_sdist(sdist):
def run(self):
if os.path.exists('VERSION'):
@ -24,26 +25,28 @@ class eo_sdist(sdist):
if os.path.exists('VERSION'):
os.remove('VERSION')
def get_version():
if os.path.exists('VERSION'):
with open('VERSION', 'r') as v:
return v.read()
if os.path.exists('.git'):
p = subprocess.Popen(['git','describe','--dirty=.dirty','--match=v*'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p = subprocess.Popen(
['git', 'describe', '--dirty=.dirty', '--match=v*'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
result = p.communicate()[0]
if p.returncode == 0:
result = result.decode('ascii').strip()[1:] # strip spaces/newlines and initial v
if '-' in result: # not a tagged version
result = result.decode('ascii').strip()[1:] # strip spaces/newlines and initial v
if '-' in result: # not a tagged version
real_number, commit_count, commit_hash = result.split('-', 2)
version = '%s.post%s+%s' % (real_number, commit_count, commit_hash)
else:
version = result
return version
else:
return '0.0.post%s' % len(
subprocess.check_output(
['git', 'rev-list', 'HEAD']).splitlines())
return '0.0.post%s' % len(subprocess.check_output(['git', 'rev-list', 'HEAD']).splitlines())
return '0.0'
@ -60,6 +63,7 @@ class compile_translations(Command):
def run(self):
try:
from django.core.management import call_command
for path, dirs, files in os.walk('combo_plugin_nanterre'):
if 'locale' not in dirs:
continue
@ -100,7 +104,8 @@ setup(
'Programming Language :: Python',
'Programming Language :: Python :: 2',
],
install_requires=['django>=1.8, <1.12',
install_requires=[
'django>=1.8, <1.12',
],
zip_safe=False,
entry_points={
@ -111,5 +116,5 @@ setup(
'compile_translations': compile_translations,
'install_lib': install_lib,
'sdist': eo_sdist,
}
},
)