class SemesterStatusDetailSerializer(SemesterStatusSerializer): contract = FileField(required=False, allow_null=True) statistics = FileField(required=False, allow_null=True) evaluation = FileField(required=False, allow_null=True) contract_name = CharField(source='contract_id', read_only=True) statistics_name = CharField(source='statistics_id', read_only=True) evaluation_name = CharField(source='evaluation_id', read_only=True) class Meta: model = SemesterStatus fields = ('id', 'semester', 'contacted_status', 'contract', 'statistics', 'evaluation', 'contract_name', 'statistics_name', 'evaluation_name')
class GalleryPictureSerializer(serializers.ModelSerializer): file = ImageField(required=True, options={'height': 700, 'smart': True}) thumbnail = ImageField(source='file', read_only=True, options={ 'height': 200, 'width': 300, 'smart': True }) raw_file = FileField(source='file', read_only=True) comments = CommentSerializer(read_only=True, many=True) comment_target = CharField(read_only=True) taggees = PublicUserField(many=True, queryset=User.objects.all(), required=False) class Meta: model = GalleryPicture fields = ('id', 'gallery', 'description', 'taggees', 'active', 'file', 'thumbnail', 'raw_file', 'comments', 'comment_target') read_only_fields = ('raw_file', 'thumbnail', 'gallery') def validate(self, attrs): gallery = Gallery.objects.get( pk=self.context['view'].kwargs['gallery_pk']) return {'gallery': gallery, **attrs}
class GalleryPictureSerializer(serializers.ModelSerializer): file = ImageField(required=True, options={"height": 700, "smart": True}) thumbnail = ImageField( source="file", read_only=True, options={"height": 200, "width": 300, "smart": True}, ) raw_file = FileField(source="file", read_only=True) comments = CommentSerializer(read_only=True, many=True) content_target = CharField(read_only=True) taggees = PublicUserField(many=True, queryset=User.objects.all(), required=False) class Meta: model = GalleryPicture fields = ( "id", "gallery", "description", "taggees", "active", "file", "thumbnail", "raw_file", "comments", "content_target", ) read_only_fields = ("raw_file", "thumbnail", "gallery") def validate(self, attrs): gallery = Gallery.objects.get(pk=self.context["view"].kwargs["gallery_pk"]) return {"gallery": gallery, **attrs}
class CompanyFileSerializer(serializers.ModelSerializer): file = FileField() class Meta: model = CompanyFile fields = ("id", "file") def create(self, validated_data): company = Company.objects.get(pk=self.context["view"].kwargs["company_pk"]) validated_data["company"] = company return super().create(validated_data)
class CompanyFileSerializer(serializers.ModelSerializer): file = FileField() class Meta: model = CompanyFile fields = ('id', 'file') def create(self, validated_data): company = Company.objects.get( pk=self.context['view'].kwargs['company_pk']) validated_data['company'] = company return super().create(validated_data)
class SemesterStatusDetailSerializer(SemesterStatusSerializer): contract = FileField(required=False, allow_null=True) statistics = FileField(required=False, allow_null=True) evaluation = FileField(required=False, allow_null=True) contract_name = CharField(source="contract_id", read_only=True) statistics_name = CharField(source="statistics_id", read_only=True) evaluation_name = CharField(source="evaluation_id", read_only=True) class Meta: model = SemesterStatus fields = ( "id", "semester", "contacted_status", "contract", "statistics", "evaluation", "contract_name", "statistics_name", "evaluation_name", )
class FileFieldTestCase(BaseTestCase): fixtures = ["test_users.yaml", "test_files.yaml"] def setUp(self): self.field = FileField() self.file = File.objects.get(key="abakus.png") def test_options(self): field = FileField(allowed_types=["image"]) self.assertEqual(field.allowed_types, ["image"]) @mock.patch("lego.apps.files.fields.storage.generate_signed_url") def test_to_representation(self, mock_generate_signed_url): """To representation should return a presigned get""" file_mock = mock.Mock() self.assertEqual( self.field.to_representation(file_mock), mock_generate_signed_url.return_value, ) mock_generate_signed_url.assert_called_once_with( File.bucket, file_mock.pk) def test_to_internal_value(self): """Make sure to_internal_value only accepts a key with valid token""" self.assertRaises(ValidationError, self.field.to_internal_value, "file.png") self.assertRaises(ValidationError, self.field.to_internal_value, "file.png:") self.assertRaises(ValidationError, self.field.to_internal_value, "file.png:token:") self.assertEqual(self.field.to_internal_value("abakus.png:token"), self.file) self.assertRaises(ValidationError, self.field.to_internal_value, "abakus.png:_token")
def test_options(self): field = FileField(allowed_types=["image"]) self.assertEqual(field.allowed_types, ["image"])
def setUp(self): self.field = FileField() self.file = File.objects.get(key="abakus.png")