symposion_app/pinaxcon/monkey_patch.py
Sachi King befa544bec Use outbound mail provider for admin BCC
We can use our outbound provider to BCC any required administive
retention that is required.
2017-03-31 11:54:46 +11:00

116 lines
3.4 KiB
Python

from django.conf import settings
from functools import wraps
class MonkeyPatchMiddleware(object):
''' Ensures that our monkey patching only gets called after it is safe to do so.'''
def process_request(self, request):
do_monkey_patch()
def do_monkey_patch():
patch_speaker_profile_form()
fix_sitetree_check_access_500s()
never_cache_login_page()
patch_stripe_payment_form()
# Remove this function from existence
global do_monkey_patch
do_monkey_patch = lambda: None # noqa: E731
def patch_speaker_profile_form():
''' Replaces textarea widgets with markdown editors. '''
import widgets
from symposion.speakers.forms import SpeakerForm
fields = SpeakerForm.base_fields
fields["biography"].widget = widgets.AceMarkdownEditor()
fields["experience"].widget = widgets.AceMarkdownEditor()
fields["accessibility"].widget = widgets.AceMarkdownEditor()
def fix_sitetree_check_access_500s():
''' django-sitetree has a bug: https://github.com/idlesign/django-sitetree/pull/167/files
-- it swallows the cause of all 500 errors. This swallows KeyErrors from
the failing function. '''
from sitetree.sitetreeapp import SiteTree
old_check_access = SiteTree.check_access
@wraps(SiteTree.check_access)
def check_access(self, *a, **k):
try:
return old_check_access(self, *a, **k)
except KeyError:
return False
SiteTree.check_access = check_access
def never_cache_login_page():
from django.views.decorators.cache import never_cache
from account.views import LoginView
LoginView.get = never_cache(LoginView.get)
def patch_stripe_payment_form(): # noqa: C901
import inspect # Oh no.
from django.http.request import HttpRequest
from registripe.forms import CreditCardForm
from pinaxcon.registrasion import models
old_init = CreditCardForm.__init__
@wraps(old_init)
def new_init(self, *a, **k):
# Map the names from our attendee profile model
# To the values expected in the Stripe card model
mappings = (
("address_line_1", "address_line1"),
("address_line_2", "address_line2"),
("address_suburb", "address_city"),
("address_postcode", "address_zip"),
("state", "address_state"),
("country", "address_country"),
)
initial = "initial"
if initial not in k:
k[initial] = {}
initial = k[initial]
# Find request context maybe?
frame = inspect.currentframe()
attendee_profile = None
if frame:
context = frame.f_back.f_locals
for name, value in (context.items() or {}):
if not isinstance(value, HttpRequest):
continue
user = value.user
if not user.is_authenticated():
break
try:
attendee_profile = models.AttendeeProfile.objects.get(
attendee__user=user
)
except models.AttendeeProfile.DoesNotExist:
# Profile is still none.
pass
break
if attendee_profile:
for us, stripe in mappings:
i = getattr(attendee_profile, us, None)
if i:
initial[stripe] = i
old_init(self, *a, **k)
CreditCardForm.__init__ = new_init