forked from readthedocs/readthedocs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a mixin class for dashboard views on models with project relations (
readthedocs#2353) * Add a mixin class for dashboard views on models with project relations As we overhaul the project admin dashboard, a good amount of code can be cleaned up by using a CBV rather than repeating the view code like is currently used. I had a need for this outside this code base, but implemented it here instead. Views would look like: class DomainMixin(ProjectRelationMixin): model = Domain lookup_url_kwarg = 'pk' class ListDomainView(DomainMixin, ListView): pass class DetailDomainView(DomainMixin, DetailView): pass Views would then have access to a `project` and `domains` in template context data. * Alter pattern for ProjectRelationMixin and context data
- Loading branch information
Showing
2 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""Mixin classes for project views""" | ||
|
||
from django.shortcuts import get_object_or_404 | ||
|
||
from readthedocs.projects.models import Project | ||
|
||
|
||
class ProjectRelationMixin(object): | ||
|
||
"""Mixin class for constructing model views for project dashboard | ||
This mixin class is used for model views on models that have a relation | ||
to the :py:cls:`Project` model. | ||
:cvar project_lookup_url_kwarg: URL kwarg to use in project lookup | ||
:cvar project_lookup_field: Query field for project relation | ||
:cvar project_context_object_name: Context object name for project | ||
""" | ||
|
||
project_lookup_url_kwarg = 'project_slug' | ||
project_lookup_field = 'project' | ||
project_context_object_name = 'project' | ||
|
||
def get_project_queryset(self): | ||
return Project.objects.for_admin_user(user=self.request.user) | ||
|
||
def get_project(self): | ||
if self.project_lookup_url_kwarg not in self.kwargs: | ||
return None | ||
return get_object_or_404( | ||
self.get_project_queryset(), | ||
slug=self.kwargs[self.project_lookup_url_kwarg] | ||
) | ||
|
||
def get_queryset(self): | ||
return self.model.objects.filter( | ||
**{self.project_lookup_field: self.get_project()} | ||
) | ||
|
||
def get_context_data(self, **kwargs): | ||
context = super(ProjectRelationMixin, self).get_context_data(**kwargs) | ||
context[self.project_context_object_name] = self.get_project() | ||
return context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters