From 91b84710a4b87fa6f71870fcf091727939daec2d Mon Sep 17 00:00:00 2001 From: dave Date: Tue, 18 Jun 2019 18:38:01 -0700 Subject: [PATCH] hash, set, and file retrival endpoints --- photoapp/api.py | 21 +++++++++++++++------ photoapp/types.py | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/photoapp/api.py b/photoapp/api.py index f084390..9c0a87c 100644 --- a/photoapp/api.py +++ b/photoapp/api.py @@ -21,10 +21,6 @@ class PhotosApi(object): class PhotosApiV1(object): def __init__(self): - # self.tpl.filters.update(mime2ext=mime2ext, - # basename=os.path.basename, - # ceil=math.ceil, - # statusstr=lambda x: str(x).split(".")[-1]) pass @cherrypy.expose @@ -37,5 +33,18 @@ class PhotosApiV1(object): @cherrypy.expose @cherrypy.tools.json_out() - def findbysha(self, sha): - pass + def byhash(self, sha): + f = db().query(Photo).filter(Photo.hash == sha).first() + return f.to_json() + + @cherrypy.expose + @cherrypy.tools.json_out() + def set(self, uuid): + s = db().query(PhotoSet).filter(PhotoSet.uuid == uuid).first() + return s.to_json() + + @cherrypy.expose + def download(self, uuid): + f = db().query(Photo).filter(Photo.uuid == uuid).first() + return cherrypy.lib.static.serve_file(os.path.abspath(os.path.join("./library", f.path)), + f.format)#TODO no hardcode path diff --git a/photoapp/types.py b/photoapp/types.py index a8df0a6..2e8965e 100644 --- a/photoapp/types.py +++ b/photoapp/types.py @@ -33,6 +33,15 @@ class PhotoSet(Base): status = Column(Enum(PhotoStatus), default=PhotoStatus.private) + def to_json(self): + s = {attr: getattr(self, attr) for attr in {"uuid", "title", "description"}} + s["lat"] = str(self.lat) + s["lon"] = str(self.lon) + s["date"] = self.date.isoformat() + s["files"] = {i.uuid: i.to_json() for i in self.files} + s["tags"] = [t.name for t in self.tags] + return s + class Photo(Base): __tablename__ = 'files' @@ -51,6 +60,12 @@ class Photo(Base): path = Column(Unicode) format = Column(String(length=64)) # TODO how long can a mime string be + def to_json(self): + j = {attr: getattr(self, attr) for attr in + {"uuid", "size", "width", "height", "orientation", "format"}} + j["set"] = self.set.uuid + return j + class Tag(Base): __tablename__ = 'tags'