Skip to content

Instantly share code, notes, and snippets.

@spenoir
Created October 28, 2015 12:19
Show Gist options
  • Save spenoir/5f1d9140a7044bf72835 to your computer and use it in GitHub Desktop.
Save spenoir/5f1d9140a7044bf72835 to your computer and use it in GitHub Desktop.
extract from views.py showing a base view example
class BasePageContextMixin(object):
" adds specific page rendering items to context amongst other things "
def get_context_data(self, **kwargs):
context = super(BasePageContextMixin, self).get_context_data(**kwargs)
extra_context = {
'slug': self.kwargs.get('slug'),
# 'basket': self.basket
}
#extra_context.update(self.basket.cart_totals(self.request))
context.update(extra_context)
return context
class BasePageView(BasePageContextMixin, UpdateView):
" Base view for basic pages "
context_object_name = 'page'
template_name = 'pages/default.html'
def get_template_names(self):
names = super(BasePageView, self).get_template_names()
tpl = getattr(self.object, 'base_template')
if tpl:
names.reverse()
names.append(tpl)
names.reverse()
return names
def get_object(self, queryset=None):
# fix for homepage slug ie. no slug
if not self.kwargs.get('slug') and self.model == WebPage:
# return the home page
try:
return self.model.objects.get(slug='', live=True)
except self.model.DoesNotExist:
page, created = self.model.objects.get_or_create(
slug='', title='Home', template='pages/home.html')
return page
return get_object_or_404(self.model, slug=self.kwargs.get('slug'), live=True)
def get(self, request, **kwargs):
response = super(BasePageView, self).get(request, **kwargs)
return response
def post(self, request, *args, **kwargs):
response = super(BasePageView, self).post(request, **kwargs)
return response
def get_context_data(self, **kwargs):
context = super(BasePageView, self).get_context_data(**kwargs)
# get the next or previous and update context
context.update(self.get_next_and_prev())
return context
def get_next_and_prev(self, *args, **kwargs):
# this method is intended to get the next and previous WebPage
# by weight
try:
next_pages = self.model.objects.filter(weight__gt=self.object.weight) \
.order_by('weight')
prev_pages = self.model.objects.filter(weight__lt=self.object.weight) \
.order_by('-weight')
return {
'next': next_pages[0] if next_pages.count() else False,
'prev': prev_pages[0] if prev_pages.count() else False
}
except:
logging.debug('No next and previous pages found for: %s' % self)
return {
'next': False,
'prev': False
}
@classonlymethod
def as_view(cls, **initkwargs):
""" Specialised so we can cache view. """
view = super(BasePageView, cls).as_view(**initkwargs)
cached_view = cache_page(
view, settings.PAGE_CACHE_TIME,
key_prefix=settings.CACHE_KEY_PREFIX)
logger.debug('called BasePageview.as_view() DEBUG: %s and CACHE_PAGES: %s' % (settings.DEBUG, settings.CACHE_PAGES))
if not settings.DEBUG and settings.CACHE_PAGES:
return cached_view
else:
return view
class WebPageView(BasePageView):
model = WebPage
webpage_view = WebPageView.as_view()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment