Skip to content
Snippets Groups Projects
Commit 6b3010b8 authored by Tomáš Valenta's avatar Tomáš Valenta
Browse files

finished old contract import

parent c7d22fbc
No related branches found
No related tags found
1 merge request!3Release
Pipeline #12516 failed
......@@ -6,6 +6,7 @@ from datetime import date, datetime
import yaml
from django.conf import settings
from django.core.files import File
from django.core.management.base import BaseCommand
from django.db import models
from postal.parser import parse_address
......@@ -14,6 +15,7 @@ from git import Repo
from ...models import (
Contract,
ContractFile,
ContractFilingArea,
ContractType,
Contractee,
......@@ -408,6 +410,33 @@ class Command(BaseCommand):
return area_name
def normalize_filename(self, filename: str) -> str:
filename = string.capwords(filename)
patterns = (
(
r"\s\s+",
" "
),
(
r"^((P|p)odepsaná (V|v)erze|Strojově Čitelná Verze)$",
"Anonymizovaná verze",
),
(
r"^(P|p)odepsaná (V|v)erze 2$",
"Anonymizovaná verze 2"
),
(
r"^(U|u)pravitelná (V|v)erze$",
"Upravitelná verze"
)
)
for pattern in patterns:
filename = re.sub(pattern[0], pattern[1], filename)
return filename
def parse_index(
self,
contract_root: str,
......@@ -775,8 +804,37 @@ class Command(BaseCommand):
instance.department = self.normalize_department(signing_party_value)
self.normalization_count += 1
if model is Signee:
if (
"s.r.o" in instance.name
or "s. r. o." in instance.name
or "a.s." in instance.name
or "a. s." in instance.name
or "o.s." in instance.name
or "o. s." in instance.name
or "z.s." in instance.name
or "z. s." in instance.name
or "1" in instance.name
or "2" in instance.name
or "3" in instance.name
or "4" in instance.name
or "5" in instance.name
or "6" in instance.name
or "7" in instance.name
or "8" in instance.name
or "9" in instance.name
or "0" in instance.name
or len(instance.name.split(" ")) not in (2, 3)
):
instance.entity_type = instance.EntityTypes.LEGAL_ENTITY
else:
if instance.ico_number is None:
instance.entity_type = instance.EntityTypes.NATURAL_PERSON
else:
instance.entity_type = instance.EntityTypes.BUSINESS_NATURAL_PERSON
# Do our best to merge signing parties together.
existing_instance = model.objects.filter(
existing_instances = model.objects.filter(
(
models.Q(name=instance.name)
& (
......@@ -812,32 +870,37 @@ class Command(BaseCommand):
if instance.ico_number is not None
else models.Value(False)
)
).first()
).all()
if existing_instance is not None:
if len(existing_instances) != 0:
for position, existing_instance in enumerate(existing_instances):
if existing_instance.ico_number is None and instance.ico_number is not None:
existing_instance.ico_number = instance.ico_number
existing_instance.save()
instance = existing_instance
break
elif existing_instance.ico_number == instance.ico_number or instance.ico_number is None:
instance = existing_instance
else:
break
elif position == len(existing_instances) - 1:
instance.save()
else:
instance.save()
return instance, representatives, is_contractee, issue_count
def assign_contract_metadata(
def assign_contract_data(
self,
contract: Contract,
metadata: dict,
slug: str,
contract_root: str,
) -> None:
filing_area = None
types = []
signees = []
contractees = []
files = []
is_already_imported = False
observed_issues_count = 0
......@@ -1109,9 +1172,72 @@ class Command(BaseCommand):
continue
for filename in value:
if not isinstance(filename, str):
for file_data in value:
if not isinstance(file_data, dict):
observed_issues_count += 1
contract.notes += f"Špatně zadané informace o souboru: {file_data}.\n"
if self.verbosity >= 2:
self.stderr.write(
self.style.NOTICE(
f"File data in {slug} is not a dict: {file_data}."
)
)
continue
for file_key, file_value in file_data.items():
file_key = file_key.strip()
if file_key.lower() in ("název", "náhled", "náhlad"):
continue
if not isinstance(file_value, str):
observed_issues_count += 1
contract.notes += f"Špatně zadaný název souboru {file_key}: {file_value}.\n"
if self.verbosity >= 2:
self.stderr.write(
self.style.NOTICE(
f"Filename in {slug} for file {file_key} invalid: {file_value}."
)
)
continue
file_path = os.path.join(
contract_root,
file_value
)
if not os.path.isfile(file_path):
observed_issues_count += 1
contract.notes += f"Neexistující soubor: {file_value}.\n"
if self.verbosity >= 2:
self.stderr.write(
self.style.NOTICE(
f"Filename in {slug} does not correspond to a file: {file_value}."
)
)
continue
with open(file_path, "rb") as open_file:
self.normalization_count += 1
file = ContractFile(
contract=contract,
name=self.normalize_filename(file_key),
is_public=True
)
file.file.save(
file_value,
File(open_file),
save=False,
)
files.append(file)
if not is_already_imported:
if contract.name in (None, "") or (
......@@ -1173,6 +1299,9 @@ class Command(BaseCommand):
representative.signature = signature
representative.save()
for file in files:
file.save()
contract.filing_area = filing_area
contract.types.set(types)
contract.save()
......@@ -1209,10 +1338,11 @@ class Command(BaseCommand):
return
self.assign_contract_metadata(
self.assign_contract_data(
contract,
metadata,
os.path.basename(contract_root),
contract_root,
)
elif file_.endswith(".pdf"):
# TODO
......@@ -1332,6 +1462,7 @@ class Command(BaseCommand):
Contract,
ContractType,
ContractFilingArea,
ContractFile,
Contractee,
ContracteeSignature,
Signee,
......
......@@ -6,6 +6,7 @@ $(window).ready(
`<datalist id="file-types">
<option value="Původní verze">
<option value="Anonymizovaná verze">
<option value="Upravitelná verze">
</datalist>`
);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment