Пример #1
0
def user_add(request):
    '''  api/user/add POST
    添加用户。
    para: name(str), department(str), role([...])
    return: code =
        200: success
        201: parameter error
        400: Validation Error when saving user
    '''

    name, department_id, roles = parse_args(request.body,
                                            'name',
                                            'department',
                                            'role',
                                            department='')
    try:
        department = Department.objects.get(id=department_id)
    except Department.DoesNotExist:
        department = Department.root()

    user = User(username=name, department=department)
    user.set_password('123456')
    user.full_clean()
    user.save()
    user.set_roles(roles)
    return gen_response(code=200, message=f'添加用户 {name}')
Пример #2
0
def user_edit(request):
    '''  api/user/edit POST
    编辑用户。
    para: name(str), password(str), department_id(str), role([...])
    return: code =
        200: success
        201: parameter error
        202:no such user
    '''
    name, pwd, department_id, roles = parse_args(request.body,
                                                 'name',
                                                 'password',
                                                 'department',
                                                 'role',
                                                 department='')

    if name == 'admin':
        return gen_response(code=203, message="admin 的信息不能被修改")

    user = User.objects.get(username=name)
    if pwd != '':
        user.set_password(pwd)
    try:
        department = Department.objects.get(id=department_id)
    except Department.DoesNotExist:
        department = Department.root()
    user.department = department
    user.save()
    user.set_roles(roles)

    return gen_response(code=200, message=f'{user.username} 信息修改')
Пример #3
0
def add_admin():
    ''' 增加用户名密码均为 admin 的超级用户 '''
    from department.models import Department
    from .models import User
    if not User.objects.filter(username='******').exists():
        admin = User(username='******',
                     department=Department.root(),
                     is_superuser=True)
        admin.set_password('admin')
        admin.save()