-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.py
354 lines (311 loc) · 14 KB
/
main.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
from typing import Collection
from flask import Flask, request, redirect, url_for, render_template, send_file, jsonify, make_response
from deta import Deta
from dotenv import load_dotenv
import os
import datetime
from werkzeug.utils import secure_filename
import pytz
import random
from flask_cors import CORS
load_dotenv()
path = "" if os.getenv("LOCAL_DEV") else "https://" + os.getenv("DETA_PATH")
host = ".deta.app" if os.getenv("DETA_SPACE_APP") else "" if os.getenv(
"LOCAL_DEV") else ".deta.dev"
app = Flask(__name__)
CORS(app)
deta = Deta()
utc = pytz.utc
collectionsDB = deta.Base("collections")
contentDB = deta.Base("content")
drive = deta.Drive("files")
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
@app.route('/', methods=["GET"])
def index():
print(request.args.get('search'))
items = collectionsDB.fetch({
"titleCaps?contains": request.args.get('search').upper()
}if request.args.get('search') != None and request.args.get('search').replace(' ', '') != '' else {}, pages=1, buffer=50000)
for sub_list in items:
items = sub_list
print(items)
r = make_response(render_template('index.html', collections=items, searchPlaceholder=request.args.get(
'search')if request.args.get('search') != None and request.args.get('search').replace(' ', '') != '' else 'Filter results'))
r.headers.set('cache-control', 'no-store')
return r
@app.route("/<id>/filesJSON", methods=['GET', 'POST'])
def filesJSON(id):
if request.method == 'POST':
# check if the post request has the file part
if id not in request.files:
print('No file part')
return redirect(request.url)
file = request.files[id]
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '':
print('No selected file')
return redirect(request.url)
if file:
filename = secure_filename(file.filename)
date = str(datetime.datetime.now(utc))
res = drive.put(date.replace(' ', '') + filename, file)
return {'file': date.replace(' ', '') + filename}
items = drive.list()
print(items)
return render_template('files.html', title='Files', files=items['names'], filesPage=True, collection=False)
@app.route("/files", methods=['GET', 'POST'])
def files():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
print('No file part')
return redirect(request.url)
file = request.files['file']
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '':
print('No selected file')
return redirect(request.url)
if file:
filename = secure_filename(file.filename)
res = drive.put(str(datetime.datetime.now(
utc)).replace(' ', '') + filename, file)
items = drive.list()
print(items)
return render_template('files.html', title='Files', files=items['names'])
items = drive.list()
if request.args.get('search') != None and request.args.get('search').replace(' ', '') != '':
print(items)
items = {'names': [a for a in items['names']
if request.args.get('search').upper() in a.upper()]}
print(items)
return render_template('files.html', title='Files', files=items['names'], filesPage=True, collection=False, searchPlaceholder=request.args.get('search')if request.args.get('search') != None and request.args.get('search').replace(' ', '') != '' else 'Filter results')
@app.route("/api", methods=['GET', 'POST'])
def api():
items = collectionsDB.fetch({}, pages=1, buffer=50000)
for sub_list in items:
items = sub_list
try:
contentItems = contentDB.fetch(
{"collectionKey": items[0]["key"]}, pages=1, buffer=50000)
print(contentItems)
for sub_list in contentItems:
contentItems = sub_list
except:
contentItems = []
print(contentItems)
return render_template('api.html', collections=items, exampleKey=contentItems[0]['key'] if len(contentItems) > 0 else None)
@app.route('/new', methods=['GET', 'POST'])
def new():
if request.method == 'POST':
formData = list(request.form.items())
finalData = [{}]
for x, y in enumerate(formData):
if 'fieldType' in y[0]:
finalData.insert(
(int(y[0].replace('fieldType', '')) - 1), {'type': y[1]})
if 'fieldTitle' in y[0]:
finalData[int(y[0].replace('fieldTitle', '')) -
1]['title'] = y[1]
for x, y in enumerate(finalData):
print(x, y)
finalData[x]['id'] = random.randint(0, 10000000000000)
collectionsDB.insert({
"title": request.form['title'],
"titleCaps": request.form['title'].upper(),
"templateItems": finalData,
"lastUpdated": str(datetime.datetime.now(utc))
})
return redirect(f"/")
else:
return render_template('new.html')
@app.route('/collection/<id>', methods=['GET'])
def collection(id):
data = collectionsDB.get(id)
items = contentDB.fetch({"collectionKey": id,
"titleCaps?contains": request.args.get('search').upper()
}if request.args.get('search') != None and request.args.get('search').replace(' ', '') != '' else {"collectionKey": id}, pages=1, buffer=50000)
for sub_list in items:
items = sub_list
print(items)
return render_template("collection.html", title=data['title'], items=items, filesPage=False, collection=True, collectionId=id, searchPlaceholder=request.args.get('search')if request.args.get('search') != None and request.args.get('search').replace(' ', '') != '' else 'Filter results', collectionIdDisplay='('+id+')')
@app.route('/collection/<id>/edit', methods=['GET'])
def collectionEdit(id):
data = collectionsDB.get(id)
return render_template("editCollection.html", title=data['title'], collectionId=id, collectionIdDisplay='('+id+')', collectionItems = data['templateItems'])
@app.route('/collection/<id>/edit/new-field', methods=['POST'])
def collectionAddField(id):
data = collectionsDB.get(id)
print(data['templateItems'])
data['templateItems'].append({
"id": random.randint(0, 10000000000000),
"title": request.form['fieldTitle'],
"type": request.form['fieldType']
})
collectionsDB.put(data)
return redirect(f"/collection/"+id+"/edit")
@app.route('/collection/<id>/edit/edit-field/<field>', methods=['POST'])
def collectionEditField(id, field):
data = collectionsDB.get(id)
for templateItemIndex, templateItem in enumerate(data['templateItems']):
if int(templateItem['id']) == int(field):
data['templateItems'][templateItemIndex] = {
"id": int(field),
"title": request.form['fieldTitle'],
"type": request.form['fieldType']
}
collectionsDB.put(data)
return redirect(f"/collection/"+id+"/edit")
@app.route('/collection/<id>/edit/delete-field/<field>', methods=['GET'])
def collectionDeleteField(id, field):
data = collectionsDB.get(id)
for templateItemIndex, templateItem in enumerate(data['templateItems']):
if int(templateItem['id']) == int(field):
del data['templateItems'][templateItemIndex]
collectionsDB.put(data)
return redirect(f"/collection/"+id+"/edit")
@app.route('/collection/<id>/delete', methods=['GET'])
def collectionDelete(id):
collectionsDB.delete(id)
return redirect(f"/")
@app.route('/content/<id>/delete', methods=['GET'])
def contentDelete(id):
contentData = contentDB.get(id)
contentDB.delete(id)
return redirect(f"/collection/"+contentData['collectionKey'])
@app.route('/content/<id>', methods=['GET', 'POST'])
def content(id):
if request.method == 'POST':
formData = list(request.form.items())
print(formData)
contentData = contentDB.get(id)
collectionData = collectionsDB.get(contentData['collectionKey'])
content = contentData['content']
if contentData['content'] == {}:
for x in collectionData['templateItems']:
if 'title' in x:
content[x['id']] = {'type': x['type'], 'title': x['title']}
else:
for x in collectionData['templateItems']:
if 'title' in x and x['id'] not in content:
content[x['id']] = x
title = ""
published = False
for x in enumerate(formData):
if x[1][0] == 'content_title':
title = x[1][1]
elif x[1][0] == 'published-checkbox':
published = True
elif '-files-upload-box' in x[1][0]:
print('skipping')
elif '-file-checkbox-' in x[1][0]:
if 'value' not in content[int(x[1][0].split('-file-checkbox-')[0])]:
content[int(x[1][0].split('-file-checkbox-')[0])]['value'] = []
if x[1][1] == 'on':
content[int(x[1][0].split('-file-checkbox-')[0])]['value'].append(x[1][0].split('-file-checkbox-')[1])
elif content[int(x[1][0])]["type"] == 'String Array':
content[int(x[1][0])]['value'] = x[1][1].split(',')
else:
content[int(x[1][0])]['value'] = x[1][1]
contentArray = list(content.items())
contentDB.update({'content': content, 'published': published, 'title': title, 'titleCaps': title.upper(),
"lastUpdated": str(datetime.datetime.now(utc))}, id)
return redirect(f"/collection/{contentData['collectionKey']}")
getContentData = contentDB.get(id)
getCollectionData = collectionsDB.get(getContentData['collectionKey'])
getContent = getContentData['content']
templateItemsKeys = []
for x in getCollectionData['templateItems']:
print()
templateItemsKeys.append(str(x['id']))
if getContentData['content'] == {}:
print('hi!')
for x in getCollectionData['templateItems']:
print(x)
if 'title' in x:
getContent[str(x['id'])] = {'type': x['type'], 'title': x['title']}
else:
for x in getCollectionData['templateItems']:
print(x)
if 'title' in x and str(x['id']) not in getContent:
print(x)
print('test:')
print(x)
getContent[int(x['id'])] = x
elif 'title' in x and str(x['id']) in getContent:
x['value'] = getContent[str(x['id'])]['value'] if 'value' in getContent[str(x['id'])] else ''
getContent[str(x['id'])] = x
for x in list(getContent):
print(x)
if str(x) not in templateItemsKeys:
print('deleting:')
print(x)
del getContent[x]
getContentArray = list(getContent.items())
return render_template('edit.html', content=getContentArray, title=getContentData['title'], published="checked"if getContentData['published'] == True else "", contentId=id)
@app.route('/collection/<id>/new', methods=['GET', 'POST'])
def newContent(id):
res = contentDB.insert(
{"collectionKey": id, "content": {}, "title": "Unnamed Content", "published": False, "lastUpdated": str(datetime.datetime.now(utc))})
print(res)
return redirect(f"/content/" + res['key'])
@app.route('/api/content/<id>', methods=['GET'])
def apiContent(id):
getContentData = contentDB.get(id)
return jsonify({
"collectionKey": getContentData["collectionKey"],
"content": getContentData["content"],
"lastUpdated": getContentData["lastUpdated"],
"title": getContentData["title"]
})
@app.route('/api/collection/<id>', methods=['GET'])
def apiCollection(id):
includeContent = False
def formatItem(n):
return {"key": n["key"],
"lastUpdated": n["lastUpdated"],
"title": n['title']} if includeContent == False else {"key": n["key"],
"lastUpdated": n["lastUpdated"],
"title": n['title'], 'content': n['content']}
data = collectionsDB.get(id)
providedDetaQuery = request.args.to_dict()
detaQuery = {}
for x in providedDetaQuery:
detaQuery[x.replace('!', '?')] = providedDetaQuery[x]
detaQuery['collectionKey'] = id
detaQuery['published'] = True
if 'content' in detaQuery:
includeContent = True
detaQuery.pop('content', None)
items = contentDB.fetch(detaQuery, pages=1, buffer=50000)
for sub_list in items:
items = sub_list
return jsonify({"title": data["title"], "items": list(map(formatItem, items))})
@app.route('/api/collections', methods=['GET'])
def apiCollections():
includeTemplate = False
def formatItem(n):
return {"key": n["key"],
"lastUpdated": n["lastUpdated"],
"title": n['title']} if includeTemplate == False else {"key": n["key"],
"lastUpdated": n["lastUpdated"],
"title": n['title'], 'templateItems': n['templateItems']}
providedDetaQuery = request.args.to_dict()
detaQuery = {}
for x in providedDetaQuery:
detaQuery[x.replace('!', '?')] = providedDetaQuery[x]
if 'template' in detaQuery:
includeTemplate = True
detaQuery.pop('template', None)
print(detaQuery)
items = collectionsDB.fetch(detaQuery, pages=1, buffer=50000)
for sub_list in items:
items = sub_list
return jsonify(list(map(formatItem, items)))
@app.route('/file/<id>', methods=['GET'])
def getFile(id):
res = drive.get(id)
return send_file(res, download_name=id)
if __name__ == "__main__":
app.run(debug=True)