-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
216 lines (186 loc) · 7.29 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
from flask import g, Flask, render_template, request, send_file, redirect, session, jsonify
import os
import re
import sqlite3
import glob
import re
import base64
import html
import requests
import uuid
from werkzeug.utils import secure_filename
app = Flask(__name__)
UPLOAD_PATH_REL = "static/uploads"
UPLOAD_PATH = os.path.join(os.path.dirname(__file__), UPLOAD_PATH_REL)
DATABASE = './database.db'
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
return db
def init_db():
with app.app_context():
db = get_db()
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
def query_db(query, args=(), one=False):
cur = get_db().execute(query, args)
rv = cur.fetchall()
cur.close()
return (rv[0] if rv else None) if one else rv
def parse_multi_form(form):
data = {}
for url_k, v in form.lists():
if ('' in v):
continue
v = v[0]
ks = []
while url_k:
if '[' in url_k:
k, r = url_k.split('[', 1)
ks.append(k)
if r[0] == ']':
ks.append('')
url_k = r.replace(']', '', 1)
else:
ks.append(url_k)
break
sub_data = data
for i, k in enumerate(ks):
if k.isdigit():
k = int(k)
if i+1 < len(ks):
if not isinstance(sub_data, dict):
break
if k in sub_data:
sub_data = sub_data[k]
else:
sub_data[k] = {}
sub_data = sub_data[k]
else:
if isinstance(sub_data, dict):
sub_data[k] = v
return data
def fnIterCategories(aCategories, aNewCategories, aOpened, iLevel=0):
for oCategory in aCategories:
sID = oCategory[0]
# SELECT * FROM categories WHERE parent_id=?
aQueryCategories = query_db("SELECT * FROM categories WHERE parent_id=?", (sID,))
aIterCategories = []
if (sID in aOpened) and aQueryCategories and len(aQueryCategories)>0:
aIterCategories = fnIterCategories(aQueryCategories, aNewCategories, aOpened, iLevel+1)
oCntImages = query_db("SELECT COUNT(*) FROM images WHERE category_id=?", (oCategory[0],))
oCategory = list(oCategory)
sSpace = ""
if iLevel:
sSpace = " "
oCategory[1] = iLevel * " " + sSpace + oCategory[1]
oCategory.append(oCntImages[0][0])
aNewCategories = aNewCategories + [oCategory] + aIterCategories
print(">>", aNewCategories)
return aNewCategories
@app.route("/", methods=['GET', 'POST'])
def index():
if (request.args.get('init_db', '')=='1'):
print("=========================================================")
print("INIT DB")
init_db()
print("=========================================================")
return redirect("/")
sBaseURL = request.url
sSelGroup = request.args.get('sSelGroup', '')
sSelCategory = request.args.get('sSelCategory', '')
sSelImage = request.args.get('sSelImage', '')
print("args ", request.args)
print("form ", request.form)
if ("action" in request.form):
print(">> 1", request.form)
if request.form["action"]=="cancel":
return redirect("/")
if request.form["action"]=="accept_create_image":
aFiles = request.form.getlist("files[]")
for oFile in aFiles:
sPath = os.path.join(UPLOAD_PATH, oFile.filename)
sFileName = os.path.basename(sPath)
oFile.save(sPath)
sRelFilePath = f"{UPLOAD_PATH_REL}/{oFile.filename}"
get_db().execute("INSERT INTO image (name, path) VALUES (?, ?)", (sFileName, sRelFilePath))
get_db().commit()
sHTML = request.form.get("html-images")
oM = re.findall(r"<img[^<]*src=\"([^\"]+)\"", sHTML)
if oM != None and len(oM)>0:
for sM in oM:
response = requests.get(sM)
sExt = "png"
try:
sExt = response.headers['Content-Type'].split('/')[1]
except:
pass
filename = str(uuid.uuid4())
sFileName = f"{filename}.{sExt}"
sFilePath = f"{UPLOAD_PATH}/{sFileName}"
sRelFilePath = f"{UPLOAD_PATH_REL}/{sFileName}"
open(sFilePath, "wb").write(response.content)
get_db().execute("INSERT INTO images (name, path, category_id) VALUES (?, ?, ?)", (sFileName, sRelFilePath, sSelCategory))
get_db().commit()
return redirect("/")
if ("action" in request.args):
print(">> 2", request.args)
if request.args["action"]=="create_image":
return render_template('image_create.html')
if request.args["action"]=="image_upload":
pass
if request.args["action"]=="create_category":
pass
if request.args["action"]=="remove_category":
pass
if request.args["action"]=="remove_image":
aImages = request.args.getlist('images[]')
print(">>3 ", aImages)
for sImageID in aImages:
oImage = query_db('SELECT * FROM images WHERE id=? LIMIT 1', (sImageID,))
if oImage:
try:
os.unlink(oImage[0][2])
except:
pass
get_db().execute("DELETE FROM images WHERE id=?", (sImageID,))
get_db().commit()
aGroups = query_db('SELECT * FROM groups')
# if sSelGroup=='':
# if len(aGroups)>0 and len(aGroups[0])>1:
# sSelGroup=aGroups[0][0]
dForm = parse_multi_form(request.args)
print(request.args)
aOpenedCategories = dict()
if dForm.get('c', '') != '':
aOpenedCategories = dForm['c']
print(aOpenedCategories)
aCategories = query_db('SELECT * FROM categories WHERE group_id=? AND parent_id IS NULL', (sSelGroup, ))
aCategories = fnIterCategories(aCategories, [], list(aOpenedCategories))
aImages = query_db('SELECT * FROM images WHERE category_id=?', (sSelCategory,))
return render_template('index.html',
sSelGroup=sSelGroup,
sSelCategory=sSelCategory,
sSelImage=sSelImage,
aGroups=aGroups,
aCategories=aCategories,
aImages=aImages,
aOpenedCategories=aOpenedCategories
)
@app.route('/upload', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
uploaded_files = request.files.getlist("file[]")
for file in uploaded_files:
sPath = os.path.join(UPLOAD_PATH, file.filename)
sFileName = os.path.basename(sPath)
file.save(sPath)
get_db().execute("INSERT INTO image (name, path) VALUES (?, ?)", (sFileName, sPath))
get_db().commit()