def get_pulp_href(obj): """ Get pulp_href for a given model object. """ if obj: return reverse(get_view_name_for_model(obj.cast(), "detail"), args=[obj.pk])
def get_content_removed_hrefs(self, obj): """ Generate URLs for the content types removed in the RepositoryVersion. For each content type removed in the RepositoryVersion, create the URL of the viewset of that variety of content along with a query parameter which filters it by removal in this RepositoryVersion. Args: obj (pulpcore.app.models.RepositoryVersion): The RepositoryVersion being serialized. Returns: dict: {<_type>: <url>} """ content_urls = {} for ctype in obj.removed().values_list('_type', flat=True).distinct(): ctype_model = obj.content.filter( _type=ctype).first().cast().__class__ ctype_view = get_view_name_for_model(ctype_model, 'list') try: ctype_url = reverse(ctype_view) except django.urls.exceptions.NoReverseMatch: # We've hit a content type for which there is no viewset. # There's nothing we can do here, except to skip it. continue rv_href = "/pulp/api/v3/repositories/{repo}/versions/{version}/".format( repo=obj.repository.pk, version=obj.number) full_url = "{base}?repository_version_removed={rv_href}".format( base=ctype_url, rv_href=rv_href) content_urls[ctype] = full_url return content_urls
def content_href(self): """ Generate URLs for the content types present in the RepositoryVersion. For each content type present in the RepositoryVersion, create the URL of the viewset of that variety of content along with a query parameter which filters it by presence in this RepositoryVersion. Args: obj (pulpcore.app.models.RepositoryVersion): The RepositoryVersion being serialized. Returns: dict: {<_type>: <url>} """ ctype_model = Content.objects.filter( _type=self.content_type).first().cast().__class__ ctype_view = get_view_name_for_model(ctype_model, 'list') try: ctype_url = reverse(ctype_view) except django.urls.exceptions.NoReverseMatch: # We've hit a content type for which there is no viewset. # There's nothing we can do here, except to skip it. return rv_href = "/pulp/api/v3/repositories/{repo}/versions/{version}/".format( repo=self.repository_version.repository.pk, version=self.repository_version.number) if self.count_type == self.ADDED: partial_url_str = "{base}?repository_version_added={rv_href}" elif self.count_type == self.PRESENT: partial_url_str = "{base}?repository_version={rv_href}" elif self.count_type == self.REMOVED: partial_url_str = "{base}?repository_version_removed={rv_href}" full_url = partial_url_str.format(base=ctype_url, rv_href=rv_href) return full_url
def get_content_removed_hrefs(self, obj): """ Generate URLs for the content types removed in the RepositoryVersion. For each content type removed in the RepositoryVersion, create the URL of the viewset of that variety of content along with a query parameter which filters it by removal in this RepositoryVersion. Args: obj (pulpcore.app.models.RepositoryVersion): The RepositoryVersion being serialized. Returns: dict: {<_type>: <url>} """ content_urls = {} for ctype in obj.removed().values_list('_type', flat=True).distinct(): ctype_model = obj.content.filter(_type=ctype).first().cast().__class__ ctype_view = get_view_name_for_model(ctype_model, 'list') try: ctype_url = reverse(ctype_view) except django.urls.exceptions.NoReverseMatch: # We've hit a content type for which there is no viewset. # There's nothing we can do here, except to skip it. continue rv_href = "/pulp/api/v3/repositories/{repo}/versions/{version}/".format( repo=obj.repository.pk, version=obj.number) full_url = "{base}?repository_version_removed={rv_href}".format( base=ctype_url, rv_href=rv_href) content_urls[ctype] = full_url return content_urls
def content_href(self): """ Generate URLs for the content types added, removed, or present in the RepositoryVersion. For each content type present in or removed from this RepositoryVersion, create the URL of the viewset of that variety of content along with a query parameter which filters it by presence in this RepositoryVersion summary. Args: obj (pulpcore.app.models.RepositoryVersion): The RepositoryVersion being serialized. Returns: dict: {<pulp_type>: <url>} """ repository = self.repository_version.repository.cast() repository_content = RepositoryContent.objects.filter(repository=repository) ctype_query = Content.objects.filter( pulp_type=self.content_type, pk__in=repository_content.values_list("content", flat=True) ) ctype_model = ctype_query.first().cast().__class__ ctype_view = get_view_name_for_model(ctype_model, "list") try: ctype_url = reverse(ctype_view) except django.urls.exceptions.NoReverseMatch: # We've hit a content type for which there is no viewset. # There's nothing we can do here, except to skip it. return repository_view = get_view_name_for_model(repository.__class__, "list") repository_url = reverse(repository_view) rv_href = ( repository_url + str(repository.pk) + "/versions/{version}/".format(version=self.repository_version.number) ) if self.count_type == self.ADDED: partial_url_str = "{base}?repository_version_added={rv_href}" elif self.count_type == self.PRESENT: partial_url_str = "{base}?repository_version={rv_href}" elif self.count_type == self.REMOVED: partial_url_str = "{base}?repository_version_removed={rv_href}" full_url = partial_url_str.format(base=ctype_url, rv_href=rv_href) return full_url
def _view_name(self, obj): # this is probably memoizeable based on the model class if we want to get cachey try: obj = obj.cast() except AttributeError: # The normal message that comes up here is unhelpful, so do like other DRF # fails do and be a little more helpful in the exception message. msg = ('Expected a detail model instance, not {}. Do you need to add "many=True" to ' 'this field definition in its serializer?').format(type(obj)) raise ValueError(msg) return get_view_name_for_model(obj, 'detail')
def _view_name(self, obj): # this is probably memoizeable based on the model class if we want to get cachey try: obj = obj.cast() except AttributeError: # The normal message that comes up here is unhelpful, so do like other DRF # fails do and be a little more helpful in the exception message. msg = ('Expected a detail model instance, not {}. Do you need to add "many=True" to ' 'this field definition in its serializer?').format(type(obj)) raise ValueError(msg) return get_view_name_for_model(obj, 'detail')
def get_url(model): """ Get a resource url for the specified model object. This returns the path component of the resource URI. This is used in our resource locking/reservation code to identify resources. Args: model (django.models.Model): A model object. Returns: str: The path component of the resource url """ return reverse(get_view_name_for_model(model, "detail"), args=[model.pk])
def test_repository(self): """ Use Repository as an example that should work. """ ret = util.get_view_name_for_model(models.Repository(), 'foo') self.assertEqual(ret, 'repositories-foo')
def test_repository(self): """ Use Repository as an example that should work. """ ret = util.get_view_name_for_model(models.Repository(), 'foo') self.assertEqual(ret, 'repositories-foo')
def test_repository(self): """ Use Repository as an example that should work. """ ret = util.get_view_name_for_model(models.Artifact(), "foo") self.assertEqual(ret, "artifacts-foo")