gallery.accords-library.com/server/szurubooru/api/context.py

83 lines
2.9 KiB
Python
Raw Normal View History

import falcon
from szurubooru import errors
2016-05-05 11:40:32 +00:00
from szurubooru.func import net
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):
2016-05-05 11:40:32 +00:00
return name in self.files or name + 'Url' in self.input
2016-04-30 21:17:08 +00:00
def get_file(self, name, required=False):
if name in self.files:
return self.files[name]
2016-05-05 11:40:32 +00:00
if name + 'Url' in self.input:
return net.download(self.input[name + 'Url'])
2016-04-30 21:17:08 +00:00
if not required:
return None
raise errors.MissingRequiredFileError(
'Required file %r is missing.' % name)
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
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)
return param
if not required:
return default
2016-04-30 21:17:08 +00:00
raise errors.MissingRequiredParameterError(
'Required paramter %r is missing.' % name)
# pylint: disable=redefined-builtin,too-many-arguments
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(
'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(
'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(
'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(
'Required parameter %r is missing.' % name)
class Request(falcon.Request):
context_type = Context