Browse Source

feat(import): vérifie les domaines à l import ; fix #14

pull/34/head
François Poulain 11 months ago
committed by Cliss XXI - dev
parent
commit
becbcbd0ef
  1. 2
      gvot/base/templatetags/accessors.py
  2. 21
      gvot/base/views.py
  3. 4
      requirements/base.txt

2
gvot/base/templatetags/accessors.py

@ -25,4 +25,4 @@ def get_champ_perso(obj, key):
@register.filter(name="get")
def _get(obj, key):
"""Equivalent d'un get dans les templates."""
return obj.get(key, "")
return obj.get(key, "") if obj else None

21
gvot/base/views.py

@ -1,10 +1,12 @@
import csv
from django.conf import settings
from django.core.exceptions import ValidationError
from django.shortcuts import redirect
from django.urls import reverse, reverse_lazy
from django.views.generic import FormView, RedirectView, detail
import dns.resolver
from wagtail.admin import messages
from . import forms, models
@ -337,7 +339,7 @@ class ImportConfirm(FormInvalidMixin, FormView):
for data, _ in datas:
data.update({'ponderation': data.get('ponderation', 1) or 1})
return (
return [
models.Pouvoir(
scrutin_id=self.scrutin_id,
**model_data,
@ -349,11 +351,22 @@ class ImportConfirm(FormInvalidMixin, FormView):
]
)
for model_data, other_data in datas
)
]
def check_objects_mx(self, object_list):
domains = set([p.courriel.split('@')[-1] for p in object_list])
bad_mx_domains = {}
for domain in domains:
try:
dns.resolver.query(domain, 'MX')
except Exception as e:
bad_mx_domains[domain] = str(e)
return bad_mx_domains
def crible_data(self):
"""Crible les lignes entre ce qu'on prend et ce qu'on rejette."""
object_list = self.data_to_python()
bad_mx_domains = self.check_objects_mx(object_list)
ok, warn, ko = [], [], []
# champs identifiants (doublons)
@ -403,6 +416,10 @@ class ImportConfirm(FormInvalidMixin, FormView):
elif (obj.courriel,) in courriels_in_import:
warn.append((index, obj, warnings_msg[3]))
if obj.courriel.split('@')[-1] in bad_mx_domains:
bad_mx_msg = "Expédition impossible : domaine en erreur."
raise ValidationError({'courriel': bad_mx_msg})
if not warn or warn[-1][0] != index:
ok.append((index, obj, None))
courriels_in_import.add((obj.courriel,))

4
requirements/base.txt

@ -16,3 +16,7 @@ wagtailmenus >=3.0,<3.1 # https://github.com/rkhleics/wagtailmenus
sphinx
sphinx-rtd-theme
django-docs
# Utils
# ------------------------------------------------------------------------------
dnspython

Loading…
Cancel
Save
Map all the world