Skip to content

Commit

Permalink
improved create_data_source function
Browse files Browse the repository at this point in the history
  • Loading branch information
reichie020212 committed Dec 6, 2023
1 parent 1fe3ad5 commit 2bf7708
Showing 1 changed file with 10 additions and 30 deletions.
40 changes: 10 additions & 30 deletions spp_registry_data_source/models/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,16 @@ class SppDataSource(models.Model):
@api.model
@api.returns("self", lambda value: value.id)
def create_data_source(self, vals):
"""
The function creates a data source and its associated paths if it doesn't already exist.
:param vals: The `vals` parameter is a dictionary that contains the values to be used for
creating a new data source. It may contain the following keys:
:return: the data_source_id.
"""
name = vals.get("name")

data_source_id = self.env["spp.data.source"].search(
[("name", "=", name)], limit=1
)

if not data_source_id:
paths = []
if vals.get("paths"):
paths = vals.pop("paths")

data_source_id = self.env["spp.data.source"].create(vals)

for path in paths:
self.env["spp.data.source.path"].create(
{
"data_source_id": data_source_id.id,
"name": path.get("name"),
"path": path.get("path"),
}
)

return data_source_id
data_source_id = self.search([("name", "=", vals.get("name"))], limit=1)
if data_source_id:
return data_source_id
paths, path_create_vals = vals.pop("paths", []), []
for path in paths:
path_create_vals.append(
(0, 0, {"name": path.get("name"), "path": path.get("path")})
)
vals["data_source_path_ids"] = path_create_vals
return self.create(vals)

def get_field_mapping_key_value_pair(self):
return self.data_source_field_mapping_ids.get_mapping()
Expand Down

0 comments on commit 2bf7708

Please sign in to comment.