-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add a mixin class for dashboard views on models with project relations #2353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""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 = {} | ||
try: | ||
context = super(ProjectRelationMixin, self).get_context_data(**kwargs) | ||
except AttributeError: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this check really needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only because this was written as a mixin class independent of any base class. But I think perhaps basing this class on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nevermind, MRO here means the |
||
pass | ||
context[self.project_context_object_name] = self.get_project() | ||
return context |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we need the
try...except
block, I'd say movecontext = {}
to