Skip to content

Commit

Permalink
Inherit order matters
Browse files Browse the repository at this point in the history
NestedViewSetMixin has to be on the left of ProjectQuerySetMixin to
filter nested results properly.
  • Loading branch information
humitos committed May 8, 2019
1 parent 8ebfb1c commit e4ac327
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
32 changes: 32 additions & 0 deletions readthedocs/api/v3/tests/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,35 @@ def test_projects_versions_builds_list_post(self):
response_json,
self._get_response_dict('projects-versions-builds-list_POST'),
)

def test_projects_versions_detail_unique(self):
second_project = fixture.get(
Project,
name='second project',
slug='second-project',
related_projects=[],
main_language_project=None,
users=[self.me],
versions=[],
)
second_version = fixture.get(
Version,
slug=self.version.slug,
verbose_name=self.version.verbose_name,
identifier='a1b2c3',
project=second_project,
active=True,
built=True,
type='tag',
)
self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.token.key}')
response = self.client.get(
reverse(
'projects-versions-detail',
kwargs={
'parent_lookup_project__slug': self.project.slug,
'version_slug': self.version.slug,
}),

)
self.assertEqual(response.status_code, 200)
17 changes: 10 additions & 7 deletions readthedocs/api/v3/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class APIv3Settings:
metadata_class = SimpleMetadata


class ProjectsViewSet(APIv3Settings, ProjectQuerySetMixin, NestedViewSetMixin,
class ProjectsViewSet(APIv3Settings, NestedViewSetMixin, ProjectQuerySetMixin,
FlexFieldsMixin, ReadOnlyModelViewSet):

# Markdown docstring is automatically rendered by BrowsableAPIRenderer.
Expand Down Expand Up @@ -173,8 +173,8 @@ def superproject(self, request, project_slug):
return Response(status=404)


class SubprojectRelationshipViewSet(APIv3Settings, ProjectQuerySetMixin,
NestedViewSetMixin, FlexFieldsMixin,
class SubprojectRelationshipViewSet(APIv3Settings, NestedViewSetMixin,
ProjectQuerySetMixin, FlexFieldsMixin,
ListModelMixin, GenericViewSet):

# Markdown docstring exposed at BrowsableAPIRenderer.
Expand All @@ -195,8 +195,8 @@ class SubprojectRelationshipViewSet(APIv3Settings, ProjectQuerySetMixin,
queryset = Project.objects.all()


class TranslationRelationshipViewSet(APIv3Settings, ProjectQuerySetMixin,
NestedViewSetMixin, FlexFieldsMixin,
class TranslationRelationshipViewSet(APIv3Settings, NestedViewSetMixin,
ProjectQuerySetMixin, FlexFieldsMixin,
ListModelMixin, GenericViewSet):

# Markdown docstring exposed at BrowsableAPIRenderer.
Expand All @@ -216,7 +216,10 @@ class TranslationRelationshipViewSet(APIv3Settings, ProjectQuerySetMixin,
queryset = Project.objects.all()


class VersionsViewSet(APIv3Settings, ProjectQuerySetMixin, NestedViewSetMixin,
# Inherit order is important here. ``NestedViewSetMixin`` has to be on the left
# of ``ProjectQuerySetMixin`` to make calling ``super().get_queryset()`` work
# properly and filter nested dependencies
class VersionsViewSet(APIv3Settings, NestedViewSetMixin, ProjectQuerySetMixin,
FlexFieldsMixin, UpdateModelMixin, ReadOnlyModelViewSet):

model = Version
Expand Down Expand Up @@ -259,7 +262,7 @@ def update(self, request, *args, **kwargs):
return Response(status=204)


class BuildsViewSet(APIv3Settings, ProjectQuerySetMixin, NestedViewSetMixin,
class BuildsViewSet(APIv3Settings, NestedViewSetMixin, ProjectQuerySetMixin,
FlexFieldsMixin, ReadOnlyModelViewSet):
model = Build
lookup_field = 'pk'
Expand Down

0 comments on commit e4ac327

Please sign in to comment.