Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Rough in sending a confirmation email
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Feb 8, 2017
1 parent 9e090cb commit 11bfc72
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 15 deletions.
7 changes: 7 additions & 0 deletions gratipay/models/package/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@ def from_names(cls, package_manager, name):
"""
return cls.db.one("SELECT packages.*::packages FROM packages "
"WHERE package_manager=%s and name=%s", (package_manager, name))


# Emails
# ======

def send_confirmation_email(self, address):
pass
35 changes: 35 additions & 0 deletions js/gratipay/packages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Gratipay.packages = {};

Gratipay.packages.post = function(e) {
e.preventDefault();
var $this = $(this);
var action = 'add-email-and-claim-package';
var $inputs = $('input, button');
var $selection = $('input[name=email]:checked');
var address = $selection.val();
var package_id = $selection.parent().parent().data('package-id');

$inputs.prop('disabled', true);

$.ajax({
url: '/~' + Gratipay.username + '/emails/modify.json',
type: 'POST',
data: {action: action, address: address, package_id: package_id},
dataType: 'json',
success: function (msg) {
if (msg) {
Gratipay.notification(msg, 'success');
}
if (action == 'add-email') {
$('input.add-email').val('');
setTimeout(function(){ window.location.reload(); }, 3000);
return;
}
$inputs.prop('disabled', false);
},
error: [
function () { $inputs.prop('disabled', false); },
Gratipay.error
],
});
};
12 changes: 6 additions & 6 deletions tests/ttw/test_package_claiming.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

class Tests(BrowserHarness):

def test_sending_a_confirmation_email_works(self):
def test_sending_a_confirmation_email_appears_to_work(self):
self.make_package()
self.make_participant('alice', claimed_time='now')
self.sign_in('alice')
self.make_participant('bob', claimed_time='now')
self.sign_in('bob')
self.visit('/on/npm/foo/')
self.css('input[type=radio]').click()
self.css('button').click()
self.css('input[type=radio]')[0].click()
self.css('button')[0].click()
assert self.has_element('.notification.notification-success', 1)
assert self.has_text('A confirmation email has been sent')
assert self.has_text('Check [email protected] for a confirmation email.')
26 changes: 17 additions & 9 deletions www/on/npm/%package/index.html.spt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ url = 'https://npmjs.com/package/' + package.name
</a>
{% endblock %}

{% block scripts %}
<script>
$(document).ready(function() {
$('button.send').on('click', Gratipay.packages.post);
});
</script>
{{ super() }}
{% endblock %}

{% block content %}
<h2>{{ _( '{npm_package} has not been claimed on Gratipay.'
, npm_package=('<a href="' + url + '">' + 'npm/' + package_name + '</a>')|safe
Expand All @@ -40,14 +49,13 @@ url = 'https://npmjs.com/package/' + package.name
{% else %}
<p>{{ _('Is this yours? You can claim it on Gratipay with one of these email addresses:')
}}</p>
<form action="" method="POST">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<ul>
{% for email in package.emails %}
<li><input type="radio" name="email"> {{ email }}</li>
{% endfor %}
</ul>
<button type="submit">{{ _('Send confirmation link') }}</button>
</form>

<ul data-package-id="{{ package.id }}">
{% for i, email in enumerate(package.emails) %}
<li><input type="radio" name="email" value="{{ email }}" id="email-{{i}}">
<label for="email-{{ i }}">{{ email }}</a></li>
{% endfor %}
</ul>
<button type="submit" class="send">{{ _('Send confirmation link') }}</button>
{% endif %}
{% endblock %}
6 changes: 6 additions & 0 deletions www/~/%username/emails/modify.json.spt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import re
from aspen import Response
from gratipay.exceptions import ProblemChangingEmail
from gratipay.utils import get_participant
from gratipay.models.package import Package

email_re = re.compile(r'^[^@]+@[^@]+\.[^@]+$')

Expand Down Expand Up @@ -36,6 +37,11 @@ elif action == 'set-primary':
participant.update_email(address)
elif action == 'remove':
participant.remove_email(address)
elif action == 'add-email-and-claim-package':
package_id = request.body['package_id']
package = Package.from_id(package_id)
package.send_confirmation_email(address)
msg = _("Check {email_address} for a confirmation email.", email_address=address)
else:
raise Response(400, 'unknown action "%s"' % action)

Expand Down

0 comments on commit 11bfc72

Please sign in to comment.