-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
192 lines (127 loc) · 4.85 KB
/
server.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
from flask import Flask, render_template, redirect, request, session, jsonify
from helper import new_song, save_file, save_image, validate_input
from model import User, Song, db, connect_to_db
from flask_bcrypt import Bcrypt
import shutil
import os
app = Flask(__name__)
app.secret_key = os.environ['SECRET_KEY']
bcrypt = Bcrypt(app)
if not os.path.exists('static/user_files'):
os.makedirs('static/user_files')
@app.route('/')
def index():
"""Loads homepage."""
if 'user_id' in session:
user = User.query.filter_by(user_id=session['user_id']).first()
song_path = 'static/user_files/user' + str(user.user_id) + '/temp/temp_song.wav'
return render_template('index.html', song_path=song_path, username=user.username)
return render_template('index.html', song_path='static/user_files/user/temp/temp_song.wav')
@app.route('/profile')
def profile():
"""If logged in, loads user's profile page."""
if 'user_id' not in session:
return redirect('/')
user = User.query.filter_by(user_id=session['user_id']).first()
songs = Song.query.filter_by(user_id=session['user_id']).all()
return render_template('profile.html', songs=songs, username=user.username)
@app.route('/logout')
def logout():
"""If logged in, logs user out and redirects to homepage."""
if 'user_id' in session:
song_path = 'static/user_files/user' + str(session['user_id']) + '/temp/temp_song.wav'
if os.path.exists(song_path):
os.remove(song_path)
session.pop('user_id')
return redirect('/')
@app.route('/signin.json', methods=['POST'])
def signin():
username = request.form.get('username')
password = request.form.get('password')
user = User.query.filter_by(username=username).first()
results = {}
if user:
if bcrypt.check_password_hash(user.password, password):
results['success'] = True
session['user_id'] = user.user_id
return jsonify(results)
results['success'] = False
message = 'Username or password incorrect.'
results['message'] = message
return jsonify(results)
@app.route('/signup.json', methods=['POST'])
def signup():
username = request.form.get('username')
password = request.form.get('password')
user = User.query.filter_by(username=username).first()
results = {}
if user:
results['success'] = False
message = 'Username is taken!'
results['message'] = message
return jsonify(results)
encrypted_pword = bcrypt.generate_password_hash(password)
user = User(username=username, password=encrypted_pword)
db.session.add(user)
db.session.flush()
session['user_id'] = user.user_id
db.session.commit()
results['success'] = True
return jsonify(results)
@app.route('/save', methods=['POST'])
def save_song():
"""Handles saving songs to database.
Saves song file to a new filename and logs it into the database.
"""
if 'user_id' not in session:
return ''
name = request.form.get('name')
image_data = request.form.get('image')
song = Song(user_id=session['user_id'], name=name)
db.session.add(song)
db.session.flush()
path = 'static/user_files/user%i/' % (session['user_id'])
music_path = path + 'music/'
image_path = path + 'scores/'
music_file = 'song' + str(song.song_id) + '.wav'
image_file = 'song_score' + str(song.song_id) + '.png'
save_file(music_path, music_file, session['user_id'])
save_image(image_path, image_file, image_data)
song.song_path = music_path + music_file
song.score_path = image_path + image_file
db.session.commit()
return ''
@app.route('/delete_song', methods=['POST'])
def delete_song():
"""Deletes song from database."""
song_id = int(request.form.get('song_id')[4:])
Song.query.filter_by(song_id=song_id).delete()
db.session.commit()
return '/profile'
@app.route('/delete_profile')
def delete_profile():
"""Deletes users profile."""
user_path = 'static/user_files/user' + str(session['user_id'])
shutil.rmtree(user_path)
Song.query.filter_by(user_id=session['user_id']).delete()
User.query.filter_by(user_id=session['user_id']).delete()
db.session.commit()
session.pop('user_id')
return '/'
@app.route('/process_song', methods=['POST'])
def song():
"""Processes creating a new song."""
melody = request.form.get('melody')
if not validate_input(melody):
return jsonify({'success': False})
if 'user_id' in session:
notes, chords = new_song(melody, session['user_id'])
else:
notes, chords = new_song(melody)
data = {'notes': notes, 'chords': chords, 'success': True}
return jsonify(data)
if __name__ == '__main__':
connect_to_db(app)
app.debug = True
app.jinja_env.auto_reload = app.debug
app.run(port=5000, host='0.0.0.0')