Beispiel #1
0
def delete_data(id, putD):
    """
    删除数据
    """
    q = Question.get_question(id)
    if not q:
        return fail_return("题目不存在")
    d = Data2Test.query.filter_by(que_id=id, input_data=putD).first()
    DatabaseOperation.delete(d)
    return success_return(msg="删除成功")
Beispiel #2
0
def delete_user(id):
    """
    删除用户
    :param id:
    :return:
    """
    user = User.get(id)
    if not user:
        return fail_return(msg='用户不存在')
    DatabaseOperation.delete(user, id)
    return success_return(g.current_user.to_json(), msg='用户已删除')
Beispiel #3
0
def submit(id):
    """提交代码"""
    language = request.form.get('language')
    content = request.form.get('content')
    sub = Submit(language=language,
                 content=content,
                 user=g.current_user,
                 question=Question.get_question(id))
    if sub:
        DatabaseOperation.add(sub)
        return success_return(sub.to_json(), "提交成功")
    return fail_return(msg="提交失败")
Beispiel #4
0
def change_name():
    """
    修改用户名名字
    :return:
    """
    new_name = request.form.get('newName')
    if not new_name:
        return fail_return(msg="名字不能为空")
    user = User.query.filter_by(username=new_name).first()
    if user:
        return fail_return(msg="用户名已存在")
    g.current_user.username = new_name
    DatabaseOperation.update()
    return success_return(g.current_user.to_json(), "用户名修改成功")
Beispiel #5
0
def confirm(token):
    """
    确认邮件激活用户
    :param token: str
    :return: json
    """
    id = User.confirm(token)
    user = User.query.filter_by(id=id).first()
    if not user:
        return fail_return(msg='用户不存在')
    if user.confirmed:
        return fail_return(msg='用户已激活')
    user.confirmed = True
    DatabaseOperation.update()
    return success_return(data=user.to_json(), msg='激活成功')
Beispiel #6
0
def delete_question(id):
    """
    删除题目
    :param id:
    :return:
    """
    q = Question.get_question(id)
    if not q:
        return fail_return(msg='所对应题目不存在')
    # data = Data2Test.query.filter_by(que_id=id)
    # print(data.que_id)
    # if data:
    #     DatabaseOperation.delete(data)
    DatabaseOperation.delete(q, id)
    return success_return(msg='删除成功')
Beispiel #7
0
def change_pwd():
    """
    修改密码(登录状态下)
    :return:
    """
    old_pwd = request.form.get('oldPwd')
    if not old_pwd:
        return fail_return(msg="密码不能为空")
    if g.current_user.verity_password(old_pwd):
        new_pwd = request.form.get('newPwd')
        if not new_pwd:
            return fail_return(msg="密码不能为空")
        g.current_user.password = new_pwd
        DatabaseOperation.update()
        return success_return(g.current_user.to_json(), "密码修改成功")
    else:
        return fail_return(msg='原密码错误')
Beispiel #8
0
def forget_pwd():
    """
    忘记密码发送邮件找回密码
    :return:
    """
    email = request.form.get('email')
    user = User.query.filter_by(email=email).first()
    token = user.generate_confirmation_token()

    if user:
        user.find_pwd_token = token
        DatabaseOperation.update()
        send_token_email(user, "changePwd", token)
        return success_return(data={
            'find_password_url':
            url_for('api.find_pwd', token=token, _external=True)
        },
                              msg='邮件已发送')
    return fail_return(msg="用户不存在")
Beispiel #9
0
def add_data(id):
    """添加数据"""
    q = Question.get_question(id)
    input_type = request.form.get('input_type')
    input_data = request.form.get('input_data')
    output_type = request.form.get('output_type')
    output_data = request.form.get('output_data')
    time_limit = request.form.get('time_limit')
    mem_limit = request.form.get('mem_limit')
    d = Data2Test(input_data=input_data,
                  input_type=input_type,
                  output_data=output_data,
                  output_type=output_type,
                  question=q,
                  time_limit=time_limit,
                  mem_limit=mem_limit)
    if d:
        DatabaseOperation.add(d)
        return success_return(d.to_json(), "添加成功")
    return fail_return(msg="添加失败")
Beispiel #10
0
def find_pwd(token):
    """
    点击邮件链接 找回密码
    :param token: str
    :return:
    """
    id = User.confirm(token)
    email = request.form.get('email')
    user = User.query.filter_by(id=id).first()
    user1 = User.query.filter_by(email=email).first()
    if user and user1 and user is user1:
        if user.find_pwd_token != token:
            return fail_return(msg="链接失效")
        new_password = request.form.get('newPwd')
        if not new_password:
            return fail_return(msg="密码不能为空")
        user.password = new_password
        user.find_pwd_token = None
        DatabaseOperation.update()
        return success_return(user.to_json(), "密码修改成功")
    return fail_return("用户不存在")
Beispiel #11
0
def modify_question(id):
    """
    修改题目信息
    :param id:
    :return:
    """
    que = Question.query.filter_by(id=id).first()
    if not que:
        return fail_return(msg='所对应题目不存在')
    topic = request.form.get('topic')
    content = request.form.get('content')
    notes = request.form.get('notes')
    example = request.form.get('example')
    if topic and content:
        que.topic = topic
        que.content = content
        que.notes = notes
        que.example = example
        DatabaseOperation.update()
        return success_return(que.to_json(), '修改成功')
    return fail_return('题目和内容不能为空')
Beispiel #12
0
 def authenticate(username, password):
     """
     用户登录,登录成功返回token,写将登录时间写入数据库;登录失败返回失败原因
     :param password:
     :return: json
     """
     user = User.query.filter_by(username=username).first()
     if user is None:
         return public.fail_return(data='', msg='找不到用户')
     else:
         if user.verity_password(password=password):
             if user.confirmed:
                 login_time = int(time.time())
                 user.login_time = login_time
                 DatabaseOperation.update()
                 token = Auth.encode_auth_token(user.id, login_time)
                 return public.success_return({"token": token.decode()},
                                              '登录成功')
             else:
                 return public.fail_return(msg='账号未激活')
         else:
             return public.fail_return('', '密码不正确')
Beispiel #13
0
def add_question():
    """
    添加题目
    :return: json
    """
    topic = request.form.get('topic')
    content = request.form.get('content')
    notes = request.form.get('notes')
    example = request.form.get('example')
    maker = g.current_user

    input_type = request.form.get('input_type')
    input_data = request.form.get('input_data')
    output_type = request.form.get('output_type')
    output_data = request.form.get('output_data')

    if topic and content:
        que = Question(topic=topic,
                       content=content,
                       notes=notes,
                       example=example,
                       maker=maker)
        DatabaseOperation.add(que)
        if que:
            if input_data and input_type and output_data and output_type:
                data = Data2Test(input_data=input_data,
                                 input_type=input_type,
                                 output_data=output_data,
                                 output_type=output_type,
                                 question=que)
                if data:
                    DatabaseOperation.add(data)
                    return success_return(que.to_json(), '添加成功')
                return fail_return(msg="题目添加成功,数据添加出错")
            return success_return(que.to_json(), '添加成功')
        return fail_return(msg='添加失败')
    return fail_return('题目和内容不能为空')
Beispiel #14
0
def register():
    """
    用户注册
    :return:json
    """
    email = request.form.get('email')
    username = request.form.get('username')
    password = request.form.get("password")
    if not email or not username or not password:
        return fail_return("", "用户名、密码和邮件不能为空")
    if User.query.filter_by(email=email).first():
        return fail_return(msg="邮箱已被注册")
    if User.query.filter_by(username=username).first():
        return fail_return(msg="用户名已被注册")

    user = User(username=username, email=email, password=password)
    result = DatabaseOperation.add(user)
    if user.id:
        token = user.generate_confirmation_token()
        send_token_email(user, "confirm", token)
        return success_return(user.to_json(), "邮件发送成功")
    else:
        print(result)
        return fail_return(msg="用户注册失败")