示例#1
0
 def create(self, data):
     # hash_password = hashlib.md5(data['password'].encode()).hexdigest()
     # changed_data['password'] = hash_password
     # changed_data = data
     # del changed_data['groups']
     # del changed_data['user_permissions']
     u = Users(
         first_name=data['first_name'],
         last_name=data['last_name'],
         username=data['username'],
         password=data['password'],
         email=data['email'],
     )
     u.set_password(data['password'])
     u.save()
     return u
示例#2
0
文件: views.py 项目: sjkcdpc/devops-4
def user_manage(request, aid=None, action=None):
    if request.user.has_perms(['asset.view_user', 'asset.edit_user']):
        page_name = ''
        if aid:
            user = get_object_or_404(Users, pk=aid)
            if action == 'edit':
                page_name = '编辑用户'
            elif action == 'delete':
                if user == request.user:
                    Message.objects.create(type=u'用户管理',
                                           user=request.user,
                                           action=u'删除用户',
                                           action_ip=UserIP(request),
                                           content=u'不能删除当前登录用户')
                else:
                    user.delete()
                    Message.objects.create(
                        type=u'用户管理',
                        user=request.user,
                        action=u'删除用户',
                        action_ip=UserIP(request),
                        content=u'删除用户 %s%s,用户名 %s' %
                        (user.last_name, user.first_name, user.username))
                return redirect('user_list')
            elif action == 'view':
                user_detail = Users.objects.filter(id=aid)
                return render(request, 'user/user_profile.html',
                              {'user_detail': user_detail})

        else:
            user = Users()
            action = 'add'
            page_name = '新增用户'

        if request.method == 'POST':
            form = UserForm(request.POST, instance=user)
            if form.is_valid():
                password1 = request.POST.get('password1')
                password2 = request.POST.get('password2')
                perm_select = request.POST.getlist('perm_sel')
                perm_delete = request.POST.getlist('perm_del')
                if action == 'add' or action == 'edit':
                    form.save()
                    if password1 and password1 == password2:
                        user.set_password(password1)
                    user.save()
                    # 授予用户权限
                    user.user_permissions.add(*perm_select)
                    user.user_permissions.remove(*perm_delete)
                    Message.objects.create(type=u'用户管理',
                                           user=request.user,
                                           action=page_name,
                                           action_ip=UserIP(request),
                                           content=u'%s %s%s,用户名 %s' %
                                           (page_name, user.last_name,
                                            user.first_name, user.username))
                    return redirect('user_list')
        else:
            form = UserForm(instance=user)
        data = {
            'form': form,
            'page_name': page_name,
            'action': action,
            'aid': aid
        }

        return render(request, 'user/user_manage.html', data)
    else:
        raise Http404