diff options
-rw-r--r-- | api/v1/db.py | 4 | ||||
-rw-r--r-- | api/v1/server.py | 14 | ||||
-rw-r--r-- | api/v1/util.py | 7 |
3 files changed, 20 insertions, 5 deletions
diff --git a/api/v1/db.py b/api/v1/db.py index 1d9f469..f19f306 100644 --- a/api/v1/db.py +++ b/api/v1/db.py @@ -37,8 +37,8 @@ class DataPack(BaseModel): class TagRelation(BaseModel): - tags = ForeignKeyField(Tag) - packs = ForeignKeyField(DataPack) + tag = ForeignKeyField(Tag) + pack = ForeignKeyField(DataPack) class Comment(BaseModel): diff --git a/api/v1/server.py b/api/v1/server.py index 96ff8da..a5bf613 100644 --- a/api/v1/server.py +++ b/api/v1/server.py @@ -1,7 +1,7 @@ from flask import Blueprint, jsonify, abort, make_response, request -from .db import Category, DataPack -from .util import model_paginator, query_paginator, ConstrainFailed +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__) @@ -36,13 +36,21 @@ def list_categories(): @bp.route('/list/datapacks') def list_datapacks(): category = request.args.get('category', '') - query = DataPack.select() + 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, + }) diff --git a/api/v1/util.py b/api/v1/util.py index f2dddad..3b79b82 100644 --- a/api/v1/util.py +++ b/api/v1/util.py @@ -3,6 +3,8 @@ import typing from flask import jsonify, request from peewee import Model, ModelSelect +from api.v1.db import DataPack, Tag, TagRelation + _V = typing.TypeVar('_V', bound=Model) @@ -59,3 +61,8 @@ def query_paginator(query: ModelSelect, mapping: typing.Callable[[_V], dict]): 'next': offset + size if more else None, 'results': res, }) + + +def get_tags_for_datapack(datapack: DataPack): + return [tag.name for tag in + Tag.select().join(TagRelation).join(DataPack).where(TagRelation.pack.id == int(datapack.id))] |