Projet

Général

Profil

0001-misc-replace-get_logger-.error-with-newer-record_err.patch

Paul Marillonnet, 12 juillet 2021 10:20

Télécharger (28,2 ko)

Voir les différences:

Subject: [PATCH] misc: replace get_logger().error() with newer record_error()
 (#55437)

 tests/form_pages/test_formdata.py |  7 ++++---
 tests/test_datasource.py          | 11 +++--------
 tests/test_fc_auth.py             |  6 ++++--
 wcs/backoffice/management.py      |  2 +-
 wcs/portfolio.py                  | 10 +++++++---
 wcs/qommon/emails.py              | 23 ++++++++++++-----------
 wcs/qommon/ident/franceconnect.py | 23 +++++++++++------------
 wcs/qommon/ident/idp.py           |  3 +--
 wcs/qommon/ident/password.py      |  6 +++---
 wcs/qommon/saml2.py               |  7 +++----
 wcs/root.py                       | 19 ++++++++++---------
 wcs/wf/export_to_model.py         |  7 ++++---
 wcs/wf/notification.py            | 10 +++++-----
 wcs/wf/profile.py                 |  4 ++--
 wcs/wf/register_comment.py        |  7 +++----
 wcs/wf/roles.py                   | 12 +++++++++---
 wcs/workflows.py                  | 17 +++++++++--------
 17 files changed, 91 insertions(+), 83 deletions(-)
tests/form_pages/test_formdata.py
761 761
        http_post_request.return_value = None, 400, 'null', None  # fail
762 762
        resp = resp.form.submit('button_export_to')
763 763
        assert http_post_request.call_count == 1
764
        assert caplog.records[-1].message.startswith(
765
            "file 'template.pdf' failed to be pushed to portfolio of 'Foo"
766
        )
764
        if pub.is_using_postgresql():
765
            assert pub.loggederror_class.select()[0].summary.startswith(
766
                "file 'template.pdf' failed to be pushed to portfolio of 'Foo"
767
            )
767 768

  
768 769
    # failed to push to portfolio, but document is here
769 770
    resp = resp.follow()  # $form/$id/create_doc
tests/test_datasource.py
141 141
    # invalid python expression
142 142
    datasource = {'type': 'formula', 'value': 'foobar', 'notify_on_errors': True, 'record_on_errors': True}
143 143
    assert data_sources.get_items(datasource) == []
144
    assert 'Failed to eval() Python data source' in emails.get_latest('subject')
144
    assert "Failed to eval() Python data source ('foobar')" in emails.get_latest('subject')
145 145
    if pub.is_using_postgresql():
146 146
        assert pub.loggederror_class.count() == 1
147 147
        logged_error = pub.loggederror_class.select()[0]
148 148
        assert logged_error.workflow_id is None
149
        assert (
150
            logged_error.summary == "[DATASOURCE] Exception: Failed to eval() Python data source ('foobar')"
151
        )
149
        assert "Failed to eval() Python data source ('foobar')" in logged_error.summary
152 150

  
153 151
    # expression not iterable
154 152
    datasource = {'type': 'formula', 'value': '2', 'notify_on_errors': True, 'record_on_errors': True}
......
158 156
        assert pub.loggederror_class.count() == 2
159 157
        logged_error = pub.loggederror_class.select()[1]
160 158
        assert logged_error.workflow_id is None
161
        assert (
162
            logged_error.summary
163
            == "[DATASOURCE] Exception: Python data source ('2') gave a non-iterable result"
164
        )
159
        assert "Python data source ('2')" in logged_error.summary
165 160

  
166 161

  
167 162
def test_python_datasource_with_evalutils(pub):
tests/test_fc_auth.py
194 194
            }
195 195
        )
196 196
    )
197
    assert 'user did not authorize login' in caplog.records[-1].message
197
    if pub.is_using_postgresql():
198
        assert 'user did not authorize login' in pub.loggederror_class.select()[-1].summary
198 199
    resp = app.get(
199 200
        '/ident/fc/callback?%s'
200 201
        % urllib.parse.urlencode(
......
204 205
            }
205 206
        )
206 207
    )
207
    assert 'whatever' in caplog.records[-1].message
208
    if pub.is_using_postgresql():
209
        assert 'whatever' in pub.loggederror_class.select()[-1].summary
208 210

  
209 211
    # Login existing user
210 212
    def logme(login_url):
wcs/backoffice/management.py
202 202
            try:
203 203
                sms_class.send(sender, [form.get_widget('sms').parse()], message)
204 204
            except errors.SMSError as e:
205
                get_logger().error(e)
205
                get_publisher().record_error(_('Could not send SMS'), formdata=formdata, exception=e)
206 206
            get_session().message = ('info', _('SMS with tracking code sent to the user'))
207 207
        else:
208 208
            # send mail
wcs/portfolio.py
62 62
def push_document(user, filename, stream):
63 63
    if not user:
64 64
        return
65
    charset = get_publisher().site_charset
65
    publisher = get_publisher()
66
    charset = publisher.site_charset
66 67
    payload = {}
67 68
    if user.name_identifiers:
68 69
        payload['user_nameid'] = force_text(user.name_identifiers[0], 'ascii')
69 70
    elif user.email:
70 71
        payload['user_email'] = force_text(user.email, 'ascii')
71
    payload['origin'] = urllib.parse.urlparse(get_publisher().get_frontoffice_url()).netloc
72
    payload['origin'] = urllib.parse.urlparse(publisher.get_frontoffice_url()).netloc
72 73
    payload['file_name'] = force_text(filename, charset)
73 74
    stream.seek(0)
74 75
    payload['file_b64_content'] = force_text(base64.b64encode(stream.read()))
......
80 81
        if status == 200:
81 82
            get_logger().info('file %r pushed to portfolio of %r', filename, user.display_name)
82 83
        else:
83
            get_logger().error('file %r failed to be pushed to portfolio of %r', filename, user.display_name)
84
            publisher.record_error(
85
                _('file %(filename)r failed to be pushed to portfolio of %(display_name)r')
86
                % {'filename': filename, 'display_name': user.display_name}
87
            )
84 88

  
85 89
    if get_response():
86 90
        get_response().add_after_job(
wcs/qommon/emails.py
43 43
from django.utils.safestring import mark_safe
44 44
from quixote import get_publisher, get_request, get_response
45 45

  
46
from . import errors, force_str
46
from . import _, errors, force_str
47 47
from .admin.emails import EmailsDirectory
48 48
from .publisher import get_cfg, get_logger
49 49
from .template import Template
......
350 350

  
351 351

  
352 352
def create_smtp_server(emails_cfg, smtp_timeout=None):
353
    publisher = get_publisher()
353 354
    try:
354 355
        s = smtplib.SMTP(emails_cfg.get('smtp_server', None) or 'localhost', timeout=smtp_timeout)
355
    except socket.timeout:
356
        get_logger().error('Failed to connect to SMTP server (timeout)')
356
    except socket.timeout as e:
357
        publisher.record_error(_('Failed to connect to SMTP server (timeout)'), exception=e)
357 358
        raise errors.EmailError('Failed to connect to SMTP server (timeout)')
358 359
    except OSError:
359 360
        # XXX: write message in a queue somewhere?
360
        get_logger().error('Failed to connect to SMTP server')
361
        publisher.record_error(_('Failed to connect to SMTP server'), exception=e)
361 362
        raise errors.EmailError('Failed to connect to SMTP server')
362 363
    if not s.sock:
363
        get_logger().error('Failed to connect to SMTP server')
364
        publisher.record_error(_('Failed to connect to SMTP server'))
364 365
        raise errors.EmailError('Failed to connect to SMTP server')
365 366
    rc_code, ehlo_answer = s.ehlo()
366 367
    if rc_code != 250:
367
        get_logger().error('Failed to EHLO to SMTP server (%s)', rc_code)
368
        publisher.record_error(_('Failed to EHLO to SMTP server (%s)') % rc_code)
368 369
        raise errors.EmailError('Failed to EHLO to SMTP server (%s)' % rc_code)
369 370
    if b'STARTTLS' in ehlo_answer:
370 371
        rc_code = s.starttls()[0]
371 372
        if rc_code != 220:
372
            get_logger().error('Failed to STARTTLS to SMTP server (%s)', rc_code)
373
            publisher.record_error(_('Failed to STARTTLS to SMTP server (%s)') % rc_code)
373 374
            raise errors.EmailError('Failed to STARTTLS to SMTP server (%s)' % rc_code)
374 375
    if emails_cfg.get('smtp_login'):
375 376
        try:
376 377
            s.login(emails_cfg.get('smtp_login') or '', emails_cfg.get('smtp_password') or '')
377
        except smtplib.SMTPAuthenticationError:
378
            get_logger().error('Failed to authenticate to SMTP server')
378
        except smtplib.SMTPAuthenticationError as e:
379
            publisher.record_error(_('Failed to authenticate to SMTP server'), exception=e)
379 380
            raise errors.EmailError('Failed to authenticate to SMTP server')
380
        except smtplib.SMTPException:
381
            get_logger().error('Failed to authenticate to SMTP server, unknown error.')
381
        except smtplib.SMTPException as e:
382
            publisher.record_error(_('Failed to authenticate to SMTP server, unknown error.'), exception=e)
382 383
            raise errors.EmailError('Failed to authenticate to SMTP server, unknown error.')
383 384
    return s
384 385

  
wcs/qommon/ident/franceconnect.py
29 29
from wcs.formdata import flatten_dict
30 30
from wcs.workflows import WorkflowStatusItem
31 31

  
32
from .. import _, get_cfg, get_logger, template
32
from .. import _, get_cfg, template
33 33
from ..backoffice.menu import html_top
34 34
from ..form import (
35 35
    CompositeWidget,
......
325 325
        return False
326 326

  
327 327
    def get_access_token(self, code):
328
        logger = get_logger()
328
        publisher = get_publisher()
329 329
        session = get_session()
330 330
        fc_cfg = get_cfg('fc', {})
331 331
        client_id = fc_cfg.get('client_id')
......
346 346
            },
347 347
        )
348 348
        if status != 200:
349
            logger.error('status from FranceConnect token_url is not 200')
349
            publisher.record_error(_('Status from FranceConnect token_url is not 200'))
350 350
            return None
351 351
        result = json_loads(data)
352 352
        if 'error' in result:
353
            logger.error('FranceConnect code resolution failed: %s', result['error'])
353
            publisher.record_error(_('FranceConnect code resolution failed: %s') % result['error'])
354 354
            return None
355 355
        # check id_token nonce
356 356
        id_token = result['id_token']
......
359 359
        payload = json_loads(base64url_decode(force_bytes(payload)))
360 360
        nonce = hashlib.sha256(force_bytes(session.id)).hexdigest()
361 361
        if payload['nonce'] != nonce:
362
            logger.error('FranceConnect returned nonce did not match')
362
            publisher.record_error(_('FranceConnect returned nonce did not match'))
363 363
            return None
364 364
        return access_token, id_token
365 365

  
366 366
    def get_user_info(self, access_token):
367
        logger = get_logger()
367
        publisher = get_publisher()
368 368
        dummy, status, data, dummy = http_get_page(
369 369
            self.get_user_info_url(),
370 370
            headers={
......
372 372
            },
373 373
        )
374 374
        if status != 200:
375
            logger.error(
376
                'status from FranceConnect user_info_url is not 200 but %s and data is' ' %s',
377
                status.data[:100],
375
            publisher.record_error(
376
                _('Status from FranceConnect user_info_url is not 200 but %(status)s and data is %(data)s')
377
                % {'status': status, 'data': data[:100]}
378 378
            )
379 379
            return None
380 380
        return json_loads(data)
......
453 453
        pub = get_publisher()
454 454
        request = get_request()
455 455
        session = get_session()
456
        logger = get_logger()
457 456
        state = request.form.get('state', '')
458 457
        next_url = (session.extra_user_variables or {}).pop(
459 458
            'fc_next_url_' + state, ''
......
465 464
            if error:
466 465
                # we log only errors whose user is not responsible
467 466
                msg = self.AUTHORIZATION_REQUEST_ERRORS.get(error)
468
                logger.error(_('FranceConnect authentication failed: %s'), msg if msg else error)
467
                pub.record_error(_('FranceConnect authentication failed: %s') % msg if msg else error)
469 468
            return redirect(next_url)
470 469
        access_token, id_token = self.get_access_token(request.form['code'])
471 470
        if not access_token:
......
494 493

  
495 494
        if not (user.name and user.email):
496 495
            # we didn't get useful attributes, forget it.
497
            logger.error('failed to get name and/or email attribute from FranceConnect')
496
            pub.record_error(_('Failed to get name and/or email attribute from FranceConnect'))
498 497
            return redirect(next_url)
499 498

  
500 499
        user.store()
wcs/qommon/ident/idp.py
29 29
from quixote.directory import Directory
30 30
from quixote.html import TemplateIO, htmltext
31 31

  
32
from .. import _, errors, get_cfg, get_logger, misc, saml2utils, template, x509utils
32
from .. import _, errors, get_cfg, misc, saml2utils, template, x509utils
33 33
from ..admin.menu import command_icon
34 34
from ..backoffice.menu import html_top
35 35
from ..form import (
......
84 84
        idps = get_cfg('idp', {})
85 85

  
86 86
        if not lasso:
87
            get_logger().error('/login unavailable - lasso is not installed')
88 87
            raise Exception("lasso is missing, idp method cannot be used")
89 88

  
90 89
        if len(idps) == 0:
wcs/qommon/ident/password.py
23 23

  
24 24
from wcs.qommon.admin.texts import TextsDirectory
25 25

  
26
from .. import _, emails, errors, get_cfg, get_logger, misc, ngettext
26
from .. import _, emails, errors, get_cfg, misc, ngettext
27 27
from .. import storage as st
28 28
from .. import template, tokens
29 29
from ..admin.emails import EmailsDirectory
......
606 606

  
607 607
        if identities_cfg.get('email-confirmation', False):
608 608
            if not user.email:
609
                get_logger().error(
609
                get_publisher().record_error(
610 610
                    _(
611 611
                        'Accounts are configured to require confirmation but accounts can be created without emails'
612 612
                    )
......
624 624

  
625 625
        if passwords_cfg.get('generate', True):
626 626
            if not user.email:
627
                get_logger().error(
627
                get_publisher().record_error(
628 628
                    _(
629 629
                        'Accounts are configured to have a generated password '
630 630
                        'but accounts can be created without emails'
wcs/qommon/saml2.py
80 80
        try:
81 81
            return method(*args, **kwargs)
82 82
        except Exception as e:
83
            get_logger().error('Exception in method %r: %s; returning a SOAP error' % (method, e))
83
            get_publisher().record_error(
84
                _('Exception in method %r: returning a SOAP error') % method, exception=e
85
            )
84 86
            fault = lasso.SoapFault.newFull('Internal Server Error', str(e))
85 87
            body = lasso.SoapBody()
86 88
            body.any = [fault]
......
126 128
    def _q_traverse(self, path):
127 129
        # if lasso is not installed, hide the saml endpoints
128 130
        if lasso is None:
129
            if does_idp_authentication():
130
                rel_path = os.path.join('/saml', *path)
131
                get_logger().error('%s unavailable - lasso is not installed' % rel_path)
132 131
            raise errors.TraversalError()
133 132
        return Directory._q_traverse(self, path)
134 133

  
wcs/root.py
82 82
            method = ident_methods[0]
83 83
            try:
84 84
                return ident.login(method)
85
            except KeyError:
86
                msg = 'failed to login with method %s' % method
87
                get_logger().error(msg)
85
            except KeyError as e:
86
                msg = _('Failed to login with method %s') % method
87
                get_publisher().record_error(msg, exception=e)
88 88
                return errors.TraversalError(msg)
89 89
        else:
90 90
            form = Form(enctype='multipart/form-data')
......
108 108
                else:
109 109
                    try:
110 110
                        return ident.login(method)
111
                    except KeyError:
112
                        msg = 'failed to login with method %s' % method
113
                        get_logger().error(msg)
111
                    except KeyError as e:
112
                        get_publisher().record_error(
113
                            _('Failed to login with method %s') % method, exception=e
114
                        )
114 115
                        return errors.TraversalError()
115 116
            else:
116 117
                template.html_top(_('Login'))
......
148 149
            method = ident_methods[0]
149 150
            try:
150 151
                return ident.register(method)
151
            except KeyError:
152
                get_logger().error('failed to register with method %s' % method)
152
            except KeyError as e:
153
                get_publisher().record_error(_('Failed to register with method %s') % method, exception=e)
153 154
                return errors.TraversalError()
154 155
        else:
155 156
            form = Form(enctype='multipart/form-data')
......
332 333
                        }
333 334
                    )
334 335
                except UploadStorageError as e:
335
                    get_logger().error('upload storage error: %s' % e)
336
                    get_publisher().record_error(_('Upload storage error'), exception=e)
336 337
                    results.append({'error': _('failed to store file (system error)')})
337 338

  
338 339
        get_response().set_content_type('application/json')
wcs/wf/export_to_model.py
45 45
    template_on_formdata,
46 46
)
47 47

  
48
from ..qommon import _, ezt, force_str, get_logger, misc
48
from ..qommon import _, ezt, force_str, misc
49 49
from ..qommon.form import (
50 50
    CheckboxWidget,
51 51
    ComputedExpressionWidget,
......
509 509
                )
510 510
            )
511 511
        except TemplateError as e:
512
            url = formdata.get_url()
513
            get_logger().error('error in template for export to model [%s]: %s' % (url, str(e)))
512
            get_publisher().record_error(
513
                _('Error in template for export to model'), formdata=formdata, exception=e
514
            )
514 515
            raise TemplatingError(_('Error in template: %s') % str(e))
515 516

  
516 517
    def apply_od_template_to_formdata(self, formdata):
wcs/wf/notification.py
20 20

  
21 21
from wcs.workflows import WorkflowStatusItem, register_item_class, template_on_formdata
22 22

  
23
from ..qommon import _, get_logger
23
from ..qommon import _
24 24
from ..qommon.form import ComputedExpressionWidget, SingleSelectWidget, StringWidget, TextWidget, WidgetList
25 25
from ..qommon.template import TemplateError
26 26
from .wscall import WebserviceCallStatusItem
......
124 124
        try:
125 125
            title = template_on_formdata(formdata, self.compute(self.title, render=False), autoescape=False)
126 126
        except TemplateError as e:
127
            get_logger().error(
128
                'error in template for notification title, ' 'mail could not be generated: %s' % str(e)
127
            get_publisher().record_error(
128
                _('error in template for notification title, mail could not be generated'), exception=e
129 129
            )
130 130
            return
131 131

  
132 132
        try:
133 133
            body = template_on_formdata(formdata, self.compute(self.body, render=False), autoescape=False)
134 134
        except TemplateError as e:
135
            get_logger().error(
136
                'error in template for notification body, ' 'mail could not be generated: %s' % str(e)
135
            get_publisher().record_error(
136
                _('error in template for notification body, mail could not be generated'), exception=e
137 137
            )
138 138
            return
139 139

  
wcs/wf/profile.py
30 30
from ..qommon.form import CompositeWidget, ComputedExpressionWidget, SingleSelectWidget, WidgetListAsTable
31 31
from ..qommon.ident.idp import is_idp_managing_user_attributes
32 32
from ..qommon.misc import JSONEncoder, http_patch_request
33
from ..qommon.publisher import get_cfg, get_logger
33
from ..qommon.publisher import get_cfg
34 34

  
35 35

  
36 36
def user_ws_url(user_uuid):
......
208 208
                url, payload, headers={'Content-type': 'application/json'}
209 209
            )
210 210
            if status != 200:
211
                get_logger().error('failed to update profile for user %r', user)
211
                get_publisher().record_error(_('Failed to update profile for user %r') % user)
212 212

  
213 213
        if get_request():
214 214
            get_response().add_after_job(_('Updating user profile'), after_job)
wcs/wf/register_comment.py
27 27
    template_on_formdata,
28 28
)
29 29

  
30
from ..qommon import _, ezt, get_logger
30
from ..qommon import _, ezt
31 31
from ..qommon.form import SingleSelectWidget, TextWidget, WidgetList
32 32
from ..qommon.template import TemplateError
33 33

  
......
141 141
            formdata.evolution[-1].add_part(JournalEvolutionPart(formdata, self.comment, self.to))
142 142
            formdata.store()
143 143
        except TemplateError as e:
144
            url = formdata.get_url()
145
            get_logger().error(
146
                'error in template for comment [%s], ' 'comment could not be generated: %s' % (url, str(e))
144
            get_publisher().record_error(
145
                _('Error in template, comment could not be generated'), formdata=formdata, exception=e
147 146
            )
148 147

  
149 148

  
wcs/wf/roles.py
27 27
from ..qommon.form import SingleSelectWidgetWithOther
28 28
from ..qommon.ident.idp import is_idp_managing_user_attributes
29 29
from ..qommon.misc import http_delete_request, http_post_request
30
from ..qommon.publisher import get_cfg, get_logger
30
from ..qommon.publisher import get_cfg
31 31

  
32 32

  
33 33
def roles_ws_url(role_uuid, user_uuid):
......
113 113
            signed_url = sign_ws_url(url)
114 114
            dummy, status, dummy, dummy = http_post_request(signed_url)
115 115
            if status != 201:
116
                get_logger().error('failed to add role %r to user %r', role, user)
116
                get_publisher().record_error(
117
                    _('Failed to add role %(role)r to user %(user)r') % {'role': role, 'user': user},
118
                    formdata=formdata,
119
                )
117 120

  
118 121
        if get_request():
119 122
            get_response().add_after_job(_('Adding role'), after_job)
......
184 187
            # pylint: disable=unused-variable
185 188
            response, status, data, auth_header = http_delete_request(signed_url)
186 189
            if status != 200:
187
                get_logger().error('failed to remove role %r from user %r', role, user)
190
                get_publisher().record_error(
191
                    _('Failed to remove role %(role)r from user %(user)r') % {'role': role, 'user': user},
192
                    formdata=formdata,
193
                )
188 194

  
189 195
        if get_request():
190 196
            get_response().add_after_job(_('Removing role'), after_job)
wcs/workflows.py
36 36
from .formdata import Evolution
37 37
from .formdef import FormDef, FormdefImportError
38 38
from .mail_templates import MailTemplate
39
from .qommon import _, emails, errors, ezt, force_str, get_cfg, get_logger, misc
39
from .qommon import _, emails, errors, ezt, force_str, get_cfg, misc
40 40
from .qommon.form import (
41 41
    CheckboxWidget,
42 42
    ComputedExpressionWidget,
......
2940 2940
        if not (self.subject and self.body) and not self.mail_template:
2941 2941
            return
2942 2942

  
2943
        url = formdata.get_url()
2944 2943
        body = self.body
2945 2944
        subject = self.subject
2946 2945
        extra_attachments = None
......
2963 2962
                formdata, self.compute(body, render=False), autoescape=body.startswith('<')
2964 2963
            )
2965 2964
        except TemplateError as e:
2966
            get_logger().error(
2967
                'error in template for email body [%s], ' 'mail could not be generated: %s' % (url, str(e))
2965
            get_publisher().record_error(
2966
                _('Error in body template, mail could not be generated'), formdata=formdata, exception=e
2968 2967
            )
2969 2968
            return
2970 2969

  
......
2973 2972
                formdata, self.compute(subject, render=False), autoescape=False
2974 2973
            )
2975 2974
        except TemplateError as e:
2976
            get_logger().error(
2977
                'error in template for email subject [%s], ' 'mail could not be generated: %s' % (url, str(e))
2975
            get_publisher().record_error(
2976
                _('Error in subject template, mail could not be generated'), formdata=formdata, exception=e
2978 2977
            )
2979 2978
            return
2980 2979

  
......
3147 3146
        try:
3148 3147
            sms_body = template_on_formdata(formdata, self.compute(self.body, render=False))
3149 3148
        except TemplateError as e:
3150
            get_logger().error('error in template for sms [%s], sms could not be generated' % str(e))
3149
            get_publisher().record_error(
3150
                _('Error in template, sms could not be generated'), formdata=formdata, exception=e
3151
            )
3151 3152
            return
3152 3153

  
3153 3154
        from .qommon import sms
......
3157 3158
        try:
3158 3159
            sms.SMS.get_sms_class().send(sender, destinations, sms_body)
3159 3160
        except errors.SMSError as e:
3160
            get_logger().error(e)
3161
            get_publisher().record_error(_('Could not send SMS'), formdata=formdata, exception=e)
3161 3162

  
3162 3163

  
3163 3164
register_item_class(SendSMSWorkflowStatusItem)
3164
-