Esempio n. 1
0
 def post(self, request):
     serializer = ClientSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response({'error': serializer.errors},
                     status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Esempio n. 2
0
 def put(self, request, client_id):
     client = self.get_object(client_id)
     serializer = ClientSerializer(instance=client, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data,
                         status=status.HTTP_426_UPGRADE_REQUIRED)
     return Response({'error': serializer.errors},
                     status=status.HTTP_406_NOT_ACCEPTABLE)
Esempio n. 3
0
 def post(self, request, pk=None):
     profile = UserProfile.get(request)
     client = None
     if pk is not None:
         client = Client.objects.filter(pk=pk).first()
     serializer = ClientSerializer(client, data=request.data)
     if serializer.is_valid():
         serializer.save(user=profile)
         return Response(serializer.data)
     print(serializer.errors)
     return Response(status=500)
Esempio n. 4
0
    def update(self, request):
        serializer = ClientSerializer(data=request.data)

        if not serializer.is_valid():
            return Response({'The serializer is not valid': 'true'})
        
        Client.objects.select_for_update().filter(token_id=serializer.data['token_id']).update(
            email=serializer.data['email'],
            password=generate_hash(serializer.data['password']),
            name=serializer.data['name'],
            address=serializer.data['address'],
            token_id=generate_token(serializer.data['email'], serializer.data['password'])
        )

        return Response(serializer.data)
Esempio n. 5
0
def register(request: Request):
    from api.serializers import ClientSerializer

    client_id = request.data['client_id']

    if Client.objects.filter(id=client_id).exists():
        return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR,
                        data={'error': 'Client already registered'})

    client = Client(id=client_id)
    client.secret_bytes = os.urandom(16)

    profile = Contact(display_name='You')
    profile.save()

    energy = Energy(regen_rate=1, pool_size=5)
    energy.save()

    try:
        client.profile_id = profile.id
        client.energy_id = energy.id
        client.save()
        return Response(status=status.HTTP_201_CREATED,
                        data={
                            'secret': client.secret,
                            'client': ClientSerializer(client).data
                        })
    except Exception as ex:
        import traceback
        traceback.print_exc()
        return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR,
                        data={
                            'error': 'Register Error!',
                            'message': str(ex)
                        })
Esempio n. 6
0
def client_status(request: Request):
    from api.serializers import ClientSerializer

    client_id = request.user.id
    client = Client.objects.get(id=client_id)

    return Response(status=status.HTTP_200_OK,
                    data=ClientSerializer(client).data)
Esempio n. 7
0
    def get(self, request):
        user = Client.objects.filter(user_id=request.user.id).first()

        if user is not None:
            return Response(ClientSerializer(user).data, status.HTTP_200_OK)
        else:
            return Response(
                AdminSerializer(Admin.objects.filter(user_id=request.user.id).first()).data,
                status.HTTP_200_OK
            )
    def test_view_client_detail(self):
        """Test viewing a client detail"""
        client = Client.objects.create(
            name='Test User',
            nickname='TestUser1'
        )
        url = detail_url(client.uuid)
        res = self.client.get(url)

        serializer = ClientSerializer(client)
        self.assertEqual(res.data, serializer.data)
Esempio n. 9
0
    def patch(self, request):
        user = Client.objects.get(user_id=request.user.id)
        user.phone = request.data.get('phone')
        user.address = request.data.get('address')
        user.user.first_name = request.data.get('first_name')
        user.user.last_name = request.data.get('last_name')
        user.user.email = request.data.get('email')
        user.user.save()

        user.save()

        return Response(ClientSerializer(user).data, status.HTTP_200_OK)
Esempio n. 10
0
    def test_retrieve_clients_list(self):
        """Test retrieving a list of clients"""
        Client.objects.create(
            name='Test User',
            nickname='TestUser1'
        )
        Client.objects.create(
            name='Test User 2',
            nickname='TestUser2'
        )

        res = self.client.get(CLIENTS_URL)

        clients = Client.objects.all()
        serializer = ClientSerializer(clients, many=True)

        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(res.data, serializer.data)
Esempio n. 11
0
    def post(self, request):
        serializer = self.serializer(data=request.data)

        if not serializer.is_valid():
            return Response(status=status.HTTP_400_BAD_REQUEST)

        username = serializer.data.get('username')
        password = make_password(serializer.data.get('password'))

        user = self.model.objects.create(
            username=username,
            password=password,
            first_name="Ім'я не вказано",
            last_name="Прізвище не вказано",
        )

        client = Client()
        client.user = user

        client.save()

        return Response(ClientSerializer(client).data, status.HTTP_200_OK)
Esempio n. 12
0
def client(request):
    try:
        client = Client.objects.all()
    except Client.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = ClientSerializer(client)
        return JsonResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = ClientSerializer(client, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        client.delete()
        return HttpResponse(status=204)
Esempio n. 13
0
    def get(self, requesy, pk):
        user = User.objects.get(pk=pk)
        try:
            client = user.client
            serializer = ClientSerializer(client)
            data = serializer.data
            data["user_type"] = "client"
            return Response(data)

        except ObjectDoesNotExist:

            try:
                employee = user.employee
                serializer = EmployeeSerializer(employee)
                data = serializer.data
                data["user_type"] = "employee"
                return Response(data)
            except ObjectDoesNotExist:
                owner = user.owner
                serializer = OwnerSerializer(owner)
                data = serializer.data
                data["user_type"] = "owner"
                return Response(data)
Esempio n. 14
0
 def get(self, request, pk):
     gym = self.get_objects(pk)
     clients = gym.client_set.all()
     serializer = ClientSerializer(clients, many=True)
     return Response(serializer.data, status=status.HTTP_200_OK)
Esempio n. 15
0
 def get(self, request, pk):
     client = Client.objects.get(id=pk)
     return Response(ClientSerializer(client).data)
Esempio n. 16
0
 def get(self, request):
     clients = Client.objects.all()
     serializer = ClientSerializer(clients, many=True)
     return Response(serializer.data)
Esempio n. 17
0
 def post(self, request, pk):
     serializer = ClientSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_200_OK)
     return Response(serializer.errors)
Esempio n. 18
0
 def get(self, request, pk, pk2):
     client = self.get_object_client(pk, pk2)
     serializers = ClientSerializer(client)
     return Response(serializers.data, status=status.HTTP_200_OK)
Esempio n. 19
0
 def get(self, request, client_id):
     client = self.get_object(client_id)
     serializer = ClientSerializer(client)
     return Response(serializer.data)