generated from dopt-python/py311
continue work on new data model
This commit is contained in:
@@ -13,9 +13,12 @@ from sqlalchemy.dialects.sqlite import insert as sqlite_insert
|
||||
from wce_crm import db
|
||||
from wce_crm.constants import TIMEZONE_CEST
|
||||
from wce_crm.data_models import (
|
||||
FIELD_DB_MAPPING_GRUNDERERFASSUNG_PERSONEN,
|
||||
FIELD_DB_MAPPING_GRUNDERERFASSUNG_UNTERNEHMEN,
|
||||
Beratungsgespraech_Einzelgespraech,
|
||||
Beratungsgespraech_Vorgang,
|
||||
InitRec,
|
||||
Page_InitRec_Form_Data,
|
||||
)
|
||||
from wce_crm.logging import logger_back as logger
|
||||
from wce_crm.types import (
|
||||
@@ -24,14 +27,17 @@ from wce_crm.types import (
|
||||
CompanyProfileConsultations,
|
||||
ConsultingType,
|
||||
ContactPersonInfo,
|
||||
EntityIds,
|
||||
EntityType,
|
||||
LinkingConsultationsEntryCompany,
|
||||
LinkingConsultationsEntryPerson,
|
||||
MainPageEntry,
|
||||
NewEntityIds,
|
||||
RecordingType,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from dopt_basics.datastructures import DualDict
|
||||
|
||||
from wce_crm.types import ConsId, ExtAnId, ExtMaId, RecId
|
||||
|
||||
|
||||
@@ -348,14 +354,201 @@ def initrec_comp_contact_person_search_get_info(
|
||||
# return dict(row)
|
||||
|
||||
|
||||
def _rename_db_data(
|
||||
db_data: dict[str, Any],
|
||||
mapping: DualDict,
|
||||
) -> dict[str, Any]:
|
||||
new_db_data = copy.deepcopy(db_data)
|
||||
|
||||
for k, v in db_data.items():
|
||||
if k in mapping:
|
||||
new_db_data[mapping[k]] = v
|
||||
del new_db_data[k]
|
||||
|
||||
if k in mapping.inverted:
|
||||
new_db_data[mapping.inverted[k]] = v
|
||||
del new_db_data[k]
|
||||
|
||||
return new_db_data
|
||||
|
||||
|
||||
def page_initrec_to_db(
|
||||
data: InitRec,
|
||||
) -> InitRec: ...
|
||||
) -> InitRec:
|
||||
|
||||
with db.ENGINE.begin() as conn:
|
||||
# person
|
||||
dump_data = _rename_db_data(
|
||||
data.form_data.person, FIELD_DB_MAPPING_GRUNDERERFASSUNG_PERSONEN
|
||||
)
|
||||
dump_data["geloescht"] = data.geloescht
|
||||
|
||||
if data.ids.pers_id is None:
|
||||
logger.debug("[InitRec -- backend] Insert...")
|
||||
|
||||
stmt = sa.insert(db.t_grunderfassung_personen).returning(
|
||||
db.t_grunderfassung_personen.c.pers_id,
|
||||
db.t_grunderfassung_personen.c.aktualisiert,
|
||||
db.t_grunderfassung_personen.c.geloescht,
|
||||
)
|
||||
stmt_compiled = str(stmt.compile(db.ENGINE))
|
||||
logger.debug(
|
||||
"[InitRec -- backend] Data to insert:\nStatement: %s\n%s",
|
||||
stmt_compiled,
|
||||
pformat(dump_data),
|
||||
)
|
||||
|
||||
ret = conn.execute(stmt, dump_data)
|
||||
|
||||
from_db = ret.mappings().fetchall()
|
||||
assert len(from_db) == 1, "expected excactly one returned row"
|
||||
data_from_db = from_db[0]
|
||||
|
||||
data.ids.pers_id = data_from_db["pers_id"]
|
||||
data.geloescht = data_from_db["geloescht"]
|
||||
data.form_data.person["Metadaten_aktualisierung"] = data_from_db["aktualisiert"]
|
||||
|
||||
logger.debug("[AutoForm -- backend] Inserted InitRec Person successfully")
|
||||
|
||||
else:
|
||||
logger.debug("[AutoForm -- backend] Update...")
|
||||
stmt = (
|
||||
db.t_grunderfassung_personen.update()
|
||||
.where(db.t_grunderfassung_personen.c.pers_id == data.ids.pers_id)
|
||||
.returning(
|
||||
db.t_grunderfassung_personen.c.Metadaten_aktualisierung,
|
||||
db.t_grunderfassung_personen.c.geloescht,
|
||||
)
|
||||
)
|
||||
ret = conn.execute(stmt, dump_data)
|
||||
|
||||
from_db = ret.mappings().fetchall()
|
||||
assert len(from_db) == 1, "expected excatly one returned row"
|
||||
data_from_db = from_db[0]
|
||||
|
||||
data.geloescht = data_from_db["geloescht"]
|
||||
data.form_data.person["Metadaten_aktualisierung"] = data_from_db[
|
||||
"Metadaten_aktualisierung"
|
||||
]
|
||||
|
||||
logger.debug(
|
||||
"[InitRec -- backend] Updated InitRec Person with ID %d successfully",
|
||||
data.ids.pers_id,
|
||||
)
|
||||
|
||||
# company
|
||||
if data.recording_type is RecordingType.WITH_COMPANY:
|
||||
# insert into company table and add additional information
|
||||
assert data.ids.un_id, "company ID not set"
|
||||
assert data.ids.an_id, "contact person (an) ID not set"
|
||||
assert data.ids.pers_id, "person ID not set"
|
||||
|
||||
stmt = (
|
||||
sqlite_insert(db.t_unternehmen)
|
||||
.values(un_id=data.ids.un_id, geloescht=None)
|
||||
.on_conflict_do_nothing(index_elements=["un_id"])
|
||||
)
|
||||
conn.execute(stmt)
|
||||
|
||||
# additional information
|
||||
dump_data = _rename_db_data(
|
||||
data.form_data.company, FIELD_DB_MAPPING_GRUNDERERFASSUNG_UNTERNEHMEN
|
||||
)
|
||||
dump_data["geloescht"] = data.geloescht
|
||||
stmt = sqlite_insert(db.t_zuordnung_personen_unternehmen).values(
|
||||
**dump_data, pers_id=data.ids.pers_id
|
||||
)
|
||||
stmt = stmt.on_conflict_do_update(
|
||||
index_elements=["un_id", "pers_id"],
|
||||
index_where=sa.text("geloescht IS NULL"),
|
||||
set_={
|
||||
"an_id": stmt.excluded.an_id,
|
||||
"Partnersuche__kanal_aufmerksamkeit": stmt.excluded.Partnersuche__kanal_aufmerksamkeit,
|
||||
"aktualisiert": datetime.datetime.now(datetime.UTC),
|
||||
},
|
||||
).returning(db.t_zuordnung_personen_unternehmen.c.id)
|
||||
ret = conn.execute(stmt)
|
||||
|
||||
from_db = ret.mappings().fetchall()
|
||||
assert len(from_db) == 1, "expected excatly one returned row"
|
||||
data_from_db = from_db[0]
|
||||
data.ids.link_id = data_from_db["id"]
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def page_initrec_from_db(
|
||||
ids: NewEntityIds,
|
||||
) -> InitRec: ...
|
||||
ids: EntityIds,
|
||||
) -> InitRec:
|
||||
recording_type = RecordingType.WITHOUT_COMPANY
|
||||
if ids.valid(EntityType.COMPANY):
|
||||
recording_type = RecordingType.WITH_COMPANY
|
||||
|
||||
# person
|
||||
assert ids.pers_id, "person ID not set"
|
||||
logger.debug("[AutoForm -- backend] Call database loading routine...")
|
||||
stmt = db.t_grunderfassung_personen.select().where(
|
||||
db.t_grunderfassung_personen.c.pers_id == ids.pers_id
|
||||
)
|
||||
with db.ENGINE.connect() as conn:
|
||||
ret = conn.execute(stmt)
|
||||
|
||||
from_db = ret.mappings().all()
|
||||
assert len(from_db) == 1, "not excactly one company initial recording obtained"
|
||||
data_person_table = dict(from_db[0])
|
||||
|
||||
data_person_table = _rename_db_data(
|
||||
data_person_table, FIELD_DB_MAPPING_GRUNDERERFASSUNG_PERSONEN
|
||||
)
|
||||
geloescht = data_person_table["geloescht"]
|
||||
del data_person_table["geloescht"]
|
||||
|
||||
data_company_table: dict[str, Any] = {}
|
||||
if recording_type is RecordingType.WITH_COMPANY:
|
||||
assert ids.un_id, "company ID not set"
|
||||
|
||||
if ids.link_id:
|
||||
stmt = sa.select(
|
||||
db.t_zuordnung_personen_unternehmen.c.un_id,
|
||||
db.t_zuordnung_personen_unternehmen.c.an_id,
|
||||
db.t_zuordnung_personen_unternehmen.c.Partnersuche__kanal_aufmerksamkeit,
|
||||
).where(
|
||||
db.t_zuordnung_personen_unternehmen.c.id == ids.link_id,
|
||||
)
|
||||
else:
|
||||
stmt = sa.select(
|
||||
db.t_zuordnung_personen_unternehmen.c.un_id,
|
||||
db.t_zuordnung_personen_unternehmen.c.an_id,
|
||||
db.t_zuordnung_personen_unternehmen.c.Partnersuche__kanal_aufmerksamkeit,
|
||||
).where(
|
||||
db.t_zuordnung_personen_unternehmen.c.pers_id == ids.pers_id,
|
||||
db.t_zuordnung_personen_unternehmen.c.un_id == ids.un_id,
|
||||
db.t_zuordnung_personen_unternehmen.c.geloescht.is_(None),
|
||||
)
|
||||
|
||||
with db.ENGINE.connect() as conn:
|
||||
ret = conn.execute(stmt)
|
||||
|
||||
from_db = ret.mappings().all()
|
||||
assert len(from_db) == 1, "not excactly one company initial recording obtained"
|
||||
data_company_table = dict(from_db[0])
|
||||
|
||||
data_company_table = _rename_db_data(
|
||||
data_person_table, FIELD_DB_MAPPING_GRUNDERERFASSUNG_UNTERNEHMEN
|
||||
)
|
||||
|
||||
form_data = Page_InitRec_Form_Data(
|
||||
metadata={},
|
||||
person=data_person_table,
|
||||
company=data_company_table,
|
||||
)
|
||||
|
||||
return InitRec(
|
||||
ids=ids,
|
||||
recording_type=recording_type,
|
||||
geloescht=geloescht,
|
||||
form_data=form_data,
|
||||
)
|
||||
|
||||
|
||||
# def initrec_company_delete_initial_recording(
|
||||
@@ -857,7 +1050,7 @@ def _main_page_get_company_list() -> list[MainPageEntry]:
|
||||
rec_id=rec_id,
|
||||
display_name=display_name,
|
||||
Metadaten_aktualisierung=datetime_akt,
|
||||
type=RecordingType.COMPANY,
|
||||
type=RecordingType.WITH_COMPANY,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -894,7 +1087,7 @@ def _main_page_get_person_list() -> list[MainPageEntry]:
|
||||
rec_id=rec_id,
|
||||
display_name=display_name,
|
||||
Metadaten_aktualisierung=datetime_akt,
|
||||
type=RecordingType.PERSON,
|
||||
type=RecordingType.WITHOUT_COMPANY,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user