def get(self, query_args): q = query_args.pop("q", None) field = str(query_args.pop("field", None)) filters = build_model_filters(model=Users, query=q, field=field) if is_admin() and request.args.get("view") == "admin": users = (Users.query.filter_by(**query_args).filter( *filters).paginate(per_page=50, max_per_page=100)) else: users = (Users.query.filter_by( banned=False, hidden=False, **query_args).filter(*filters).paginate(per_page=50, max_per_page=100)) response = UserSchema(view="user", many=True).dump(users.items) if response.errors: return {"success": False, "errors": response.errors}, 400 return { "meta": { "pagination": { "page": users.page, "next": users.next_num, "prev": users.prev_num, "pages": users.pages, "per_page": users.per_page, "total": users.total, } }, "success": True, "data": response.data, }
def get(self, query_args): q = query_args.pop("q", None) field = str(query_args.pop("field", None)) filters = build_model_filters(model=Hints, query=q, field=field) hints = Hints.query.filter_by(**query_args).filter(*filters).all() response = HintSchema(many=True).dump(hints) if response.errors: return {"success": False, "errors": response.errors}, 400 return {"success": True, "data": response.data}
def get(self, query_args): q = query_args.pop("q", None) field = str(query_args.pop("field", None)) filters = build_model_filters(model=Pages, query=q, field=field) pages = Pages.query.filter_by(**query_args).filter(*filters).all() schema = PageSchema(exclude=["content"], many=True) response = schema.dump(pages) if response.errors: return {"success": False, "errors": response.errors}, 400 return {"success": True, "data": response.data}
def get(self, query_args): q = query_args.pop("q", None) field = str(query_args.pop("field", None)) filters = build_model_filters(model=Unlocks, query=q, field=field) unlocks = Unlocks.query.filter_by(**query_args).filter(*filters).all() schema = UnlockSchema() response = schema.dump(unlocks) if response.errors: return {"success": False, "errors": response.errors}, 400 return {"success": True, "data": response.data}
def get(self, query_args): q = query_args.pop("q", None) field = str(query_args.pop("field", None)) filters = build_model_filters(model=Notifications, query=q, field=field) notifications = (Notifications.query.filter_by(**query_args).filter( *filters).all()) schema = NotificationSchema(many=True) result = schema.dump(notifications) if result.errors: return {"success": False, "errors": result.errors}, 400 return {"success": True, "data": result.data}
def get(self, query_args): q = query_args.pop("q", None) field = str(query_args.pop("field", None)) filters = build_model_filters(model=Teams, query=q, field=field) if is_admin() and request.args.get("view") == "admin": teams = ( Teams.query.filter_by(**query_args) .filter(*filters) .paginate(per_page=50, max_per_page=100) ) else: teams = ( Teams.query.filter_by(hidden=False, banned=False, **query_args) .filter(*filters) .paginate(per_page=50, max_per_page=100) ) user_type = get_current_user_type(fallback="user") view = copy.deepcopy(TeamSchema.views.get(user_type)) view.remove("members") response = TeamSchema(view=view, many=True).dump(teams.items) if response.errors: return {"success": False, "errors": response.errors}, 400 return { "meta": { "pagination": { "page": teams.page, "next": teams.next_num, "prev": teams.prev_num, "pages": teams.pages, "per_page": teams.per_page, "total": teams.total, } }, "success": True, "data": response.data, }
def get(self, query_args): # Build filtering queries q = query_args.pop("q", None) field = str(query_args.pop("field", None)) filters = build_model_filters(model=Challenges, query=q, field=field) # This can return None (unauth) if visibility is set to public user = get_current_user() # Admins can request to see everything if is_admin() and request.args.get("view") == "admin": challenges = (Challenges.query.filter_by(**query_args).filter( *filters).order_by(Challenges.value).all()) solve_ids = set([challenge.id for challenge in challenges]) else: challenges = (Challenges.query.filter( and_(Challenges.state != "hidden", Challenges.state != "locked")).filter_by( **query_args).filter(*filters).order_by( Challenges.value).all()) if user: solve_ids = (Solves.query.with_entities( Solves.challenge_id).filter_by( account_id=user.account_id).order_by( Solves.challenge_id.asc()).all()) solve_ids = set([value for value, in solve_ids]) # TODO: Convert this into a re-useable decorator if is_admin(): pass else: if config.is_teams_mode() and get_current_team() is None: abort(403) else: solve_ids = set() response = [] tag_schema = TagSchema(view="user", many=True) for challenge in challenges: if challenge.requirements: requirements = challenge.requirements.get("prerequisites", []) anonymize = challenge.requirements.get("anonymize") prereqs = set(requirements) if solve_ids >= prereqs: pass else: if anonymize: response.append({ "id": challenge.id, "type": "hidden", "name": "???", "value": 0, "category": "???", "tags": [], "template": "", "script": "", }) # Fallthrough to continue continue try: challenge_type = get_chal_class(challenge.type) except KeyError: # Challenge type does not exist. Fall through to next challenge. continue # Challenge passes all checks, add it to response response.append({ "id": challenge.id, "type": challenge_type.name, "name": challenge.name, "value": challenge.value, "category": challenge.category, "tags": tag_schema.dump(challenge.tags).data, "template": challenge_type.templates["view"], "script": challenge_type.scripts["view"], }) db.session.close() return {"success": True, "data": response}