Skip to content

Instantly share code, notes, and snippets.

@codekoala
Created June 16, 2010 20:37
Show Gist options
  • Select an option

  • Save codekoala/441233 to your computer and use it in GitHub Desktop.

Select an option

Save codekoala/441233 to your computer and use it in GitHub Desktop.
# ---- some file that is processed early in Django's execution (like a urls.py or __init__.py file)
from product.models import ProductManager
def deal_o_day(self):
"""Determines which product is the current Deal of the Day"""
return self.filter(stuff_in_stock__gt=0, foo=bar)
ProductManager.deal_o_day = deal_o_day
# ---- middleware.py
from django.http import HttpResponseRedirect
from product.models import Product
class DealODayMiddleware(object):
def process_request(self, request):
"""Redirects users to the product detail page for the deal of the day when they access the site root"""
if request.path == '/':
product = Product.objects.deal_o_day()
return HttpResponseRedirect(product.get_absolute_url())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment