def user_permissions(): result = {"error": None, "data": {}} parser = RequestParser() parser.argument("account", type=helpers.account, required=True) args = parser.parse() account = args["account"] result["data"] = account.list_permissions() return result
def new_comment(): result = {"error": None, "data": {}} parser = RequestParser() parser.argument("subject", type=str, required=True, choices=choices) parser.argument("text", type=helpers.string, required=True) parser.argument("slug", type=str, required=True) args = parser.parse() service = get_service(args["subject"]) subject = service.get_by_slug(args["slug"]) comment = CommentService.create(subject, request.account, args["text"]) result["data"] = comment.dict() return result
def list_comments(): result = {"error": None, "data": []} parser = RequestParser() parser.argument("subject", type=str, required=True, choices=choices) parser.argument("slug", type=str, required=True) parser.argument("page", type=int, default=0) args = parser.parse() service = get_service(args["subject"]) subject = service.get_by_slug(args["slug"]) comments = CommentService.list(subject, args["page"]) for comment in comments: result["data"].append(comment.dict()) return result
def list_status(): result = {"error": None, "data": []} parser = RequestParser() parser.argument("subject", type=helpers.content, required=True) parser.argument("page", type=int, default=0) args = parser.parse() statuses = StatusService.get_by_account(request.account, args["subject"]) for status in statuses: result["data"].append(status.dict()) return result
def new_descriptor(): result = {"error": None, "data": {}} parser = RequestParser() parser.argument("service", type=helpers.descriptor_service, required=True) parser.argument("name", type=str, required=True) parser.argument("slug", type=str, required=True) parser.argument("description", type=str) args = parser.parse() check = DescriptorService.get_by_slug(args["service"], args["slug"]) if check: return abort(args["service"], "slug-exists") descriptor = DescriptorService.create( args["service"], args["name"], args["slug"], args["description"]) result["data"] = descriptor.dict() return result
def add_member(): result = {"error": None, "data": {}} parser = RequestParser() parser.argument("account", type=helpers.account, required=True) parser.argument("slug", type=helpers.team, required=True) args = parser.parse() account = args["account"] team = args["slug"] TeamService.add_member(team, account) result["data"] = team.dict(True) return result
def remove_member(): result = {"error": None, "data": {}} parser = RequestParser() parser.argument("account", type=helpers.account, required=True) parser.argument("slug", type=helpers.team, required=True) args = parser.parse() account = args["account"] team = args["slug"] TeamService.remove_member(team, account) PermissionService.remove(account, "global", "publishing") result["data"] = team.dict(True) return result
def check_status(): result = {"error": None, "data": {}} parser = RequestParser() parser.argument("subject", type=helpers.content, required=True) parser.argument("slug", type=str, default=None) args = parser.parse() subject = None content = static.slug("content", args["subject"]) if content == "anime": subject = helpers.anime(args["slug"]) status = StatusService.get(subject, request.account, args["subject"]) result["data"] = status.dict() return result
def delete_episode(): result = {"error": None, "data": {}} parser = RequestParser() parser.argument("position", type=helpers.position, required=True) parser.argument("slug", type=helpers.anime, required=True) args = parser.parse() anime = args["slug"] helpers.is_member(request.account, anime.teams) index = AnimeService.position_index(anime, args["position"]) if index is None: return abort("episode", "not-found") FileService.destroy(anime.episodes[index].video) FileService.destroy(anime.episodes[index].thumbnail) AnimeService.remove_episode(anime, anime.episodes[index]) result["data"] = anime.dict(True) return result
def update_descriptor(): result = {"error": None, "data": {}} parser = RequestParser() parser.argument("service", type=helpers.descriptor_service, required=True) parser.argument("slug", type=str, required=True) parser.argument("params", type=dict, default={}) args = parser.parse() descriptor = DescriptorService.get_by_slug(args["service"], args["slug"]) if descriptor is None: return abort(args["service"], "not-found") keys = ["name", "slug", "description"] update = utils.filter_dict(args["params"], keys) update_document(descriptor, update) try: descriptor.save() except Exception: return abort("general", "empty-required") result["data"] = descriptor.dict() return result
def update_status(): result = {"error": None, "data": {}} parser = RequestParser() parser.argument("rating", type=int, default=None, choices=range(1, 11)) parser.argument("position", type=helpers.position, default=None) parser.argument("subject", type=helpers.content, required=True) parser.argument("status", type=helpers.status, default=None) parser.argument("time", type=int, default=None) parser.argument("slug", type=str, default=None) args = parser.parse() subject = None content = static.slug("content", args["subject"]) if content == "anime": subject = helpers.anime(args["slug"]) status = StatusService.get(subject, request.account, args["subject"]) fields = ["rating", "status", "position", "time"] for field in fields: if args[field]: # Record rewatch if field == "position": if status[field] and args[field] < status[field]: status["rewatch"] += 1 status[field] = args[field] status.updated = datetime.utcnow() status.save() result["data"] = status.dict() return result
def update_comment(): result = {"error": None, "data": {}} parser = RequestParser() parser.argument("counter", type=int, required=True) parser.argument("params", type=dict, default={}) args = parser.parse() params_parser = RequestParser() params_parser.argument("text", type=helpers.string, required=True, location=("params")) params_args = params_parser.parse(req=args) comment = CommentService.get_by_counter(args["counter"], request.account) if comment is None: return abort("comment", "not-found") if datetime.utcnow() - comment.created > timedelta(minutes=20): return abort("comment", "not-editable") comment.text = params_args["text"] comment.updated = datetime.utcnow() comment.save() result["data"] = comment.dict() return result
def manage_permissions(): result = {"error": None, "data": {}} parser = RequestParser() parser.argument("action", type=str, required=True, choices=("add", "remove")) parser.argument("account", type=helpers.account, required=True) parser.argument("scope", type=str, required=True) parser.argument("name", type=str, required=True) args = parser.parse() account = args["account"] if args["action"] == "add": PermissionService.add(account, args["scope"], args["name"]) elif args["action"] == "remove": PermissionService.remove(account, args["scope"], args["name"]) result["data"] = account.list_permissions() return result
def add_episode(): result = {"error": None, "data": {}} parser = RequestParser() parser.argument("opening", type=helpers.opening, location="json") parser.argument("position", type=helpers.position, required=True) parser.argument("slug", type=helpers.anime, required=True) parser.argument("ending", type=int) parser.argument("name", type=str) args = parser.parse() anime = args["slug"] helpers.is_member(request.account, anime.teams) index = AnimeService.position_index(anime, args["position"]) if index is None: episode = AnimeService.get_episode(args["position"]) AnimeService.add_episode(anime, episode) else: episode = anime.episodes[index] fields = ["name", "opening", "ending"] for field in fields: if args[field]: episode[field] = args[field] anime.save() result["data"] = anime.dict(True) return result
def list_anime(): result = {"error": None, "data": []} parser = RequestParser() parser.argument("franchises", type=list, default=[], location="json") parser.argument("categories", type=list, default=[], location="json") parser.argument("ordering", type=list, default=[], location="json") parser.argument("states", type=list, default=[], location="json") parser.argument("genres", type=list, default=[], location="json") parser.argument("teams", type=list, default=[], location="json") parser.argument("query", type=str, default="") parser.argument("page", type=int, default=0) parser.argument("year", type=dict) args = parser.parse() year_parser = RequestParser() year_parser.argument("min", type=int, location="year") year_parser.argument("max", type=int, location="year") year_args = year_parser.parse(req=args) query = utils.search_query(args["query"]) categories = [] franchises = [] ordering = [] genres = [] states = [] teams = [] for slug in args["categories"]: category = helpers.category(slug) categories.append(category) for slug in args["genres"]: genre = helpers.genre(slug) genres.append(genre) for slug in args["franchises"]: franchise = helpers.franchise(slug) franchises.append(franchise) for slug in args["states"]: state = helpers.state(slug) states.append(state) for slug in args["teams"]: team = helpers.team(slug) teams.append(team) for item in args["ordering"]: check = item if item[0] in ["+", "-"]: check = item[1:] if check in ("rating"): ordering.append(check) anime = AnimeService.search(query, year_args, categories, genres, franchises, states, teams, ordering, False, args["page"], account=request.account) for item in anime: result["data"].append(item.dict()) return result
def new_anime(): result = {"error": None, "data": {}} parser = RequestParser() parser.argument("franchises", type=list, default=[], location="json") parser.argument("subtitles", type=list, default=[], location="json") parser.argument("voiceover", type=list, default=[], location="json") parser.argument("aliases", type=list, default=[], location="json") parser.argument("genres", type=list, default=[], location="json") parser.argument("category", type=helpers.category, required=True) parser.argument("description", type=helpers.string, required=True) parser.argument("state", type=helpers.state, required=True) parser.argument("team", type=helpers.team, required=True) parser.argument("season", type=int, choices=range(1, 5)) parser.argument("title", type=dict, required=True) parser.argument("year", type=int, required=True) parser.argument("external", type=dict) parser.argument("total", type=int) args = parser.parse() title_parser = RequestParser() title_parser.argument("ua", type=helpers.string, location="title", required=True) title_parser.argument("jp", type=helpers.string, location="title") title_args = title_parser.parse(req=args) external_parser = RequestParser() external_parser.argument("myanimelist", type=int, location="external") external_parser.argument("toloka", type=int, location="external") external_args = external_parser.parse(req=args) title = AnimeService.get_title(title_args["ua"], title_args["jp"]) slug = utils.create_slug(title_args["ua"]) team = args["team"] helpers.is_member(request.account, [team]) anime = AnimeService.create(title, args["description"], args["category"], args["state"], slug) for alias in args["aliases"]: if type(alias) is not str: return abort("general", "alias-invalid-type") anime.genres = [] for slug in args["genres"]: genre = helpers.genre(slug) anime.genres.append(genre) anime.franchises = [] for slug in args["franchises"]: franchise = helpers.franchise(slug) anime.franchises.append(franchise) fields = ["aliases", "year", "total"] for field in fields: anime[field] = args[field] fields = ["subtitles", "voiceover"] for field in fields: anime[field] = [] for username in args[field]: account = helpers.account(username) anime[field].append(account) search = utils.create_search(anime.title.ua, anime.title.jp, anime.aliases) external = AnimeService.get_external(external_args["myanimelist"], external_args["toloka"]) anime.external = external anime.search = search anime.teams = [team] if anime.external.myanimelist: anime.rating = utils.rating(anime.external.myanimelist) anime.save() result["data"] = anime.dict() anime.save() return result
def edit_team(): result = {"error": None, "data": {}} parser = RequestParser() parser.argument("slug", type=helpers.team, required=True) parser.argument("params", type=dict) args = parser.parse() params_parser = RequestParser() params_parser.argument("description", type=helpers.string, location=("params")) params_parser.argument("name", type=helpers.string, location=("params")) params_args = params_parser.parse(req=args) team = args["slug"] if params_args["description"]: team.description = params_args["description"] if params_args["name"]: team.name = params_args["name"] team.save() result["data"] = team.dict(True) return result
def new_team(): result = {"error": None, "data": {}} parser = RequestParser() parser.argument("members", type=list, default=[], location="json") parser.argument("admins", type=list, default=[], location="json") parser.argument("description", type=str, required=True) parser.argument("name", type=str, required=True) parser.argument("slug", type=str, required=True) args = parser.parse() team = TeamService.get_by_slug(args["slug"]) if team: return abort("team", "slug-exists") team = TeamService.create(args["name"], args["slug"], args["description"]) for username in args["members"]: account = helpers.account(username) TeamService.add_member(team, account) if account.username in args["admins"]: PermissionService.add(account, "global", "publishing") result["data"] = team.dict(True) return result
def edit_anime(): result = {"error": None, "data": {}} parser = RequestParser() parser.argument("params", type=dict, default={}, location="json") parser.argument("slug", type=helpers.anime, required=True) args = parser.parse() params_parser = RequestParser() params_parser.argument("season", type=int, choices=range(1, 5), location="params") params_parser.argument("description", type=helpers.string, location="params") params_parser.argument("category", type=helpers.category, location="params") params_parser.argument("state", type=helpers.state, location="params") params_parser.argument("franchises", type=list, location="params") params_parser.argument("subtitles", type=list, location="params") params_parser.argument("voiceover", type=list, location="params") params_parser.argument("external", type=dict, location="params") params_parser.argument("selected", type=bool, location="params") params_parser.argument("aliases", type=list, location="params") params_parser.argument("genres", type=list, location="params") params_parser.argument("title", type=dict, location="params") params_parser.argument("total", type=int, location="params") params_parser.argument("year", type=int, location="params") params_args = params_parser.parse(req=args) title_parser = RequestParser() title_parser.argument("jp", type=str, location="title") title_parser.argument("ua", type=str, location="title") title_args = title_parser.parse(req=params_args) external_parser = RequestParser() external_parser.argument("myanimelist", type=int, location="external") external_parser.argument("toloka", type=int, location="external") external_args = external_parser.parse(req=params_args) anime = args["slug"] helpers.is_member(request.account, anime.teams) if params_args["aliases"]: for alias in args["aliases"]: if type(alias) is not str: return abort("general", "alias-invalid-type") if params_args["genres"]: anime.genres = [] for slug in params_args["genres"]: genre = helpers.genre(slug) anime.genres.append(genre) if params_args["franchises"]: anime.franchises = [] for slug in params_args["franchises"]: franchise = helpers.franchise(slug) anime.franchises.append(franchise) fields = [ "category", "description", "state", "year", "total", "selected", "aliases", "season" ] for field in fields: if params_args[field]: anime[field] = params_args[field] fields = ["subtitles", "voiceover"] for field in fields: if params_args[field]: anime[field] = [] for username in params_args[field]: account = helpers.account(username) anime[field].append(account) fields = ["jp", "ua"] for field in fields: if title_args[field]: anime.title[field] = title_args[field] if params_args["external"]: if external_args["myanimelist"]: anime.external.myanimelist = external_args["myanimelist"] anime.rating = utils.rating(anime.external.myanimelist) if external_args["toloka"]: anime.external.toloka = external_args["toloka"] if PermissionService.check(request.account, "global", "admin"): if params_args["selected"]: anime["selected"] = params_args["selected"] anime.search = utils.create_search(anime.title.ua, anime.title.jp, anime.aliases) anime.save() result["data"] = anime.dict() return result
def file_upload(): result = {"error": None, "data": {}} choices = ("account", "anime", "episode") parser = RequestParser() parser.argument("subject", type=str, choices=choices, required=True) parser.argument("type", type=helpers.string, required=True) parser.argument("file", type=FileStorage, location="files") parser.argument("uuid", type=helpers.uuid, required=True) parser.argument("offset", type=int, required=True) parser.argument("index", type=int, required=True) parser.argument("total", type=int, required=True) parser.argument("size", type=int, required=True) parser.argument("slug", type=helpers.string) parser.argument("position", type=int) args = parser.parse() upload_type = args["type"] uuid = args["uuid"] if args["subject"] == "account": if upload_type not in ("avatar"): return abort("file", "bad-upload-type") subject = request.account folder = subject.username if args["subject"] == "anime": if upload_type not in ("poster", "banner"): return abort("file", "bad-upload-type") subject = helpers.anime(args["slug"]) folder = subject.slug if args["subject"] == "episode": if upload_type not in ("video", "thumbnail"): return abort("file", "bad-upload-type") subject = helpers.anime(args["slug"]) index = AnimeService.position_index(subject, args["position"]) if index is None: return abort("episode", "not-found") folder = subject.slug + "/" + str(args["position"]) chunks = upload.ChunkHelper(request.account, folder, args["file"], upload_type, uuid) chunks.load(args["size"], args["index"], args["total"], args["offset"]) if args["index"] + 1 == args["total"]: helper = upload.UploadHelper(request.account, chunks.blob_file, upload_type) if args["subject"] in ("account", "anime"): file = helper.upload_image() FileService.destroy(subject[upload_type]) subject[upload_type] = file if args["subject"] == "episode": if upload_type == "video": file = helper.upload_video() else: file = helper.upload_image() FileService.destroy(subject.episodes[index][upload_type]) subject.episodes[index][upload_type] = file subject.save() result["data"] = subject.dict() chunks.clean(True) else: result["data"]["chunk"] = True return result