Exemplo n.º 1
0
    def delete(self, request, uuid, format=None):
        """
          Modify the status to discarded the budget request with the uuid obtained by the request
          
        :param request: Data dictionary of the request 
        :param uuid: Identification of the budget request to be modified
        :param format: None
        :return: Data dictionary with a message and status 200
        """
        budget = get_object_or_404(Budget, uuid=uuid)

        if budget.status == DISCARDED:
            return Response(
                {'message': _("The budget has already been discarded")},
                status=status.HTTP_403_FORBIDDEN)
        else:
            serializer = BudgetSerializer(budget,
                                          data={'status': DISCARDED},
                                          partial=True)
            if serializer.is_valid():
                serializer.save()
                return Response(
                    {'message': _("The budget has been discarded")},
                    status=status.HTTP_200_OK)
            else:
                return Response(serializer.error_messages,
                                status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 2
0
 def post(self, request):
     """ 
         Create a new budget request, if there isn't user with that email, create a new user
         
     :param request: Data dictionary of the request with the information of a new budget request
     :return: Data dictionary with a message, status 200 and identification of the budget request created
     """
     request_data = request.data
     parameters_required = ['description', 'email', 'phone', 'address']
     self.check_parameters_required(parameters_required, request_data)
     user, data = self.check_user(request_data)
     data['uuid'] = Budget.get_new_uuid()
     data['user'] = user.pk
     data['title'] = request_data.get('title', '')
     data['description'] = request_data.get('description')
     if 'category' in data:
         category_name = data.get('category', '').lower()
         category = Categories.objects.filter(name=category_name).first()
         if not category:
             category = Categories.objects.create(name=category_name)
         data['category'] = category.pk
     serializer = BudgetSerializer(data=data)
     if serializer.is_valid():
         serializer.save()
         context = {
             'uuid': data['uuid'],
             'message': _("The budget has been created")
         }
         return Response(context, status=status.HTTP_201_CREATED)
     else:
         return Response(serializer.error_messages,
                         status.HTTP_400_BAD_REQUEST)
Exemplo n.º 3
0
    def post(self, request):
        """
            Modify the title, description or category fields the budget request with the uuid obtained by the request
            
        :param request: Data dictionary of the request with uuid the budget request
        :return: Data dictionary with a message and status 200
        """
        request_data = request.data
        parameters_required = ['uuid']
        self.check_parameters_required(parameters_required, request_data)

        if len(request_data) == 1:
            return Response(
                {
                    'message':
                    _("You need at least one parameter: title category descripcion"
                      )
                },
                status=status.HTTP_400_BAD_REQUEST)

        budget = get_object_or_404(Budget, uuid=request_data['uuid'])
        self.check_status(budget.status, {})
        serializer = BudgetSerializer(budget, data=request.data, partial=True)
        if serializer.is_valid():
            budget = serializer.save()
            return Response({'message': _("The budget has been modify")})
        return Response(serializer.error_messages,
                        status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 4
0
    def put(self, request, uuid):
        """
            Change the status to the published budget request with the uuid obtained by the request
            
        :param request:  None
        :param uuid: Identification of the budget request to be modified
        :return:  Data dictionary with a message and status 200
        """
        budget = get_object_or_404(Budget, uuid=uuid)
        is_error, error = self.check_status(budget.status, {
            'message':
            _("The '%s' is empty, you should modify your budget"),
        })
        if not budget.title:
            is_error = True
            error['message'] = error['message'] % _("title")
        elif not budget.category:
            is_error = True
            error['message'] = error['message'] % _("category")

        if is_error:
            return Response(error, status=status.HTTP_400_BAD_REQUEST)
        serializer = BudgetSerializer(budget,
                                      data={"status": PUBLISHED},
                                      partial=True)
        if serializer.is_valid():
            budget = serializer.save()
            return Response({'message': _("The budget has been published")})
        else:
            return Response(serializer.error_messages,
                            status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 5
0
    def test_unitary_update_serializer(self):
        """Correct update user by serializerr"""

        data = {
            'title': "Construción de casa precio asequible",
        }
        budget = Budget.objects.get(uuid=self.UUID)
        serializer_test = BudgetSerializer(budget, data=data, partial=True)
        if serializer_test.is_valid():
            serializer_test.save()
            self.assertEqual(serializer_test.data['title'], budget.title)
        else:
            raise AttributeError(serializer_test.error_messages)
Exemplo n.º 6
0
 def test_unitary_create_serializer(self):
     """Correct create user by serializer"""
     data = {
         'uuid':
         Budget.get_new_uuid(),
         'user':
         User.objects.get(email="*****@*****.**").pk,
         'title':
         "Nueva Mampara",
         'category':
         Categories.objects.get(name="reformas baños").pk,
         'description':
         "Una mampara de ducha de una puerta abatible de 70 no me importaria que fuera barata de expocicion."
     }
     serializer_test = BudgetSerializer(data=data)
     if serializer_test.is_valid():
         serializer_test.save()
         self.assertEqual(
             Budget.objects.get(uuid=data['uuid']).title, data['title'])
     else:
         raise AttributeError(serializer_test.error_messages)
Exemplo n.º 7
0
 def test_unitary_serializer_instance_user(self):
     """Correct instance serializer by a User"""
     budget = Budget.objects.get(uuid=self.UUID)
     serializer_test = BudgetSerializer(budget)
     data_test = serializer_test.data
     self.assertEqual(data_test['title'], budget.title)