Skip to content

Commit

Permalink
Napraw problem z matchowaniem po PESELach
Browse files Browse the repository at this point in the history
  • Loading branch information
mpasternak committed Feb 17, 2019
1 parent 557c1ba commit ad60347
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
26 changes: 22 additions & 4 deletions src/import_dyscyplin/core.py
@@ -1,7 +1,6 @@
from hashlib import md5

import xlrd
from django.db import transaction
from django.db.models import Q
from xlrd import XLRDError

Expand Down Expand Up @@ -53,7 +52,8 @@ def matchuj_autora(imiona, nazwisko, jednostka, pesel_md5=None):
try:
return (Autor.objects.get(qry & Q(aktualna_jednostka=jednostka)), "")
except Autor.MultipleObjectsReturned:
return (None, "wielu autorów pasuje do tego rekordu (dopasowanie po imieniu, nazwisku i aktualnej jednostce)")
return (
None, "wielu autorów pasuje do tego rekordu (dopasowanie po imieniu, nazwisku i aktualnej jednostce)")

except Autor.DoesNotExist:
return (None, "taki autor nie istnieje (dopasowanie po imieniu, nazwisku i aktualnej jednostce)")
Expand All @@ -62,6 +62,23 @@ def matchuj_autora(imiona, nazwisko, jednostka, pesel_md5=None):
return (None, "taki autor nie istnieje (dopasowanie po imieniu i nazwisku)")


def pesel_md5(value_from_xls):
"""Zakoduj wartość PESEL z XLS, która to może być np liczbą
zmiennoprzecinkową do sumy kontrolnej MD5.
"""
original_pesel = value_from_xls

if type(original_pesel) == int:
original_pesel = str(original_pesel)
elif type(original_pesel) == float:
original_pesel = str(int(original_pesel))
else:
original_pesel = str(original_pesel)
original_pesel = original_pesel.encode("utf-8")

return md5(original_pesel).hexdigest()


def przeanalizuj_plik_xls(sciezka, parent):
"""
:param sciezka:
Expand Down Expand Up @@ -100,8 +117,9 @@ def przeanalizuj_plik_xls(sciezka, parent):
if not original['nazwisko'].strip():
continue

original['pesel_md5'] = md5(str(original['pesel']).encode("utf-8")).hexdigest()
original['nazwa_jednostki'] = original['nazwa jednostki'] # templatka wymaga
original['pesel_md5'] = pesel_md5(original['pesel'])

original['nazwa_jednostki'] = original['nazwa jednostki'] # templatka wymaga

del original['pesel']

Expand Down
6 changes: 5 additions & 1 deletion src/import_dyscyplin/tests/test_core.py
Expand Up @@ -2,7 +2,7 @@
from model_mommy import mommy

from bpp.models import Wydzial, Jednostka, Autor
from import_dyscyplin.core import przeanalizuj_plik_xls, matchuj_wydzial, matchuj_jednostke, matchuj_autora
from import_dyscyplin.core import przeanalizuj_plik_xls, matchuj_wydzial, matchuj_jednostke, matchuj_autora, pesel_md5
from import_dyscyplin.exceptions import ImproperFileException, HeaderNotFoundException, BadNoOfSheetsException
from import_dyscyplin.models import Import_Dyscyplin_Row

Expand Down Expand Up @@ -104,3 +104,7 @@ def test_matchuj_autora_po_aktualnej_jednostce():
jednostka=j2
)
assert a == a2


def test_pesel_md5():
assert pesel_md5(1.0) == pesel_md5(1) == pesel_md5('1')

0 comments on commit ad60347

Please sign in to comment.