Skip to content
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

Use the new ContextMixin from Django 1.5 #27

Merged
merged 1 commit into from Nov 26, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 10 additions & 9 deletions extra_views/advanced.py
Expand Up @@ -3,6 +3,7 @@
from extra_views.formsets import BaseInlineFormSetMixin
from django.http import HttpResponseRedirect
from django.forms.formsets import all_valid
from .compat import ContextMixin


class InlineFormSet(BaseInlineFormSetMixin):
Expand Down Expand Up @@ -93,15 +94,15 @@ class UpdateWithInlinesView(SingleObjectTemplateResponseMixin, BaseUpdateWithInl
template_name_suffix = '_form'


class NamedFormsetsMixin(object):
class NamedFormsetsMixin(ContextMixin):
inlines_names = []

def get_context_data(self, **kwargs):
ctx = super(NamedFormsetsMixin, self).get_context_data(**kwargs)
if not self.inlines_names:
return ctx
# We have formset or inlines in context, but never both
ctx.update(zip(self.inlines_names, kwargs.get('inlines',[])))
if 'formset' in kwargs:
ctx[self.inlines_names[0]] = kwargs['formset']
return ctx
context = {}
if self.inlines_names:
# We have formset or inlines in context, but never both
context.update(zip(self.inlines_names, kwargs.get('inlines', [])))
if 'formset' in kwargs:
context[self.inlines_names[0]] = kwargs['formset']
context.update(kwargs)
return super(NamedFormsetsMixin, self).get_context_data(**context)
15 changes: 15 additions & 0 deletions extra_views/compat.py
@@ -0,0 +1,15 @@
# ContextMixin was introduced in Django 1.5, we provide a copy for earlier
# versions.
try:
from django.views.generic.base import ContextMixin
except ImportError:
class ContextMixin(object):
"""
A default context mixin that passes the keyword arguments received by
get_context_data as the template context.
"""

def get_context_data(self, **kwargs):
if 'view' not in kwargs:
kwargs['view'] = self
return kwargs
10 changes: 6 additions & 4 deletions extra_views/contrib/mixins.py
Expand Up @@ -3,6 +3,7 @@
from django.db.models import Q
import datetime
import operator
from ..compat import ContextMixin

VALID_STRING_LOOKUPS = ('iexact', 'contains', 'icontains', 'startswith', 'istartswith', 'endswith', 'iendswith',
'search', 'regex', 'iregex')
Expand Down Expand Up @@ -123,7 +124,7 @@ def get_sort(self):
return sort


class SortableListMixin(object):
class SortableListMixin(ContextMixin):
"""
You can provide either sort_fields as a plain list like ['id', 'some', 'foo__bar', ...]
or, if you want to hide original field names you can provide list of tuples with aliace that will be used:
Expand Down Expand Up @@ -158,7 +159,8 @@ def get_queryset(self):
return self._sort_queryset(qs)

def get_context_data(self, **kwargs):
ctx = super(SortableListMixin, self).get_context_data(**kwargs)
context = {}
if hasattr(self, 'sort_helper'):
ctx['sort_helper'] = self.sort_helper
return ctx
context['sort_helper'] = self.sort_helper
context.update(kwargs)
return super(SortableListMixin, self).get_context_data(**context)
20 changes: 12 additions & 8 deletions extra_views/formsets.py
Expand Up @@ -5,6 +5,7 @@
from django.views.generic.detail import SingleObjectMixin, SingleObjectTemplateResponseMixin
from django.views.generic.list import MultipleObjectMixin, MultipleObjectTemplateResponseMixin
from django.forms.models import BaseInlineFormSet
from .compat import ContextMixin


class BaseFormSetMixin(object):
Expand Down Expand Up @@ -59,10 +60,7 @@ def get_factory_kwargs(self):
return kwargs


class FormSetMixin(BaseFormSetMixin):
def get_context_data(self, **kwargs):
return kwargs

class FormSetMixin(BaseFormSetMixin, ContextMixin):
def get_success_url(self):
if self.success_url:
url = self.success_url
Expand All @@ -84,14 +82,19 @@ class ModelFormSetMixin(FormSetMixin, MultipleObjectMixin):
formfield_callback = None

def get_context_data(self, **kwargs):
context = kwargs
context = {}

if self.object_list:
context['object_list'] = self.object_list
context_object_name = self.get_context_object_name(self.get_queryset())
if context_object_name:
context[context_object_name] = self.object_list
return context
context.update(kwargs)

# MultipleObjectMixin get_context_data() doesn't work when object_list
# is not provided in kwargs, so we skip MultipleObjectMixin and call
# ContextMixin directly.
return ContextMixin.get_context_data(self, **context)

def construct_formset(self):
return self.get_formset()(queryset=self.get_queryset(), **self.get_formset_kwargs())
Expand Down Expand Up @@ -129,14 +132,15 @@ class BaseInlineFormSetMixin(BaseFormSetMixin):
save_as_new = False

def get_context_data(self, **kwargs):
context = kwargs
context = {}

if self.object:
context['object'] = self.object
context_object_name = self.get_context_object_name(self.object)
if context_object_name:
context[context_object_name] = self.object
return context
context.update(kwargs)
return super(BaseInlineFormSetMixin, self).get_context_data(**context)

def construct_formset(self):
return self.get_formset()(instance=self.object, **self.get_formset_kwargs())
Expand Down
6 changes: 2 additions & 4 deletions extra_views/multi.py
Expand Up @@ -5,6 +5,7 @@
from django.forms.models import modelformset_factory
from django.forms import models as model_forms
from django.forms import ValidationError
from .compat import ContextMixin


class FormProvider(object):
Expand Down Expand Up @@ -40,7 +41,7 @@ def get_form(self, caller, prefix):
return form


class MultiFormMixin(object):
class MultiFormMixin(ContextMixin):
"""
Handle multiple forms in a single view
"""
Expand Down Expand Up @@ -101,9 +102,6 @@ def get_form_kwargs(self, prefix):
})
return kwargs

def get_context_data(self, **kwargs):
return kwargs

def get_success_url(self):
if self.success_url:
url = self.success_url
Expand Down