Skip to content

Commit

Permalink
Merge pull request #1 from meetghodasara-crest/bugfix/storing-seriali…
Browse files Browse the repository at this point in the history
…zed-data

Fixing the bug for the data versioning and data fields duplication by…
  • Loading branch information
meetghodasara-crest authored Dec 2, 2024
2 parents 056fa76 + 482a56b commit 55765da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
29 changes: 15 additions & 14 deletions opentaxii/persistence/sqldb/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -983,24 +983,29 @@ def add_objects(
self.db.session.commit()
job_details = []
for obj in objects:
if not obj.get("modified"):
obj["modified"] = obj.get("created") or datetime.datetime.now(datetime.timezone.utc).strftime(DATETIMEFORMAT)
modified_date = obj.get("modified") or obj.get("created") or datetime.datetime.now(datetime.timezone.utc).strftime(DATETIMEFORMAT)
version = datetime.datetime.strptime(
obj["modified"], DATETIMEFORMAT
modified_date, DATETIMEFORMAT
).replace(tzinfo=datetime.timezone.utc)
if (
not self.db.session.query(literal(True))
.filter(
modified_object = True
if obj.get("modified") or obj.get("created"):
modified_object = not self.db.session.query(literal(True)).filter(
self.db.session.query(taxii2models.STIXObject)
.filter(
taxii2models.STIXObject.id == obj["id"],
taxii2models.STIXObject.collection_id == collection_id,
taxii2models.STIXObject.version == version,
)
.exists()
)
.scalar()
):
).scalar()
else:
stored_object = self.db.session.query(taxii2models.STIXObject).filter(
taxii2models.STIXObject.id == obj["id"],
taxii2models.STIXObject.collection_id == collection_id,
).order_by(taxii2models.STIXObject.date_added.desc()).first()
if stored_object and stored_object.serialized_data == obj:
modified_object = False
if modified_object:
self.db.session.add(
taxii2models.STIXObject(
id=obj["id"],
Expand All @@ -1009,11 +1014,7 @@ def add_objects(
spec_version=obj.get("spec_version", 2.1),
date_added=datetime.datetime.now(datetime.timezone.utc),
version=version,
serialized_data={
key: value
for (key, value) in obj.items()
if key not in ["id", "type", "spec_version"]
},
serialized_data=obj,
)
)
job_detail = taxii2models.JobDetail(
Expand Down
6 changes: 0 additions & 6 deletions opentaxii/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,6 @@ def objects_get_handler(self, api_root_id, collection_id_or_alias):
"more": more,
"objects": [
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in objects
Expand Down Expand Up @@ -752,9 +749,6 @@ def object_get_handler(self, api_root_id, collection_id_or_alias, object_id):
"more": more,
"objects": [
{
"id": obj.id,
"type": obj.type,
"spec_version": obj.spec_version,
**obj.serialized_data,
}
for obj in versions
Expand Down

0 comments on commit 55765da

Please sign in to comment.