diff --git a/gns3server/controller/snapshot.py b/gns3server/controller/snapshot.py index 127495b63..9030c7c0d 100644 --- a/gns3server/controller/snapshot.py +++ b/gns3server/controller/snapshot.py @@ -35,16 +35,18 @@ import logging log = logging.getLogger(__name__) -# The string use to extract the date from the filename -FILENAME_TIME_FORMAT = "%d%m%y_%H%M%S" +# Used to extract the date and time from the filename +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: """ 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" @@ -66,8 +68,13 @@ class Snapshot: else: self._name = filename.rsplit("_", 2)[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._path = os.path.join(project.path, "snapshots", filename) @@ -79,13 +86,17 @@ class Snapshot: def name(self): return self._name + @property + def description(self): + return self._description + @property def path(self): return self._path @property def created_at(self): - return int(self._created_at) + return self._created_at async def create(self): """ @@ -150,6 +161,7 @@ class Snapshot: return { "snapshot_id": self._id, "name": self._name, + "description": self._description, "created_at": self._created_at, "filename": self._filename, "project_id": self._project.id diff --git a/gns3server/schemas/snapshot.py b/gns3server/schemas/snapshot.py index 7481c6457..ddc241171 100644 --- a/gns3server/schemas/snapshot.py +++ b/gns3server/schemas/snapshot.py @@ -25,6 +25,10 @@ SNAPSHOT_CREATE_SCHEMA = { "description": "Snapshot name", "minLength": 1 }, + "description": { + "description": "Snapshot description", + "minLength": 1 + }, }, "additionalProperties": False, "required": ["name"] @@ -59,11 +63,16 @@ SNAPSHOT_OBJECT_SCHEMA = { "type": "string", "minLength": 1 }, + "description": { + "description": "Snapshot description", + "type": "string", + "minLength": 1 + }, "created_at": { "description": "Date of the snapshot (UTC timestamp)", "type": "integer" } }, "additionalProperties": False, - "required": ["snapshot_id", "name", "filename", "created_at", "project_id"] + "required": ["snapshot_id", "name", "filename", "created_at", "description", "project_id"] } diff --git a/tests/controller/test_snapshot.py b/tests/controller/test_snapshot.py index e57cc5c5f..27299c7d9 100644 --- a/tests/controller/test_snapshot.py +++ b/tests/controller/test_snapshot.py @@ -65,6 +65,7 @@ def test_json(project): "snapshot_id": snapshot._id, "name": "snapshot_test", "project_id": project.id, + "description": "Snapshot 'snapshot_test' taken on 2016-07-26 at 10:04:39", "filename": "snapshot_test_260716_100439.gns3project", "created_at": 1469527479 } @@ -76,6 +77,7 @@ def test_json(project): "name": "snapshot_test2", "project_id": project.id, "filename": "snapshot_test2.gns3snapshot", + "description": mock.ANY, "created_at": mock.ANY }