add documentation for Flask backend
This commit is contained in:
parent
94adb2a820
commit
6756d86118
2 changed files with 53 additions and 4 deletions
|
|
@ -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
|
||||
|
|
|
|||
38
flask/documentation.html
Normal file
38
flask/documentation.html
Normal 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"> <br><strong class="title">backend</strong></td>
|
||||
<table class="section">
|
||||
<tr class="decor functions-decor heading-text">
|
||||
<td class="section-title" colspan=3> <br><strong class="bigsection">Functions</strong></td></tr>
|
||||
|
||||
<tr><td class="decor functions-decor"><span class="code"> </span></td><td> </td>
|
||||
<td class="singlecolumn"><dl><dt><a name="-fetchItem"><strong>fetchItem</strong></a>(store)</dt><dd><span class="code">Fetch an item from given store that matches request arguments</span></dd></dl>
|
||||
<dl><dt><a name="-fetchItems"><strong>fetchItems</strong></a>(store)</dt><dd><span class="code">Fetch all items from a given store</span></dd></dl>
|
||||
<dl><dt><a name="-fetchStore"><strong>fetchStore</strong></a>(key=None)</dt><dd><span class="code">Fetch a store with the given key. If no key is provided, fetch store based on location data in request arguments</span></dd></dl>
|
||||
<dl><dt><a name="-get"><strong>get</strong></a>()</dt><dd><span class="code">Returns store based on request arguments. If no arguments are provided, returns all stores in database</span></dd></dl>
|
||||
<dl><dt><a name="-getImage"><strong>getImage</strong></a>()</dt><dd><span class="code">Returns store image based on image key provided in request arguments</span></dd></dl>
|
||||
<dl><dt><a name="-post"><strong>post</strong></a>()</dt><dd><span class="code">Adds a store or item to the database depending on request arguments</span></dd></dl>
|
||||
<dl><dt><a name="-put"><strong>put</strong></a>()</dt><dd><span class="code">Updates item or store picture depending on request arguments</span></dd></dl>
|
||||
<dl><dt><a name="-search"><strong>search</strong></a>()</dt><dd><span class="code">Returns items matching query string in searchable radius (currently 50mi/80km)</span></dd></dl>
|
||||
<dl><dt><a name="-updateCheapest"><strong>updateCheapest</strong></a>(item, store)</dt><dd><span class="code">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</span></dd></dl>
|
||||
</td></tr></table><p>
|
||||
<table class="section">
|
||||
<tr class="decor data-decor heading-text">
|
||||
<td class="section-title" colspan=3> <br><strong class="bigsection">Data</strong></td></tr>
|
||||
|
||||
<tr><td class="decor data-decor"><span class="code"> </span></td><td> </td>
|
||||
<td class="singlecolumn"><strong>RADIUS</strong> = 80000<br>
|
||||
<strong>app</strong> = <Flask 'backend'><br>
|
||||
<strong>deta</strong> = <deta.Deta object><br>
|
||||
<strong>drive</strong> = <deta.drive._Drive object><br>
|
||||
<strong>itemsDB</strong> = <deta.base._Base object><br>
|
||||
<strong>request</strong> = <LocalProxy unbound><br>
|
||||
<strong>storesDB</strong> = <deta.base._Base object></td></tr></table>
|
||||
</body></html>
|
||||
Loading…
Add table
Reference in a new issue