This repository has been archived by the owner on Oct 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.py
180 lines (154 loc) · 4.66 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#! /usr/bin/env python3
from wrangle import (
fetch_data,
get_sorted,
item_repr,
)
from flask import (
send_from_directory,
render_template,
redirect, request,
session
)
from flask_api import FlaskAPI, exceptions
from flask_caching import Cache
from airtable import airtable
import os
from random import getrandbits
from urllib.parse import unquote_plus
from dotenv import load_dotenv
load_dotenv()
try:
VERSION = os.getenv("VERCEL_GIT_COMMIT_SHA", 1)
SORT_KEY = os.getenv("SORT_KEY", None)
SORT_REVERSE = os.getenv("SORT_REVERSE", None)
BASE_FIELDS = os.getenv("BASE_FIELDS", '')
POPUP_FIELDS = os.getenv("POPUP_FIELDS", '')
REQUIRED_FIELDS = os.getenv("REQUIRED_FIELDS", POPUP_FIELDS)
START_EMPTY = int(os.getenv("START_EMPTY", 0))
MAPBOX_KEY = os.getenv("MAPBOX_KEY", '')
TABLE_NAME = os.environ["AIRTABLE_TABLE"]
AIRTABLE_LINK = os.getenv("AIRTABLE_LINK", '')
AIRTABLE_FORM = os.getenv("AIRTABLE_FORM", '')
at = airtable.Airtable(
os.environ["AIRTABLE_BASE"],
os.environ["AIRTABLE_KEY"],
)
except KeyError:
print("Environment not ready: see README for required keys")
exit(1)
# Check for reverse option
if SORT_KEY.startswith('-'):
SORT_REVERSE = True
SORT_KEY = SORT_KEY[1:]
# Set up the Flask App
app = FlaskAPI(__name__, static_url_path='/static')
app.secret_key = bytes([getrandbits(8) for _ in range(0, 16)])
app.config.update(
SORT_KEY=SORT_KEY,
SORT_REVERSE=SORT_REVERSE,
MAPBOX_KEY=MAPBOX_KEY,
BASE_FIELDS=BASE_FIELDS,
POPUP_FIELDS=POPUP_FIELDS,
REQUIRED_FIELDS=REQUIRED_FIELDS,
START_EMPTY=START_EMPTY,
AIRTABLE_LINK=AIRTABLE_LINK,
AIRTABLE_FORM=AIRTABLE_FORM,
TABLE_NAME=TABLE_NAME,
VERSION=VERSION,
)
cache = Cache(config={'CACHE_TYPE': 'SimpleCache'})
cache.init_app(app)
@app.route("/items", methods=['GET'])
@cache.cached(timeout=500)
def items_list():
"""
List sorted items
"""
if 'items' not in session:
session['items'] = fetch_data(at)
g_items = session['items']
g_sortd = get_sorted(g_items, SORT_REVERSE)
return [item_repr(g_items[idx], idx, request.host_url) for idx in g_sortd]
@app.route("/filter", methods=['GET'])
def items_filter():
"""
Filter by category
"""
q = unquote_plus(request.args.get('q'))
if q == '':
return items_list()
print('Filtering:', q)
if 'items' not in session:
session['items'] = fetch_data(at)
g_items = session['items']
gquery = q.split(',')
gfilter = {}
for idx in g_items.keys():
matching = None
gk = g_items[idx]
for gfield in gquery:
flt, value = gfield.split(':')
# All popup fields must match
if flt in gk and flt in POPUP_FIELDS and matching in [None, True]:
matching = value in gk[flt]
if matching:
gfilter[idx] = gk
g_sortd = get_sorted(gfilter, SORT_REVERSE)
return [item_repr(g_items[idx], idx, request.host_url) for idx in g_sortd]
@app.route("/search", methods=['GET'])
def items_search():
"""
Search by keyword
"""
q = request.args.get('q')
print('Searching:', q)
if 'items' not in session:
session['items'] = fetch_data(at)
g_items = session['items']
gfilter = {}
for idx in g_items.keys():
gk = g_items[idx]
matching = False
for fld in gk.keys():
s = None
if isinstance(gk[fld], str):
s = gk[fld]
elif isinstance(gk[fld], list) and isinstance(gk[fld][0], str):
s = gk[fld][0]
if s and q.lower() in s.lower():
matching = True
break
if matching:
gfilter[idx] = gk
g_sortd = get_sorted(gfilter, SORT_REVERSE)
return [item_repr(g_items[idx], idx, request.host_url) for idx in g_sortd]
@app.route("/detail/<key>/", methods=['GET'])
@cache.cached(timeout=250)
def item_detail(key):
"""
Retrieve instances
"""
if 'items' not in session:
session['items'] = fetch_data(at)
g_items = session['items']
if key not in g_items:
raise exceptions.NotFound()
return item_repr(g_items, key, request.host_url)
@app.route('/')
@app.route('/app')
@cache.cached(timeout=1000)
def route_index():
if MAPBOX_KEY:
return render_template('map.html')
else:
return render_template('gallery.html')
@app.route('/refresh')
def route_refresh():
session['items'] = fetch_data(at)
return redirect("/", code=302)
@app.route('/static/<path:path>')
def route_static(path):
return send_from_directory('static', path)
if __name__ == "__main__":
app.run(debug=True)