def test_no_source_on_child(self): with pytest.raises(AssertionError) as exc_info: IntegerRangeField(child=serializers.IntegerField(source='other')) assert str(exc_info.value) == ( "The `source` argument is not meaningful when applied to a `child=` field. " "Remove `source=` from the field declaration.")
class TestIntegerRangeField(FieldValues): """ Values for `ListField` with CharField as child. """ serializer_class = IntegerRangeSerializer valid_inputs = [ ({'lower': '1', 'upper': 2, 'bounds': '[)'}, NumericRange(**{'lower': 1, 'upper': 2, 'bounds': '[)'})), ({'lower': 1, 'upper': 2}, NumericRange(**{'lower': 1, 'upper': 2})), ({'lower': 1}, NumericRange(**{'lower': 1})), ({'upper': 1}, NumericRange(**{'upper': 1})), ({'empty': True}, NumericRange(**{'empty': True})), ({}, NumericRange()), ] invalid_inputs = [ ({'lower': 'a'}, ['A valid integer is required.']), ('not a dict', ['Expected a dictionary of items but got type "str".']), ({'foo': 'bar'}, ['Extra content not allowed "foo".']), ({'lower': 2, 'upper': 1}, ['The start of the range must not exceed the end of the range.']), ] outputs = [ (NumericRange(**{'lower': '1', 'upper': '2'}), {'lower': 1, 'upper': 2, 'bounds': '[)'}), (NumericRange(**{'empty': True}), {'empty': True}), (NumericRange(), {'bounds': '[)', 'lower': None, 'upper': None}), ({'lower': '1', 'upper': 2, 'bounds': '[)'}, {'lower': 1, 'upper': 2, 'bounds': '[)'}), ({'lower': 1, 'upper': 2}, {'lower': 1, 'upper': 2, 'bounds': None}), ({'lower': 1}, {'lower': 1, 'upper': None, 'bounds': None}), ({'upper': 1}, {'lower': None, 'upper': 1, 'bounds': None}), ({}, {}), ] field = IntegerRangeField() def test_no_source_on_child(self): with pytest.raises(AssertionError) as exc_info: IntegerRangeField(child=serializers.IntegerField(source='other')) assert str(exc_info.value) == ( "The `source` argument is not meaningful when applied to a `child=` field. " "Remove `source=` from the field declaration." )
class BatSerializer(serializers.ModelSerializer): # TODO: Get current image to display an absolute path over API id = serializers.ReadOnlyField() rarity = ChoiceField(choices=Bat.RARITY_CHOICES) habits = serializers.ListField(child=ChoiceField( choices=Bat.HABIT_CHOICES)) size = FloatRangeField() pups = IntegerRangeField() risk = serializers.ListField(child=ChoiceField(choices=Bat.RISK_CHOICES)) risk_scope = serializers.ListField(child=ChoiceField( choices=Bat.SCOPE_CHOICES)) bat_image = ImageRenditionField('fill-200x200') class Meta: model = Bat fields = ('id', 'common_name', 'scientific_name', 'rarity', 'habits', 'size', 'pups', 'risk', 'risk_scope', 'bat_image')
class IntegerRangeSerializer(serializers.Serializer): range = IntegerRangeField()
class JobSerializer(DynamicFieldsModelSerializer): created_by = serializers.PrimaryKeyRelatedField( read_only=True, default=serializers.CurrentUserDefault()) application = serializers.SerializerMethodField() location = serializers.SlugRelatedField(slug_field='name_std', queryset=City.objects.only( "id", "name", "name_std").all()) required_gender = ChoicesField(choices=choices.GENDER_CHOICES) likes = serializers.SerializerMethodField(read_only=True) required_information_to_apply = RequiredInformationSerializerFiled( read_only=True) reason_for_rejection = serializers.SerializerMethodField() group = GroupSerializer(required=False) images = ImageSerializer(many=True, read_only=True) status = ChoicesField(choices=choices.JOB_STATE_CHOICES, default=choices.PENDING_APPROVAL) job_type = ChoicesField(choices=choices.JOB_TYPE_CHOICES, default=choices.OTHER) submission_deadline = serializers.DateField(read_only=True) ages = IntegerRangeField() heights = FloatRangeField() budgets = IntegerRangeField() audition_range = DateRangeField() class Meta: model = Job fields = ('id', 'created_at', 'created_by', 'role_position', 'title', 'description', 'ages', 'required_gender', 'location', 'required_information_to_apply', 'required_tokens', 'status', 'reason_for_rejection', 'application', 'submission_deadline', 'group', 'likes', 'images', 'number_of_vacancies', 'featured', 'skin_type', 'hair_type', 'eye_color', 'body_type', 'audition_range', 'language', 'heights', 'budgets', 'job_type') def create(self, validated_data): return Job.objects.create(**validated_data) def get_required_gender(self, obj): return obj.get_required_gender_display() def get_job_type(self, obj): return obj.get_job_type_display() def get_application(self, obj): try: if 'request' in self.context: request = self.context['request'] user = request.user from application.serializers import ApplicationSerializer return ApplicationSerializer(obj.applications.get(user=user), context={ 'request': self.context['request'] }).data except: pass return None def get_reason_for_rejection(self, obj): if obj.status == choices.REMOVED: return obj.reason_for_rejection else: return None def get_likes(self, obj): try: if 'request' in self.context: user = self.context['request'].user like = Like.objects.filter( sender=user, receiver_content_type=ContentType.objects.get_for_model( Job), receiver_object_id=obj.id) if like: return True except: pass return False
class TestIntegerRangeChildAllowNullField(FieldValues): serializer_class = IntegerRangeChildAllowNullSerializer valid_inputs = [ ({ 'lower': '1', 'upper': 2, 'bounds': '[)' }, NumericRange(**{ 'lower': 1, 'upper': 2, 'bounds': '[)' })), ({ 'lower': 1, 'upper': 2 }, NumericRange(**{ 'lower': 1, 'upper': 2 })), ({ 'lower': 1 }, NumericRange(**{'lower': 1})), ({ 'upper': 1 }, NumericRange(**{'upper': 1})), ({ 'empty': True }, NumericRange(**{'empty': True})), ({}, NumericRange()), ({ 'lower': 1, 'upper': None, 'bounds': '[)' }, NumericRange(**{ 'lower': 1, 'upper': None, 'bounds': '[)' })), ({ 'lower': None, 'upper': 1, 'bounds': '[)' }, NumericRange(**{ 'lower': None, 'upper': 1, 'bounds': '[)' })), ] invalid_inputs = [ ({ 'lower': 'a' }, ['A valid integer is required.']), ('not a dict', ['Expected a dictionary of items but got type "str".']), ({ 'foo': 'bar' }, ['Extra content not allowed "foo".']), ({ 'lower': 2, 'upper': 1 }, ['The start of the range must not exceed the end of the range.']), ] outputs = [ (NumericRange(**{ 'lower': '1', 'upper': '2' }), { 'lower': 1, 'upper': 2, 'bounds': '[)' }), (NumericRange(**{'empty': True}), { 'empty': True }), (NumericRange(), { 'bounds': '[)', 'lower': None, 'upper': None }), ({ 'lower': '1', 'upper': 2, 'bounds': '[)' }, { 'lower': 1, 'upper': 2, 'bounds': '[)' }), ({ 'lower': 1, 'upper': 2 }, { 'lower': 1, 'upper': 2, 'bounds': None }), ({ 'lower': 1 }, { 'lower': 1, 'upper': None, 'bounds': None }), ({ 'upper': 1 }, { 'lower': None, 'upper': 1, 'bounds': None }), ({}, {}), ] field = IntegerRangeField(child_attrs={"allow_null": True})
class IntegerRangeChildAllowNullSerializer(serializers.Serializer): range = IntegerRangeField(child_attrs={"allow_null": True})
class Meta: model = ant_models.AntSpecies flight_hour_range = IntegerRangeField(source='flight_hour_range') fields = ('id', 'name', 'flight_months', 'flight_climate', 'flight_hour_range') read_only_fields = fields