Changeset - 6fe13959a3b3
[Not reviewed]
3 2 4
Brett Smith (brett) - 5 years ago 2016-12-02 17:50:21
brett@sfconservancy.org
supporter: Support donors upgrading to supporters.

With a valid URL, a past donor can upgrade to a supporter by donating
the rest of the amount. Django validates the data and, if it passes,
puts it in the form. The JavaScript does most of the work from there.
8 files changed with 49 insertions and 5 deletions:
0 comments (0 inline, 0 general)
www/conservancy/__init__.py
Show inline comments
 
import hashlib
 

	
 
from django.conf import settings
 
from django.shortcuts import render_to_response
 
from django.template import RequestContext
 

	
 
HASH_ENCODING = 'utf-8'
 

	
 
def render_template_with_context(request, template_path, context_dict):
 
    return render_to_response(template_path, context_dict,
 
                              context_instance=RequestContext(request))
 

	
 
def param_if_valid(params, param_name, hash_param_name, default=None):
 
    try:
 
        seed = settings.CONSERVANCY_SECRET_KEY
 
        param_value = params[param_name]
 
        param_bytes = param_value.encode(HASH_ENCODING)
 
        given_hash = params[hash_param_name]
 
    except (AttributeError, KeyError, UnicodeEncodeError):
 
        return default
 
    good_hash = hashlib.sha256()
 
    good_hash.update(seed)
 
    good_hash.update(param_bytes)
 
    if given_hash == unicode(good_hash.hexdigest()):
 
        return param_value
 
    else:
 
        return default
www/conservancy/apps/supporter/__init__.py
Show inline comments
 
new file 100644
www/conservancy/apps/supporter/urls.py
Show inline comments
 
new file 100644
 
from django.conf.urls import patterns
 

	
 
INDEX_VIEW = 'conservancy.apps.supporter.views.index'
 
pattern_pairs = [(r'^/?$', INDEX_VIEW)]
 
pattern_pairs.extend(
 
    (r'^{}(?:\.html|/|)$'.format(basename), INDEX_VIEW)
 
    for basename in ['index', '2015-supporter-appeal', '2016-supporter-appeal']
 
)
 
pattern_pairs.append((r'', 'conservancy.static.views.index'))
 

	
 
urlpatterns = patterns('', *pattern_pairs)
www/conservancy/apps/supporter/views.py
Show inline comments
 
new file 100644
 
import conservancy
 

	
 
def index(request):
 
    partial_amount = conservancy.param_if_valid(request.GET, 'upgrade', 'upgrade_id', 0)
 
    context = {
 
        'partial_amount': partial_amount,
 
        'minimum_amount': 120 - partial_amount,
 
    }
 
    return conservancy.render_template_with_context(request, "supporter/index.html", context)
www/conservancy/static/js/supporter-page.js
Show inline comments
...
 
@@ -173,7 +173,11 @@ $(document).ready(function() {
 
    };
 
    $window.bind("hashchange", selectSupportTypeFromHash);
 
    var $selectorLink = selectSupportTypeFromHash();
 
    if ($selectorLink.length === 0) {
 
    if (parseInt($("form#annual").upgradeFromAmount) > 0) {
 
        supportTypeSelector("#annual").click();
 
        $(".supporter-type-selector").hide();
 
    }
 
    else if ($selectorLink.length === 0) {
 
        supportTypeSelector("#annual").click();
 
    }
 

	
www/conservancy/static/supporter/2015-supporter-appeal.html
Show inline comments
 
deleted file
www/conservancy/static/supporter/index.html
Show inline comments
 
deleted file
www/conservancy/templates/supporter/index.html
Show inline comments
 
file renamed from www/conservancy/static/supporter/2016-supporter-appeal.html to www/conservancy/templates/supporter/index.html
...
 
@@ -61,7 +61,8 @@
 
<p><strong>Note:</strong> annual supporter is not an automatic renewal
 
  relationship.  If you join today, you'll receive an email in about one year to remind you to
 
  optionally renew.</p>
 
<form id="annual" class="supporter-form" action="https://www.paypal.com/cgi-bin/webscr" method="post" name="supporter">
 
<form id="annual" class="supporter-form" action="https://www.paypal.com/cgi-bin/webscr" method="post" name="supporter"
 
      data-upgrade-from-amount="{{ partial_amount }}">
 
<div class="supporter-form-inputs">
 
            <input type="hidden" name="return" value="https://sfconservancy.org/supporter/thank-you.html" />
 
            <input type="hidden" name="cmd" value="_xclick" />
...
 
@@ -69,7 +70,7 @@
 
            <input type="hidden" name="item_name" value="Conservancy Supporter, Annual" />
 
            <input id="no_shipping" type="hidden" name="no_shipping" value="0" />
 
  <label for="amount"><strong>Amount:</strong> $</label>
 
  <input id="amount" type="number" name="amount" size="7" min="120" value="120" />
 
  <input id="amount" type="number" name="amount" size="7" min="{{ minimum_amount }}" value="{{ minimum_amount }}" />
 
  <span id="error" class="form-error-show">$120 is a minimum for Conservancy
 
  Supporters.  <a href="/donate">Donate smaller amounts here</a>.</span><br/>
 

	
0 comments (0 inline, 0 general)