2016-04-15 15:54:21 +00:00
|
|
|
import falcon
|
|
|
|
from szurubooru import errors
|
|
|
|
|
|
|
|
class Context(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.session = None
|
|
|
|
self.user = None
|
|
|
|
self.files = {}
|
|
|
|
self.input = {}
|
|
|
|
self.output = None
|
|
|
|
|
|
|
|
def has_param(self, name):
|
|
|
|
return name in self.input
|
|
|
|
|
2016-04-30 21:17:08 +00:00
|
|
|
def has_file(self, name):
|
|
|
|
return name in self.files
|
|
|
|
|
|
|
|
def get_file(self, name, required=False):
|
|
|
|
if name in self.files:
|
|
|
|
return self.files[name]
|
|
|
|
if not required:
|
|
|
|
return None
|
|
|
|
raise errors.MissingRequiredFileError(
|
|
|
|
'Required file %r is missing.' % name)
|
2016-04-15 15:54:21 +00:00
|
|
|
|
2016-04-15 20:49:34 +00:00
|
|
|
def get_param_as_list(self, name, required=False, default=None):
|
|
|
|
if name in self.input:
|
|
|
|
param = self.input[name]
|
|
|
|
if not isinstance(param, list):
|
|
|
|
return [param]
|
|
|
|
return param
|
|
|
|
if not required:
|
|
|
|
return default
|
2016-04-30 21:17:08 +00:00
|
|
|
raise errors.MissingRequiredParameterError(
|
|
|
|
'Required paramter %r is missing.' % name)
|
2016-04-15 20:49:34 +00:00
|
|
|
|
2016-04-15 15:54:21 +00:00
|
|
|
def get_param_as_string(self, name, required=False, default=None):
|
|
|
|
if name in self.input:
|
|
|
|
param = self.input[name]
|
|
|
|
if isinstance(param, list):
|
2016-04-24 07:50:11 +00:00
|
|
|
try:
|
|
|
|
param = ','.join(param)
|
|
|
|
except:
|
2016-04-30 21:17:08 +00:00
|
|
|
raise errors.InvalidParameterError(
|
|
|
|
'Parameter %r is invalid - expected simple string.'
|
|
|
|
% name)
|
2016-04-15 15:54:21 +00:00
|
|
|
return param
|
|
|
|
if not required:
|
|
|
|
return default
|
2016-04-30 21:17:08 +00:00
|
|
|
raise errors.MissingRequiredParameterError(
|
|
|
|
'Required paramter %r is missing.' % name)
|
2016-04-15 15:54:21 +00:00
|
|
|
|
2016-04-16 13:07:33 +00:00
|
|
|
# pylint: disable=redefined-builtin,too-many-arguments
|
2016-04-15 15:54:21 +00:00
|
|
|
def get_param_as_int(
|
|
|
|
self, name, required=False, min=None, max=None, default=None):
|
|
|
|
if name in self.input:
|
|
|
|
val = self.input[name]
|
|
|
|
try:
|
|
|
|
val = int(val)
|
|
|
|
except (ValueError, TypeError):
|
2016-04-30 21:17:08 +00:00
|
|
|
raise errors.InvalidParameterError(
|
2016-04-15 15:54:21 +00:00
|
|
|
'Parameter %r is invalid: the value must be an integer.'
|
|
|
|
% name)
|
|
|
|
if min is not None and val < min:
|
2016-04-30 21:17:08 +00:00
|
|
|
raise errors.InvalidParameterError(
|
2016-04-15 15:54:21 +00:00
|
|
|
'Parameter %r is invalid: the value must be at least %r.'
|
|
|
|
% (name, min))
|
|
|
|
if max is not None and val > max:
|
2016-04-30 21:17:08 +00:00
|
|
|
raise errors.InvalidParameterError(
|
2016-04-15 15:54:21 +00:00
|
|
|
'Parameter %r is invalid: the value may not exceed %r.'
|
|
|
|
% (name, max))
|
|
|
|
return val
|
|
|
|
if not required:
|
|
|
|
return default
|
2016-04-30 21:17:08 +00:00
|
|
|
raise errors.MissingRequiredParameterError(
|
2016-04-15 15:54:21 +00:00
|
|
|
'Required parameter %r is missing.' % name)
|
|
|
|
|
|
|
|
class Request(falcon.Request):
|
|
|
|
context_type = Context
|