Beispiel #1
0
 def put(self, request, pk):
     subscriber = get_object_or_404(erp_models.Subscriber, pk=pk)
     serializer = erp_serializers.SubscriberSerializer(subscriber,
                                                       data=request.data,
                                                       partial=True)
     if serializer.is_valid(raise_exception=True):
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_201_CREATED)
    def test_valid_update_subscribers(self):
        sub = self.sub

        sub_update = {
            "address_number_and_street": "2, rue de la Paix"
        }
        
        serializer = erp_serializers.SubscriberSerializer(instance=sub, data=sub_update, partial=True)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        self.assertEqual(sub.address_number_and_street, "2, rue de la Paix")
    def test_invalid_update_subscribers(self):
        """Read_only fields don't raise an error when trying to update them.
        """
        sub = erp_factories.SubscriberFactory(
            user__username='******'
        )

        sub_update = {
            "user": {"username": "******"}
        }

        serializer = erp_serializers.SubscriberSerializer(instance=sub, data=sub_update, partial=True)
        serializer.is_valid(raise_exception=True)
        new_sub = serializer.save()

        self.assertEqual(new_sub.user.username, '*****@*****.**')
Beispiel #4
0
    def get(self, request, pk):
        sub = get_object_or_404(erp_models.Subscriber, pk=pk)

        # don't forget many=True with querysets
        sub_current_rentals = erp_serializers.SubscriberRentalSerializer(
            sub.current_rentals, many=True).data
        sub_current_bookings = erp_serializers.SubscriberBookingSerializer(
            sub.current_bookings, many=True).data
        sub = erp_serializers.SubscriberSerializer(sub).data

        # this replaces better another complex serializer for just this view
        sub.update({
            'current_rentals': sub_current_rentals,
            'current_bookings': sub_current_bookings,
        })

        return Response(sub)
 def test_deserialize_invalid_subscriber_not_matching_fieldtype_requirements(self):
     """Subscriber with misformed email address (DRF's job)
     """
     sub = {
         "address_number_and_street": "1, rue de la Paix",
         "address_zipcode": "75001",
         "iban": "FR7630056009271234567890182",
         "user": {
             "email": "john.doe",
             "first_name": "John",
             "last_name": "Doe",
             "password": "******"
         }
     }
     sub_ser = erp_serializers.SubscriberSerializer(data=sub)
     with self.assertRaises(ValidationError):
         sub_ser.is_valid(raise_exception=True)
     self.assertEqual(set(sub_ser.errors['user']), set(['email']))
 def test_deserialize_invalid_sub_missing_required_field_in_related_model(self):
     """Job done with a custom validate() method in the Subscriber serializer"""
     sub = {
         "address_number_and_street": "1, rue de la Paix",
         "address_zipcode": "75001",
         "iban": "FR7630056009271234567890182",
         "user": {
             "email": "*****@*****.**",
             "last_name": "Doe",
             "password": "******"
         }
     }
     serializer = erp_serializers.SubscriberSerializer(data=sub)
     with self.assertRaises(ValidationError) as e:
         serializer.is_valid(raise_exception=True)
     self.assertEqual(
         serializer.errors['non_field_errors'],
         [ErrorDetail(string='The user first_name is missing', code='invalid')]
     )
 def test_deserialize_invalid_subscriber_missing_required_field(self):
     """Subscriber with address_zipcode missing
     """
     sub = {
         "address_number_and_street": "1, rue de la Paix",
         "iban": "FR7630056009271234567890182",
         "user": {
             "email": "*****@*****.**",
             "first_name": "John",
             "last_name": "Doe",
             "password": "******"
         }
     }
     serializer = erp_serializers.SubscriberSerializer(data=sub)
     with self.assertRaises(ValidationError):
         serializer.is_valid(raise_exception=True)
     # can't do errors.keys() because drf relies on heavy odict (ordered dict)
     # sets are required to get a flat, standard python data type
     self.assertEqual(set(serializer.errors), set(['address_zipcode']))
    def test_deserialize_valid_subscriber(self):
        sub = {
            "address_number_and_street": "1, rue de la Paix",
            "address_zipcode": "75001",
            "iban": "FR7630056009271234567890182",
            "user": {
                "email": "*****@*****.**",
                "first_name": "John",
                "last_name": "Doe",
                "password": "******"
            }
        }

        serializer = erp_serializers.SubscriberSerializer(data=sub)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        self.assertTrue(bool(erp_models.Subscriber.objects.get(user__username='******')))
        sub = erp_models.Subscriber.objects.get(user__username='******')

        self.assertEqual(sub.subscription_date, date.today())
        self.assertFalse(sub.has_issue)
        self.assertFalse(sub.has_received_warning)
        self.assertFalse(bool(sub.current_rentals))
Beispiel #9
0
 def post(self, request):
     serializer = erp_serializers.SubscriberSerializer(data=request.data)
     if serializer.is_valid(raise_exception=True):
         serializer.save()
         return Response(serializer.data, status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Beispiel #10
0
 def get(self, request):
     subscribers = erp_models.Subscriber.objects.all()
     page = self.paginate_queryset(subscribers, request, view=self)
     if page is not None:
         serializer = erp_serializers.SubscriberSerializer(page, many=True)
         return self.get_paginated_response(serializer.data)
    def test_serialize_subscriber_list(self):
        subs = erp_factories.SubscriberFactory.create_batch(5)

        serializer = erp_serializers.SubscriberSerializer(subs, many=True)
        self.assertEqual(len(serializer.data), 5)
    def test_serialize_subscriber(self):
        sub = self.sub

        serializer = erp_serializers.SubscriberSerializer(sub)
        self.assertTrue('address_number_and_street' in set(serializer.data))
        self.assertTrue('first_name' in serializer.data['user'].keys())