From 6756d861189e9cb5ef5bbb701e33f8dd7d3af6fe Mon Sep 17 00:00:00 2001 From: ak Date: Wed, 27 Dec 2023 14:47:12 -0800 Subject: [PATCH] add documentation for Flask backend --- flask/backend.py | 19 +++++++++++++++---- flask/documentation.html | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 flask/documentation.html diff --git a/flask/backend.py b/flask/backend.py index 1960ac1..9500694 100644 --- a/flask/backend.py +++ b/flask/backend.py @@ -19,7 +19,10 @@ itemsDB = deta.Base('items') storesDB = deta.Base('stores') drive = deta.Drive('beerbuddy') +RADIUS = 80000 + def fetchStore(key = None): + """ Fetch a store with the given key. If no key is provided, fetch store based on location data in request arguments """ if key is not None: return storesDB.get(key) else: northing = float(request.args['northing']) @@ -49,6 +52,7 @@ def fetchStore(key = None): return store def fetchItem(store): + """ Fetch an item from given store that matches request arguments """ fetch = itemsDB.fetch({ 'lowername': request.args['itemName'].lower(), 'store': store['key'], @@ -70,6 +74,7 @@ def fetchItem(store): return item def fetchItems(store): + """ Fetch all items from a given store """ fetch = itemsDB.fetch({ 'store': store['key'], }) @@ -85,6 +90,7 @@ def fetchItems(store): return items def updateCheapest(item, store): + """ Compare price of current item to other items in the given store and update store's cheapest item if this item is cheaper than the others """ cheapest = True storeItems = fetchItems(store) itemPerFloz = item['perFloz'] @@ -99,6 +105,7 @@ def updateCheapest(item, store): @app.route('/', methods=['GET']) def get(): + """ Returns store based on request arguments. If no arguments are provided, returns all stores in database """ if request.args: if 'storeKey' in request.args: return fetchStore(request.args['storeKey']) @@ -126,6 +133,7 @@ def get(): @app.route('/', methods=['POST']) def post(): + """ Adds a store or item to the database depending on request arguments """ if request.args: if 'easting' in request.args and 'northing' in request.args and 'zone' in request.args and 'zoneLetter' and 'name' in request.args: northing = float(request.args['northing']) @@ -211,6 +219,7 @@ def post(): @app.route('/', methods=['PUT']) def put(): + """ Updates item or store picture depending on request arguments """ if request.args: if 'storeKey' in request.args: store = fetchStore(request.args['storeKey']) @@ -247,6 +256,7 @@ def put(): @app.route('/img', methods=['GET']) def getImage(): + """ Returns store image based on image key provided in request arguments """ if request.args: if 'imageKey' in request.args: mimetype = filetype.guess(drive.get(request.args['imageKey']).read()).mime @@ -255,21 +265,22 @@ def getImage(): @app.route('/search', methods=['GET']) def search(): + """ Returns items matching query string in searchable radius (currently 50mi/80km) """ if request.args: if 'easting' in request.args and 'northing' and 'query' in request.args: northing = float(request.args['northing']) easting = float(request.args['easting']) fetch = storesDB.fetch({ - 'easting?r': [easting - 80000, easting + 80000], - 'northing?r': [northing - 80000, northing + 80000] + 'easting?r': [easting - RADIUS, easting + RADIUS], + 'northing?r': [northing - RADIUS, northing + RADIUS] }) stores = fetch.items while (fetch.last is not None): fetch = storesDB.fetch({ - 'easting?r': [easting + 80000, easting + 80000], - 'northing?r': [northing - 80000, northing + 80000], + 'easting?r': [easting + RADIUS, easting + RADIUS], + 'northing?r': [northing - RADIUS, northing + RADIUS], last: fetch.last }) stores = stores + fetch.items diff --git a/flask/documentation.html b/flask/documentation.html new file mode 100644 index 0000000..00c9fbe --- /dev/null +++ b/flask/documentation.html @@ -0,0 +1,38 @@ + + + + +Python: module backend + + + + + +
 
backend
+ + + + +
 
Functions
       
fetchItem(store)
Fetch an item from given store that matches request arguments
+
fetchItems(store)
Fetch all items from a given store
+
fetchStore(key=None)
Fetch a store with the given key. If no key is provided, fetch store based on location data in request arguments
+
get()
Returns store based on request arguments. If no arguments are provided, returns all stores in database
+
getImage()
Returns store image based on image key provided in request arguments
+
post()
Adds a store or item to the database depending on request arguments
+
put()
Updates item or store picture depending on request arguments
+
search()
Returns items matching query string in searchable radius (currently 50mi/80km)
+
updateCheapest(item, store)
Compare price of current item to other items in the given store and update store's cheapest item if this item is cheaper than the others
+

+ + + + + +
 
Data
       RADIUS = 80000
+app = <Flask 'backend'>
+deta = <deta.Deta object>
+drive = <deta.drive._Drive object>
+itemsDB = <deta.base._Base object>
+request = <LocalProxy unbound>
+storesDB = <deta.base._Base object>
+ \ No newline at end of file