Skip to content

Commit

Permalink
Ostatnie poprawki
Browse files Browse the repository at this point in the history
  • Loading branch information
mpasternak committed Nov 29, 2022
1 parent ace17a4 commit 471f49f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
35 changes: 30 additions & 5 deletions src/bpp/management/commands/import_oplaty_publikacje.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
)

from bpp.models import ModelZOplataZaPublikacje, Rekord
from bpp.util import pbar


class Command(BaseCommand):
Expand All @@ -25,8 +24,18 @@ def add_arguments(self, parser: OptionParser):
@transaction.atomic
def handle(self, pliki, *args, **options):

for plik in pbar(pliki):
xlsx = pandas.read_excel(plik)
for plik in pliki:
print()
print()
print()
print(plik.name)
print("=" * 80)
print()

try:
xlsx = pandas.read_excel(plik)
except ValueError:
print(f"Nie umiem otworzyc pliku {plik.name}")

for wiersz, row in enumerate(xlsx.iloc):
pk = normalize_rekord_id(row[1])
Expand All @@ -35,7 +44,22 @@ def handle(self, pliki, *args, **options):

rekord = Rekord.objects.get(pk=pk)
original: ModelZOplataZaPublikacje = rekord.original
assert rekord.tytul_oryginalny == row["Tytuł oryginalny"]

try:
row["Tytuł oryginalny"]
except KeyError:
print(
"Plik nie ma kolumny 'Tytuł oryginalny', nie importuję pliku w ogóle"
)
continue

if rekord.tytul_oryginalny != row["Tytuł oryginalny"]:
print(
f"wiersz {wiersz} -- tytuł rekordu inny niz w bazie, nie importuję (plik: "
f"{row['Tytuł oryginalny']}, baza {rekord.tytul_oryginalny})"
)
print()
continue

try:
normalize_oplaty_za_publikacje(
Expand All @@ -53,9 +77,10 @@ def handle(self, pliki, *args, **options):
)
except ValidationError as e:
print(
f"Plik {plik} Wiersz {wiersz} -- problem z walidacją rekordu {rekord.tytul_oryginalny} -- "
f"wiersz {wiersz} -- problem z walidacją rekordu {rekord.tytul_oryginalny} -- "
f"{e}. Zmiany nie zostały wprowadzone do bazy. "
)
print()
continue

original.save()
26 changes: 22 additions & 4 deletions src/import_common/normalization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from decimal import Decimal
from typing import Union

from django.core.exceptions import ValidationError
from numpy import isnan
from pathspec.util import normalize_file

Expand Down Expand Up @@ -189,10 +190,21 @@ def normalize_nulldecimalfield(probably_decimal):
if probably_decimal is None:
return

if probably_decimal is not None and isnan(probably_decimal):
return
try:
if probably_decimal is not None and isnan(probably_decimal):
return
except TypeError:
raise ValidationError(
f"Nie mogę skonwertować liczby w formacie {probably_decimal} na typ Decimal"
)

return Decimal(probably_decimal)
try:
# float() zeby pozbyc sie np numpy.int64
return Decimal(float(probably_decimal))
except TypeError:
raise ValidationError(
f"Nie mogę skonwertować liczby w formacie {probably_decimal} na typ Decimal"
)


def normalize_oplaty_za_publikacje(
Expand Down Expand Up @@ -240,4 +252,10 @@ def normalize_rekord_id(rekord_id):
if not rekord_id:
return

return rekord_id.replace("(", "{").replace(")", "}")
ret = rekord_id.replace("(", "{").replace(")", "}").strip()
if not ret.startswith("{"):
ret = "{" + ret
if not ret.endswith("}"):
ret += "}"

return ret

0 comments on commit 471f49f

Please sign in to comment.