Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fireworks/flask_site/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def _addq_WF(q):
def datetime(value):
import datetime as dt

date = dt.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f")
date = dt.datetime.fromisoformat(value)
return date.strftime("%m/%d/%Y")


Expand Down
29 changes: 29 additions & 0 deletions fireworks/utilities/filepad.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import zlib
from typing import TYPE_CHECKING

from uuid import uuid4
import gridfs
from bson.objectid import ObjectId
from monty.json import MSONable
Expand Down Expand Up @@ -154,6 +155,34 @@ def add_file(self, path, identifier=None, compress=True, metadata=None):
contents = f.read()
return self._insert_contents(contents, identifier, root_data, compress)

def add_contents(self, contents, identifier=None, compress=True, metadata=None):
"""Insert the file specified by the path into gridfs. The gridfs id and identifier are returned.
Note: identifier must be unique, i.e, no insertion if the identifier already exists in the db.

Args:
contents (bytes): bytes object to insert
identifier (str): file identifier. If identifier = None then the identifier is set to the object id
returned by gridfs insertion.
compress (bool): compress or not
metadata (dict): file metadata

Returns:
(str, str): the id returned by gridfs, identifier
"""
if identifier is not None:
_, doc = self.get_file(identifier)
if doc is not None:
self.logger.warning(f"{identifier=} exists. Skipping insertion")
return doc["gfs_id"], doc["identifier"]

root_data = {
"identifier": identifier,
"metadata": metadata,
"compressed": compress,
}

return self._insert_contents(contents, identifier, root_data, compress)

def get_file(self, identifier):
"""Get file by identifier.

Expand Down