2020-02-03 11:51:46 +00:00
|
|
|
from flask import Blueprint, jsonify, request
|
|
|
|
|
|
|
|
import logging
|
2020-05-08 15:19:27 +01:00
|
|
|
import os
|
2020-05-15 23:25:19 +01:00
|
|
|
import json
|
2020-02-03 11:51:46 +00:00
|
|
|
|
2022-08-16 18:00:38 +01:00
|
|
|
from music.api.decorators import login_or_jwt, cloud_task, no_locked_users
|
2020-05-08 15:19:27 +01:00
|
|
|
from music.cloud.function import update_tag as serverless_update_tag
|
|
|
|
from music.tasks.update_tag import update_tag
|
2020-02-03 11:51:46 +00:00
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
from music.model.tag import Tag
|
|
|
|
|
2020-02-03 11:51:46 +00:00
|
|
|
blueprint = Blueprint('task', __name__)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/tag', methods=['GET'])
|
2022-08-08 22:02:14 +01:00
|
|
|
@login_or_jwt
|
2022-08-16 18:00:38 +01:00
|
|
|
@no_locked_users
|
2022-08-08 22:02:14 +01:00
|
|
|
def tags(auth=None, user=None):
|
2020-04-30 14:54:05 +01:00
|
|
|
logger.info(f'retrieving tags for {user.username}')
|
2020-02-03 11:51:46 +00:00
|
|
|
return jsonify({
|
2020-04-30 14:54:05 +01:00
|
|
|
'tags': [i.to_dict() for i in Tag.collection.parent(user.key).fetch()]
|
2020-02-03 11:51:46 +00:00
|
|
|
}), 200
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/tag/<tag_id>', methods=['GET', 'PUT', 'POST', "DELETE"])
|
2022-08-08 22:02:14 +01:00
|
|
|
@login_or_jwt
|
2022-08-16 18:00:38 +01:00
|
|
|
@no_locked_users
|
2022-08-08 22:02:14 +01:00
|
|
|
def tag_route(tag_id, auth=None, user=None):
|
2020-02-03 11:51:46 +00:00
|
|
|
if request.method == 'GET':
|
2020-04-30 14:54:05 +01:00
|
|
|
return get_tag(tag_id, user)
|
2020-02-03 11:51:46 +00:00
|
|
|
elif request.method == 'PUT':
|
2020-04-30 14:54:05 +01:00
|
|
|
return put_tag(tag_id, user)
|
2020-02-03 11:51:46 +00:00
|
|
|
elif request.method == 'POST':
|
2020-04-30 14:54:05 +01:00
|
|
|
return post_tag(tag_id, user)
|
2020-02-03 11:51:46 +00:00
|
|
|
elif request.method == 'DELETE':
|
2020-04-30 14:54:05 +01:00
|
|
|
return delete_tag(tag_id, user)
|
2020-02-03 11:51:46 +00:00
|
|
|
|
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
def get_tag(tag_id, user):
|
2020-08-13 17:48:20 +01:00
|
|
|
logger.info(f'retrieving {tag_id} for {user.username}')
|
2020-02-03 11:51:46 +00:00
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
db_tag = Tag.collection.parent(user.key).filter('tag_id', '==', tag_id).get()
|
2020-02-03 11:51:46 +00:00
|
|
|
if db_tag is not None:
|
|
|
|
return jsonify({
|
|
|
|
'tag': db_tag.to_dict()
|
|
|
|
}), 200
|
|
|
|
else:
|
|
|
|
return jsonify({"error": 'tag not found'}), 404
|
|
|
|
|
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
def put_tag(tag_id, user):
|
|
|
|
logger.info(f'updating {tag_id} for {user.username}')
|
2020-02-03 11:51:46 +00:00
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
db_tag = Tag.collection.parent(user.key).filter('tag_id', '==', tag_id).get()
|
2020-02-03 11:51:46 +00:00
|
|
|
|
|
|
|
if db_tag is None:
|
|
|
|
return jsonify({"error": 'tag not found'}), 404
|
|
|
|
|
|
|
|
request_json = request.get_json()
|
|
|
|
|
|
|
|
if request_json.get('name'):
|
2020-05-08 15:19:27 +01:00
|
|
|
db_tag.name = request_json['name'].strip()
|
2020-02-03 11:51:46 +00:00
|
|
|
|
2020-08-13 17:48:20 +01:00
|
|
|
if request_json.get('time_objects') is not None:
|
|
|
|
db_tag.time_objects = request_json['time_objects']
|
|
|
|
|
2020-02-03 23:37:18 +00:00
|
|
|
if request_json.get('tracks') is not None:
|
2020-05-08 15:19:27 +01:00
|
|
|
db_tag.tracks = [
|
|
|
|
{
|
|
|
|
'name': track['name'].strip(),
|
|
|
|
'artist': track['artist'].strip()
|
|
|
|
}
|
|
|
|
for track in request_json['tracks']
|
|
|
|
if track.get('name') and track.get('artist')
|
|
|
|
]
|
|
|
|
|
2020-02-03 23:37:18 +00:00
|
|
|
if request_json.get('albums') is not None:
|
2020-05-08 15:19:27 +01:00
|
|
|
db_tag.albums = [
|
|
|
|
{
|
|
|
|
'name': album['name'].strip(),
|
|
|
|
'artist': album['artist'].strip()
|
|
|
|
}
|
|
|
|
for album in request_json['albums']
|
|
|
|
if album.get('name') and album.get('artist')
|
|
|
|
]
|
|
|
|
|
2020-02-03 23:37:18 +00:00
|
|
|
if request_json.get('artists') is not None:
|
2020-05-08 15:19:27 +01:00
|
|
|
db_tag.artists = [
|
|
|
|
{
|
|
|
|
'name': artist['name'].strip()
|
|
|
|
}
|
|
|
|
for artist in request_json['artists']
|
|
|
|
if artist.get('name')
|
|
|
|
]
|
|
|
|
|
|
|
|
db_tag.update()
|
2020-02-03 11:51:46 +00:00
|
|
|
|
|
|
|
return jsonify({"message": 'tag updated', "status": "success"}), 200
|
|
|
|
|
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
def post_tag(tag_id, user):
|
|
|
|
logger.info(f'creating {tag_id} for {user.username}')
|
2020-02-03 11:51:46 +00:00
|
|
|
|
2020-05-08 15:19:27 +01:00
|
|
|
tag_id = tag_id.replace(' ', '_').strip()
|
|
|
|
|
|
|
|
existing_ids = [i.tag_id for i in Tag.collection.parent(user.key).fetch()]
|
|
|
|
while tag_id in existing_ids:
|
2020-05-15 23:25:19 +01:00
|
|
|
tag_id += '_'
|
2020-02-03 23:37:18 +00:00
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
tag = Tag(parent=user.key)
|
|
|
|
tag.tag_id = tag_id
|
|
|
|
tag.name = tag_id
|
|
|
|
tag.username = user.username
|
|
|
|
tag.save()
|
|
|
|
|
2020-02-03 23:37:18 +00:00
|
|
|
return jsonify({"message": 'tag added', "status": "success"}), 201
|
2020-02-03 11:51:46 +00:00
|
|
|
|
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
def delete_tag(tag_id, user):
|
|
|
|
logger.info(f'deleting {tag_id} for {user.username}')
|
2020-02-03 11:51:46 +00:00
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
db_tag = Tag.collection.parent(user.key).filter('tag_id', '==', tag_id).get()
|
|
|
|
Tag.collection.parent(user.key).delete(key=db_tag.key)
|
2020-02-03 11:51:46 +00:00
|
|
|
|
2020-04-30 14:54:05 +01:00
|
|
|
return jsonify({"message": 'tag deleted', "status": "success"}), 201
|
2020-02-03 11:51:46 +00:00
|
|
|
|
|
|
|
|
2020-02-03 23:37:18 +00:00
|
|
|
@blueprint.route('/tag/<tag_id>/update', methods=['GET'])
|
2022-08-08 22:02:14 +01:00
|
|
|
@login_or_jwt
|
2022-08-16 18:00:38 +01:00
|
|
|
@no_locked_users
|
2022-08-08 22:02:14 +01:00
|
|
|
def tag_refresh(tag_id, auth=None, user=None):
|
2020-04-30 14:54:05 +01:00
|
|
|
logger.info(f'updating {tag_id} tag for {user.username}')
|
2020-05-08 15:19:27 +01:00
|
|
|
|
|
|
|
if os.environ.get('DEPLOY_DESTINATION', None) == 'PROD':
|
|
|
|
serverless_update_tag(username=user.username, tag_id=tag_id)
|
|
|
|
else:
|
2021-02-08 16:18:16 +00:00
|
|
|
update_tag(user=user, tag=tag_id)
|
2020-05-08 15:19:27 +01:00
|
|
|
|
2020-02-03 23:37:18 +00:00
|
|
|
return jsonify({"message": 'tag updated', "status": "success"}), 200
|
2020-05-15 23:25:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/tag/update/task', methods=['POST'])
|
|
|
|
@cloud_task
|
|
|
|
def run_tag_task():
|
|
|
|
|
|
|
|
payload = request.get_data(as_text=True)
|
|
|
|
if payload:
|
|
|
|
payload = json.loads(payload)
|
|
|
|
|
|
|
|
logger.info(f'running {payload["username"]} / {payload["tag_id"]}')
|
|
|
|
|
|
|
|
serverless_update_tag(username=payload['username'], tag_id=payload['tag_id'])
|
|
|
|
|
|
|
|
return jsonify({'message': 'executed playlist', 'status': 'success'}), 200
|