[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] [taler-merchant-frontends] branch master updated: Removing
From: |
gnunet |
Subject: |
[GNUnet-SVN] [taler-merchant-frontends] branch master updated: Removing errors dedicated module, and adapting donations and blog to that. |
Date: |
Fri, 03 Mar 2017 12:51:06 +0100 |
This is an automated email from the git hooks/post-receive script.
marcello pushed a commit to branch master
in repository merchant-frontends.
The following commit(s) were added to refs/heads/master by this push:
new b5d7a6a Removing errors dedicated module, and adapting donations and
blog to that.
b5d7a6a is described below
commit b5d7a6a6b905273d8ff1d90aa0b97d07fb644b73
Author: Marcello Stanisci <address@hidden>
AuthorDate: Fri Mar 3 12:48:55 2017 +0100
Removing errors dedicated module, and adapting donations
and blog to that.
---
talerfrontends/blog/blog.py | 3 +-
talerfrontends/donations/donations.py | 47 +++-------
talerfrontends/errors.py | 162 ----------------------------------
talerfrontends/helpers.py | 80 +----------------
4 files changed, 14 insertions(+), 278 deletions(-)
diff --git a/talerfrontends/blog/blog.py b/talerfrontends/blog/blog.py
index d40a9b8..4f20218 100644
--- a/talerfrontends/blog/blog.py
+++ b/talerfrontends/blog/blog.py
@@ -33,8 +33,7 @@ import datetime
from pprint import pprint
from talerfrontends.talerconfig import TalerConfig
from talerfrontends.helpers import (make_url,
-expect_parameter, expect_parameter_int, join_urlparts,
-get_query_string)
+expect_parameter, join_urlparts, get_query_string)
from talerfrontends.blog.content import (articles,
get_article_file, get_image_file)
diff --git a/talerfrontends/donations/donations.py
b/talerfrontends/donations/donations.py
index ac7d2ba..b18ba0a 100644
--- a/talerfrontends/donations/donations.py
+++ b/talerfrontends/donations/donations.py
@@ -26,8 +26,6 @@ import time
from datetime import datetime
import jinja2
from talerfrontends.talerconfig import TalerConfig
-from talerfrontends.errors import BackendError
-
from talerfrontends.helpers import (make_url,
expect_parameter, amount_to_string,
amount_from_float, amount_to_float,
@@ -113,18 +111,17 @@ def generate_contract():
)
print("order: ", order)
r = requests.post(urljoin(BACKEND_URL, 'proposal'), json=dict(order=order))
- if r.status_code != 200:
- logger.error("failed to POST to '%s'", url)
- raise BackendError(r.status_code, r.text)
- proposal_resp = r.json()
- return flask.jsonify(**proposal_resp)
+ return flask.jsonify(**proposal_resp), r.status_code
@app.route('/donate')
def donate():
donation_receiver = expect_parameter("donation_receiver")
donation_amount = expect_parameter("donation_amount")
response =
flask.make_response(flask.render_template('templates/fallback.html'), 402)
- response.headers["X-Taler-Contract-Url"] = make_url("/generate-contract",
("donation_receiver", donation_receiver), ("donation_amount", donation_amount))
+ response.headers["X-Taler-Contract-Url"] =
+ make_url("/generate-contract",
+ ("donation_receiver", donation_receiver),
+ ("donation_amount", donation_amount))
return response
@@ -150,17 +147,15 @@ def fulfillment():
return response
address@hidden('/pay', methods=["POST"])
address@hidden("/pay", methods=["POST"])
def pay():
deposit_permission = flask.request.get_json()
if deposit_permission is None:
e = flask.jsonify(error="no json in body")
return e, 400
-
r = requests.post(urljoin(BACKEND_URL, 'pay'), json=deposit_permission)
if 200 != r.status_code:
- raise BackendError(r.status_code, r.text)
-
+ return flask.jsonify(r.json()), r.status_code
proposal_data = r.json()['proposal_data']
order_id = proposal_data['order_id']
payed_order_ids = flask.session["payed_order_ids"] =
flask.session.get("payed_order_ids", {})
@@ -168,10 +163,8 @@ def pay():
donation_receiver=proposal_data['merchant']['instance'],
donation_amount=amount_to_float(proposal_data['amount'])
)
-
print("received payment for", order_id)
-
- return r.text, 200
+ return flask.jsonify(r.json()), r.status_code
@app.route("/backoffice")
def track():
@@ -181,34 +174,16 @@ def track():
@app.route("/history")
def history():
- # FIXME now JS needs to give a *timestamp*, NOT
- # days anymore..
qs = get_query_string().decode("utf-8")
url = urljoin(BACKEND_URL, "history")
r = requests.get(url, params=dict(parse_qsl(qs)))
- if r.status_code != 200:
- logger.error("failed to GET to '%s'", url)
- raise BackendError(r.status_code, r.text)
- return r.text
+ return flask.jsonify(r.json()), r.status_code
+
@app.route("/track/order")
def track_order():
-
instance = expect_parameter("instance")
order_id = expect_parameter("order_id")
-
url = urljoin(BACKEND_URL, 'track/transaction')
r = requests.get(url, params=dict(order_id=order_id, instance=instance))
-
- if r.status_code not in [200, 202, 424, 404]:
- logger.error("failed to GET to '%s'", url)
- raise BackendError(r.status_code, r.text)
- if 202 == r.status_code:
- return flask.jsonify(r.json()), 202
- if 404 == r.status_code:
- raise CustomError(r.code, "Unknown order_id", s.status_code)
- if 424 == r.status_code:
- raise TrackTransactionConflictError(r.status_code, order_id, r.text)
-
- return flask.jsonify(r.json())
-
+ return flask.jsonify(r.json()), r.status_code
diff --git a/talerfrontends/errors.py b/talerfrontends/errors.py
deleted file mode 100644
index f2e80d0..0000000
--- a/talerfrontends/errors.py
+++ /dev/null
@@ -1,162 +0,0 @@
-# This file is part of TALER
-# (C) 2016 INRIA
-#
-# TALER is free software; you can redistribute it and/or modify it under the
-# terms of the GNU Affero General Public License as published by the Free
Software
-# Foundation; either version 3, or (at your option) any later version.
-#
-# TALER is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR
-# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License along with
-# TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
-#
-# @author Florian Dold
-# @author Marcello Stanisci
-
-from flask import make_response, Blueprint
-
-# This class is used when the wallet detects any error
-# and it calls the onError handler installed by the merchant
-class CustomError(Exception):
- def __init__(self, code, hint, status):
- self.code = code
- self.hint = hint
- self.status = status
- super().__init__(code)
-
-
-class MissingParameterError(Exception):
- def __init__(self, name):
- self.name = name
- super().__init__(name)
-
-
-class MalformedDateError(Exception):
- def __init__(self, date):
- self.date = date
- super().__init__()
-
-
-class NoSessionActiveError(Exception):
- def __init__(self):
- super().__init__()
-
-
-class MalformedParameterError(Exception):
- def __init__(self, name):
- self.name = name
- super().__init__(name)
-
-
-class BackendError(Exception):
- def __init__(self, status, response):
- self.status = status
- self.response = response
- super().__init__(status, response)
-
-
-class TrackNotReadyError(Exception):
- def __init__(self, status, error_code, order_id):
- self.status = status
- self.order_id = order_id
- self.error_code = error_code
- super().__init__(status, 'Track not ready')
-
-
-class TrackTransactionConflictError(Exception):
- def __init__(self, status, transaction_id, response):
- self.status = status
- self.response = response
- self.transaction_id = transaction_id
- super().__init__(status, response)
-
-
-class TrackTransferConflictError(Exception):
- def __init__(self, status, wtid, response):
- self.status = status
- self.response = response
- self.wtid = wtid
- super().__init__(status, response)
-
-
-def make_400(ret):
- return make_response(ret, 400)
-
-
-def handle_missing_parameter_error(error):
- logger.warn("Missing parameter '%s' in request from client '%s'",
- error.name,
- request.remote_addr)
- return make_400(render_template("error.html",
- error="missing parameter",
- details=error.name))
-
-
-def handle_no_session_active_error(error):
- logger.warn("No session active")
- return make_400(render_template("error.html",
- error="no session active",
- details="purchase was not initiated
correctly"))
-
-
-def handle_malformed_parameter_error(error):
- logger.warn("Malformed parameter '%s' in request from client '%s'",
- error.name,
- request.remote_addr)
- return make_400(render_template("error.html",
- error="malformed parameter",
- details=error.name))
-
-
-def handle_malformed_parameter_error(error):
- logger.warn("Malformed date gotten")
- return make_400(render_template("error.html",
- error="malformed date",
- details="got '%s'" % error.date))
-
-
-def handle_backend_error(error):
- logger.warn("Backend error, status %s, body %s", error.status,
repr(error.response))
- return make_response(render_template("error.html",
- error="backend error",
- details=error.response),
- error.status)
-
-def handle_custom_error(error):
- logger.warn("Triggering custom error: %s, hint: %s, status code: %d" %
(error.code, error.hint, error.status))
- return make_response(render_template("error.html",
- error=error.code,
- details=error.hint),
- error.status)
-
-def handle_track_not_ready_error(error):
- logger.warn("Order %s has not been transferred yet" % error.order_id)
- return make_response(render_template("error.html",
- error = error.error_code,
- details = "Order %s is still waiting
for being paid back" % error.order_id))
-
-def handle_track_transaction_conflict_error(error):
- logger.warn("Transaction %s is conflicting" % error.transaction_id)
- return make_response(render_template("transaction_conflict.html",
- transaction_id = error.transaction_id,
- details = json.loads(error.response)),
- error.status)
-
-
-from . import helpers
-def handle_track_transfer_conflict_error(error):
- logger.info(error)
- logger.warn("Transfer %s is conflicting" % error.wtid)
- error_object = json.loads(error.response)
- offset = error_object["conflict_offset"]
- conflicting_coin =
error_object["exchange_transfer_proof"]["deposits"][offset]
- transferred_amount_with_fee =
amount_sum(conflicting_coin["deposit_value"], conflicting_coin["deposit_fee"])
- return make_response(render_template("transfer_conflict.html",
- wtid = error.wtid,
- transferred_amount =
helpers.amount_to_string(transferred_amount_with_fee),
- expected_amount =
helpers.amount_to_string(error_object["amount_with_fee"]),
- details = error.response,
-
transaction_id=conflicting_coin["transaction_id"]),
- error.status)
diff --git a/talerfrontends/helpers.py b/talerfrontends/helpers.py
index 389fb7c..751b8d9 100644
--- a/talerfrontends/helpers.py
+++ b/talerfrontends/helpers.py
@@ -23,8 +23,7 @@ import re
import datetime
import json
from .talerconfig import TalerConfig
-from .errors import (MalformedDateError,
-MissingParameterError, MalformedParameterError)
+from .errors import MissingParameterError
logger = logging.getLogger(__name__)
@@ -75,37 +74,6 @@ def amount_sum(a, b):
return ret
-# Give a - b. Mostly needed by backoffice
-def amount_sub(a, b):
- assert(a["currency"] == b["currency"])
-
- # Normalize
- a["value"] += int(a["fraction"] / FRACTION)
- b["value"] += int(b["fraction"] / FRACTION)
- a["fraction"] = a["fraction"] % FRACTION
- b["fraction"] = b["fraction"] % FRACTION
-
- # Extra care for fraction
- if a["fraction"] < b["fraction"]:
- a["fraction"] += FRACTION
- a["value"] -= 1
- assert(a["value"] >= 0)
-
- # Sub
- ret = amount_get_zero()
- ret["value"] = a["value"] - b["value"]
- ret["fraction"] = a["fraction"] - b["fraction"]
-
- assert(ret["value"] >= 0)
- assert(ret["fraction"] >= 0)
-
- return ret
-
-
-def amount_get_zero():
- return {"value": 0, "fraction": 0, "currency": CURRENCY}
-
-
def make_url(page, *query_params):
"""
Return a URL to a page in the current Flask application with the given
@@ -123,56 +91,12 @@ def make_url(page, *query_params):
return url.replace("%24", "$").replace("%7B", "{").replace("%7D", "}")
-def talerdate_to_obj(talerdate):
- try:
- timestamp = re.search('/Date\(([0-9]+)\)/', talerdate).group(1)
- ret = datetime.datetime.fromtimestamp(int(timestamp))
- return ret
- except AttributeError:
- logger.error("Wrong date given")
- raise MalformedDateError(date=talerdate)
-
-
-def talerdate(t):
- return "/Date(" + str(t) + ")/"
-
def expect_parameter(name, alt=None):
value = request.args.get(name, None)
if value is None and alt is None:
- raise MissingParameterError(name)
+ return flask.jsonify(error="Missing parameter '%s'." % name), 400
return value if value else alt
-def expect_parameter_int(name):
- v_str = expect_parameter(name)
- try:
- v = int(v_str)
- except ValueError:
- raise MalformedParameterError(name)
- return v
def get_query_string():
return request.query_string
-
-def expect_form(name):
- value = request.form.get(name, None)
- if value is None:
- raise MissingParameterError(name)
- return value
-
-#def track_transfer(wtid, exchange_uri, instance):
-# url = urljoin(current_app.config["BACKEND_URL"], 'track/transfer')
-# r = requests.get(url, dict(wtid=wtid, exchange=exchange_uri,
instance=instance))
-# if r.status_code not in [200, 424, 404]:
-# logger.error("failed to GET to '%s'", url)
-# logger.info(r.text)
-# raise BackendError(r.status_code, r.text if r.text else "{\"hard
error\": \"No error given by backend\"}")
-# if 424 == r.status_code:
-# error_code = json.loads(r.text)['code']
-# if TRACK_TRANSFER_CONFLICT == error_code:
-# raise TrackTransferConflictError(r.status_code, wtid, r.text)
-# else:
-# raise CustomError(error_code, r.details, r.status_code)
-# if 404 == r.status_code:
-# raise CustomError(error_code, "Unknown wtid supplied", r.status_code)
-# track = r.json()
-# return track
--
To stop receiving notification emails like this one, please contact
address@hidden
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] [taler-merchant-frontends] branch master updated: Removing errors dedicated module, and adapting donations and blog to that.,
gnunet <=