api code stability

This commit is contained in:
aj 2019-08-05 21:43:09 +01:00
parent dcc4725047
commit daf80b5c0b
4 changed files with 80 additions and 41 deletions

View File

@ -72,11 +72,14 @@ def playlist():
return jsonify({'error': "no name provided"}), 400 return jsonify({'error': "no name provided"}), 400
playlist_name = request_json['name'] playlist_name = request_json['name']
playlist_parts = request_json.get('parts', None) playlist_parts = request_json.get('parts', None)
playlist_references = request_json.get('playlist_references', None) playlist_references = request_json.get('playlist_references', None)
playlist_id = request_json.get('id', None) playlist_id = request_json.get('id', None)
playlist_shuffle = request_json.get('shuffle', None) playlist_shuffle = request_json.get('shuffle', None)
playlist_type = request_json.get('type', None) playlist_type = request_json.get('type', None)
playlist_day_boundary = request_json.get('day_boundary', None) playlist_day_boundary = request_json.get('day_boundary', None)
playlist_recommendation = request_json.get('include_recommendations', None) playlist_recommendation = request_json.get('include_recommendations', None)
@ -101,8 +104,8 @@ def playlist():
'include_recommendations': playlist_recommendation if playlist_recommendation is not None else False, 'include_recommendations': playlist_recommendation if playlist_recommendation is not None else False,
'recommendation_sample': playlist_recommendation_sample if playlist_recommendation_sample is not None else 10, 'recommendation_sample': playlist_recommendation_sample if playlist_recommendation_sample is not None else 10,
'playlist_id': None, 'playlist_id': None,
'shuffle': playlist_shuffle, 'shuffle': playlist_shuffle if playlist_shuffle is not None else False,
'type': playlist_type 'type': playlist_type if playlist_type is not None else 'default'
} }
if user_ref.get().to_dict()['spotify_linked']: if user_ref.get().to_dict()['spotify_linked']:
@ -114,7 +117,7 @@ def playlist():
playlists.document().set(to_add) playlists.document().set(to_add)
return jsonify({"message": 'playlist added', "status": "success"}), 200 return jsonify({"message": 'playlist added', "status": "success"}), 201
elif request.method == 'POST': elif request.method == 'POST':
@ -124,15 +127,6 @@ def playlist():
if len(queried_playlist) > 1: if len(queried_playlist) > 1:
return jsonify({'error': "multiple playlists exist"}), 500 return jsonify({'error': "multiple playlists exist"}), 500
if playlist_parts is None and \
playlist_references is None and \
playlist_id is None and \
playlist_shuffle is None and \
playlist_day_boundary is None and \
playlist_recommendation is None and \
playlist_recommendation_sample is None:
return jsonify({'error': "no chnages to make"}), 400
playlist_doc = playlists.document(queried_playlist[0].id) playlist_doc = playlists.document(queried_playlist[0].id)
dic = {} dic = {}
@ -149,7 +143,7 @@ def playlist():
else: else:
dic['playlist_references'] = playlist_references dic['playlist_references'] = playlist_references
if playlist_id: if playlist_id is not None:
dic['playlist_id'] = playlist_id dic['playlist_id'] = playlist_id
if playlist_shuffle is not None: if playlist_shuffle is not None:
@ -164,6 +158,9 @@ def playlist():
if playlist_recommendation_sample is not None: if playlist_recommendation_sample is not None:
dic['recommendation_sample'] = playlist_recommendation_sample dic['recommendation_sample'] = playlist_recommendation_sample
if len(dic) == 0:
return jsonify({"message": 'no changes to make', "status": "error"}), 400
playlist_doc.update(dic) playlist_doc.update(dic)
return jsonify({"message": 'playlist updated', "status": "success"}), 200 return jsonify({"message": 'playlist updated', "status": "success"}), 200
@ -202,15 +199,28 @@ def user():
actionable_user = database.get_user_doc_ref(request_json['username']) actionable_user = database.get_user_doc_ref(request_json['username'])
if actionable_user.get().exists is False:
return jsonify({"message": 'non-existent user', "status": "error"}), 400
dic = {} dic = {}
if 'locked' in request_json: if 'locked' in request_json:
dic['locked'] = request_json['locked'] dic['locked'] = request_json['locked']
if len(dic) > 0: if 'spotify_linked' in request_json:
actionable_user.update(dic) if request_json['spotify_linked'] is False:
dic.update({
'access_token': None,
'refresh_token': None,
'spotify_linked': False
})
return jsonify({'message': 'account locked', 'status': 'succeeded'}), 200 if len(dic) == 0:
return jsonify({"message": 'no changes to make', "status": "error"}), 400
actionable_user.update(dic)
return jsonify({'message': 'account updated', 'status': 'succeeded'}), 200
else: else:
return jsonify({'error': 'not logged in'}), 401 return jsonify({'error': 'not logged in'}), 401
@ -240,7 +250,7 @@ def users():
dic['accounts'].append(user_dic) dic['accounts'].append(user_dic)
return jsonify(dic) return jsonify(dic), 200
else: else:
return jsonify({'error': 'not logged in'}), 401 return jsonify({'error': 'not logged in'}), 401
@ -267,11 +277,10 @@ def change_password():
current_user.update({'password': generate_password_hash(request_json['new_password'])}) current_user.update({'password': generate_password_hash(request_json['new_password'])})
response = {"message": 'password changed', "status": "success"} return jsonify({"message": 'password changed', "status": "success"}), 200
return jsonify(response), 200
else: else:
return jsonify({'error': 'wrong password provided'}), 403 return jsonify({'error': 'wrong password provided'}), 401
else: else:
return jsonify({'error': 'malformed request, no old_password/new_password'}), 400 return jsonify({'error': 'malformed request, no old_password/new_password'}), 400
@ -337,7 +346,7 @@ def run_users():
@blueprint.route('/playlist/run/users/cron', methods=['GET']) @blueprint.route('/playlist/run/users/cron', methods=['GET'])
def run_users_cron(): def run_users_cron():
if request.headers.get('X-Appengine-Cron'): if request.headers.get('X-Appengine-Cron', None):
execute_all_users() execute_all_users()
return jsonify({'status': 'success'}), 200 return jsonify({'status': 'success'}), 200
else: else:
@ -345,9 +354,9 @@ def run_users_cron():
def execute_all_users(): def execute_all_users():
all_users = [i.to_dict() for i in db.collection(u'spotify_users').stream()]
for iter_user in all_users: for iter_user in [i.to_dict() for i in db.collection(u'spotify_users').stream()]:
if iter_user['spotify_linked'] and not iter_user['locked']: if iter_user['spotify_linked'] and not iter_user['locked']:
execute_user(iter_user['username']) execute_user(iter_user['username'])
@ -359,7 +368,7 @@ def execute_user(username):
for iterate_playlist in playlists: for iterate_playlist in playlists:
if len(iterate_playlist['parts']) > 0 or len(iterate_playlist['playlist_references']) > 0: if len(iterate_playlist['parts']) > 0 or len(iterate_playlist['playlist_references']) > 0:
if iterate_playlist.get('playlist_id'): if iterate_playlist.get('playlist_id', None):
execute_playlist(username, iterate_playlist['name']) execute_playlist(username, iterate_playlist['name'])

View File

@ -21,8 +21,14 @@ def login():
session.pop('username', None) session.pop('username', None)
username = request.form['username'].lower() username = request.form.get('username', None)
password = request.form['password'] password = request.form.get('password', None)
if username is None or password is None:
flash('malformed request')
return redirect(url_for('index'))
username = username.lower()
users = database.get_user_query_stream(username) users = database.get_user_query_stream(username)
@ -75,19 +81,25 @@ def register():
return render_template('register.html') return render_template('register.html')
else: else:
username = request.form['username'].lower() username = request.form.get('username', None)
password = request.form['password'] password = request.form.get('password', None)
password_again = request.form['password_again'] password_again = request.form.get('password_again', None)
if username is None or password is None or password_again is None:
flash('malformed request')
return redirect('authapi.register')
username = username.lower()
if password != password_again:
flash('password mismatch')
return redirect('authapi.register')
if username in [i.to_dict()['username'] for i in if username in [i.to_dict()['username'] for i in
db.collection(u'spotify_users').where(u'username', u'==', username).stream()]: db.collection(u'spotify_users').where(u'username', u'==', username).stream()]:
flash('username already registered') flash('username already registered')
return redirect('authapi.register') return redirect('authapi.register')
if password != password_again:
flash('password mismatch')
return redirect('authapi.register')
db.collection(u'spotify_users').add({ db.collection(u'spotify_users').add({
'access_token': None, 'access_token': None,
'email': None, 'email': None,
@ -132,7 +144,8 @@ def token():
code = request.args.get('code', None) code = request.args.get('code', None)
if code is None: if code is None:
error = request.args.get('error', None) flash('authorization failed')
return redirect('app_route')
else: else:
app_credentials = db.document('key/spotify').get().to_dict() app_credentials = db.document('key/spotify').get().to_dict()
@ -147,15 +160,21 @@ def token():
req = requests.post('https://accounts.spotify.com/api/token', data=data, headers=headers) req = requests.post('https://accounts.spotify.com/api/token', data=data, headers=headers)
resp = req.json() if 200 <= req.status_code < 300:
user_reference = database.get_user_doc_ref(session['username']) resp = req.json()
user_reference.update({ user_reference = database.get_user_doc_ref(session['username'])
'access_token': resp['access_token'],
'refresh_token': resp['refresh_token'], user_reference.update({
'spotify_linked': True 'access_token': resp['access_token'],
}) 'refresh_token': resp['refresh_token'],
'spotify_linked': True
})
else:
flash('http error on token request')
return redirect('app_route')
return redirect('/app/settings/spotify') return redirect('/app/settings/spotify')

View File

@ -20,6 +20,7 @@ def index():
if 'username' in session: if 'username' in session:
logged_in = True logged_in = True
return redirect(url_for('app_route'))
else: else:
logged_in = False logged_in = False

View File

@ -19,6 +19,16 @@
<h1 class="title">sarsoo</h1> <h1 class="title">sarsoo</h1>
</div> </div>
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for message in messages %}
<div class="row card pad-12">
<p class="center-text" style="color: red">{{ message }}</p>
</div>
{% endfor %}
{% endif %}
{% endwith %}
<br><br> <br><br>
<div id="react"></div> <div id="react"></div>