Skip to content

Commit

Permalink
[fix bug 765783] Implement callback url support.
Browse files Browse the repository at this point in the history
- For each successful subscription in /contribute we POST to
  callbackurl GET parameter if available.

- Added /contribute/event/ page that renders the same page as
  /contribute but returns user to the same page after successfull
  subscription, so a new user can directly join.

Conflicts:
	apps/mozorg/urls.py
	apps/mozorg/views.py
  • Loading branch information
glogiotatidis committed Aug 29, 2012
1 parent 9baeda5 commit 3425f80
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 96 deletions.
9 changes: 7 additions & 2 deletions apps/mozorg/forms.py
Expand Up @@ -92,6 +92,11 @@ class ContributeForm(forms.Form):
email = forms.EmailField(widget=EmailInput(attrs={'required':'true'}))
privacy = forms.BooleanField(widget=PrivacyWidget)
newsletter = forms.BooleanField(required=False)
interest = forms.ChoiceField(choices=INTEREST_CHOICES)
comments = forms.CharField(widget=forms.widgets.Textarea(attrs={'required':'true', 'rows':'', 'cols':''}))
interest = forms.ChoiceField(
choices=INTEREST_CHOICES,
widget=forms.Select(attrs={'required':'true'}))
comments = forms.CharField(
widget=forms.widgets.Textarea(attrs={'required':'true',
'rows':'',
'cols':''}))
captcha = ReCaptchaField(attrs={'theme':'clean'})
137 changes: 71 additions & 66 deletions apps/mozorg/templates/mozorg/contribute-form.html
@@ -1,87 +1,92 @@
{% if not success %}
{% if not success or return_to_form %}
<form class="billboard{% if form.errors %} has-errors{% endif %}"
action="#help-form" id="help-form" method="post">
action="#help-form" id="help-form" method="post">
<input type="hidden" name="contribute-form" value="Y" />

{% if success and return_to_form %}
<h4 id="thank-you">
{{ _('Thank you for getting in touch! You will hear from us soon.') }}
</h4>
{% endif %}

{% if form.errors %}
{{ form.non_field_errors()|safe }}
{{ form.non_field_errors()|safe }}

<ul class="errorlist">
{% if form.email.errors %}
<li>{{_('Please enter a valid email address.')}}</li>
{% endif %}
<ul class="errorlist">
{% if form.email.errors %}
<li>{{_('Please enter a valid email address.')}}</li>
{% endif %}

{% if form.privacy.errors %}
<li>{{_('You must agree to the privacy policy.')}}</li>
{% endif %}
{% if form.privacy.errors %}
<li>{{_('You must agree to the privacy policy.')}}</li>
{% endif %}

{% if form.interest.errors %}
<li>{{_('Please select an area of interest.')}}</li>
{% endif %}
{% if form.interest.errors %}
<li>{{_('Please select an area of interest.')}}</li>
{% endif %}

{% if form.comments.errors %}
<li>{{_('Please tell us more about your interests.')}}</li>
{% endif %}
{% if form.comments.errors %}
<li>{{_('Please tell us more about your interests.')}}</li>
{% endif %}

{% if form.captcha.errors %}
<li>{{_('Your CAPTCHA response did not match. Please try again.')}}</li>
{% endif %}
</ul>
{% if form.captcha.errors %}
<li>{{_('Your CAPTCHA response did not match. Please try again.')}}</li>
{% endif %}
</ul>
{% endif %}

<fieldset>

<div class="form-row">
<div class="form-column-1">
<h3>{{_('Want to help?')}}</h3>
</div>
<div class="form-column-2">
<div class="field field-email">
{{ field_with_attrs(form.email, placeholder=_('YOUR EMAIL HERE'))|safe }}
</div>
</div>
<div class="form-column-3">
<div class="field field-interest">
{{ form.interest|safe }}
</div>
</div>
</div>

<div class="form-row" id="form-details">
<div class="form-column-1">
<p>{{_('Send us a note and we can get you started right away.')}}</p>
</div>
<div class="form-column-2">
<div class="field field-comments">
{{ field_with_attrs(form.comments, placeholder=_('Hi, I’m interested in…'))|safe }}
<div id="form-content"
{% if success and return_to_form %} style="display:none"{% endif %}>
<fieldset>
<div class="row">
<div class="form-column-1">
<h3>{{_('Want to help?')}}</h3>
</div>
<div class="form-column-2">
<div class="field field-email">
{{ field_with_attrs(form.email, placeholder=_('YOUR EMAIL HERE'))|safe }}
</div>
</div>
<div class="form-column-3">
<div class="field field-interest">
{{ form.interest|safe }}
</div>
</div>
</div>
</div>
<div class="form-column-3">
<div class="field field-privacy {% if form.privacy.errors%}field-error{% endif %}">
{{ form.privacy|safe }}
</div>
<div class="field field-newsletter">
<label for="id_newsletter">
{{ form.newsletter|safe }}
{{_('I’d like to receive regular contribution news by email')}}
</label>
</div>
<div class="field field-captcha">
{{ form.captcha }}
</div>
</div>

</fieldset>

<div class="form-row" id="form-details">
<div class="form-column-1">
<p>{{_('Send us a note and we can get you started right away.')}}</p>
</div>
<div class="form-column-2">
<div class="field field-comments">
{{ field_with_attrs(form.comments, placeholder=_('Hi, I’m interested in…'))|safe }}
</div>
</div>
<div class="form-column-3">
<div class="field field-privacy {% if form.privacy.errors%}field-error{% endif %}">
{{ form.privacy|safe }}
</div>
<div class="field field-newsletter">
<label for="id_newsletter">
{{ form.newsletter|safe }}
{{_('I’d like to receive regular contribution news by email')}}
</label>
</div>
<div class="field field-captcha">
{{ form.captcha }}
</div>
</div>
</div>
</fieldset>
<div class="form-submit">
<input type="submit" class="button-blue" id="form-submit" value="{{_('Submit&nbsp;»')}}">
</div>

</div>
</form>
{% else %}
<div id="help-form" class="billboard thank">
<div id="help-form" class="billboard thank">
<h4>{{_('Thank you for getting in touch.')}}</h4>
<p>{{_('Someone from Mozilla will get back to you soon with information about how to get involved. While you’re waiting, browse through some of the volunteer opportunities available below.')}}
</p>
</div>
</div>
{% endif %}
13 changes: 11 additions & 2 deletions apps/mozorg/urls.py
Expand Up @@ -15,7 +15,16 @@
page('mission', 'mozorg/mission.html'),
page('mobile', 'mozorg/mobile.html'),

url('^contribute/$', views.contribute, name='mozorg.contribute'),
url('^contribute/page/$', views.contribute_page, name='mozorg.contribute_page'),
url('^contribute/$', views.contribute, name='mozorg.contribute',
kwargs={'template': 'mozorg/contribute.html',
'return_to_form': False}),
url('^contribute/event/$', views.contribute,
kwargs={'template': 'mozorg/contribute.html',
'return_to_form': True},
name='mozorg.contribute_event'),
url('^contribute/page/$', views.contribute,
kwargs={'template': 'mozorg/contribute-page.html',
'return_to_form': False},
name='mozorg.contribute_page'),
redirect(r'^projects/$', 'mozorg.products'),
)
19 changes: 8 additions & 11 deletions apps/mozorg/views.py
Expand Up @@ -25,16 +25,7 @@ def handle_contribute_form(request, form):


@csrf_exempt
def contribute_page(request):
form = ContributeForm(request.POST or None)
success = handle_contribute_form(request, form)
return l10n_utils.render(request,
'mozorg/contribute-page.html',
{'form': form,
'success': success})

@csrf_exempt
def contribute(request):
def contribute(request, template, return_to_form):
def has_contribute_form():
return (request.method == 'POST' and
'contribute-form' in request.POST)
Expand All @@ -56,6 +47,10 @@ def has_newsletter_form():
if has_contribute_form():
form = ContributeForm(request.POST)
success = handle_contribute_form(request, form)
if success:
# If form was submitted successfully, return a new, empty
# one.
form = ContributeForm()
else:
form = ContributeForm()

Expand Down Expand Up @@ -86,7 +81,9 @@ def has_newsletter_form():
{'form': form,
'success': success,
'newsletter_form': newsletter_form,
'newsletter_success': newsletter_success})
'newsletter_success': newsletter_success,
'return_to_form': return_to_form})


def contribute_send(data):
ccs = {
Expand Down
69 changes: 54 additions & 15 deletions media/js/contribute-page.js
@@ -1,32 +1,71 @@
$(document).ready(function() {
function getUrlVars() {
// Function to read URL Get variables.
// from http://papermashup.com/read-url-get-variables-withjavascript/
var vars = {};
window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}

function validate_domain(url, trusted_domains) {
// Function to validate that callback url points
// to a trusted domain.
for (var domain in trusted_domains) {
var regex = new RegExp(trusted_domains[domain]);
if (regex.test(url)) {
return true;
}
}
return false;
}

var $opportunities = $('#opportunities');
function scrollTo($el) {
var top = $el.offset().top;
$("html:not(:animated),body:not(:animated)").animate(
{ scrollTop: top - 20 },
100
);
var top = $el.offset().top;
$("html:not(:animated),body:not(:animated)").animate(
{ scrollTop: top - 20 },
100
);
}

var pager = Mozilla.Pager.rootPagers[0];

$('#interest-header').click(function(e) {
e.preventDefault();
pager.setPageWithAnimation(pager.pagesById['interest']);
scrollTo($opportunities);
e.preventDefault();
pager.setPageWithAnimation(pager.pagesById['interest']);
scrollTo($opportunities);
});

$('#location-header').click(function(e) {
e.preventDefault();
pager.setPageWithAnimation(pager.pagesById['location']);
scrollTo($opportunities);
e.preventDefault();
pager.setPageWithAnimation(pager.pagesById['location']);
scrollTo($opportunities);
});

$('#time-header').click(function(e) {
e.preventDefault();
pager.setPageWithAnimation(pager.pagesById['time']);
scrollTo($opportunities);
e.preventDefault();
pager.setPageWithAnimation(pager.pagesById['time']);
scrollTo($opportunities);
});

});
$('#help-form').submit(function(e) {
// Validate that the callback points to one of the allowed domains.

callback_url = getUrlVars()['callbackurl'];

trusted_domains = ['^https://reps.mozilla.org/',
'^https://reps.allizom.org/',
'^https://reps-dev.allizom.org/',
'^http://127.0.0.1:8000/'];

if (callback_url && validate_domain(callback_url, trusted_domains)) {
$.post(callback_url);
}
});

$('#thank-you').delay(8500).fadeOut('fast', function() {
$('#form-content').fadeIn('fast');
});
});

0 comments on commit 3425f80

Please sign in to comment.