示例#1
0
def django_ldap_login(request):
    print(1)

    print(request.data)
    print(type(request.data))
    serializer = LoginSerializer(data=request.data)
    # LoginSerializer登录序列化器在这里的作用,做post表单数据的验证,减少代码量
    """
    When a serializer is passed a `data` keyword argument you must call `.is_valid()` before attempting to access the serialized `.data` representation.
You should either call `.is_valid()` first, or access `.initial_data` instead.
    """

    # 对值进入验证判断
    if not serializer.is_valid():
        return MyJsonResponse(code=4001, message="invalid data")

    data = serializer.data

    # 做用户名和密码的认证,认证成功返回一个用户名,认证失败则返回 None
    user = authenticate(request,
                        username=data['username'],
                        password=data['password'])

    if user is None:
        return MyJsonResponse(code=4001,
                              message="error username or password...")

    # 用户进行登录
    login(request, user)

    # print(get_user_model())
    # <class 'django.contrib.auth.models.User'>
    return MyJsonResponse(message="login success", data=data['username'])
    '''
示例#2
0
def django_login(request):

    username = '******'
    password = '******'
    user = authenticate(request, username=username, password=password)
    if user is None:
        return MyJsonResponse(message='error username or password', code=401)
    login(request, user)
    return MyJsonResponse(message='login success')
示例#3
0
def django_is_login(request):

    # 返回登录的用户名,如果没有登录,返回 AnonymousUser(如果没有登录的话,那么输出AnonymousUser)
    print(request.user)

    # request.user.is_authenticated 验证是否通过登录认证,认证返回 True,没有认证返回 False
    # return HttpResponse(request.user.is_authenticated)

    if request.user.is_authenticated:
        return MyJsonResponse(message='login success')

    return MyJsonResponse(message='login failed')
示例#4
0
    def kill_processlist_by_id(self, request, pk=None, *args, **kwargs):
        """
        http://127.0.0.1:8080/api/meta_manage/mysql_schema/1/get_schema_processlist/
        pk=1

        """
        if pk is None:
            raise Http404

        # 通过序列化器来在服务端做参数的数据验证
        serializer = KillMySQLProcessSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        # if not serializer.is_valid():
        #     print(11111)
        #     return MyJsonResponse(message="invalid data", code=4001)

        # 获取process_id参数
        process_id = serializer.validated_data.get('process_id')
        print("process_id: {}".format(process_id))
        db = self.get_connection(pk)
        c = db.cursor()
        c.execute("kill %d" % process_id)
        c.close()
        db.close()
        return MyJsonResponse(message="success")
示例#5
0
 def create(self, request):
     serializer = HostSerializer(data=request.data)
     serializer.is_valid(True)
     instance = serializer.save()
     # 用在生成具体ID的时候,才会用到 reverse
     print(reverse('host-list'))  # 对应 /meta_manage/host_view/
     url = reverse('host-detail', args=(instance.id,))
     return MyJsonResponse(data={'url': url})
示例#6
0
 def put(self, request, *args, **kwargs):
     user = request.user
     serializer = UserDetailSerializer(instance=user,
                                       data=request.data,
                                       partial=True)
     serializer.is_valid(True)
     serializer.save()
     return MyJsonResponse(data=UserDetailSerializer(user).data)
示例#7
0
def celery_debug(request):

    res = tasks.add.delay(1, 3)
    #任务逻辑
    # return JsonResponse({'status':'successful','task_id':res.task_id})

    # print(res.task_id)
    print(res)

    return MyJsonResponse(message="success")
示例#8
0
 def get(self, request, host_id=None, *args, **kwargs):
     if host_id is None:
         hosts = Host.objects.all()
         serializers = HostSerializer(hosts.values(), many=True)
         
     else:
         h = Host.objects.get(id=host_id)
         # serializers = HostSerializer(h.values())
         #  "message": "'Host' object has no attribute 'values'",
         serializers = HostSerializer(h)
     return MyJsonResponse(data=serializers.data)
示例#9
0
def get_schema_by_ip(request):
    ip = request.GET.get("ip")

    if not isinstance(ip, str) or len(ip) == 0:
        raise ValidationError("invalid parameter: ip")

    data = SchemaModel.objects.filter(ip=ip).all().values()
    serializer = SchemaSerializer(data, many=True)

    if len(serializer.data) != 1:
        return MyJsonResponse(code=404, data="")

    # print(serializer.data[0]['schema'])
    schema_data = serializer.data[0]

    dict_data = {
        "schema": schema_data['schema'],
        "db_type": schema_data['db_type'],
        "schema_ip": schema_data['ip'],
        "schema_port": schema_data['port'],
    }

    return MyJsonResponse(data=dict_data)
示例#10
0
    def put(self, request, host_id, *args, **kwargs):
        host = Host.objects.get(pk=host_id)
        # print(1)
        serializer = HostSerializer(host, data=request.data, partial=True)
        serializer.is_valid(True)

        """
        for attr, value in serializer.validated_data.items():
            # validated_data.items().pop('name')
            setattr(host, attr, value)
        host.save()
        """
        serializer.save()

        return MyJsonResponse(message="success")
示例#11
0
    def post(self, request, *args, **kwargs):
        serializer = HostSerializer(data=request.data)
        serializer.is_valid()

        """
        <class 'collections.OrderedDict'>
        OrderedDict([('name', 'hostname-03'), ('memory', '64'), ('cpu', '16')])
        """
        """
        data = serializer.validated_data
        print(type(data))
        print(data)
        host = Host(**data)  # Host(name="xxx", memory=64, cpu=16)
        host.save()
        """
        serializer.save()
        return MyJsonResponse(message="success")


        """
示例#12
0
 def get(self, request, *args, **kwargs):
     # 获取当前登录的用户,返回用户名
     user = request.user
     print(user)
     serializer = UserDetailSerializer(user)
     return MyJsonResponse(data=serializer.data)
示例#13
0
def current_user(request):

    if request.user.is_authenticated:
        return MyJsonResponse(message='login success', data='apple')

    return MyJsonResponse(message='login failed', code=4001)
示例#14
0
 def destroy(self, request, pk=None):
     h = Host.objects.get(pk=pk)
     h.delete()
     return MyJsonResponse("success")
示例#15
0
 def partial_update(self, request, pk=None):
     host = Host.objects.get(pk=pk)
     serializer = HostSerializer(host, data=request.data, partial=True)
     serializer.is_valid(True)
     serializer.save()
     return MyJsonResponse("success")
示例#16
0
 def retrieve(self, request, pk=None):
     h = Host.objects.get(pk=pk)
     serializer = HostSerializer(h)
     return MyJsonResponse(serializer.data)
示例#17
0
def logout_user(request):

    logout(request)

    return MyJsonResponse(message='退出成功')
示例#18
0
 def list(self, request):
     hosts = Host.objects.all().values()
     serializer = HostSerializer(hosts, many=True)
     return MyJsonResponse(data=serializer.data)