Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade from Aspen 0.42 to Pando 0.43 #407

Merged
merged 4 commits into from
Sep 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions error.spt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import absolute_import, division, print_function, unicode_literals

from aspen.http import status_strings
from pando.http import status_strings

from liberapay.exceptions import LazyResponse
from liberapay.utils.i18n import HTTP_ERRORS
Expand Down Expand Up @@ -48,4 +48,4 @@ if code == 500 and not err:
, "error_message_long": err
}
[----------------------------------------] text/plain
{{err}}
{{err or msg}}
14 changes: 1 addition & 13 deletions liberapay/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from __future__ import print_function, unicode_literals

from mimetypes import guess_type

from six.moves.urllib.parse import urlsplit, urlunsplit

from aspen.http.request import Line
from pando.http.request import Line

from . import constants

Expand Down Expand Up @@ -62,13 +60,3 @@ def canonize(request, website):

def insert_constants():
return {'constants': constants}


def fill_accept_header(state, request, accept_header):
"""Work around aspen's content negotiation weirdness

This sets `accept_header` to `application/json` when the requested URL ends
in `.json` and the `Accept` header is missing.
"""
if not accept_header:
state['accept_header'] = guess_type(request.path.raw, strict=False)[0]
4 changes: 2 additions & 2 deletions liberapay/billing/exchanges.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

from decimal import Decimal, ROUND_UP

from aspen import Response
from aspen.utils import typecheck
from mangopaysdk.entities.payin import PayIn
from mangopaysdk.entities.payout import PayOut
from mangopaysdk.entities.transfer import Transfer
from mangopaysdk.entities.wallet import Wallet
from mangopaysdk.types.exceptions.responseexception import ResponseException
from mangopaysdk.types.money import Money
from pando import Response
from pando.utils import typecheck

from liberapay.billing import (
mangoapi,
Expand Down
17 changes: 9 additions & 8 deletions liberapay/billing/payday.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import os.path
import pickle

import aspen.utils
from babel.dates import format_timedelta
import pando.utils
from psycopg2 import IntegrityError

from liberapay import constants
Expand Down Expand Up @@ -58,7 +59,7 @@ def start(cls):
""", back_as=dict)
log("Picking up payday #%s." % d['id'])

d['ts_start'] = d['ts_start'].replace(tzinfo=aspen.utils.utc)
d['ts_start'] = d['ts_start'].replace(tzinfo=pando.utils.utc)

log("Payday started at %s." % d['ts_start'])

Expand All @@ -75,7 +76,7 @@ def run(self, log_dir='.', keep_log=False):
"""
self.db.self_check()

_start = aspen.utils.utcnow()
_start = pando.utils.utcnow()
log("Greetings, program! It's PAYDAY!!!!")

self.shuffle(log_dir)
Expand All @@ -89,10 +90,10 @@ def run(self, log_dir='.', keep_log=False):
if not keep_log:
os.unlink(self.transfers_filename)

_end = aspen.utils.utcnow()
_end = pando.utils.utcnow()
_delta = _end - _start
fmt_past = "Script ran for %%(age)s (%s)." % _delta
log(aspen.utils.to_age(_start, fmt_past=fmt_past))
msg = "Script ran for %s ({0})."
log(msg.format(_delta) % format_timedelta(_delta, locale='en'))

if keep_log:
output_log_path = log_dir+'/payday-%i.txt' % self.id
Expand Down Expand Up @@ -465,7 +466,7 @@ def update_stats(self):
log("Updated payday stats.")

def update_cached_amounts(self):
now = aspen.utils.utcnow()
now = pando.utils.utcnow()
with self.db.get_cursor() as cursor:
self.prepare(cursor, now)
self.transfer_virtually(cursor)
Expand Down Expand Up @@ -558,7 +559,7 @@ def end(self):
SET ts_end=now()
WHERE ts_end='1970-01-01T00:00:00+00'::timestamptz
RETURNING ts_end AT TIME ZONE 'UTC'
""", default=NoPayday).replace(tzinfo=aspen.utils.utc)
""", default=NoPayday).replace(tzinfo=pando.utils.utc)

def notify_participants(self):
previous_ts_end = self.db.one("""
Expand Down
2 changes: 1 addition & 1 deletion liberapay/constants.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# coding: utf8
from __future__ import print_function, unicode_literals

from aspen.utils import utc
from collections import namedtuple, OrderedDict
from datetime import date, datetime, timedelta
from decimal import Decimal
import re

from jinja2 import StrictUndefined
from pando.utils import utc


class CustomUndefined(StrictUndefined):
Expand Down
9 changes: 6 additions & 3 deletions liberapay/elsewhere/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
from urllib import quote
import xml.etree.ElementTree as ET

from aspen import Response
from aspen.utils import to_age, utc
from babel.dates import format_timedelta
from pando import Response
from pando.utils import utc
from oauthlib.oauth2 import TokenExpiredError
from requests_oauthlib import OAuth1Session, OAuth2Session

Expand Down Expand Up @@ -161,10 +162,12 @@ def log_ratelimit_headers(self, limit, remaining, reset):
return
percent_remaining = remaining/limit
if percent_remaining < 0.5:
reset_delta = reset - datetime.now().replace(tz=utc)
reset_delta = format_timedelta(reset_delta, locale='en')
log_msg = (
'{0} API: {1:.1%} of ratelimit has been consumed, '
'{2} requests remaining, resets {3}.'
).format(self.name, 1 - percent_remaining, remaining, to_age(reset))
).format(self.name, 1 - percent_remaining, remaining, reset_delta)
log_lvl = logging.WARNING
if percent_remaining < 0.2:
log_lvl = logging.ERROR
Expand Down
2 changes: 1 addition & 1 deletion liberapay/elsewhere/bitbucket.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import absolute_import, division, print_function, unicode_literals

from aspen import Response
from pando import Response
from liberapay.elsewhere._base import PlatformOAuth1
from liberapay.elsewhere._extractors import any_key, key, not_available
from liberapay.elsewhere._paginators import keys_paginator
Expand Down
2 changes: 1 addition & 1 deletion liberapay/elsewhere/bountysource.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import requests

from aspen import Response
from pando import Response

from liberapay.elsewhere._base import Platform
from liberapay.elsewhere._extractors import key, not_available
Expand Down
2 changes: 1 addition & 1 deletion liberapay/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import print_function, unicode_literals

from aspen import Response
from dependency_injection import resolve_dependencies
from pando import Response

from .constants import MAX_TIP, MIN_TIP, PASSWORD_MIN_SIZE, PASSWORD_MAX_SIZE

Expand Down
51 changes: 28 additions & 23 deletions liberapay/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

import aspen
import aspen.http.mapping
import pando
from pando.algorithms.website import fill_response_with_output

from liberapay import canonize, fill_accept_header, insert_constants, utils, wireup
from liberapay import canonize, insert_constants, utils, wireup
from liberapay.cron import Cron
from liberapay.models.community import Community
from liberapay.models.participant import Participant
Expand Down Expand Up @@ -74,7 +76,7 @@ def _assert(x):
# =================

def return_500_for_exception(website, exception):
response = aspen.Response(500)
response = pando.Response(500)
if website.show_tracebacks:
import traceback
response.body = traceback.format_exc()
Expand All @@ -90,6 +92,7 @@ def return_500_for_exception(website, exception):
algorithm = website.algorithm
algorithm.functions = [
algorithm['parse_environ_into_request'],
algorithm['insert_variables_for_aspen'],
algorithm['parse_body_into_request'],
algorithm['raise_200_for_OPTIONS'],

Expand All @@ -101,16 +104,17 @@ def return_500_for_exception(website, exception):
csrf.reject_forgeries,
authentication.authenticate_user_if_possible,

algorithm['dispatch_request_to_filesystem'],
algorithm['dispatch_path_to_filesystem'],
algorithm['handle_dispatch_exception'],

http_caching.get_etag_for_file if env.cache_static else noop,
http_caching.try_to_serve_304 if env.cache_static else noop,

algorithm['apply_typecasters_to_path'],
algorithm['get_resource_for_request'],
algorithm['extract_accept_from_request'],
fill_accept_header,
algorithm['get_response_for_resource'],
algorithm['load_resource_from_filesystem'],
algorithm['create_response_object'],
algorithm['render_resource'],
algorithm['fill_response_with_output'],

tell_sentry,
algorithm['get_response_for_exception'],
Expand All @@ -129,8 +133,8 @@ def return_500_for_exception(website, exception):
]


# Monkey patch aspen
# ==================
# Monkey patch aspen and pando
# ============================

pop = aspen.http.mapping.Mapping.pop
def _pop(self, name, default=aspen.http.mapping.NO_DEFAULT):
Expand All @@ -140,31 +144,32 @@ def _pop(self, name, default=aspen.http.mapping.NO_DEFAULT):
raise aspen.Response(400, "Missing key: %s" % repr(name))
aspen.http.mapping.Mapping.pop = _pop

if hasattr(aspen.Response, 'redirect'):
raise Warning('aspen.Response.redirect() already exists')
if hasattr(pando.Response, 'redirect'):
raise Warning('pando.Response.redirect() already exists')
def _redirect(response, url, code=302):
response.code = code
response.headers['Location'] = url
raise response
aspen.Response.redirect = _redirect
pando.Response.redirect = _redirect

if hasattr(aspen.Response, 'render'):
raise Warning('aspen.Response.render() already exists')
if hasattr(pando.Response, 'render'):
raise Warning('pando.Response.render() already exists')
def _render(response, path, state, **extra):
state.update(extra)
assert response is state['response']
aspen.resources.get(state['website'], path).respond(state)
request_processor = state['request_processor']
output = aspen.resources.get(request_processor, path).render(state)
fill_response_with_output(output, response, request_processor)
raise response
aspen.Response.render = _render
pando.Response.render = _render

if hasattr(aspen.Response, 'set_cookie'):
raise Warning('aspen.Response.set_cookie() already exists')
if hasattr(pando.Response, 'set_cookie'):
raise Warning('pando.Response.set_cookie() already exists')
def _set_cookie(response, *args, **kw):
set_cookie(response.headers.cookie, *args, **kw)
aspen.Response.set_cookie = _set_cookie
pando.Response.set_cookie = _set_cookie

if hasattr(aspen.Response, 'erase_cookie'):
raise Warning('aspen.Response.erase_cookie() already exists')
if hasattr(pando.Response, 'erase_cookie'):
raise Warning('pando.Response.erase_cookie() already exists')
def _erase_cookie(response, *args, **kw):
erase_cookie(response.headers.cookie, *args, **kw)
aspen.Response.erase_cookie = _erase_cookie
pando.Response.erase_cookie = _erase_cookie
4 changes: 2 additions & 2 deletions liberapay/models/account_elsewhere.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

from six.moves.urllib.parse import urlsplit, urlunsplit

from aspen import Response
from aspen.utils import utcnow
from pando import Response
from pando.utils import utcnow
from postgres.orm import Model
from psycopg2 import IntegrityError
import xmltodict
Expand Down
2 changes: 1 addition & 1 deletion liberapay/models/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

from six.moves.urllib.parse import urlencode

from aspen.utils import utcnow
import aspen_jinja2_renderer
from html2text import html2text
from markupsafe import escape as htmlescape
from pando.utils import utcnow
from postgres.orm import Model
from psycopg2 import IntegrityError
from psycopg2.extras import Json
Expand Down
2 changes: 1 addition & 1 deletion liberapay/renderers/csv_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import csv
from io import BytesIO

from aspen import renderers
from aspen.simplates import renderers


class Renderer(renderers.Renderer):
Expand Down
28 changes: 16 additions & 12 deletions liberapay/renderers/jinja2_htmlescaped.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,31 @@
from liberapay.constants import JINJA_ENV_COMMON


class HTMLRenderer(base.Renderer):
def render_content(self, context):
class Renderer(base.Renderer):

autoescape = True

def render_content(self, context):
# Extend to inject an HTML-escaping function. Since autoescape is on,
# template authors shouldn't normally need to use this function, but
# having it in the simplate context makes it easier to implement i18n.

context['escape'] = context['state']['escape'] = htmlescape

if self.is_sgml:
context['escape'] = context['state']['escape'] = htmlescape
return base.Renderer.render_content(self, context)


class Factory(base.Factory):

Renderer = HTMLRenderer
Renderer = Renderer

def compile_meta(self, configuration):
# Override to turn on autoescaping.
# Override to add our own JINJA_ENV_COMMON conf
loader = base.FileSystemLoader(configuration.project_root)
return base.Environment(
loader=loader,
autoescape=True, extensions=['jinja2.ext.autoescape'],
**JINJA_ENV_COMMON
)
return {
'default_env': base.Environment(loader=loader, **JINJA_ENV_COMMON),
'htmlescaped_env': base.Environment(
loader=loader,
autoescape=True, extensions=['jinja2.ext.autoescape'],
**JINJA_ENV_COMMON
),
}
2 changes: 1 addition & 1 deletion liberapay/security/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from six.moves.urllib.parse import urlencode

from aspen import Response
from pando import Response

from liberapay.constants import SESSION, SESSION_TIMEOUT
from liberapay.exceptions import AuthRequired
Expand Down
2 changes: 1 addition & 1 deletion liberapay/security/csrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from datetime import timedelta
import re

from aspen import Response
from pando import Response

from .crypto import constant_time_compare, get_random_string

Expand Down
Loading