Beispiel #1
0
def detail():
    _id = request.args.get('_id')
    if not _id or not ObjectId.is_valid(_id):
        return jsonify({'code': 201, 'msg': u'参数错误'})
    _obj = role.find_one({'_id': ObjectId(_id)})
    if not _obj:
        return jsonify({'code': 202, 'msg': u'不存在'})
    _obj['_id'] = _id
    return jsonify({'code': 200, 'data': _obj})
Beispiel #2
0
def add():
    data = {key: dict(request.form)[key][0] for key in dict(request.form)}
    if not data:
        data = request.get_json()
    role_obj = role.find_one({'name': data.get('name')})
    if role_obj:
        return jsonify({'code': 202, 'msg': u'此角色已经存在,请不要重复添加'})
    result = _insert(data)
    if result.get('status', False):
        return jsonify({'code': 200, 'msg': u'成功'})
    else:
        return jsonify({'code': 203, 'msg': result.get('msg', '')})
Beispiel #3
0
def update():
    _update = {}
    data = {key: dict(request.form)[key][0] for key in dict(request.form)}
    if not data:
        data = request.get_json()
    _id = data.get('_id')
    if not _id or not ObjectId.is_valid(_id):
        return jsonify({'code': 201, 'msg': u'参数错误'})
    _obj = role.find_one({'_id': ObjectId(_id)})
    if not _obj:
        return jsonify({'code': 202, 'msg': u'不存在'})
    for key in default_values:
        if key in data:
            _values = data.get(key)
            if _values:
                # if isinstance(_values, str) or isinstance(_values, unicode):
                if isinstance(_values, str):
                    _values = _values.strip()
                if key in int_key:
                    try:
                        _values = int(_values)
                    except:
                        return jsonify({'code': 201, 'msg': u'参数错误'})
                if _obj.get(key) != _values:
                    if key == 'password':
                        _values = generate_password_hash(_values)
                    _update.update({key: _values})
    if _update:
        _update.update({'updated_time': timestamp_to_strftime(time.time())})
        try:
            role.update_one({'_id': ObjectId(_id)}, {'$set': _update})
            return jsonify({'code': 200, 'msg': u'成功'})
        except:
            pass
    else:
        return jsonify({'code': 203, 'msg': u'无更新数据'})
    return jsonify({'code': 204, 'msg': u'失败'})
Beispiel #4
0
def edit_list():
    role_obj = role.find_one({'name': u'编辑'})
    role_id = str(role_obj.get('_id'))
    user_id_list = []
    user_role_cur = user_role.find({'role_id': role_id})
    for user_role_obj in user_role_cur:
        user_id_list.append(ObjectId(user_role_obj.get('user_id')))
    criteria = {'_id': {'$in': user_id_list}}
    try:
        page_num = int(request.args.get('page_num', 1)) - 1
        limit = int(request.args.get('limit', 20))
    except:
        return jsonify({'code': 201, 'msg': u'参数错误'})

    _cur = admin_user.find(criteria)
    _count = _cur.count()
    if _count % limit:
        num = _count / limit + 1
    else:
        num = _count / limit
    if _count > (page_num * limit):
        _list = []
        cur_list = _cur.sort([('_id', -1)]).skip(page_num * limit).limit(limit)
        for _obj in cur_list:
            try:
                _obj['_id'] = str(_obj['_id'])
                _obj['status_name'] = status_values.get(_obj.get('status', 0))
                role_id_list = []
                user_role_cur = user_role.find({'user_id': _obj['_id']})
                for user_role_obj in user_role_cur:
                    role_id_list.append(ObjectId(user_role_obj.get('role_id')))
                if role_id_list:
                    role_cur = role.find({'_id': {'$in': role_id_list}})
                    role_list = []
                    for role_obj in role_cur:
                        role_list.append(role_obj.get('name'))
                    if role_list:
                        _obj['role_name'] = ','.join(role_list)
                    else:
                        _obj['role_name'] = '-'
                else:
                    _obj['role_name'] = '-'
                _obj['lgoin_times'] = login_log.find({
                    'user_id': _obj['_id']
                }).count()
                try:
                    _obj['lgoin_time_last'] = login_log.find({
                        'user_id':
                        _obj['_id']
                    }).sort([('_id', -1)])[0].get('created_time')
                except:
                    _obj['lgoin_time_last'] = '-'
                _list.append(_obj)
            except Exception as e:
                print(e)
        return jsonify({
            'code': 200,
            'data': {
                'num': num,
                'count': _count,
                'page': page_num + 1,
                'list': _list
            }
        })
    else:
        return jsonify({
            'code': 200,
            'data': {
                'num': num,
                'count': _count,
                'page': page_num + 1,
                'list': []
            }
        })
Beispiel #5
0
from libs.utils import timestamp_to_strftime

if __name__ == '__main__':

    role_dict = {
        'name': u'管理员',
        'remark': u'管理员',
        'created_time': timestamp_to_strftime(time.time()),
        'updated_time': timestamp_to_strftime(time.time())
    }

    role_id = role.update_one(role_dict, {
        '$set': role_dict
    }, upsert=True).upserted_id
    if not role_id:
        role_id = role.find_one()['_id']

    permissions = [
        {
            'role_id': str(role_id),
            'authority': 'admin:create'
        },
        {
            'role_id': str(role_id),
            'authority': 'admin:delete'
        },
        {
            'role_id': str(role_id),
            'authority': 'admin:edit'
        },
        {