summaryrefslogtreecommitdiff
path: root/talerbank/app/schemas.py
diff options
context:
space:
mode:
authorMarcello Stanisci <stanisci.m@gmail.com>2019-05-31 15:00:51 +0200
committerMarcello Stanisci <stanisci.m@gmail.com>2019-05-31 15:00:51 +0200
commit327f37bb62cbe9c058c93c97ce8e8cc07f86d600 (patch)
tree42999b968a2b6cb046fa1dd3be9628608b02efd8 /talerbank/app/schemas.py
parentef37242e94698d13ecdf2448c77a0fc3148918cb (diff)
downloadbank-327f37bb62cbe9c058c93c97ce8e8cc07f86d600.tar.gz
bank-327f37bb62cbe9c058c93c97ce8e8cc07f86d600.tar.bz2
bank-327f37bb62cbe9c058c93c97ce8e8cc07f86d600.zip
5715.
Initial change in the structure. Just porting the /history validation to use the 'form' API for validating GET arguments. Subsequent changes (for GET requests) will follow the same pattern.
Diffstat (limited to 'talerbank/app/schemas.py')
-rw-r--r--talerbank/app/schemas.py94
1 files changed, 57 insertions, 37 deletions
diff --git a/talerbank/app/schemas.py b/talerbank/app/schemas.py
index cf11bdc..b75ff6a 100644
--- a/talerbank/app/schemas.py
+++ b/talerbank/app/schemas.py
@@ -24,11 +24,67 @@ from validictory import validate
from validictory.validator import \
(RequiredFieldValidationError,
FieldValidationError)
-
from django.conf import settings
+from django.core.exceptions import ValidationError
+from django import forms
+from django.core.validators import RegexValidator
##
+# Exception class to be raised when at least one expected URL
+# parameter is either not found or malformed.
+class URLParamValidationError(ValidationError):
+
+ ##
+ # Init method.
+ #
+ # @param self the object itself.
+ # @param param the missing URL parameter name.
+ # @param http_status_code the HTTP response code to return
+ # to the caller (client).
+ def __init__(self, error, http_status_code):
+ self.hint = ["%s: %s, " % (k, error[k]) for k in error]
+ self.http_status_code = http_status_code
+ super().__init__()
+
+
+##
+# Form specification that validates GET parameters from a
+# /history request.
+class HistoryParams(forms.Form):
+ auth = forms.CharField(
+ validators=[RegexValidator(
+ "^basic$",
+ message="Only 'basic' is allowed")])
+
+ cancelled = forms.CharField(
+ required=False,
+ empty_value="show",
+ validators=[RegexValidator(
+ "^(omit|show)$",
+ message="Only 'omit' or 'show' are valid")])
+
+ # FIXME: adjust min/max values.
+ delta = forms.IntegerField()
+ # FIXME: adjust min/max values.
+ start = forms.IntegerField(required=False)
+
+ ordering = forms.CharField(
+ required=False,
+ empty_value="descending",
+ validators=[RegexValidator(
+ "^(ascending|descending)$",
+ message="Only 'ascending' or 'descending' are valid")])
+
+ direction = forms.CharField(
+ validators=[RegexValidator(
+ "^(debit|credit|both|cancel\+|cancel-)$",
+ message="Only: debit/credit/both/cancel+/cancel-")])
+
+ # FIXME: adjust min/max values.
+ account_number = forms.IntegerField(required=False)
+
+##
# Exception class to be raised when a expected URL parameter
# is not found.
class URLParameterMissing(ValueError):
@@ -86,7 +142,6 @@ AMOUNT_SCHEMA = {
"type": "string",
"pattern": "^[A-Za-z0-9_-]+:([0-9]+)\.?([0-9]+)?$"}
-
##
# Definition that withdraw request bodies have to match.
WITHDRAW_SESSION_SCHEMA = {
@@ -173,32 +228,6 @@ HISTORY_RANGE_REQUEST_SCHEMA = {
##
-# Definition for /history request URL parameters.
-HISTORY_REQUEST_SCHEMA = {
- "type": "object",
- "properties": {
- "auth": {"type": "string", "pattern": "^basic$"},
- "cancelled": {"type": "string",
- "pattern": "^(omit|show)$",
- "required": False},
- "delta": {"type": "string",
- "pattern": r"^([\+-])?([0-9])+$"},
- "start": {"type": "string",
- "pattern": "^([0-9]+)$",
- "required": False},
- "ordering": {"type": "string",
- "pattern": r"^(ascending|descending)$",
- "required": False},
- "direction": {"type": "string",
- "pattern": r"^(debit|credit|both|cancel\+|cancel-)$"},
- "account_number": {"type": "string",
- "pattern": "^([0-9]+)$",
- "required": False}
- }
-}
-
-
-##
# Definition for /add/incoming request bodies.
INCOMING_REQUEST_SCHEMA = {
"type": "object",
@@ -210,7 +239,6 @@ INCOMING_REQUEST_SCHEMA = {
}
}
-
##
# Definition for PIN/TAN request URL parameters.
PIN_TAN_ARGS = {
@@ -268,13 +296,6 @@ def validate_reject(data):
validate(data, REJECT_REQUEST_SCHEMA)
##
-# Check /history input data.
-#
-# @param data dict representing /history's GET parameters.
-def validate_history(data):
- validate(data, HISTORY_REQUEST_SCHEMA)
-
-##
# Check /history-range input data.
#
# @param data dict representing /history's GET parameters.
@@ -319,7 +340,6 @@ def check_withdraw_session(data):
def validate_data(request, data):
switch = {
"/reject": validate_reject,
- "/history": validate_history,
"/history-range": validate_history_range,
"/admin/add/incoming": validate_add_incoming,
"/pin/verify": check_withdraw_session,