This repository has been archived on 2025-03-03. You can view files and clone it, but cannot push or open issues or pull requests.
facturio/subjects/views.py

39 lines
1.3 KiB
Python
Raw Normal View History

2023-12-19 14:39:59 +01:00
from ares_util.ares import call_ares, validate_czech_company_id
from ares_util.exceptions import InvalidCompanyIDError
from django.http import HttpRequest, HttpResponse
from django.shortcuts import render
2023-12-19 13:49:34 +01:00
ARES_BASE_URL = "https://wwwinfo.mfcr.cz/cgi-bin/ares/darv_rzp.cgi?ico=27074358&xml=0&ver=1.0.4"
2023-12-19 14:39:59 +01:00
def build_address(street: str, zip_code: int | str, city: str, city_part: str) -> str:
return f"{street}, {zip_code}, {city} - {city_part}"
def test(req: HttpRequest, ico: str) -> HttpResponse:
try:
validate_czech_company_id(ico)
except InvalidCompanyIDError as ex:
return render(req, "subjects/ares.html", dict(error=ex, ares_data={}))
ares_data = call_ares(ico)
ares_address_data = ares_data["address"]
ares_legal_data = ares_data["legal"]
address_line = build_address(
ares_address_data["street"],
ares_address_data["zip_code"],
ares_address_data["city"],
ares_address_data["city_part"]
)
important_data = dict(
copmany_name=ares_legal_data["company_name"],
company_id=ares_legal_data["company_id"],
company_vat_id=ares_legal_data["company_vat_id"],
address_line=address_line,
)
return render(req, "subjects/ares.html", dict(error="", ares_data=important_data))