Add description for snapshots

This commit is contained in:
grossmj 2026-04-09 20:25:44 +08:00
parent fbea9b655d
commit 274cef6cf5
No known key found for this signature in database
GPG Key ID: 1E7DD6DBB53FF3D7
3 changed files with 29 additions and 6 deletions

View File

@ -35,16 +35,18 @@ import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
# The string use to extract the date from the filename # Used to extract the date and time from the filename
FILENAME_TIME_FORMAT = "%d%m%y_%H%M%S" FILENAME_DATETIME_FORMAT = "%d%m%y_%H%M%S"
# Used to create a description of the snapshot with a human-readable date and time
DESCRIPTION_DATETIME_FORMAT = "%Y-%m-%d at %H:%M:%S"
class Snapshot: class Snapshot:
""" """
A snapshot object A snapshot object
""" """
def __init__(self, project, snapshot_id=None, name=None, filename=None, created_at=None): def __init__(self, project, snapshot_id=None, name=None, filename=None, created_at=None, description=None):
assert filename or name, "You need to pass a name or a filename" assert filename or name, "You need to pass a name or a filename"
@ -66,8 +68,13 @@ class Snapshot:
else: else:
self._name = filename.rsplit("_", 2)[0] self._name = filename.rsplit("_", 2)[0]
datestring = filename.replace(self._name + "_", "").split(".")[0] datestring = filename.replace(self._name + "_", "").split(".")[0]
self._created_at = int(datetime.strptime(datestring, FILENAME_TIME_FORMAT).replace(tzinfo=timezone.utc).timestamp()) self._created_at = int(datetime.strptime(datestring, FILENAME_DATETIME_FORMAT).replace(tzinfo=timezone.utc).timestamp())
if not description:
date = datetime.fromtimestamp(self._created_at, tz=timezone.utc).replace(tzinfo=None).strftime(DESCRIPTION_DATETIME_FORMAT)
description = "Snapshot '{}' taken on {}".format(self._name, date)
self._description = description
self._filename = filename self._filename = filename
self._path = os.path.join(project.path, "snapshots", filename) self._path = os.path.join(project.path, "snapshots", filename)
@ -79,13 +86,17 @@ class Snapshot:
def name(self): def name(self):
return self._name return self._name
@property
def description(self):
return self._description
@property @property
def path(self): def path(self):
return self._path return self._path
@property @property
def created_at(self): def created_at(self):
return int(self._created_at) return self._created_at
async def create(self): async def create(self):
""" """
@ -150,6 +161,7 @@ class Snapshot:
return { return {
"snapshot_id": self._id, "snapshot_id": self._id,
"name": self._name, "name": self._name,
"description": self._description,
"created_at": self._created_at, "created_at": self._created_at,
"filename": self._filename, "filename": self._filename,
"project_id": self._project.id "project_id": self._project.id

View File

@ -25,6 +25,10 @@ SNAPSHOT_CREATE_SCHEMA = {
"description": "Snapshot name", "description": "Snapshot name",
"minLength": 1 "minLength": 1
}, },
"description": {
"description": "Snapshot description",
"minLength": 1
},
}, },
"additionalProperties": False, "additionalProperties": False,
"required": ["name"] "required": ["name"]
@ -59,11 +63,16 @@ SNAPSHOT_OBJECT_SCHEMA = {
"type": "string", "type": "string",
"minLength": 1 "minLength": 1
}, },
"description": {
"description": "Snapshot description",
"type": "string",
"minLength": 1
},
"created_at": { "created_at": {
"description": "Date of the snapshot (UTC timestamp)", "description": "Date of the snapshot (UTC timestamp)",
"type": "integer" "type": "integer"
} }
}, },
"additionalProperties": False, "additionalProperties": False,
"required": ["snapshot_id", "name", "filename", "created_at", "project_id"] "required": ["snapshot_id", "name", "filename", "created_at", "description", "project_id"]
} }

View File

@ -65,6 +65,7 @@ def test_json(project):
"snapshot_id": snapshot._id, "snapshot_id": snapshot._id,
"name": "snapshot_test", "name": "snapshot_test",
"project_id": project.id, "project_id": project.id,
"description": "Snapshot 'snapshot_test' taken on 2016-07-26 at 10:04:39",
"filename": "snapshot_test_260716_100439.gns3project", "filename": "snapshot_test_260716_100439.gns3project",
"created_at": 1469527479 "created_at": 1469527479
} }
@ -76,6 +77,7 @@ def test_json(project):
"name": "snapshot_test2", "name": "snapshot_test2",
"project_id": project.id, "project_id": project.id,
"filename": "snapshot_test2.gns3snapshot", "filename": "snapshot_test2.gns3snapshot",
"description": mock.ANY,
"created_at": mock.ANY "created_at": mock.ANY
} }