Esempio n. 1
0
def register():
    data = get_data_from_ajax()
    username = data.get('username', None)
    password = data.get('password', None)
    role = data.get('role', None)
    if username is None or password is None or role is None:
        abort(400, u"必须填写username和password和role")

    username = unicode(username)
    userindex = UserController.get_userindex_byname(username)
    if userindex is not None:
        abort(403, u"该用户(%s)已经注册过" % username)

    password = hashutil.hash_md5(unicode(password))
    try:
        role = int(role) if role is not None else UserRole.Student
    except ValueError, e:
        abort(403, u"Role 必须为整数")
Esempio n. 2
0
def job_query():
    data = get_data_from_ajax()
    jobid = data.get("JobID", None)
    if jobid is None:
        abort(400, "缺少 JobID")

    # job = JobInfo.from_blockchain(jobid)
    job = JobController.get_job_by_jobid(jobid)
    if job is None:
        abort(403, u"没有查找到对应JobID: %s 的兼职信息" % jobid)
    # print type(job.id)
    job.save()

    d = job.dump_to_dict()
    username = data.get("username", None)
    if username is None:
        username = data.get("Username", None)
    if username is not None:
        userindex = UserController.get_userindex_byname(username)
        if userindex is not None:
            if jobid in userindex.JobTxMap.keys():
                d["IsApplied"] = True
            else:
                d["IsApplied"] = False

    state = data.get("State", None)
    if state is not None:
        try:
            state = int(state)
            if not (0 <= state <= 4):
                abort(400, "State 只能是 0,1,2,3,4")
        except ValueError, e:
            abort(400, "State 只能是数字")
        txs = d["Txs"]
        l = list()
        for t in txs:
            if t['State'] == state:
                l.append(t)
        d["Txs"] = l
Esempio n. 3
0
def login():
    form = UserController.LoginForm(request.form)
    msg = ''
    role = UserRole.Student
    if request.method == 'POST':
        ajax = False
        if request_wants_json():
            ajax = True
            data = get_data_from_ajax()
            form.username.data = unicode(data.get('username', None))
            form.password.data = unicode(data.get('password', None))
            role = data.get('role', None)
            role = int(role) if role is not None else UserRole.Student
        if ajax or form.validate_on_submit():
            username = form.username.data
            password = hashutil.hash_md5(form.password.data)
            userindex = UserController.get_userindex_byname(username)
            role = UserRole.get(role)
            if userindex is None:
                # userindex = UserController.create_userindex(username, password, role)
                # token = UserController.generate_token(username)
                # return return_data(None, {'token': token, 'detail': False},
                #                    msg="register success for %s" % userindex.Username)
                abort(403, "该用户不存在,请先注册!")
            else:
                if password == userindex.Password:
                    if role != userindex.Role:
                        abort(
                            403, u"该用户登陆时选择了不同的身份(应该为:%s)" %
                            UserRoleMapping.get(userindex.Role, u"学生"))

                    # enter index page
                    # session['user'] = userindex.username
                    msg = 'You were successfully login in for user:%s !' % username
                    flash(msg)
                    # if request_wants_json():
                    #     return jsonify(jsonutil.json_wrapper({}, 0, msg))
                    token = UserController.generate_token(username)

                    detail = False if userindex.UserID == '' else True
                    return return_data(None, {
                        'token': token,
                        'detail': detail
                    },
                                       msg="login success for %s" %
                                       userindex.Username)
                else:
                    msg = u"密码错误!"
                    flash(msg)
            pass
        else:
            msg = u'用户名或者密码不符合要求(最小3位最长20位)'
            flash(msg)
    else:
        msg = u'请登陆'
        flash(msg)

    if request_wants_json():
        abort(401, msg)

    return render_template('login.html', form=form, info=msg)