-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyServer.py
116 lines (100 loc) · 4.23 KB
/
PyServer.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
import os
from flask import Flask, redirect, url_for, request, render_template, make_response, abort
#from flask import sqlite3
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.dialects.postgresql import ARRAY
#~~~DATABASE STUFF~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#set projects path, sqlite:/// determines the database engine being used
project_dir = os.path.dirname(os.path.abspath(__file__))
database_file = "sqlite:///{}".format(os.path.join(project_dir, "users.db"))
app = Flask(__name__, static_url_path = '/static')
app.config['SQLALCHEMY_DATABASE_URI'] = database_file #where database is stored
db = SQLAlchemy(app)
class USER(db.Model): #database of users
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(24))
#pswd = db.Column(db.String(64))
usersongs = db.relationship('SONGUSER', backref = 'owner')
def __repr__(self): #for representing user as a string
return "<Name: {}>".format(self.name)
class SONGUSER(db.Model):
id = db.Column(db.Integer, primary_key = True)
owner_id = db.Column(db.Integer, db.ForeignKey(USER.id))
#songuserArray = db.Column(ARRAY(db.String(24), dimensions = 2), db.ForeignKey('song.SongArray'))
class SONG(db.Model): #database of songs
songId = db.Column(db.Integer, primary_key = True)
#songArray = db.Column(ARRAY(db.String(32), dimensions = 2))
#from werkzeug generate_password_hash, check_password_hash https://stackoverflow.com/questions/32493631/unboundlocalerror-local-variable-cursor-referenced-before-assignment
@app.route("/")
def index():
return redirect("/choose")
@app.route("/reg", methods=["GET", "POST"])
def register():
books = None
if request.form:
try:
user = USER(name = request.form.get("name")) #https://stackoverflow.com/questions/32493631/unboundlocalerror-local-variable-cursor-referenced-before-assignment
db.session.add(user)
db.session.commit()
except Exception as exptn:
print("~~~ERROR ADDING USER~~~")
print(exptn)
users = USER.query.all()
return render_template("register.html", users = users)
@app.route("/upd", methods=["GET", "POST"]) #WOOOOOORKKK
def update():
try:
newname = request.form.get("newname")
oldname = request.form.get("oldname")
user = USER.query.filter_by(name = oldname).first()
user.name = newname
db.session.commit()
except Exception as exptn:
print("~~~ERROR UPDATING USER DETAILS~~~")
print(exptn)
return redirect("/reg")
@app.route("/del", methods = ["POST"])
def delete():
name = request.form.get("name")
user = USER.query.filter_by(name = name).first()
print(user)
db.session.delete(user)
db.session.commit()
return redirect("/reg")
#~~~SONGS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@app.route('/choose') #pick song
def choose():
#if request.method == 'POST':
#song = request.form['sng'] #[] for POST
if request.method == 'GET':
song = request.args.get('sng') #() for GET
if song in ['02C', '02Eb', '04C', '04Eb']:
return redirect(url_for('get_mic_data',songId = song))
return render_template('choose_song.html')
@app.route('/microphone/<songId>') #play song
def get_mic_data(songId):
#return 'Welcome %s' % songId
return render_template('get_audio.html', sentSongId = songId)
#~~~~~CURRENTLY UNUSED~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
@app.route('/table')
def table():
table_values = {'1': 'a', '2': 'b', '3': 'c'}
return render_template('table.html', result = table_values)
@app.route('/loginn',methods = ['POST', 'GET'])
def login():
if request.method == 'POST':
user = request.form['nm'] #[] for POST
if user == 'thom':
return redirect(url_for('success',name = user))
else:
abort(401) #Error 401 - Unauthorised
return render_template('login.html')
#if request.method == 'GET':
#user = request.args.get('nm') #() for GET
#return redirect(url_for('success',name = user))
@app.route('/success/<name>')
def success(name):
return 'Welcome %s' % name
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if __name__ == '__main__':
app.run(debug = True, host="0.0.0.0", port=5020)