class SingleArtifactContentSerializer(BaseContentSerializer): _artifact = fields.SingleContentArtifactField( help_text=_("Artifact file representing the physical content"), ) _relative_path = serializers.CharField( help_text= _("Path where the artifact is located relative to distributions base_path" ), validators=[fields.relative_path_validator], write_only=True, ) @transaction.atomic def create(self, validated_data): """ Create the content and associate it with its Artifact. Args: validated_data (dict): Data to save to the database """ artifact = validated_data.pop('_artifact') relative_path = validated_data.pop('_relative_path') content = self.Meta.model.objects.create(**validated_data) models.ContentArtifact.objects.create( artifact=artifact, content=content, relative_path=relative_path, ) return content class Meta: model = models.Content fields = BaseContentSerializer.Meta.fields + ('_artifact', '_relative_path')
class SingleArtifactContentSerializer(BaseContentSerializer): _artifact = fields.SingleContentArtifactField( help_text=_("Artifact file representing the physical content"), ) class Meta: model = models.Content fields = base.MasterModelSerializer.Meta.fields + ('_artifact', )
class SingleArtifactContentSerializer(BaseContentSerializer): artifact = fields.SingleContentArtifactField( help_text=_("Artifact file representing the physical content"), ) relative_path = serializers.CharField( help_text= _("Path where the artifact is located relative to distributions base_path" ), validators=[fields.relative_path_validator], write_only=True, ) def __init__(self, *args, **kwargs): """ Initializer for SingleArtifactContentSerializer """ super().__init__(*args, **kwargs) # If the content model has its own database field 'relative_path', # we should not mark the field write_only if hasattr(self.Meta.model, 'relative_path') and "relative_path" in self.fields: self.fields["relative_path"].write_only = False @transaction.atomic def create(self, validated_data): """ Create the content and associate it with its Artifact. Args: validated_data (dict): Data to save to the database """ artifact = validated_data.pop('artifact') if "relative_path" not in self.fields or self.fields[ "relative_path"].write_only: relative_path = validated_data.pop('relative_path') else: relative_path = validated_data.get('relative_path') content = self.Meta.model.objects.create(**validated_data) models.ContentArtifact.objects.create( artifact=artifact, content=content, relative_path=relative_path, ) return content class Meta: model = models.Content fields = BaseContentSerializer.Meta.fields + ('artifact', 'relative_path')