summaryrefslogtreecommitdiff
path: root/api/v1/server.py
blob: a5bf61361efa51566ae8f41067ab8d91f8fe1897 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from flask import Blueprint, jsonify, abort, make_response, request

from .db import Category, DataPack, User, Tag, TagRelation
from .util import model_paginator, query_paginator, ConstrainFailed, get_tags_for_datapack

bp = Blueprint("api v1", __name__)


@bp.errorhandler(ConstrainFailed)
def handler(e: ConstrainFailed):
    return make_response(jsonify({
        'error': {
            'description': e.description,
            'parameter': e.parameter,
            'reason': e.reason,
        }
    }), 404)


@bp.route('/version/')
def version():
    return jsonify({
        'version': '1.0.0',
        'deprecated': False,
    })


@bp.route('/list/categories/')
def list_categories():
    return model_paginator(Category, lambda cat: {
        'name': cat.name,
        'id': cat.id,
    })


@bp.route('/list/datapacks')
def list_datapacks():
    category = request.args.get('category', '')
    query = DataPack.select().join(User)
    if category != '':
        query = query.join(Category).where(DataPack.category.id == category)
    return query_paginator(query, lambda dp: {
        'name': dp.name,
        'id': dp.id,
        'description': dp.description,
        'tags': get_tags_for_datapack(dp),
        'author': dp.author.id,
        'likes': dp.likes,
        'dislikes': dp.dislikes,
        'downloads': dp.downloads,
        'views': dp.views,
        'category': dp.category.id,

    })


@bp.route('/<path:invalid_path>')
def invalid_path(invalid_path):
    abort(404)