diff --git a/readthedocs/core/views/__init__.py b/readthedocs/core/views/__init__.py index 89b57582d0e..60664549d7c 100644 --- a/readthedocs/core/views/__init__.py +++ b/readthedocs/core/views/__init__.py @@ -18,8 +18,6 @@ from readthedocs.builds.models import Build from readthedocs.builds.models import Version from readthedocs.core.utils import broadcast -from readthedocs.donate.models import SupporterPromo -from readthedocs.donate.utils import offer_promo from readthedocs.projects import constants from readthedocs.projects.models import Project, ImportedFile from readthedocs.projects.tasks import remove_dir @@ -106,38 +104,20 @@ def divide_by_zero(request): return 1 / 0 -def add_promo_data(display_type): - promo_queryset = SupporterPromo.objects.filter(live=True, display_type=display_type) - promo_obj = promo_queryset.order_by('?').first() - if promo_obj: - promo_dict = offer_promo(promo_obj=promo_obj, project=None) - else: - promo_dict = None - return promo_dict - - -def server_error(request, template_name='500.html', **kwargs): +def server_error_500(request, exception, template_name='500.html'): """A simple 500 handler so we get media""" - promo_dict = add_promo_data(display_type='error') r = render_to_response(template_name, - context_instance=RequestContext(request), - context={ - 'promo_data': promo_dict, - }) + context_instance=RequestContext(request)) r.status_code = 500 return r -def server_error_404(request, template_name='404.html', **kwargs): +def server_error_404(request, exception, template_name='404.html'): """A simple 404 handler so we get media""" - promo_dict = add_promo_data(display_type='error') response = get_redirect_response(request, path=request.get_full_path()) if response: return response r = render_to_response(template_name, - context_instance=RequestContext(request), - context={ - 'promo_data': promo_dict, - }) + context_instance=RequestContext(request)) r.status_code = 404 return r diff --git a/readthedocs/donate/views.py b/readthedocs/donate/views.py index d51bb664ef3..982aef046c5 100644 --- a/readthedocs/donate/views.py +++ b/readthedocs/donate/views.py @@ -5,14 +5,18 @@ from django.views.generic import TemplateView from django.core.urlresolvers import reverse from django.utils.translation import ugettext_lazy as _ -from django.shortcuts import redirect, get_object_or_404 +from django.shortcuts import redirect, get_object_or_404, render_to_response +from django.template import RequestContext from django.core.cache import cache from django.http import Http404 from vanilla import CreateView, ListView +from readthedocs.donate.models import SupporterPromo +from readthedocs.donate.utils import offer_promo from readthedocs.payments.mixins import StripeMixin from readthedocs.projects.models import Project +from readthedocs.redirects.utils import get_redirect_response from .models import Supporter, SupporterPromo, CLICKS, VIEWS from .forms import SupporterForm, EthicalAdForm @@ -146,3 +150,40 @@ def view_proxy(request, promo_id, hash): log.warning('Duplicate view logged. {count} total clicks tried.'.format(count=count)) cache.incr(promo.cache_key(type=VIEWS, hash=hash)) return redirect(promo.image) + + +def add_promo_data(display_type): + promo_queryset = SupporterPromo.objects.filter(live=True, display_type=display_type) + promo_obj = promo_queryset.order_by('?').first() + if promo_obj: + promo_dict = offer_promo(promo_obj=promo_obj, project=None) + else: + promo_dict = None + return promo_dict + + +def promo_500(request, template_name='500.html', **kwargs): + """A simple 500 handler so we get media""" + promo_dict = add_promo_data(display_type='error') + r = render_to_response(template_name, + context_instance=RequestContext(request), + context={ + 'promo_data': promo_dict, + }) + r.status_code = 500 + return r + + +def promo_404(request, template_name='404.html', **kwargs): + """A simple 404 handler so we get media""" + promo_dict = add_promo_data(display_type='error') + response = get_redirect_response(request, path=request.get_full_path()) + if response: + return response + r = render_to_response(template_name, + context_instance=RequestContext(request), + context={ + 'promo_data': promo_dict, + }) + r.status_code = 404 + return r diff --git a/readthedocs/urls.py b/readthedocs/urls.py index 5e58e3b2ba6..9d10439a530 100644 --- a/readthedocs/urls.py +++ b/readthedocs/urls.py @@ -25,8 +25,12 @@ admin.autodiscover() -handler500 = 'readthedocs.core.views.server_error' -handler404 = 'readthedocs.core.views.server_error_404' +if 'readthedocs.donate' in settings.INSTALLED_APPS: + handler404 = 'readthedocs.donate.views.promo_404' + handler500 = 'readthedocs.donate.views.promo_500' +else: + handler404 = 'readthedocs.core.views.server_error_404' + handler500 = 'readthedocs.core.views.server_error_500' basic_urls = [ url(r'^$', HomepageView.as_view(), name='homepage'), @@ -44,6 +48,10 @@ url(r'^notifications/', include('readthedocs.notifications.urls')), # For redirects url(r'^builds/', include('readthedocs.builds.urls')), + # For testing the 404's with DEBUG on. + url(r'^404/$', handler404), + # For testing the 500's with DEBUG on. + url(r'^500/$', handler500), ] project_urls = [