-
Notifications
You must be signed in to change notification settings - Fork 1
/
flask_app.py
134 lines (98 loc) · 3.99 KB
/
flask_app.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
from flask import Flask, render_template, request, session, redirect, url_for, jsonify
from werkzeug.utils import secure_filename
from werkzeug.utils import secure_filename
from base64 import b64encode
import os
import json
import linedetection
import sqlite3 as sql
from passlib.hash import sha256_crypt
from datetime import datetime
currentfilepath = os.path.dirname(os.path.abspath(__file__))
## DB part -> 2 tables
# first table for user data
# second table to map username to gamefile + game's metadata
with sql.connect(os.path.join(currentfilepath,'ProtoSketch.db')) as conn:
conn.execute('CREATE TABLE IF NOT EXISTS users(username text UNIQUE, password text);')
conn.execute('CREATE TABLE IF NOT EXISTS games(user_creator text, filename text, about text, start_date text, thumbnail_link text);')
app = Flask(__name__)
UPLOAD_FOLDER = os.path.join(currentfilepath,"static")
OUTPUT_FOLDER = os.path.join(currentfilepath,"saved_games")
ALLOWED_EXTENSIONS = set(["jpg", "jpeg", "png", "gif"])
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route("/login")
def login():
pass
@app.route("/signup")
def signup():
pass
@app.route("/")
def index():
return render_template('index.html')
@app.route("/upload_image", methods = ['POST'])
def submit_image():
target=os.path.join(UPLOAD_FOLDER,'uploads')
if not os.path.isdir(target):
os.mkdir(target)
file = request.files['file']
print(file)
filename = secure_filename(file.filename)
destination="/".join([target, filename])
file.save(destination)
fileslist = os.listdir(OUTPUT_FOLDER)
fileslist.remove(".DS_Store")
new_file_num = str(len(fileslist) + 1)
generated_jsons_filename = new_file_num + ".txt"
json_output = linedetection.imgtojson(destination,True, "/".join([OUTPUT_FOLDER, generated_jsons_filename]), new_file_num)
startdate = str(datetime.now().strftime('%Y-%m-%d-%H:%M'))
with sql.connect(os.path.join(currentfilepath,'ProtoSketch.db')) as conn:
cur = conn.cursor()
cur.execute('INSERT INTO games VALUES (?,?,NULL,?,?);', ("Nuode_admin", "/".join([OUTPUT_FOLDER,generated_jsons_filename]), startdate, destination))
conn.commit()
#print("Json file created and stored")
#response= "Thanks for the file upload"
return json_output
@app.route("/game/<game_id>", methods = ['GET'])
def get_game(game_id):
try:
with open(OUTPUT_FOLDER + "/" + game_id + ".txt") as json_file:
data = json.load(json_file)
except:
data = "Error, file not found"
return data
@app.route("/test_games", methods = ['GET'])
def see_games():
with sql.connect(os.path.join(currentfilepath,'ProtoSketch.db')) as conn:
cur = conn.cursor()
results = cur.execute("SELECT * FROM games").fetchall()
return str(results)
@app.route("/test_users", methods = ['GET'])
def see_users():
with sql.connect(os.path.join(currentfilepath,'ProtoSketch.db')) as conn:
cur = conn.cursor()
results = cur.execute("SELECT * FROM users").fetchall()
return str(results)
@app.route("/user_games/<user_id>", methods = ['GET'])
def user_games(user_id):
#data = GamesTable.query.filter_by(user_creator = user_id).all()
with sql.connect(os.path.join(currentfilepath,'ProtoSketch.db')) as conn:
cur = conn.cursor()
data = cur.execute("SELECT * FROM games;").fetchall()
displaylist = []
for game in data:
mystr = game[4].replace('/Users/nuode/Desktop/KurtisTech/',"")
mystr = "http://127.0.0.1:5000/" + mystr
displaylist.append([game[0],"https://photosketch.pythonanywhere.com/game/" + str(game.id),game.about,str(game.start_date),mystr])
print(data)
"""
for game in data:
mystr = game.thumbnail_link.replace('/home/PhotoSketch/mysite/',"")
mystr = "https://photosketch.pythonanywhere.com/" + mystr
displaylist.append([game.id,game.user_creator,"https://photosketch.pythonanywhere.com/game/" + str(game.id),game.about,str(game.start_date),mystr])
"""
return json.dumps(displaylist)
if __name__ == "__main__":
app.secret_key = os.urandom(24)
app.run(debug=True)