From d6fabfff1a53c581aec4ca163d11bb35f56a88ac Mon Sep 17 00:00:00 2001 From: KM-R <129882581+KM-R@users.noreply.github.com> Date: Mon, 6 Nov 2023 21:40:48 +0100 Subject: [PATCH] Create company page tab "Beteiligte Personen" (#330) --- .../ui/company_elements.py | 56 +++++++++++++++++++ .../ui/data_elements.py | 36 ++++++++++++ 2 files changed, 92 insertions(+) diff --git a/src/aki_prj23_transparenzregister/ui/company_elements.py b/src/aki_prj23_transparenzregister/ui/company_elements.py index 8b81982..e35792c 100644 --- a/src/aki_prj23_transparenzregister/ui/company_elements.py +++ b/src/aki_prj23_transparenzregister/ui/company_elements.py @@ -249,6 +249,9 @@ def create_tabs(session: Session, selected_company_id: int) -> html: value="tab-2", className="tab-style", selected_className="selected-tab-style", + children=[ + person_relations_layout(selected_company_id, session) + ], ), dcc.Tab( label="Stimmung", @@ -270,6 +273,59 @@ def create_tabs(session: Session, selected_company_id: int) -> html: ) +def person_relations_layout(selected_company_id: int, session: Session) -> html: + """Create person relation tab. + + Args: + selected_company_id: Id of the chosen company in the dropdown. + session: A session connecting to the database. + + Returns: + The html div to create the person relations tab of the company page. + """ + relations = data_elements.get_person_relations_of_one_company( + selected_company_id, session + ) + if relations.empty: + return html.Div( + className="choose-metric", + children=[ + html.H3( + className="metrics-title", + children=[ + "Für dieses Unternehmen wurden leider keine beteiligten Personen gefunden." + ], + ) + ], + ) + table = ( + relations[["firstname", "lastname", "relation", "date_from", "date_to"]] + .fillna("n.a.") + .rename( + columns={ + "firstname": "Vorname", + "lastname": "Nachname", + "relation": "Rolle", + "date_from": "Von", + "date_to": "Bis", + } + ) + ) + return dash_table.DataTable( + table.to_dict("records"), + [{"name": i, "id": i} for i in table.columns], + style_table={ + "width": "80%", + "marginLeft": "auto", + "marginRight": "auto", + "padding": "20px", + "color": COLORS["raisin-black"], + }, + style_cell={"textAlign": "center"}, + style_header={"backgroundColor": COLORS["light"], "fontWeight": "bold"}, + ) + + def network_layout(selected_company_id: int) -> html: """Create network tab. diff --git a/src/aki_prj23_transparenzregister/ui/data_elements.py b/src/aki_prj23_transparenzregister/ui/data_elements.py index 1b3bed6..21beaaa 100644 --- a/src/aki_prj23_transparenzregister/ui/data_elements.py +++ b/src/aki_prj23_transparenzregister/ui/data_elements.py @@ -116,3 +116,39 @@ def get_options(session: Session | None) -> dict[int, str]: all_options = pd.concat([persons_options, companies_options]) return all_options.sort_values(ascending=True).to_dict() + + +def get_person_relations_of_one_company( + selected_company_id: int, session: Session +) -> pd.DataFrame: + """Collects all person relations of one company. + + Args: + selected_company_id: Id of the company. + session: A session connecting to the database. + + Returns: + A dataframe containing all person relations of the selected company. + """ + data = ( + session.query( + entities.Company.id, + entities.PersonRelation.relation, + entities.PersonRelation.date_from, + entities.PersonRelation.date_to, + entities.Person.id, + entities.Person.lastname, + entities.Person.firstname, + ) + .join( + entities.PersonRelation, + entities.PersonRelation.company_id == entities.Company.id, + ) + .join( + entities.Person, + entities.PersonRelation.person_id == entities.Person.id, + ) + .filter(entities.Company.id == selected_company_id) # type: ignore + .all() + ) + return pd.DataFrame(data)