class FileResource(Resource):
    def get(self, key):
        # TODO
        # maybe fs should be global
        fs = mongoengine.fields.GridFSProxy()
        file = fs.get(ObjectId(key)).read()
        return Response(file, mimetype='application/octet-stream') class MusicResource(Resource):
    @authenticated('admin')
    @marshal_with(music_fields)
    def patch(self, key):
        args = MusicPatchArgs().args
        music = get_music(key=key)[0]
        update_music(music, **args)
        music = get_music(key=key)[0]
        return music

    @authenticated('admin')
    def delete(self, key):
        music = get_music(key=key)[0]
        delete_music(music) class MusicListResource(Resource):
    def get(self):
        args = MusicQueryArgs().args
        if args == {}:
            return marshal(get_music_status(), music_status_fields)
        return marshal(get_music(**args), music_fields) class ChannelResource(Resource):
    @authenticated('admin')
    @marshal_with(channel_fields)
    def patch(self, key):
        args = ChannelPatchArgs().args
        channel = get_channel(key=key)[0]
        update_channel(channel, **args)
        channel = get_channel(key=key)[0]
        return channel

    @authenticated('admin')
    def delete(self, key):
        channel = get_channel(key=key)[0]
        delete_channel(channel) class ChannelListResource(Resource):
    def get(self):
        args = ChannelQueryArgs().args
        if args == {}:
            return marshal(get_channel_status(), channel_status_fields)
        ret_channels = get_channel(**args)
        return marshal(ret_channels, channel_fields) class PlaylistListResource(Resource):
    def get(self):
        ret_channels = get_channel(playable=True)
        return marshal(ret_channels, playlist_fields) class PlaylistResource(Resource):
    def get(self, key):
        args = PlaylistQueryArgs().args
        num = args['num']
        channel = get_channel(key=key)[0]
        return marshal(get_music_by_channel(channel, num), music_fields) class UserResource(Resource):
    @authenticated('admin')
    @marshal_with(user_fields)
    def patch(self, key):
        args = UserPatchArgs().args
        user = get_user(key=key)[0]
        update_user(user, **args)
        user = get_user(key=key)[0]
        return user

    @authenticated('admin')
    def delete(self, key):
        user = get_user(key=key)[0]
        delete_user(user) class UserListResource(Resource):
    @authenticated('admin')
    def get(self):
        args = UserQueryArgs().args
        if args == {}:
            return marshal(get_user_status(), user_status_fields)
        users = get_user(**args)
        return marshal(users, user_fields)

    def post(self):
        args = UserRegArgs().args
        user = add_user(args['name'], args['password'], 'normal')
        if user:
            return marshal(user, user_fields)
        return None class UserCurrentResource(Resource):
    def get(self):
        try:
            user = get_user(key=session['user'])[0]
            return marshal(user, user_fields)
        except:
            return None

    def post(self):
        args = UserLoginArgs().args
        user = get_user(name=args['name'])[0]
        if check_user_password(user, args['password']) and check_user_enable(user):
            session['user'] = user.key
            return marshal(user, user_fields)
        return None

    def delete(self):
        session.pop('user', None) class UserCurrentHistoryResource(Resource):
    @authenticated('normal', 'admin')
    def get(self):
        user = get_user(key=session['user'])[0]
        args = UserHistoryQueryArgs().args
        if 'start' not in args:
            args['start'] = None
        if 'end' not in args:
            args['end'] = None
        return marshal(get_user_history(user, args['start'], args['end']), history_fields)

    @authenticated('normal', 'admin')
    def post(self):
        user = get_user(key=session['user'])[0]
        args = UserHistoryPostArgs().args
        add_user_history(user, args['op'], args['key']) class UserCurrentFavorResource(Resource):
    @authenticated('normal', 'admin')
    def get(self):
        user = get_user(key=session['user'])[0]
        args = UserHistoryQueryArgs().args  # use history query args
        if 'start' not in args:
            args['start'] = None
        if 'end' not in args:
            args['end'] = None
        return marshal(get_user_favor(user, args['start'], args['end']), music_fields) SECRET_KEY = '\x0f\x18\xb5\xeeP6\x84\xd5\x1b\x1b\xe7"\x98\xb8XV\x80M\xd50\xabO-\x85'
from flask import Flask
from flask.ext.restful import Api
from resource.files import FileResource
from resource.music import MusicResource, MusicListResource
from resource.channel import ChannelResource, ChannelListResource
from resource.playlist import PlaylistListResource, PlaylistResource
from resource.user import UserResource, UserListResource, UserCurrentResource, UserCurrentHistoryResource, UserCurrentFavorResource
from config import SECRET_KEY
from flask import render_template
#!/usr/bin/env python
# encoding: utf-8

app = Flask(__name__)
api = Api(app)

app.jinja_env.variable_start_string = '[['
app.jinja_env.variable_end_string = ']]'

api.add_resource(FileResource, '/api/fs/<string:key>/')

api.add_resource(MusicListResource, '/api/music/')
api.add_resource(MusicResource, '/api/music/<string:key>/')
api.add_resource(ChannelListResource, '/api/channel/')
api.add_resource(ChannelResource, '/api/channel/<string:key>/')

api.add_resource(PlaylistListResource, '/api/playlist/')