Projet

Général

Profil

0001-emails-allow-configuring-known-valid-domains-for-typ.patch

Frédéric Péters, 03 juillet 2020 08:05

Télécharger (6,71 ko)

Voir les différences:

Subject: [PATCH] emails: allow configuring known/valid domains for typo fixes
 (#44663)

 tests/test_admin_pages.py            |  6 ++++++
 tests/test_widgets.py                |  5 ++++-
 wcs/qommon/admin/emails.py           | 13 ++++++++++++-
 wcs/qommon/form.py                   |  7 ++++++-
 wcs/qommon/publisher.py              | 16 ++++++++++++++++
 wcs/qommon/static/js/qommon.forms.js |  6 ++----
 6 files changed, 46 insertions(+), 7 deletions(-)
tests/test_admin_pages.py
4897 4897
    resp = app.get('/backoffice/settings/emails/')
4898 4898
    resp = resp.click('General Options')
4899 4899
    assert 'Warning: all emails are sent to <foo@example.net>' in resp.text
4900
    resp.form['from'] = 'test@localhost'
4901
    resp = resp.form.submit('submit')
4902
    pub.reload_cfg()
4903
    assert pub.cfg['emails']['from'] == 'test@localhost'
4904
    assert pub.cfg['emails']['well_known_domains']
4905
    assert pub.cfg['emails']['valid_known_domains']
4900 4906

  
4901 4907
    pub.cfg['debug'] = {}
4902 4908
    pub.write_cfg()
tests/test_widgets.py
5 5
import shutil
6 6
import copy
7 7

  
8
from quixote import cleanup
8
from quixote import cleanup, get_response
9 9
from quixote.http_request import parse_query
10 10
import mechanize
11 11

  
......
286 286

  
287 287

  
288 288
def test_emailwidget():
289
    pub.cfg = {'emails': {'check_domain_with_dns': True}}
290
    get_response().javascript_code_parts = []
289 291
    widget = EmailWidget('test')
290 292
    form = MockHtmlForm(widget)
291 293
    assert 'name="test"' in form.as_html
294
    assert 'WCS_WELL_KNOWN_DOMAINS' in ''.join(get_response().javascript_code_parts)
292 295
    req.form = {}
293 296
    assert widget.parse() is None
294 297

  
wcs/qommon/admin/emails.py
116 116
                title = _('Check DNS for domain name'),
117 117
                value = emails.get('check_domain_with_dns', True),
118 118
                hint = _('Use a DNS request to check domain names used in email fields'))
119
        form.add(WidgetList, 'well_known_domains',
120
                title=_('Domains to check for spelling errors'),
121
                element_type=StringWidget,
122
                element_kwargs={'render_br': False},
123
                value=get_publisher().get_email_well_known_domains())
124
        form.add(WidgetList, 'valid_known_domains',
125
                title=_('Domains that should not be considered spelling errors'),
126
                element_type=StringWidget,
127
                element_kwargs={'render_br': False},
128
                value=get_publisher().get_email_valid_known_domains())
119 129

  
120 130
        form.add_submit('submit', _('Submit'))
121 131
        form.add_submit('cancel', _('Cancel'))
......
140 150
        else:
141 151
            cfg_submit(form, 'emails', [ 'smtp_server', 'smtp_login',
142 152
                'smtp_password', 'from', 'reply_to', 'footer',
143
                'check_domain_with_dns'])
153
                'check_domain_with_dns', 'well_known_domains',
154
                'valid_known_domains'])
144 155
            return redirect('.')
145 156

  
146 157
    def _q_index(self):
wcs/qommon/form.py
904 904

  
905 905
    def add_media(self):
906 906
        get_response().add_javascript(['jquery.js', '../../i18n.js', 'qommon.forms.js'])
907
        get_response().add_javascript_code('''
908
            const WCS_WELL_KNOWN_DOMAINS = %s;
909
            const WCS_VALID_KNOWN_DOMAINS = %s;
910
        ''' % (json.dumps(get_publisher().get_email_well_known_domains()),
911
               json.dumps(get_publisher().get_email_valid_known_domains())))
907 912

  
908 913
    def _parse(self, request):
909 914
        StringWidget._parse(self, request)
......
925 930
                    return
926 931
                domain = force_text(domain, 'utf-8', errors='ignore')
927 932
                try:
928
                    domain = domain.encode('idna')
933
                    domain = force_str(domain.encode('idna'))
929 934
                except UnicodeError:
930 935
                    self.error = _('invalid address domain')
931 936
                    return
wcs/qommon/publisher.py
934 934
            return [x.strip() for x in modes.split(',')]
935 935
        return ['lazy', 'django-condition']
936 936

  
937
    def get_email_well_known_domains(self):
938
        emails_cfg = get_cfg('emails', {})
939
        well_known_domains = emails_cfg.get('well_known_domains')
940
        if not well_known_domains:
941
            well_known_domains = ['gmail.com', 'msn.com', 'hotmail.com',
942
                    'hotmail.fr', 'wanadoo.fr', 'free.fr', 'yahoo.fr',
943
                    'numericable.fr', 'laposte.fr', 'orange.fr', 'yahoo.com']
944
        return well_known_domains
945

  
946
    def get_email_valid_known_domains(self):
947
        emails_cfg = get_cfg('emails', {})
948
        valid_known_domains = emails_cfg.get('valid_known_domains')
949
        if not valid_known_domains:
950
            valid_known_domains = ['yopmail.com']
951
        return valid_known_domains
952

  
937 953
    def get_substitution_variables(self):
938 954
        from . import misc
939 955
        from wcs.variables import LazyDateObject
wcs/qommon/static/js/qommon.forms.js
108 108
    });
109 109
  }
110 110
  // common domains we want to offer suggestions for.
111
  var well_known_domains = ['gmail.com', 'msn.com', 'hotmail.com', 'hotmail.fr', 'wanadoo.fr',
112
                            'free.fr', 'yahoo.fr', 'numericable.fr', 'laposte.fr', 'orange.fr',
113
                            'yahoo.com'];
111
  var well_known_domains = WCS_WELL_KNOWN_DOMAINS;
114 112
  // existing domains we know but don't want to use in suggestion engine.
115
  var known_domains = ['yopmail.com'];
113
  var known_domains = WCS_VALID_KNOWN_DOMAINS;
116 114
  $('input[type=email]').on('change wcs:change', function() {
117 115
    var $email_input = $(this);
118 116
    var val = $email_input.val();
119
-