Example #1
0
def deal_tmp_change():
    '''
    :return: {1: 'success', 0: 'this is no type or data in post'}
    '''
    if (not isAllowed(session)):
        return redirect(url_for('deal_login'))
    if (session['level'] == 0):
        ret = {}
        inData = request.form.to_dict()
        ret['status'] = 0
        if (inData.has_key('type')):
            if (inData['type'] == 'eqp'):
                if (inData['op'] == 'edit'):
                    ret['status'] = Eqp.update_eqp2(inData, 0, session['id'])
                if (inData['op'] == 'auth'):
                    ret['status'] = Eqp.update_eqp2(inData, 0, session['id'])
            elif (inData['type'] == 'station'):
                if (inData['op'] == 'edit'):
                    ret['status'] = Station.update_station2(inData)
                elif (inData['op'] == 'del'):
                    ret['status'] = Station.delete_station2(inData)
            elif (inData['type'] == 'user'):
                if (inData['op'] == 'edit'):
                    ret['status'] = User.update_user2(inData)
                elif (inData['op'] == 'del'):
                    ret['status'] = User.del_user2(inData)
                else:
                    ret['status'] = 0
            elif (inData['type'] == 'auth'):
                if (inData['op'] == 'del'):
                    ret['status'] = Auth.delete_auth2(inData)
                else:
                    ret['status'] = 0
            elif (inData['type'] == 'sensor'):
                if (inData['op'] == 'edit'):
                    ret['status'] = Sensor.update_sensor2(inData)
                elif (inData['op'] == 'add'):
                    ret['status'] = Sensor.add2(inData)
                else:
                    ret['status'] = 0
        else:
            ret['status'] = 0

        return jsonify(ret)
    if (session['level'] == 1):
        ret = {}
        inData = request.form.to_dict()
        ret['status'] = 0
        if (inData.has_key('type')):
            if (inData['type'] == 'eqp'):
                ret['status'] = Eqp.update_eqp2(inData, 1, session['id'])
        else:
            ret['status'] = 0

        return jsonify(ret)
Example #2
0
def deal_history():
    if (not isAllowed(session)):
        return redirect(url_for('deal_login'))
    inData = None
    if (session['level'] == 2):
        return render_template('error.html', inData=inData)
    if (session['level'] == 1):
        inData = deal_privilege(session)
        inData['addEqpSensorInfo'] = []
        inData['allSensorInfo'] = []
        tmpAllSensor = Sensor.info({})['data']
        for tmp1 in tmpAllSensor:
            if (not tmp1.has_key('username')):
                continue
            elif (tmp1['username'] == session['username']):
                inData['allSensorInfo'].append(tmp1)
        return render_template('history.html', inData=inData)
    if (session['level'] == 0):
        inData = deal_privilege(session)
        inData['stationInfo'] = Station.select({})['data']
        inData['addEqpSensorInfo'] = Sensor.info({})['data']
        inData['addAuthSensorInfo'] = Sensor.specialInfo({
            'in': {},
            'out': {
                'type': 'addAuth',
                'username': session['username'],
                'level': session['level']
            }
        })['data']
        inData['addAuthUserInfo'] = User.select({'level': 1})['data']
        return render_template('history.html', inData=inData)
Example #3
0
 def get_current_user(self):
     try:
         user_id = self.session["user_id"]
         with sessionCM() as session:
             user = User.find_by_id(session, user_id)
             return user
     except Exception, e:
         logger.info(e.message)
Example #4
0
 def add_new_user(self):
     try:
         mobile = self.params.get("mobile")
         password = self.params.get("password")
         password2 = self.params.get("password2")
         if not mobile or not password:
             raise NullArgumentException
         if password != password2:
             raise ErrorArgumentError
         with sessionCM() as session:
             user = User.find_by_mobile(session, mobile)
             if user:
                 return {"status": 0, "message": "用户已存在"}
             else:
                 user = User.create(session, mobile, password)
                 self.session["user_id"] = user.id
                 return {"status": 1, "message": "注册成功!"}
     except NullArgumentException, e:
         return {"status": 0, "message": e.msg}
Example #5
0
def deal_tmp_refreshUser():
    if (not isAllowed(session)):
        return redirect(url_for('deal_login'))
    if (session['level'] == 0):
        ret = {}
        ret['data'] = User.select({})['data']
        return jsonify(ret)
    else:
        inData = {}
        inData['errorCode'] = 3
        return render_template('error.html', inData=inData)
Example #6
0
def deal_user():
    if (0 == isAllowed(session)):
        return redirect(url_for('deal_login'))
    else:
        inData = {}
        inData['newPwError'] = 0
        inData['oldPwError'] = 0
        inData['changeInfo'] = 0
        inData['username'] = session['username']
        inData['level'] = session['level']
        newname = session['username']
        if ('GET' == request.method):
            return render_template('user.html', inData=inData)
        if ('POST' == request.method):

            if (request.form['new-passwd-a'] != request.form['new-passwd-b']):
                inData['newPwError'] = 1
                return render_template('user.html', inData=inData)

            newname = session['username']

            if (request.form['newname'] != session['username']):
                newname = request.form['newname']
            tmp = User.verifyUser({
                'username': session['username'],
                'passwd': request.form['old-passwd']
            })
            #checkRet=checkUser(session['username'], request.form['old-passwd'])
            if (tmp['status'] == 1):
                User.changePw({
                    'id': session['id'],
                    'newUsername': newname,
                    'newPasswd': request.form['new-passwd-a']
                })
                session['username'] = newname
                inData['changeInfo'] = 1
                return render_template('user.html', inData=inData)
            else:
                inData['oldPwError'] = 1
                return render_template('user.html', inData=inData)
Example #7
0
def deal_tmp_addUser():
    if (not isAllowed(session)):
        return redirect(url_for('deal_login'))
    if (session['level'] == 0):
        ret = {}
        ret['status'] = 0
        errorCode = 0
        postData = request.form.to_dict()
        if (postData['passwda'] != postData['passwdb']):
            ret['status'] = 2
            return jsonify(ret)
        ret['status'] = User.add(postData['name'], postData['passwda'],
                                 postData['level'])

        return jsonify(ret)
    else:
        inData = {}
        inData['errorCode'] = 3
        return render_template('error.html', inData=inData)
Example #8
0
def is_session_valid(session):
    '''
    Return Value:
        True: session is valied
        False: session is not valid
    '''
    # Check integrity
    if (not session) or ('user_id' not in session) or \
            ('user_name' not in session) or ('user_level' not in session):
        # remove user info from the session if it's there
        session.pop('user_name', None)
        session.pop('user_id', None)
        session.pop('user_level', None)
        return False
    # Check user existence
    if User.is_exist_with_level(user_name=session['user_name'],
                                user_id=session['user_id'],
                                user_level=session['user_level']):
        return True
    return False
Example #9
0
def deal_eqp():
    inData = None
    if (isAllowed(session)):
        if (session['level'] != -1):
            inData = deal_privilege(session)
            inData['stationInfo'] = Station.select({})['data']
            inData['addEqpSensorInfo'] = Sensor.info({})['data']
            inData['addAuthSensorInfo'] = Sensor.specialInfo({
                'in': {},
                'out': {
                    'type': 'addAuth',
                    'username': session['username'],
                    'level': session['level']
                }
            })['data']
            inData['addAuthUserInfo'] = User.select({'level': 1})['data']
            return render_template('eqp.html', inData=inData)

    else:
        return redirect(url_for('deal_login'))
Example #10
0
 def log_in(self):
     mobile = self.params.get("mobile")
     pwd = self.params.get("pwd")
     mess = ""
     try:
         if not mobile or not pwd:
             raise NullArgumentException
         with sessionCM() as session:
             user = User.find_by_mobile(session, mobile)
             if not user:
                 mess = {"status": 0, "message": "用户不存在!"}
             else:
                 reg = user.check_password(pwd)
                 if not reg:
                     mess = {"status": 0, "message": "密码错误!"}
                 else:
                     self.session["user_id"] = user.id
                     self.set_cookie("MY_WEB", "true")
                     mess = {"status": 1, "message": "登陆成功!"}
     except NullArgumentException, e:
         mess = {"status": 0, "message": e.msg}
Example #11
0
def isAllowed(session):
    ret = 1
    if (('id' in session) and ('username' in session)
            and ('level' in session)):
        verifyRst = User.verifyUser({
            'id': session['id'],
            'username': session['username'],
            'level': session['level']
        })
        if (verifyRst['status'] == 5):
            ret = 1
        else:
            ret = 0
    else:
        ret = 0
    if (ret == 0):
        if ('id' in session):
            del session['id']
        if ('username' in session):
            del session['username']
        if ('level' in session):
            del session['level']
    return ret
Example #12
0
def deal_login():
    inData = {}
    if ('GET' == request.method):
        if (('id' in session) and int(session['id']) > 0):
            return redirect(url_for('deal_index'))
        inData['showError'] = 0
        return render_template('login.html', inData=inData)
    if ('POST' == request.method):
        if (isAllowed(session) == 1):
            return redirect(url_for('deal_index'))
        else:
            verifyRst = User.verifyUser({
                'username': request.form['name'],
                'passwd': request.form['passwd']
            })
            if (verifyRst['status'] == 1):
                session['id'] = verifyRst['id']
                session['username'] = verifyRst['username']
                session['level'] = verifyRst['level']
                return redirect(url_for('deal_index'))
            else:
                inData['showError'] = 1
                return render_template('login.html', inData=inData)