Beispiel #1
0
 def to_json(self):
     json = {
         'user_id': generate_id(self.user_id),
         'problem_id': generate_id(self.problem_id),
         'username': self.user.username,
         'problem': self.problem.title
     }
     return json
Beispiel #2
0
 def to_json(self):
     json = {
         'user_id': generate_id(self.user_id),
         'problem_id': generate_id(self.problem_id),
         'code_id': generate_id(self.code_id),
         'status': self.status,
         'language': self.language,
         'time_used': self.time_used,
         'memory_used': self.memory_used,
         'create_time': self.create_time,
         'update_time': self.update_time
     }
     return json
Beispiel #3
0
def search():
    # target: Model, type: Model attribute, content: going to search
    target = request.json.get('target')
    type = request.json.get('type')
    content = request.json.get('content')
    cls = eval(target)
    attr = eval(target + '.' + type)
    query = db.session.query(cls).filter(attr.like(
        '%{}%'.format(content))).all()
    tmp_list = []
    for i in query:
        tmp_dict = {}
        if hasattr(i, 'to_json'):
            tmp_list.append(i.to_json())
            continue
        if hasattr(i, 'list_to_json'):
            tmp_list.append(i.list_to_json())
            continue
        for key in i.__dict__:
            if key == '_sa_instance_state' or key == 'password_hash' or key == 'role_id':
                continue
            if key == 'id' or key == 'contest_id' or key == 'user_id' \
                    or key == 'problem_id' or key == 'code_id':
                tmp_dict.update({key: generate_id(i.__dict__[key])})
                continue
            tmp_dict.update({key: i.__dict__[key]})
        tmp_list.append(tmp_dict)
    return jsonify({'msg': 'ok', 'result': tmp_list})
Beispiel #4
0
def get_contest_user(id):
    page = request.args.get('page', 1, type=int)
    per_page = request.args.get('per_page', 20, type=int)
    is_join = request.args.get('is_join', True)
    paginate = UserJoinContest.query.filter_by(contest_id=verify_id(id), is_join=is_join).paginate(page,
                                                                                                   per_page=per_page, error_out=False)
    return jsonify({'msg': 'ok', 'result': [
        {'user_id': generate_id(item.user_id), 'user': item.user.username, 'is_join': item.is_join} for item in
        paginate.items]})
Beispiel #5
0
def get_token(duration=18000):
    token = g.current_user.generate_auth_token(duration)
    return jsonify({
        'msg':
        'ok',
        'result': [{
            'id': generate_id(g.current_user.id),
            'token': token.decode('ascii'),
            'duration': duration
        }]
    })
Beispiel #6
0
def create_problem():
    keyword = {'title', 'description', 'level',
               'tag', 'code', 'input', 'output', 'program'}
    if not set(request.json.keys()) == keyword:
        return jsonify({'msg': 'no', 'error': 'just require: {}'.format(keyword)})
    if Problem.query.filter_by(title=request.json.get('title')).first():
        return jsonify({'msg': 'no', 'error': 'title: {} is already existed'.format(request.json.get('title'))})

    problem = Problem(user_id=g.current_user.id)
    for key in request.json:
        setattr(problem, key, request.json.get(key))
    db.session.add(problem)
    db.session.flush()
    return jsonify({'msg': 'ok', 'result': [{'title': problem.title, 'id': generate_id(problem.id)}]})
Beispiel #7
0
 def to_json(self):
     user_nums = len(
         UserJoinContest.query.filter_by(contest_id=self.id,
                                         is_join=True).all())
     json = {
         'id': generate_id(self.id),
         'sponsor': self.user.username,
         'title': self.title,
         'description': self.description,
         'start_time': self.start_time,
         'end_time': self.end_time,
         'user_nums': user_nums,
         'auto_approve': self.auto_approve
     }
     return json
Beispiel #8
0
 def list_to_json(self):
     like_nums = len(
         UserLikeProblem.query.filter_by(problem_id=self.id,
                                         is_like=True).all())
     hate_nums = len(
         UserLikeProblem.query.filter_by(problem_id=self.id,
                                         is_like=False).all())
     json = {
         'id': generate_id(self.id),
         'title': self.title,
         'level': self.level,
         'tag': self.tag,
         'accepted': self.accepted,
         'submitted': self.submitted,
         'like_nums': like_nums,
         'hate_nums': hate_nums
     }
     return json
Beispiel #9
0
 def to_json(self):
     submit_nums = len(
         UserSubmitCode.query.filter_by(user_id=self.user_id).all())
     accept_nums = len(
         UserAcceptCode.query.filter_by(user_id=self.user_id).all())
     json = {
         'user_id': generate_id(self.user_id),
         'username': self.user.username,
         'role': self.user.role.name,
         'realname': self.realname,
         'profile': self.profile,
         'school': self.school,
         'about_me': self.about_me,
         'tag': self.tag,
         'submit_nums': submit_nums,
         'accept_nums': accept_nums,
         'create_time': self.create_time,
         'login_time': self.login_time
     }
     return json