Commit 443c8b02 authored by Fence's avatar Fence 🌈

add songs api

parent 8de2d380
import os
import os.path
from pprint import pprint
from flask import json_available, request, jsonify, Response
import json
from flask import request, jsonify, Response
from flask_controller import FlaskController, route
from bson import ObjectId
from werkzeug.utils import secure_filename
from mutagen.mp3 import MP3
from minor.decorators import json_required, auth_required
......@@ -34,16 +33,40 @@ class MusicController(FlaskController):
return jsonify({"code": 400, "message": "bad_request"}), 400
@auth_required
@route("/song/<id>")
@route("/songs")
def all_songs(self, payload=None):
start = request.args.get("start")
limit = 20
if start is None:
start = 0
else:
start = int(start) - 1
songs = Song.objects.skip(start).limit(limit)
count = Song.objects.count()
# ech mongo_engine is garbage
songs = list(map(lambda x : json.loads(x.to_json()), songs))
# check if songs is empty
if songs:
return jsonify({"code": 200, "message": "ok", "data": songs, "count": count, "limit": limit}), 200
else:
return jsonify({"code": 400, "message": "bad_request"}), 400
@auth_required
@route("/songs/<id>")
def song(self, id, payload=None):
song = Song.objects.get(id=id)
if song is not None:
return jsonify({"code": 200, "message": "ok", "data": song}), 200
return jsonify({"code": 200, "message": "ok", "data": json.loads(song.to_json())}), 200
else:
return jsonify({"code": 400, "message": "bad_request"}), 400
@auth_required
@route("/song/<id>/hasfile")
@route("/songs/<id>/hasfile")
def song_has_file(self, id, payload=None):
song = Song.objects.get(id=id)
if song is not None:
......@@ -57,7 +80,7 @@ class MusicController(FlaskController):
@auth_required
@json_required
@route("/song", methods=["POST"])
@route("/songs", methods=["POST"])
def song_post(self, payload=None):
json = request.get_json()
try:
......@@ -85,7 +108,7 @@ class MusicController(FlaskController):
return jsonify({"code": 400, "message": "bad_request"}), 400
@auth_required
@route("/song/<id>/upload", methods=["POST"])
@route("/songs/<id>/upload", methods=["POST"])
def song_file(self, id, payload=None):
if 'file' in request.files:
file = request.files["file"]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment