Commit ec7567dc authored by Fence's avatar Fence 🌈

fix pep8 issues

parent 118d8764
......@@ -2,10 +2,11 @@ from flask import Flask
from minor.controller import ApiController, MusicController, CoverController
from minor.error_handlers import set_error_handlers
class App(object):
def __init__(self, config):
self._flask = Flask(__name__)
self._flask.config['MONGODB_SETTINGS'] = {'db': 'minor'}
self._flask.config['MONGODB_SETTINGS'] = {'db': 'minor'}
self._flask.config.update(config)
set_error_handlers(self._flask)
......@@ -13,9 +14,13 @@ class App(object):
db.init_app(self._flask)
ApiController(self).register(self._flask)
MusicController(self._flask.config["MUSIC_UPLOAD_DIR"],
self._flask.config["IMAGE_UPLOAD_DIR"]).register(self._flask)
CoverController(self._flask.config["IMAGE_UPLOAD_DIR"]).register(self._flask)
MusicController(
self._flask.config["MUSIC_UPLOAD_DIR"],
self._flask.config["IMAGE_UPLOAD_DIR"]
).register(self._flask)
CoverController(
self._flask.config["IMAGE_UPLOAD_DIR"]
).register(self._flask)
def run(self, host="127.0.0.1", port=5000):
self._flask.run(host=host, port=port)
......
from flask import request, jsonify
from flask import jsonify
from flask_controller import FlaskController, route
from minor import version
from minor.model import MajorInstance
from minor.decorators import json_required
@route("/api")
......
......@@ -2,7 +2,7 @@ import json
import os
import uuid
from flask_controller import FlaskController, route
from flask_controller import FlaskController, route
from werkzeug.utils import secure_filename
from minor.decorators import auth_required
from flask import request, jsonify, send_file
......@@ -40,16 +40,28 @@ class CoverController(FlaskController):
count = Cover.objects.count()
# covers is an array of dicts which represent Cover models
# also songs is an array of arrays with objectid's... mongoengine is really bad for serialization
# also songs is an array of arrays with objectid's...
# mongoengine is really bad for serialization
for cover in covers:
print(cover)
cover["songs"] = list(map(lambda s: s[0]["$oid"], cover["songs"]))
if covers:
return jsonify({"code": 200, "message": "ok", "data": covers, "count": count, "limit": limit}), 200
return jsonify({
"code": 200,
"message": "ok",
"data": covers,
"count": count,
"limit": limit
}), 200
else:
return jsonify({"code": 204, "message": "no content", "count": 0, "limit": limit}), 204
return jsonify({
"code": 204,
"message":
"no content",
"count": 0,
"limit": limit
}), 204
@auth_required
@route("/<id>", methods=["GET"])
......@@ -75,7 +87,8 @@ class CoverController(FlaskController):
except Cover.DoesNotExist:
return jsonify({"code": 404, "message": "not found"}), 404
return send_file(os.path.abspath(os.path.join(self._upload_dir, secure_filename(cover.file))))
path = os.path.join(self._upload_dir, secure_filename(cover.file))
return send_file(os.path.abspath(path))
@auth_required
@route("/", methods=["POST"])
......@@ -83,7 +96,7 @@ class CoverController(FlaskController):
if 'file' in request.files:
file = request.files["file"]
if file.filename is not '':
if file.filename != '':
ext = get_ext(file.filename, img_exts)
if ext is not None:
name = secure_filename(generate_file_name(ext))
......@@ -95,7 +108,11 @@ class CoverController(FlaskController):
cover.save()
cover.reload()
return jsonify({"code": 200, "message": "ok", "data": {"id": str(cover.id)}}), 200
return jsonify({
"code": 200,
"message": "ok",
"data": {"id": str(cover.id)}
}), 200
return jsonify({"code": 400, "message": "bad_request"}), 400
......@@ -111,13 +128,14 @@ class CoverController(FlaskController):
if "song_id" in json:
try:
song = Song.objects.get(id=json["song_id"])
song = Song.objects.get(id=json["song_id"])
except Song.DoesNotExist:
return jsonify({"code": 400, "message": "song does not exist"}), 400
return jsonify({
"code": 400, "message":
"song does not exist"
}), 400
cover.update(push__songs=[song])
return jsonify({"code": 200, "message": "ok"}), 200
else:
return jsonify({"code": 400, "message": "bad_request"}), 400
import os
import os.path
import json
from os import path
from flask import request, jsonify, Response, send_file
from flask_controller import FlaskController, route
from werkzeug.utils import secure_filename
......@@ -54,9 +55,20 @@ class MusicController(FlaskController):
# check if songs is empty
if songs:
return jsonify({"code": 200, "message": "ok", "data": songs, "count": count, "limit": limit}), 200
return jsonify({
"code": 200,
"message": "ok",
"data": songs,
"count": count,
"limit": limit
}), 200
else:
return jsonify({"code": 204, "message": "no content", "count": 0, "limit": limit}), 204
return jsonify({
"code": 204,
"message": "no content",
"count": 0,
"limit": limit
}), 204
@auth_required
@route("/songs/<id>")
......@@ -92,7 +104,12 @@ class MusicController(FlaskController):
return jsonify({"code": 404, "message": "not found"}), 404
try:
cover = Cover.objects(songs=[song]).limit(1).get()
return send_file(os.path.abspath(os.path.join(self._cover_upload_dir, secure_filename(cover.file))))
file_path = path.join(
self._cover_upload_dir,
secure_filename(cover.file)
)
absolute_path = path.abspath(file_path)
return send_file(absolute_path)
except Cover.DoesNotExist:
return jsonify({"code": 204, "message": "no content"}), 204
......@@ -136,7 +153,7 @@ class MusicController(FlaskController):
except Song.DoesNotExist:
return jsonify({"code": 404, "message": "not found"}), 404
if file.filename is not '':
if file.filename != '':
ext = get_ext(file.filename)
if ext is not None:
name = secure_filename(str(db_song.id) + "." + ext)
......
import jwt
from minor.model import MajorInstance
class CryptoChecker(object):
def __init__(self, db):
......@@ -19,6 +20,6 @@ class CryptoChecker(object):
key = issuer.key
checked_payload = jwt.decode(token, key)
return checked_payload
except Exception as e:
except Exception:
return None
return None
from flask import jsonify
def set_error_handlers(app):
@app.errorhandler(404)
def four_o_four(e):
......
from mongoengine import *
from mongoengine import Document, StringField, IntField
from mongoengine import ReferenceField, ListField, CASCADE
from flask_mongoengine import MongoEngine
db = MongoEngine()
......
......@@ -7,14 +7,14 @@ song_exts = ["mp3", "ogg", "flac"]
def get_ext(filename, exts=song_exts):
if '.' in filename and filename.rsplit('.', 1)[1].lower() in exts:
return filename.rsplit('.', 1)[1].lower()
else:
else:
return None
def transform_mongo_dict(mongo_dict):
mongo_dict["id"] = mongo_dict["_id"]["$oid"]
del mongo_dict["_id"]
return mongo_dict
mongo_dict["id"] = mongo_dict["_id"]["$oid"]
del mongo_dict["_id"]
return mongo_dict
def get_page(model, start=0, limit=20):
......
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