add documentation for Flask backend

This commit is contained in:
ak 2023-12-27 14:47:12 -08:00
parent 94adb2a820
commit 6756d86118
2 changed files with 53 additions and 4 deletions

View file

@ -19,7 +19,10 @@ itemsDB = deta.Base('items')
storesDB = deta.Base('stores') storesDB = deta.Base('stores')
drive = deta.Drive('beerbuddy') drive = deta.Drive('beerbuddy')
RADIUS = 80000
def fetchStore(key = None): 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) if key is not None: return storesDB.get(key)
else: else:
northing = float(request.args['northing']) northing = float(request.args['northing'])
@ -49,6 +52,7 @@ def fetchStore(key = None):
return store return store
def fetchItem(store): def fetchItem(store):
""" Fetch an item from given store that matches request arguments """
fetch = itemsDB.fetch({ fetch = itemsDB.fetch({
'lowername': request.args['itemName'].lower(), 'lowername': request.args['itemName'].lower(),
'store': store['key'], 'store': store['key'],
@ -70,6 +74,7 @@ def fetchItem(store):
return item return item
def fetchItems(store): def fetchItems(store):
""" Fetch all items from a given store """
fetch = itemsDB.fetch({ fetch = itemsDB.fetch({
'store': store['key'], 'store': store['key'],
}) })
@ -85,6 +90,7 @@ def fetchItems(store):
return items return items
def updateCheapest(item, store): 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 cheapest = True
storeItems = fetchItems(store) storeItems = fetchItems(store)
itemPerFloz = item['perFloz'] itemPerFloz = item['perFloz']
@ -99,6 +105,7 @@ def updateCheapest(item, store):
@app.route('/', methods=['GET']) @app.route('/', methods=['GET'])
def get(): def get():
""" Returns store based on request arguments. If no arguments are provided, returns all stores in database """
if request.args: if request.args:
if 'storeKey' in request.args: return fetchStore(request.args['storeKey']) if 'storeKey' in request.args: return fetchStore(request.args['storeKey'])
@ -126,6 +133,7 @@ def get():
@app.route('/', methods=['POST']) @app.route('/', methods=['POST'])
def post(): def post():
""" Adds a store or item to the database depending on request arguments """
if request.args: 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: 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']) northing = float(request.args['northing'])
@ -211,6 +219,7 @@ def post():
@app.route('/', methods=['PUT']) @app.route('/', methods=['PUT'])
def put(): def put():
""" Updates item or store picture depending on request arguments """
if request.args: if request.args:
if 'storeKey' in request.args: if 'storeKey' in request.args:
store = fetchStore(request.args['storeKey']) store = fetchStore(request.args['storeKey'])
@ -247,6 +256,7 @@ def put():
@app.route('/img', methods=['GET']) @app.route('/img', methods=['GET'])
def getImage(): def getImage():
""" Returns store image based on image key provided in request arguments """
if request.args: if request.args:
if 'imageKey' in request.args: if 'imageKey' in request.args:
mimetype = filetype.guess(drive.get(request.args['imageKey']).read()).mime mimetype = filetype.guess(drive.get(request.args['imageKey']).read()).mime
@ -255,21 +265,22 @@ def getImage():
@app.route('/search', methods=['GET']) @app.route('/search', methods=['GET'])
def search(): def search():
""" Returns items matching query string in searchable radius (currently 50mi/80km) """
if request.args: if request.args:
if 'easting' in request.args and 'northing' and 'query' in request.args: if 'easting' in request.args and 'northing' and 'query' in request.args:
northing = float(request.args['northing']) northing = float(request.args['northing'])
easting = float(request.args['easting']) easting = float(request.args['easting'])
fetch = storesDB.fetch({ fetch = storesDB.fetch({
'easting?r': [easting - 80000, easting + 80000], 'easting?r': [easting - RADIUS, easting + RADIUS],
'northing?r': [northing - 80000, northing + 80000] 'northing?r': [northing - RADIUS, northing + RADIUS]
}) })
stores = fetch.items stores = fetch.items
while (fetch.last is not None): while (fetch.last is not None):
fetch = storesDB.fetch({ fetch = storesDB.fetch({
'easting?r': [easting + 80000, easting + 80000], 'easting?r': [easting + RADIUS, easting + RADIUS],
'northing?r': [northing - 80000, northing + 80000], 'northing?r': [northing - RADIUS, northing + RADIUS],
last: fetch.last last: fetch.last
}) })
stores = stores + fetch.items stores = stores + fetch.items

38
flask/documentation.html Normal file
View file

@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Python: module backend</title>
</head><body>
<table class="heading">
<tr class="heading-text decor">
<td class="title">&nbsp;<br><strong class="title">backend</strong></td>
<table class="section">
<tr class="decor functions-decor heading-text">
<td class="section-title" colspan=3>&nbsp;<br><strong class="bigsection">Functions</strong></td></tr>
<tr><td class="decor functions-decor"><span class="code">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></td><td>&nbsp;</td>
<td class="singlecolumn"><dl><dt><a name="-fetchItem"><strong>fetchItem</strong></a>(store)</dt><dd><span class="code">Fetch&nbsp;an&nbsp;item&nbsp;from&nbsp;given&nbsp;store&nbsp;that&nbsp;matches&nbsp;request&nbsp;arguments</span></dd></dl>
<dl><dt><a name="-fetchItems"><strong>fetchItems</strong></a>(store)</dt><dd><span class="code">Fetch&nbsp;all&nbsp;items&nbsp;from&nbsp;a&nbsp;given&nbsp;store</span></dd></dl>
<dl><dt><a name="-fetchStore"><strong>fetchStore</strong></a>(key=None)</dt><dd><span class="code">Fetch&nbsp;a&nbsp;store&nbsp;with&nbsp;the&nbsp;given&nbsp;key.&nbsp;If&nbsp;no&nbsp;key&nbsp;is&nbsp;provided,&nbsp;fetch&nbsp;store&nbsp;based&nbsp;on&nbsp;location&nbsp;data&nbsp;in&nbsp;request&nbsp;arguments</span></dd></dl>
<dl><dt><a name="-get"><strong>get</strong></a>()</dt><dd><span class="code">Returns&nbsp;store&nbsp;based&nbsp;on&nbsp;request&nbsp;arguments.&nbsp;If&nbsp;no&nbsp;arguments&nbsp;are&nbsp;provided,&nbsp;returns&nbsp;all&nbsp;stores&nbsp;in&nbsp;database</span></dd></dl>
<dl><dt><a name="-getImage"><strong>getImage</strong></a>()</dt><dd><span class="code">Returns&nbsp;store&nbsp;image&nbsp;based&nbsp;on&nbsp;image&nbsp;key&nbsp;provided&nbsp;in&nbsp;request&nbsp;arguments</span></dd></dl>
<dl><dt><a name="-post"><strong>post</strong></a>()</dt><dd><span class="code">Adds&nbsp;a&nbsp;store&nbsp;or&nbsp;item&nbsp;to&nbsp;the&nbsp;database&nbsp;depending&nbsp;on&nbsp;request&nbsp;arguments</span></dd></dl>
<dl><dt><a name="-put"><strong>put</strong></a>()</dt><dd><span class="code">Updates&nbsp;item&nbsp;or&nbsp;store&nbsp;picture&nbsp;depending&nbsp;on&nbsp;request&nbsp;arguments</span></dd></dl>
<dl><dt><a name="-search"><strong>search</strong></a>()</dt><dd><span class="code">Returns&nbsp;items&nbsp;matching&nbsp;query&nbsp;string&nbsp;in&nbsp;searchable&nbsp;radius&nbsp;(currently&nbsp;50mi/80km)</span></dd></dl>
<dl><dt><a name="-updateCheapest"><strong>updateCheapest</strong></a>(item, store)</dt><dd><span class="code">Compare&nbsp;price&nbsp;of&nbsp;current&nbsp;item&nbsp;to&nbsp;other&nbsp;items&nbsp;in&nbsp;the&nbsp;given&nbsp;store&nbsp;and&nbsp;update&nbsp;store's&nbsp;cheapest&nbsp;item&nbsp;if&nbsp;this&nbsp;item&nbsp;is&nbsp;cheaper&nbsp;than&nbsp;the&nbsp;others</span></dd></dl>
</td></tr></table><p>
<table class="section">
<tr class="decor data-decor heading-text">
<td class="section-title" colspan=3>&nbsp;<br><strong class="bigsection">Data</strong></td></tr>
<tr><td class="decor data-decor"><span class="code">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></td><td>&nbsp;</td>
<td class="singlecolumn"><strong>RADIUS</strong> = 80000<br>
<strong>app</strong> = &lt;Flask 'backend'&gt;<br>
<strong>deta</strong> = &lt;deta.Deta object&gt;<br>
<strong>drive</strong> = &lt;deta.drive._Drive object&gt;<br>
<strong>itemsDB</strong> = &lt;deta.base._Base object&gt;<br>
<strong>request</strong> = &lt;LocalProxy unbound&gt;<br>
<strong>storesDB</strong> = &lt;deta.base._Base object&gt;</td></tr></table>
</body></html>