from flask import jsonify from app.libs.red_print import RedPrint from app.models.accept_problem import AcceptProblem from app.validators.accept_problem import (SearchAcceptProblemForm, SearchAcceptProblemSummaryForm) api = RedPrint('accept_problem') @api.route("", methods=['GET']) def search_accept_problem_api(): form = SearchAcceptProblemForm().validate_for_api().data_ order = form['order'] if order is None: order = dict() if not hasattr(order, 'create_time'): order['create_time'] = 'desc' form['order'] = order return jsonify({ 'code': 0, 'data': { 'detail': AcceptProblem.search(**form), 'summary': AcceptProblem.search_distribute(form['username'], form['start_date'], form['end_date']) } })
from flask import jsonify from flask_login import current_user, login_required, login_user, logout_user from app.libs.error_code import AuthFailed, DeleteSuccess, Success from app.libs.red_print import RedPrint from app.models.user import User from app.validators.session import LoginForm, SSOLoginForm api = RedPrint('session') @api.route("", methods=['GET']) @login_required def get_session_api(): user = current_user user.fields = ['username', 'nickname', 'group', 'permission', 'status'] return jsonify({'code': 0, 'data': user}) @api.route("", methods=['POST']) def create_session_api(): form = LoginForm().validate_for_api().data_ user = User.get_by_id(form['username']) if user is None: raise AuthFailed('User not found') if not user.check_password(form['password']): raise AuthFailed('Wrong username or password') login_user(user, remember=True) raise Success('Login successful')
from flask import jsonify from app.libs.global_varible import g from app.libs.red_print import RedPrint api = RedPrint('meta') @api.route('/version', methods=['GET']) def get_version_api(): return jsonify({'code': 0, 'version': g.run_start_time})
from flask import request from app.libs.red_print import RedPrint from app.libs.jigsaw import Jigsaw from flask import jsonify api = RedPrint('/') def get_cookies(email, password): jigsaw = Jigsaw( 'https://pintia.cn/auth/login?redirect=https%3A%2F%2Fpintia.cn%2F', headless=False) jigsaw.send_keys(email, '//input[@name="email"]') jigsaw.send_keys(password, '//input[@name="password"]') jigsaw.click('//button[@class="pc-button pc-color-primary cur-p"]') t = 0 while 1: t += 1 try: jigsaw.run() test = jigsaw.wait_for_element_by_classname('notification-message') if test: raise Exception('wrong password') jigsaw.url_to_be('https://pintia.cn/problem-sets?tab=0') break except Exception as e: if str(e) == 'wrong password': jigsaw.close() raise e
from flask import jsonify from flask_login import login_required, current_user from app import redis from app.libs.error_code import Success, Forbidden from app.libs.red_print import RedPrint from app.validators.forms import RefreshAcceptProblemForm, RefreshProblemRatingForm, NoAuthUsernameForm, OJIdForm from tasks import task_crawl_all_accept_problem, task_crawl_accept_problem, task_crawl_problem_rating, \ task_calculate_user_rating, task_refresh_oj_problem_rating api = RedPrint('task') @api.route("/get_task_count", methods=['POST']) def get_task_count_api(): res = redis.llen('celery') return jsonify({'code': 0, 'data': res}) @api.route("/refresh_all_data", methods=['POST']) @login_required def refresh_all_data_api(): if not current_user.permission: raise Forbidden('Only administrators can operate') task_crawl_all_accept_problem.delay() return Success('Submit all refresh request successfully') @api.route("/refresh_accept_problem", methods=['POST']) @login_required def refresh_accept_problem_api():
from flask import jsonify from flask_login import login_required from app.libs.auth import admin_only from app.libs.error_code import CreateSuccess, DeleteSuccess, NotFound, Success from app.libs.red_print import RedPrint from app.models.oj import OJ from app.models.problem_set.problem_set import ProblemSet from app.validators.problem_set import (CreateProblemSetForm, ModifyProblemSetForm) api = RedPrint('problem_set') @api.route("/valid_oj", methods=['GET']) def get_oj_list(): oj_list = OJ.search(status=1, page_size=-1)['data'] return jsonify({'code': 0, 'data': oj_list}) @api.route("/summary", methods=['GET']) def get_summary(): problem_set_list = ProblemSet.search(page_size=-1, order={'id': 'desc'})['data'] return jsonify({'code': 0, 'data': problem_set_list}) @api.route("/<int:id_>", methods=['GET']) def get_problem_set_api(id_): problem_set = ProblemSet.get_by_id(id_) if problem_set is None:
from app.libs.spider_service import submit_crawl_course_info_task from app.models.base import db from app.models.camp_models.camp import Camp from app.models.camp_models.camp_accept_problem import CampAcceptProblem from app.models.camp_models.camp_oj import CampOJ from app.models.camp_models.camp_problem import CampProblem from app.models.camp_models.course import Course from app.models.camp_models.course_contest import CourseContest from app.models.camp_models.course_oj_username import CourseOJUsername from app.models.camp_models.user_contest import UserContest from app.models.user import User from app.validators.camp import (AppendContestForm, CreateCampForm, CreateCourseForm, ModifyCampNameForm, ModifyCourseForm, ModifyCourseUsernameForm) api = RedPrint('camp') @api.route('/valid_oj', methods=['GET']) def valid_oj_api(): oj_list = CampOJ.search(status=1, page_size=-1)['data'] return jsonify({ 'code': 0, 'data': oj_list }) @api.route('/summary', methods=['GET']) def summary_api(): camps = Camp.search(page_size=-1, order={'id': 'desc'})['data'] return jsonify({
import time from flask import current_app, jsonify from app.libs.enums import ClientTypeEnum from app.libs.error_code import AuthFailed from app.libs.red_print import RedPrint from app.models.user import User from app.validators.forms import ClientForm, TokenForm from itsdangerous import ( TimedJSONWebSignatureSerializer as Serializer, BadSignature, SignatureExpired, ) api = RedPrint("token") @api.route("", methods=["POST"]) def get_token(): # url = http://localhost:5000/v1/token form = ClientForm().validate_for_api() promise = { ClientTypeEnum.USER_EMAIL: User.verify_by_email, ClientTypeEnum.USER_MOBILE: User.verify_by_mobile, ClientTypeEnum.USER_MINA: User.verify_by_mina, ClientTypeEnum.USER_WX: User.verify_by_wx, } identity = promise[ClientTypeEnum(form.type.data)](form.account.data, form.secret.data) expiration = current_app.config["TOKEN_EXPIRATION"]
from flask import jsonify from flask_login import current_user, login_required from app.libs.auth import admin_only from app.libs.error_code import CreateSuccess, Forbidden, NotFound, Success from app.libs.red_print import RedPrint from app.models.user import User from app.validators.user import CreateUserForm, ModifyUserForm, SearchUserForm api = RedPrint('user') @api.route("/<string:id_>", methods=['GET']) def get_user_api(id_): user = User.get_by_id(id_) if user is None: raise NotFound('User not found') fields = User.fields.copy() fields.extend([ 'rating', 'oj_username', 'problem_distributed', 'last_cf_date', 'rating_trend', 'codeforces_rating', 'contest_num', 'cf_rating_trend', 'cf_statistics' ]) user.fields = fields return jsonify({"code": 0, "data": user}) @api.route("", methods=['POST']) @login_required @admin_only
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019/1/9 16:03 # @Author: yanmiexingkong # @email : [email protected] # @File : gift.py from flask import g from app.libs.error_code import DuplicateGift, Success from app.libs.red_print import RedPrint from app.libs.token_auth import auth from app.models.base import db from app.models.book import Book from app.models.gift import Gift api = RedPrint("gift") @api.route("/<int:isbn>", methods=["POST"]) @auth.login_required def create(isbn): # url = http://localhost:5000/v1/gift/<int:isbn> uid = g.user.uid with db.auto_commit(): Book.query.filter_by(isbn=isbn).first_or_404() gift = Gift.query.filter_by(isbn=isbn, uid=uid).first_or_404() if gift: raise DuplicateGift() gift = Gift() gift.isbn = isbn gift.uid = uid
from flask import jsonify from flask_login import login_required from app.libs.auth import admin_only from app.libs.error_code import CreateSuccess, DeleteSuccess, NotFound from app.libs.red_print import RedPrint from app.models.competition import Competition from app.validators.competition import (BaseSearchCompetitionForm, CreateCompetitionForm, ModifyCompetitionForm) api = RedPrint('competition') @api.route('/', methods=['GET']) def get_competition_api(): form = BaseSearchCompetitionForm().validate_for_api().data_ competition_list = Competition.search_from_now(**form) return jsonify({ 'code': 0, 'data': competition_list }) @api.route('/', methods=['POST']) @login_required @admin_only def create_competition_api(): form = CreateCompetitionForm().validate_for_api().data_ Competition.create(**form) raise CreateSuccess('Competition has been created')
from flask_login import current_user, login_required from app.libs.error_code import Forbidden, Success from app.libs.red_print import RedPrint from app.models.oj_username import OJUsername from app.validators.oj_username import CreateOJUsernameForm api = RedPrint('oj_username') @api.route("", methods=['POST']) @login_required def create_oj_username(): # 创建 修改 删除 form = CreateOJUsernameForm().validate_for_api().data_ if current_user.permission != 1: if current_user.id != form['username']: raise Forbidden() res = OJUsername.search(oj_id=form['oj_id'], username=form['username']) if res['meta']['count'] == 1: oj_username = res['data'][0] if form['oj_username'] == oj_username.oj_username: oj_username.modify(oj_password=form['oj_password']) else: oj_username.delete() OJUsername.create(oj_id=form['oj_id'], username=form['username'], oj_username=form['oj_username'], oj_password=form['oj_password']) else: OJUsername.create(oj_id=form['oj_id'], username=form['username'], oj_username=form['oj_username'], oj_password=form['oj_password'])
from flask import jsonify from app.libs.red_print import RedPrint from app.models.accept_problem import get_accept_problem_list_by_date, get_accept_problem_count_by_date, \ get_accept_problem_oj_distributed, get_accept_problem_date_distributed, get_rating_rank_list, get_rating_trend from app.models.oj import get_oj_list from app.models.problem import get_problem_by_problem_info from app.models.user import get_user_list from app.validators.forms import DateForm, InquireForm, InquireCountForm, NoAuthUsernameForm, InquireProblemIdForm api = RedPrint('data') @api.route("/get_all_accept_problem_count", methods=['POST']) def get_all_accept_problem_count_api(): form = DateForm().validate_for_api() res = list() for user in get_user_list(): if user['status']: res.append({ 'username': user['username'], 'nickname': user['nickname'], 'group': user['group'], 'accept_problem_count': get_accept_problem_count_by_date(user['username'], form.start_date.data, form.end_date.data) }) return jsonify({'code': 0, 'data': res})
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2018/12/14 18:23 # @Author: yanmiexingkong # @email : [email protected] # @File : book.py from flask import jsonify from sqlalchemy import or_ from app.libs.red_print import RedPrint from app.models.book import Book from app.validators.forms import BookSearchForm api = RedPrint("book") @api.route("/search", methods=["GET"]) def search(): # url = http://localhost:5000/v1/book/search?q={} form = BookSearchForm().validate_for_api() q = "%" + form.q.data + "%" books = Book.query.filter(or_(Book.title.like(q), Book.publisher.like(q))).all() books = [book.hide("summary") for book in books] return jsonify(books) @api.route("/<int:isbn>/detail", methods=["GET"]) def detail(isbn): # url = http://localhost:5000/v1/book/<int:isbn>/detail book = Book.query.filter_by(isbn=isbn).first_or_404() return jsonify(book)