Skip to content

Commit

Permalink
Merge pull request #764 from liberapay/currencies-1
Browse files Browse the repository at this point in the history
Currencies - part 1
  • Loading branch information
Changaco authored Oct 29, 2017
2 parents e29f783 + 5e2df3f commit bf3b989
Show file tree
Hide file tree
Showing 30 changed files with 643 additions and 44 deletions.
22 changes: 11 additions & 11 deletions emails/income.spt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{ _("You have received {0} this week", Money(total, 'EUR')) }}
{{ _("You have received {0} this week", LegacyMoney(total)) }}

[---] text/html
<p>{{ _("{0} were given to you personally.", Money(personal, 'EUR')) }}</p>
<p>{{ _("{0} were given to you personally.", LegacyMoney(personal)) }}</p>

% if not by_team
% set teams = participant.get_teams()
Expand All @@ -14,22 +14,22 @@
% endif
% elif len(by_team) == 1
% set team, amount = list(by_team.items())[0]
<p>{{ _("{0} were given to you for your role in the {1} team.", Money(amount, 'EUR'), team) }}</p>
<p>{{ _("{0} were given to you for your role in the {1} team.", LegacyMoney(amount), team) }}</p>
% else
<p>{{ _("{0} were given to you for your roles in the following teams:", Money(sum(by_team.values()), 'EUR')) }}</p>
<p>{{ _("{0} were given to you for your roles in the following teams:", LegacyMoney(sum(by_team.values()))) }}</p>
<ul>
% for team, amount in by_team.items()
<li>{{ _("{0}: {1}", team, Money(amount, 'EUR')) }}</li>
<li>{{ _("{0}: {1}", team, LegacyMoney(amount)) }}</li>
% endfor
</ul>
% endif

<p>{{ _("You now have {0} in your wallet.", Money(new_balance, 'EUR')) }}</p>
<p>{{ _("You now have {0} in your wallet.", LegacyMoney(new_balance)) }}</p>

<p><a href="{{ participant.url('wallet/') }}">{{ _("See your account's transaction logs") }}</a></p>

[---] text/plain
{{ _("{0} were given to you personally.", Money(personal, 'EUR')) }}
{{ _("{0} were given to you personally.", LegacyMoney(personal)) }}

% if not by_team
% set teams = participant.get_teams()
Expand All @@ -42,14 +42,14 @@
% endif
% elif len(by_team) == 1
% set team, amount = list(by_team.items())[0]
{{- _("{0} were given to you for your role in the {1} team.", Money(amount, 'EUR'), team) }}
{{- _("{0} were given to you for your role in the {1} team.", LegacyMoney(amount), team) }}
% else
{{- _("{0} were given to you for your roles in the following teams:", Money(sum(by_team.values()), 'EUR')) }}
{{- _("{0} were given to you for your roles in the following teams:", LegacyMoney(sum(by_team.values()))) }}
% for team, amount in by_team.items()
{{- _("- {0}: {1}", team, Money(amount, 'EUR')) }}
{{- _("- {0}: {1}", team, LegacyMoney(amount)) }}
% endfor
% endif

{{ _("You now have {0} in your wallet.", Money(new_balance, 'EUR')) }}
{{ _("You now have {0} in your wallet.", LegacyMoney(new_balance)) }}

{{ _("See your account's transaction logs") }}: {{ participant.url('wallet/') }}
2 changes: 1 addition & 1 deletion emails/invoice_accepted.spt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[---] text/html
<p>{{ _(
"Your request for a payment of {amount} from {addressee_name} has been accepted."
, addressee_name=addressee_name, amount=Money(invoice.amount, 'EUR')
, addressee_name=addressee_name, amount=LegacyMoney(invoice.amount)
) }}</p>

<p>{{ _(
Expand Down
2 changes: 1 addition & 1 deletion emails/invoice_paid.spt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<p>{{ _(
"Your request for a payment of {amount} from {addressee_name} has been accepted, "
"and the money has been transferred to your wallet."
, addressee_name=addressee_name, amount=Money(invoice.amount, 'EUR')
, addressee_name=addressee_name, amount=LegacyMoney(invoice.amount)
) }}</p>

<p>
Expand Down
2 changes: 1 addition & 1 deletion emails/invoice_rejected.spt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[---] text/html
<p>{{ _(
"Your request for a payment of {amount} from {addressee_name} has been rejected."
, addressee_name=addressee_name, amount=Money(invoice.amount, 'EUR')
, addressee_name=addressee_name, amount=LegacyMoney(invoice.amount)
) }}</p>

<p>{{ _("Reason: “{0}”", rejection_message) }}</p>
Expand Down
4 changes: 2 additions & 2 deletions emails/low_balance.spt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
[---] text/html
<p>{{ _(
"You have {0} left in your Liberapay wallet, but you need at least {1} to cover your donations next week.",
Money(low_balance, 'EUR'), Money(participant.get_giving_for_profile()[1], 'EUR')
LegacyMoney(low_balance), LegacyMoney(participant.get_giving_for_profile()[1])
) }}</p>

<p><a href="{{ participant.url('wallet/payin/') }}" style="{{ button_style('primary') }}">{{
_("Add money") }}</a></p>

[---] text/plain
{{ _("You have {0} left in your Liberapay wallet, but you need at least {1} to cover your donations next week.",
Money(low_balance, 'EUR'), Money(participant.get_giving_for_profile()[1], 'EUR')) }}
LegacyMoney(low_balance), LegacyMoney(participant.get_giving_for_profile()[1])) }}

{{ _("Add money") }}: {{ participant.url('wallet/payin/') }}
2 changes: 1 addition & 1 deletion emails/new_invoice.spt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[---] text/html
<p>{{ _(
"{sender_name} has submitted an invoice for a payment of {amount}.",
sender_name=sender_name, amount=Money(invoice.amount, 'EUR')
sender_name=sender_name, amount=LegacyMoney(invoice.amount)
) }}</p>

<p>{{ _("Description: {0}", invoice.description) }}</p>
Expand Down
2 changes: 1 addition & 1 deletion emails/payin_bankwire_created.spt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"We are ready to receive the funds. Please send exactly {0} to the "
"following account using the reference code below so we'll know the "
"money came from you:",
Money(exchange.amount + exchange.fee, 'EUR')
LegacyMoney(exchange.amount + exchange.fee)
) }}</p>

<p>
Expand Down
2 changes: 1 addition & 1 deletion emails/payin_bankwire_expired.spt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[---] text/html
<p>{{ _(
"On {date} you declared that you were going to transfer {money_amount} from your bank account to your Liberapay wallet, but the money never arrived.",
date=exchange.timestamp.date(), money_amount=Money(exchange.amount + exchange.fee, 'EUR')
date=exchange.timestamp.date(), money_amount=LegacyMoney(exchange.amount + exchange.fee)
) }}</p>

<p>{{ _("If you forgot to do it you can initiate a new payment:") }}</p>
Expand Down
2 changes: 1 addition & 1 deletion emails/payin_bankwire_failed.spt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[---] text/html
<p>{{ _(
"The attempt to transfer {0} from your bank account has failed with the following error message:",
Money(exchange.amount + exchange.fee, 'EUR')
LegacyMoney(exchange.amount + exchange.fee)
) }}</p>

<pre>{{ exchange.note }}</pre>
Expand Down
6 changes: 3 additions & 3 deletions emails/payin_bankwire_succeeded.spt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<p>{{ _(
"We're happy to report that the transfer from your bank account has succeeded. "
"Of the {0} that were sent, {1} went into your Liberapay wallet, and {2} were paid in fees."
, Money(exchange.amount + exchange.fee, 'EUR')
, Money(exchange.amount, 'EUR')
, Money(exchange.fee, 'EUR')
, LegacyMoney(exchange.amount + exchange.fee)
, LegacyMoney(exchange.amount)
, LegacyMoney(exchange.fee)
) }}</p>

<p><a href="{{ participant.url('receipts/%s' % exchange.id) }}">{{ _("View receipt") }}</a></p>
2 changes: 1 addition & 1 deletion emails/payin_directdebit_failed.spt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[---] text/html
<p>{{ _(
"The attempt to transfer {0} from your bank account has failed with the following error message:",
Money(exchange.amount + exchange.fee, 'EUR')
LegacyMoney(exchange.amount + exchange.fee)
) }}</p>

<pre>{{ exchange.note }}</pre>
Expand Down
6 changes: 3 additions & 3 deletions emails/payin_directdebit_succeeded.spt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<p>{{ _(
"We're happy to report that the transfer from your bank account has succeeded. "
"Of the {0} that were sent, {1} went into your Liberapay wallet, and {2} were paid in fees."
, Money(exchange.amount + exchange.fee, 'EUR')
, Money(exchange.amount, 'EUR')
, Money(exchange.fee, 'EUR')
, LegacyMoney(exchange.amount + exchange.fee)
, LegacyMoney(exchange.amount)
, LegacyMoney(exchange.fee)
) }}</p>

<p><a href="{{ participant.url('receipts/%s' % exchange.id) }}">{{ _("View receipt") }}</a></p>
4 changes: 2 additions & 2 deletions emails/pledgee_joined.spt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
[---] text/html
<p>{{ _(
"Your pledge to give {0} every week to {1} will be turned into action now that they have joined Liberapay. Huzzah!",
Money(amount, 'EUR'),
LegacyMoney(amount),
('<b><a href="{0}">{1}</a></b>'|safe).format(profile_url, user_name)
) }}</p>
[---] text/plain
{{ _("Your pledge to give {0} every week to {1} will be turned into action now that they have joined Liberapay. Huzzah!",
Money(amount, 'EUR'),
LegacyMoney(amount),
user_name) }}

{{ _("Follow this link to view {0}'s profile:", user_name) }}
Expand Down
2 changes: 1 addition & 1 deletion emails/withdrawal_created.spt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
<p>{{ _(
"We have initiated a transfer of {0} from your Liberapay wallet to your bank account. "
"Please note that it can take several business days for the transaction to complete.",
Money(-exchange.amount + exchange.fee, 'EUR')
LegacyMoney(-exchange.amount + exchange.fee)
) }}</p>
4 changes: 2 additions & 2 deletions emails/withdrawal_failed.spt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[---] text/html
<p>{{ _(
"We tried to transfer {0} from your Liberapay wallet to your bank account, but the transfer failed with this message:",
Money(-exchange.amount + exchange.fee, 'EUR')
LegacyMoney(-exchange.amount + exchange.fee)
) }}</p>

<pre>{{ exchange.note }}</pre>
Expand All @@ -13,7 +13,7 @@

[---] text/plain
{{ _("We tried to transfer {0} from your Liberapay wallet to your bank account, but the transfer failed with this message:",
Money(-exchange.amount + exchange.fee, 'EUR')) }}
LegacyMoney(-exchange.amount + exchange.fee)) }}

{{ exchange.note }}

Expand Down
4 changes: 0 additions & 4 deletions liberapay/billing/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
from liberapay.utils import group_by, NS


Money.__eq__ = lambda a, b: isinstance(b, Money) and a.__dict__ == b.__dict__
Money.__repr__ = lambda m: '<Money Amount=%(amount)r Currency=%(currency)r>' % m.__dict__


QUARANTINE = '%s days' % QUARANTINE.days


Expand Down
5 changes: 5 additions & 0 deletions liberapay/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import re

from jinja2 import StrictUndefined
from mangopay.utils import Money
from pando.utils import utc


Expand Down Expand Up @@ -47,6 +48,8 @@ def check_bits(bits):

BIRTHDAY = date(2015, 5, 22)

CURRENCIES = set(('EUR', 'USD'))

D_CENT = Decimal('0.01')
D_INF = Decimal('inf')
D_UNIT = Decimal('1.00')
Expand Down Expand Up @@ -236,4 +239,6 @@ def make_standard_tip(label, weekly):

USERNAME_MAX_SIZE = 32

ZERO = {c: Money(D_ZERO, c) for c in ('EUR', 'USD', None)}

del _
18 changes: 17 additions & 1 deletion liberapay/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
logger = logging.getLogger('liberapay.cron')


Daily = namedtuple('Daily', 'hour')
Weekly = namedtuple('Weekly', 'weekday hour')


Expand All @@ -20,7 +21,7 @@ def __init__(self, website):
self.exclusive_jobs = []

def __call__(self, period, func, exclusive=False):
if isinstance(period, int) and period <= 0:
if not self.website.env.run_cron_jobs:
return
if exclusive and not self.has_lock:
self.exclusive_jobs.append((period, func))
Expand All @@ -45,6 +46,21 @@ def f():
except Exception as e:
self.website.tell_sentry(e, {})
sleep(86400 * 6)
elif isinstance(period, Daily):
while True:
now = datetime.utcnow()
then = now.replace(hour=period.hour, minute=0, second=0)
seconds = (then - now).total_seconds()
if seconds > 0:
# later today
sleep(seconds)
elif seconds < -3600:
# tomorrow
sleep(3600 * 24 + seconds)
try:
func()
except Exception as e:
self.website.tell_sentry(e, {})
else:
while True:
try:
Expand Down
11 changes: 9 additions & 2 deletions liberapay/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@

import aspen
import aspen.http.mapping
from mangopay.utils import Money
import pando
from pando import json
from pando.algorithms.website import fill_response_with_output
from pando.utils import maybe_encode

from liberapay import utils, wireup
from liberapay.billing.payday import Payday, create_payday_issue
from liberapay.cron import Cron, Weekly
from liberapay.cron import Cron, Daily, Weekly
from liberapay.models.community import Community
from liberapay.models.participant import Participant
from liberapay.models.repository import refetch_repos
from liberapay.security import authentication, csrf, set_default_security_headers
from liberapay.utils import b64decode_s, b64encode_s, erase_cookie, http_caching, i18n, set_cookie
from liberapay.utils.currencies import MoneyBasket, fetch_currency_exchange_rates
from liberapay.utils.state_chain import (
attach_environ_to_request, create_response_object, canonize, insert_constants,
_dispatch_path_to_filesystem, merge_exception_into_response, return_500_for_exception,
Expand All @@ -40,6 +42,9 @@
# Configure renderers
# ===================

json.register_encoder(Money, lambda m: {'amount': str(m.amount), 'currency': m.currency})
json.register_encoder(MoneyBasket, lambda b: list(b))

website.renderer_default = 'unspecified' # require explicit renderer, to avoid escaping bugs

website.renderer_factories['csv_dump'] = csv_dump.Factory(website)
Expand Down Expand Up @@ -91,7 +96,7 @@ def _assert(x):
# =============

conf = website.app_conf
if env.run_cron_jobs and conf:
if conf:
cron = Cron(website)
cron(conf.check_db_every, website.db.self_check, True)
cron(conf.dequeue_emails_every, Participant.dequeue_emails, True)
Expand All @@ -100,6 +105,7 @@ def _assert(x):
cron(Weekly(weekday=3, hour=2), create_payday_issue, True)
cron(conf.clean_up_counters_every, website.db.clean_up_counters, True)
cron(conf.update_cached_amounts_every, Payday.update_cached_amounts, True)
cron(Daily(hour=16), lambda: fetch_currency_exchange_rates(website.db), True)


# Website Algorithm
Expand All @@ -122,6 +128,7 @@ def _assert(x):
csrf.extract_token_from_cookie,
csrf.reject_forgeries,
authentication.authenticate_user_if_possible,
i18n.add_currency_to_state,

_dispatch_path_to_filesystem,
algorithm['handle_dispatch_exception'],
Expand Down
1 change: 1 addition & 0 deletions liberapay/models/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ def remove_email(self, address):
def send_email(self, spt_name, email, **context):
self.fill_notification_context(context)
context['email'] = email
context['LegacyMoney'] = i18n.LegacyMoney
langs = i18n.parse_accept_lang(self.email_lang or 'en')
locale = i18n.match_lang(langs)
i18n.add_helpers_to_context(context, locale)
Expand Down
Loading

0 comments on commit bf3b989

Please sign in to comment.