|
1 | | -from gettext import gettext as _ |
| 1 | +import logging |
2 | 2 |
|
| 3 | +from gettext import gettext as _ |
3 | 4 | from rest_framework import serializers |
4 | 5 |
|
5 | 6 | from pulpcore.plugin import models as core_models |
6 | 7 | from pulpcore.plugin import serializers as core_serializers |
7 | 8 |
|
8 | 9 | from . import models |
9 | 10 |
|
| 11 | +log = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class IndexRootSerializer(serializers.Serializer): |
| 15 | + """ |
| 16 | + A Serializer for summary information of an index. |
| 17 | + """ |
| 18 | + |
| 19 | + dl = serializers.CharField(help_text=_("URL of the index root"), read_only=True) |
| 20 | + api = serializers.CharField(help_text=_("URL of the API root"), read_only=True) |
| 21 | + auth_required = serializers.BooleanField( |
| 22 | + help_text=_( |
| 23 | + "Indicates whether this is a private registry that requires all operations to " |
| 24 | + "be authenticated" |
| 25 | + ), |
| 26 | + read_only=True, |
| 27 | + ) |
| 28 | + |
10 | 29 |
|
11 | 30 | class RustDependencySerializer(serializers.ModelSerializer): |
12 | 31 | """ |
@@ -181,28 +200,14 @@ class Meta: |
181 | 200 | class RustRemoteSerializer(core_serializers.RemoteSerializer): |
182 | 201 | """ |
183 | 202 | A Serializer for RustRemote. |
184 | | -
|
185 | | - Add any new fields if defined on RustRemote. |
186 | | - Similar to the example above, in RustContentSerializer. |
187 | | - Additional validators can be added to the parent validators list |
188 | | -
|
189 | | - For example:: |
190 | | -
|
191 | | - class Meta: |
192 | | - validators = core_serializers.RemoteSerializer.Meta.validators |
193 | | - + [myValidator1, myValidator2] |
194 | | -
|
195 | | - By default the 'policy' field in core_serializers.RemoteSerializer only validates the choice |
196 | | - 'immediate'. To add on-demand support for more 'policy' options, e.g. 'streamed' or |
197 | | - 'on_demand', re-define the 'policy' option as follows:: |
| 203 | + """ |
198 | 204 |
|
199 | 205 | policy = serializers.ChoiceField( |
200 | 206 | help_text="The policy to use when downloading content. The possible values include: " |
201 | | - "'immediate', 'on_demand', and 'streamed'. 'immediate' is the default.", |
| 207 | + "'immediate', 'on_demand', and 'streamed'. 'streamed' is the default.", |
202 | 208 | choices=models.Remote.POLICY_CHOICES, |
203 | | - default=models.Remote.IMMEDIATE |
| 209 | + default=models.Remote.STREAMED, |
204 | 210 | ) |
205 | | - """ |
206 | 211 |
|
207 | 212 | class Meta: |
208 | 213 | fields = core_serializers.RemoteSerializer.Meta.fields |
@@ -258,3 +263,31 @@ class Meta: |
258 | 263 | class Meta: |
259 | 264 | fields = core_serializers.DistributionSerializer.Meta.fields + ("allow_uploads", "remote") |
260 | 265 | model = models.RustDistribution |
| 266 | + |
| 267 | + |
| 268 | +class RepositoryAddCachedContentSerializer( |
| 269 | + core_serializers.ValidateFieldsMixin, serializers.Serializer |
| 270 | +): |
| 271 | + remote = core_serializers.DetailRelatedField( |
| 272 | + required=False, |
| 273 | + view_name_pattern=r"remotes(-.*/.*)-detail", |
| 274 | + queryset=models.Remote.objects.all(), |
| 275 | + help_text=_( |
| 276 | + "A remote to use to identify content that was cached. This will override a " |
| 277 | + "remote set on repository." |
| 278 | + ), |
| 279 | + ) |
| 280 | + |
| 281 | + def validate(self, data): |
| 282 | + data = super().validate(data) |
| 283 | + repository = None |
| 284 | + if "repository_pk" in self.context: |
| 285 | + repository = models.Repository.objects.get(pk=self.context["repository_pk"]) |
| 286 | + remote = data.get("remote", None) or getattr(repository, "remote", None) |
| 287 | + |
| 288 | + if not remote: |
| 289 | + raise serializers.ValidationError( |
| 290 | + {"remote": _("This field is required since a remote is not set on the repository.")} |
| 291 | + ) |
| 292 | + self.check_cross_domains({"repository": repository, "remote": remote}) |
| 293 | + return data |
0 commit comments