Skip to content

Commit

Permalink
Merge pull request #107 from opengisch/edit_basket
Browse files Browse the repository at this point in the history
Enhance modificacion of baskets and datasets
  • Loading branch information
signedav authored Oct 7, 2024
2 parents 588a529 + db232bd commit 1b0934f
Show file tree
Hide file tree
Showing 12 changed files with 787 additions and 44 deletions.
19 changes: 18 additions & 1 deletion modelbaker/dbconnector/db_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,29 @@ def get_classes_relevance(self):
"""
return []

def create_basket(self, dataset_tid, topic, tilitid_value=None):
def create_basket(
self, dataset_tid, topic, tilitid_value=None, attachment_key="modelbaker"
):
"""
Returns the state and the errormessage
"""
return False, None

def edit_basket(self, basket_config: dict) -> tuple[bool, str]:
"""
Returns the state and the errormessage
The basket_config must have the following keys:
dataset_t_id
datasetname
topic
bid_value
attachmentkey
basket_t_id
"""
return False, None

def get_tid_handling(self):
"""
Returns `True` if a tid handling is enabled according to the settings table (when the database has been created with `--createTidCol`).
Expand Down
49 changes: 47 additions & 2 deletions modelbaker/dbconnector/gpkg_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __init__(self, uri, schema):
self.tid = "T_Id"
self.tilitid = "T_Ili_Tid"
self.dispName = "dispName"
self.attachmentKey = "attachmentKey"
self.basket_table_name = GPKG_BASKET_TABLE
self.dataset_table_name = GPKG_DATASET_TABLE

Expand Down Expand Up @@ -915,7 +916,9 @@ def get_classes_relevance(self):
return contents
return []

def create_basket(self, dataset_tid, topic, tilitid_value=None):
def create_basket(
self, dataset_tid, topic, tilitid_value=None, attachment_key="modelbaker"
):
if self._table_exists(GPKG_BASKET_TABLE):
cursor = self.conn.cursor()
cursor.execute(
Expand Down Expand Up @@ -947,7 +950,7 @@ def create_basket(self, dataset_tid, topic, tilitid_value=None):
cursor.execute(
"""
INSERT INTO "{basket_table}" ("{tid_name}", dataset, topic, "{tilitid_name}", attachmentkey )
VALUES (?, ?, ?, ?, 'modelbaker')
VALUES (?, ?, ?, ?, ?)
""".format(
tid_name=self.tid,
tilitid_name=self.tilitid,
Expand All @@ -958,6 +961,7 @@ def create_basket(self, dataset_tid, topic, tilitid_value=None):
dataset_tid,
topic,
tilitid_value,
attachment_key,
),
)
self.conn.commit()
Expand All @@ -973,6 +977,47 @@ def create_basket(self, dataset_tid, topic, tilitid_value=None):
).format(topic, error_message)
return False, self.tr('Could not create basket for topic "{}".').format(topic)

def edit_basket(self, basket_config: dict) -> tuple[bool, str]:
if self._table_exists(GPKG_BASKET_TABLE):
cursor = self.conn.cursor()
try:
cursor.execute(
"""
UPDATE {basket_table}
SET dataset = ?,
{t_ili_tid} = ?,
{attachment_key} = ?
WHERE {tid_name} = ?
""".format(
basket_table=GPKG_BASKET_TABLE,
t_ili_tid=self.tilitid,
attachment_key=self.attachmentKey,
tid_name=self.tid,
),
(
basket_config["dataset_t_id"],
basket_config["bid_value"],
basket_config["attachmentkey"],
basket_config["basket_t_id"],
),
)
self.conn.commit()
cursor.close()
return True, self.tr(
'Successfully edited basket for topic "{}" and dataset "{}".'
).format(basket_config["topic"], basket_config["datasetname"])
except sqlite3.Error as e:
cursor.close()
error_message = " ".join(e.args)
return False, self.tr(
'Could not edit basket for topic "{}" and dataset "{}": {}'
).format(
basket_config["topic"], basket_config["datasetname"], error_message
)
return False, self.tr(
'Could not edit basket for topic "{}" and dataset "{}"'
).format(basket_config["topic"], basket_config["datasetname"])

def get_tid_handling(self):
if self._table_exists(GPKG_SETTINGS_TABLE):
cursor = self.conn.cursor()
Expand Down
48 changes: 46 additions & 2 deletions modelbaker/dbconnector/mssql_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def __init__(self, uri, schema):
self.iliCodeName = "iliCode"
self.tid = "T_Id"
self.tilitid = "T_Ili_Tid"
self.attachmentKey = "attachmentkey"
self.dispName = "dispName"
self.basket_table_name = BASKET_TABLE
self.dataset_table_name = DATASET_TABLE
Expand Down Expand Up @@ -1053,7 +1054,9 @@ def get_classes_relevance(self):
result = self._get_dict_result(cur)
return result

def create_basket(self, dataset_tid, topic, tilitid_value=None):
def create_basket(
self, dataset_tid, topic, tilitid_value=None, attachment_key="modelbaker"
):
if self.schema and self._table_exists(BASKET_TABLE):
cur = self.conn.cursor()
cur.execute(
Expand All @@ -1080,7 +1083,7 @@ def create_basket(self, dataset_tid, topic, tilitid_value=None):
cur.execute(
"""
INSERT INTO {schema}.{basket_table} ({tid_name}, dataset, topic, {tilitid_name}, attachmentkey )
VALUES (NEXT VALUE FOR {schema}.{sequence}, {dataset_tid}, '{topic}', {tilitid}, 'modelbaker')
VALUES (NEXT VALUE FOR {schema}.{sequence}, {dataset_tid}, '{topic}', {tilitid}, {attachment_key})
""".format(
schema=self.schema,
sequence="t_ili2db_seq",
Expand All @@ -1090,6 +1093,7 @@ def create_basket(self, dataset_tid, topic, tilitid_value=None):
dataset_tid=dataset_tid,
topic=topic,
tilitid=tilitid_value,
attachment_key=attachment_key,
)
)
self.conn.commit()
Expand All @@ -1103,6 +1107,46 @@ def create_basket(self, dataset_tid, topic, tilitid_value=None):
).format(topic, error_message)
return False, self.tr('Could not create basket for topic "{}".').format(topic)

def edit_basket(self, basket_config: dict) -> tuple[bool, str]:
if self.schema and self._table_exists(BASKET_TABLE):
cur = self.conn.cursor()
try:
cur.execute(
"""
UPDATE {schema}.{basket_table}
SET dataset = ?,
{t_ili_tid} = ?,
{attachment_key} = ?
WHERE {tid_name} = ?
""".format(
schema=self.schema,
basket_table=BASKET_TABLE,
t_ili_tid=self.tilitid,
attachment_key=self.attachmentKey,
tid_name=self.tid,
),
(
basket_config["dataset_t_id"],
basket_config["bid_value"],
basket_config["attachmentkey"],
basket_config["basket_t_id"],
),
)
self.conn.commit()
return True, self.tr(
'Successfully edited basket for topic "{}" and dataset "{}".'
).format(basket_config["topic"], basket_config["datasetname"])
except pyodbc.errors.Error as e:
error_message = " ".join(e.args)
return False, self.tr(
'Could not edit basket for topic "{}" and dataset "{}": {}'
).format(
basket_config["topic"], basket_config["datasetname"], error_message
)
return False, self.tr(
'Could not edit basket for topic "{}" and dataset "{}"'
).format(basket_config["topic"], basket_config["datasetname"])

def get_tid_handling(self):
if self.schema and self._table_exists(SETTINGS_TABLE):
cur = self.conn.cursor()
Expand Down
50 changes: 48 additions & 2 deletions modelbaker/dbconnector/pg_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __init__(self, uri, schema):
self.iliCodeName = "ilicode"
self.tid = "t_id"
self.tilitid = "t_ili_tid"
self.attachmentKey = "attachmentkey"
self.dispName = "dispname"
self.basket_table_name = PG_BASKET_TABLE
self.dataset_table_name = PG_DATASET_TABLE
Expand Down Expand Up @@ -1100,7 +1101,9 @@ def get_classes_relevance(self):
return cur.fetchall()
return []

def create_basket(self, dataset_tid, topic, tilitid_value=None):
def create_basket(
self, dataset_tid, topic, tilitid_value=None, attachment_key="modelbaker"
):
if self.schema and self._table_exists(PG_BASKET_TABLE):
cur = self.conn.cursor()
cur.execute(
Expand Down Expand Up @@ -1128,7 +1131,7 @@ def create_basket(self, dataset_tid, topic, tilitid_value=None):
sql.SQL(
"""
INSERT INTO {schema}.{basket_table} ({tid_name}, dataset, topic, {tilitid_name}, attachmentkey)
VALUES (nextval(%s), %s, %s, %s, 'modelbaker')
VALUES (nextval(%s), %s, %s, %s, %s)
"""
).format(
schema=sql.Identifier(self.schema),
Expand All @@ -1141,6 +1144,7 @@ def create_basket(self, dataset_tid, topic, tilitid_value=None):
dataset_tid,
topic,
tilitid_value,
attachment_key,
),
)
self.conn.commit()
Expand All @@ -1154,6 +1158,48 @@ def create_basket(self, dataset_tid, topic, tilitid_value=None):
).format(topic, error_message)
return False, self.tr('Could not create basket for topic "{}".').format(topic)

def edit_basket(self, basket_config: dict) -> tuple[bool, str]:
if self.schema and self._table_exists(PG_BASKET_TABLE):
cur = self.conn.cursor()
try:
cur.execute(
sql.SQL(
"""
UPDATE {schema}.{basket_table}
SET dataset = %s,
{t_ili_tid} = %s,
{attachment_key} = %s
WHERE {tid_name} = %s
"""
).format(
schema=sql.Identifier(self.schema),
basket_table=sql.Identifier(PG_BASKET_TABLE),
t_ili_tid=sql.Identifier(self.tilitid),
attachment_key=sql.Identifier(self.attachmentKey),
tid_name=sql.Identifier(self.tid),
),
(
basket_config["dataset_t_id"],
basket_config["bid_value"],
basket_config["attachmentkey"],
basket_config["basket_t_id"],
),
)
self.conn.commit()
return True, self.tr(
'Successfully edited basket for topic "{}" and dataset "{}".'
).format(basket_config["topic"], basket_config["datasetname"])
except psycopg2.errors.Error as e:
error_message = " ".join(e.args)
return False, self.tr(
'Could not edit basket for topic "{}" and dataset "{}": {}'
).format(
basket_config["topic"], basket_config["datasetname"], error_message
)
return False, self.tr(
'Could not edit basket for topic "{}" and dataset "{}"'
).format(basket_config["topic"], basket_config["datasetname"])

def get_tid_handling(self):
if self.schema and self._table_exists(PG_SETTINGS_TABLE):
cur = self.conn.cursor()
Expand Down
Loading

0 comments on commit 1b0934f

Please sign in to comment.