Skip to content

Commit

Permalink
Gate promo 404/500's by donate app
Browse files Browse the repository at this point in the history
  • Loading branch information
ericholscher committed Mar 22, 2017
1 parent c84ad30 commit 5e0f4c0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 27 deletions.
28 changes: 4 additions & 24 deletions readthedocs/core/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'):

This comment has been minimized.

Copy link
@humitos

humitos Nov 22, 2017

Member

I will revert this new exception attribute from here since this is the only view that doesn't accept it in the new 1.9 version.

https://docs.djangoproject.com/en/1.9/ref/views/#django.views.defaults.server_error

Reverted at #3298

This comment has been minimized.

Copy link
@ericholscher

ericholscher Nov 27, 2017

Author Member

👍

"""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
43 changes: 42 additions & 1 deletion readthedocs/donate/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
12 changes: 10 additions & 2 deletions readthedocs/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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 = [
Expand Down

0 comments on commit 5e0f4c0

Please sign in to comment.