Skip to content

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

Merged
merged 2 commits into from
Aug 9, 2016

Conversation

agjohnson
Copy link
Contributor

@agjohnson agjohnson commented Aug 4, 2016

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.

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.
@agjohnson agjohnson force-pushed the project-relation-mixin branch from 028e88e to 9efcdf1 Compare August 4, 2016 21:54
context = {}
try:
context = super(ProjectRelationMixin, self).get_context_data(**kwargs)
except AttributeError:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this check really needed? get_context_data() comes from ContentMixin and it's defined in almost all class based views except View and RedirectView.

Copy link
Contributor Author

@agjohnson agjohnson Aug 8, 2016

Choose a reason for hiding this comment

The 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 ContextMixin is the most correct thing to do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, MRO here means the ContextMixin.get_context_data returns context data, but this should instead be able to fallback to vanilla view's get_context_data. I altered the call based on this feedback to instead expect get_context_data to always be implemented on a super class.

@berkerpeksag
Copy link
Member

LGTM

@agjohnson agjohnson merged commit 512654e into master Aug 9, 2016
@agjohnson agjohnson deleted the project-relation-mixin branch August 9, 2016 00:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants