ivals = UploadTableInitialPost
+@attr.s
class LoadedForm(collections.abc.MutableMapping):
'''
Abstract class representing an upload form.
_form Instantaiated html form object (WTForms)
_fc Class handling html form
'''
- def __init__(self, uh, fc=None, data={}, **kwargs):
- super(LoadedForm, self).__init__()
+ uh = attr.ib(default=None)
+ _store = attr.ib(factory=dict)
+ _fc = attr.ib(default=None)
+ _form = attr.ib(default=None)
+ ivals = attr.ib(default=None)
+
+ def build(self, uh, fc=None, data={}, **kwargs):
+ '''Form initialization
+ '''
self.uh = uh
if data == {}:
store = dict(kwargs)
self._store = store
self._fc = fc
self.ivals = fc.ivals().build(self.uh.request.registry.settings)
+ return self
def __iter__(self):
for item in self._store:
return response
+@attr.s
class CredsLoadedForm(LoadedForm):
'''
Acts like a dict, but with extra methods.
Methods:
read() Load form from pyramid request object.
'''
- def __init__(self, uh, fc=UserWTForm, data={}, **kwargs):
- data.update(kwargs)
- super(CredsLoadedForm, self).__init__(uh, fc, data)
+ user = attr.ib(default=None)
+ password = attr.ib(default=None)
+ action = attr.ib(default=None)
+
+ def build(self, uh, fc=UserWTForm, data={}, **kwargs):
+ return super().build(uh, fc, data, **kwargs)
def session_put(self, key, value):
'''
'''
# Read parent's data
- super(CredsLoadedForm, self).read()
+ super().read()
# Read our form data
'''
Produces the dict pyramid will use to render the form.
'''
- response = super(CredsLoadedForm, self).write(result, errors)
+ response = super().write(result, errors)
if ('havecreds' not in response
or ('havecreds' in response and not response['havecreds'])):
# We don't know if the credentials are good or
return response
+@attr.s
class AuthLoadedForm(CredsLoadedForm):
'''
Acts like a dict, but with extra methods.
_form Instantaiated html form object (WXForms)
'''
- def __init__(self, uh, fc=AuthWTForm, data={}, **kwargs):
- data.update(kwargs)
- super(AuthLoadedForm, self).__init__(uh, fc, data)
+ db = attr.ib(default=None)
def read(self):
'''
'''
# Read parent's data
- super(AuthLoadedForm, self).read()
+ super().read()
# Keep form variables handy
self['db'] = self._form.db.data
'''
Produces the dict pyramid will use to render the form.
'''
- response = super(AuthLoadedForm, self).write(result, errors)
+ response = super().write(result, errors)
response['db'] = self['db']
return response
+@attr.s
class UploadFileForm(AuthLoadedForm):
'''
Acts like a dict, but with extra methods.
Methods:
read() Load form from pyramid request object.
'''
- def __init__(self, uh, fc=UploadFileWTForm, data={}, **kwargs):
- data.update(kwargs)
- super(UploadFileForm, self).__init__(uh, fc, data)
+ upload_fmt = attr.ib(default=None)
+ trim_upload = attr.ib(default=None)
+ literal_col_headings = attr.ib(default=None)
+ filename = attr.ib(default=None)
+ localfh = attr.ib(default=None)
+
+ def build(self, uh, fc=UploadFileWTForm, data={}, **kwargs):
+ return super().build(uh, fc, data, **kwargs)
def read(self):
'''
'''
# Read parent's data
- super(UploadFileForm, self).read()
+ super().read()
# Read our own data
self['upload_fmt'] = self._form.upload_fmt.data
# Other POST variables involving a file
self['filename'] = ''
self['localfh'] = ''
- if 'action' in self:
+ if self['action']:
if self._form.datafile.data != '':
post = self.uh.request.POST
if hasattr(post['datafile'], 'filename'):
else:
literal_col_headings_checked = UNCHECKED
- response = super(UploadFileForm, self).write(result, errors)
+ response = super().write(result, errors)
response['filename'] = self['filename']
response['trim_upload'] = trim_upload_checked
response['csv_value'] = CSV_VALUE
return response
+@attr.s
class UploadDoubleFileFormMixin(UploadFormBaseMixin):
'''
Adds a last_key attribute to self, from POST
Methods:
read() Load form from pyramid request object.
'''
+ last_key = attr.ib(default=None)
def read(self):
'''
return super().write_response(response)
+@attr.s
class UploadDoubleFileForm(UploadDoubleFileFormMixin, UploadFileForm):
'''
Acts like a dict, but with extra methods.
Methods:
read() Load form from pyramid request object.
'''
- def __init__(self, uh, fc=UploadFileWTForm, data={}, **kwargs):
- data.update(kwargs)
- super().__init__(uh, fc, data)
-
def read(self):
'''
Read form data from the client
'''
- # Read parents' data
+ # Read all parents' data
super().read()
def write(self, result, errors):
return super().write_response(response)
+@attr.s
class UploadNullMixin(UploadFormBaseMixin):
'''
Acts like a dict, but with extra methods.
return super().write_response(response)
+@attr.s
class UploadTableForm(UploadNullMixin, UploadFileForm):
'''
Acts like a dict, but with extra methods.
Methods:
read() Load form from pyramid request object.
'''
- def __init__(self, uh, fc=UploadTableWTForm, data={}, **kwargs):
- data.update(kwargs)
- super().__init__(uh, fc, data)
+ def build(self, uh, fc=UploadTableWTForm, data={}, **kwargs):
+ return super().build(uh, fc, data, **kwargs)
def read(self):
'''
Read form data from the client
'''
- # Read parents' data
+ # Read all parents' data
super().read()
# Read our own data
self['table'] = self._form.table.data
return super().write_response(response)
+@attr.s
class UploadDoubleTableForm(UploadDoubleFileFormMixin, UploadTableForm):
'''
Acts like a dict, but with extra methods.
Methods:
read() Load form from pyramid request object.
'''
- def __init__(self, uh, fc=UploadTableWTForm, data={}, **kwargs):
- data.update(kwargs)
- super().__init__(uh, fc, data)
-
def read(self):
'''
Read form data from the client
'''
- # Read parents' data
+ # Read all parents' data
super().read()
def write(self, result, errors):