From 1d3ed6b27e8f78358161907f0a468e68e13f7570 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Wed, 23 Dec 2020 11:25:21 -0600 Subject: [PATCH] Change some of the form classes from inheritence to mixins --- src/pgwui_core/core.py | 101 +++++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 43 deletions(-) diff --git a/src/pgwui_core/core.py b/src/pgwui_core/core.py index 6f60d67..1e96f87 100644 --- a/src/pgwui_core/core.py +++ b/src/pgwui_core/core.py @@ -435,8 +435,18 @@ class UploadFileForm(AuthLoadedForm): return response -class UploadDoubleFileForm(UploadFileForm): +class UploadFormBaseMixin(): ''' + Mixins add to attributes to self, and to response. + ''' + def write_response(self, response): + return response + + +class UploadDoubleFileFormMixin(UploadFormBaseMixin): + ''' + Adds a last_key attribute to self, from POST + Acts like a dict, but with extra methods. Attributes: @@ -445,34 +455,28 @@ class UploadDoubleFileForm(UploadFileForm): Methods: read() Load form from pyramid request object. ''' - def __init__(self, uh, fc=UploadFileWTForm, data={}, **kwargs): - data.update(kwargs) - super(UploadDoubleFileForm, self).__init__(uh, fc, data) def read(self): ''' Read form data from the client ''' - # Read parent's data - super(UploadDoubleFileForm, self).read() + super().read() - # Read our own data post = self.uh.request.POST if 'last_key' in post: self['last_key'] = post['last_key'] else: self['last_key'] = '' - def write(self, result, errors): + def write_response(self, response): ''' Produces the dict pyramid will use to render the form. ''' - response = super(UploadDoubleFileForm, self).write(result, errors) response['last_key'] = self['last_key'] - return response + return super().write_response(response) -class UploadNullFileForm(UploadFileForm): +class UploadDoubleFileForm(UploadDoubleFileFormMixin, UploadFileForm): ''' Acts like a dict, but with extra methods. @@ -482,23 +486,44 @@ class UploadNullFileForm(UploadFileForm): Methods: read() Load form from pyramid request object. ''' - def __init__(self, uh, fc=UploadNullFileWTForm, data={}, **kwargs): + def __init__(self, uh, fc=UploadFileWTForm, data={}, **kwargs): data.update(kwargs) - super(UploadNullFileForm, self).__init__(uh, fc, data) + super().__init__(uh, fc, data) def read(self): ''' Read form data from the client ''' + # Read parents' data + super().read() - # Read parent's data - super(UploadNullFileForm, self).read() + def write(self, result, errors): + ''' + Produces the dict pyramid will use to render the form. + ''' + response = super().write(result, errors) + return super().write_response(response) - # Read our own data + +class UploadNullMixin(UploadFormBaseMixin): + ''' + Acts like a dict, but with extra methods. + + Attributes: + uh The UploadHandler instance using the form + + Methods: + read() Load form from pyramid request object. + ''' + def read(self): + ''' + Read form data from the client + ''' + super().read() self['upload_null'] = self._form.upload_null.data self['null_rep'] = self._form.null_rep.data - def write(self, result, errors): + def write_response(self, response): ''' Produces the dict pyramid will use to render the form. ''' @@ -507,13 +532,12 @@ class UploadNullFileForm(UploadFileForm): else: upload_null_checked = UNCHECKED - response = super(UploadNullFileForm, self).write(result, errors) response['upload_null'] = upload_null_checked response['null_rep'] = self['null_rep'] - return response + return super().write_response(response) -class UploadTableForm(UploadNullFileForm): +class UploadTableForm(UploadNullMixin, UploadFileForm): ''' Acts like a dict, but with extra methods. @@ -525,16 +549,15 @@ class UploadTableForm(UploadNullFileForm): ''' def __init__(self, uh, fc=UploadTableWTForm, data={}, **kwargs): data.update(kwargs) - super(UploadTableForm, self).__init__(uh, fc, data) + super().__init__(uh, fc, data) def read(self): ''' Read form data from the client ''' - # Read parent's data - super(UploadTableForm, self).read() - + # Read parents' data + super().read() # Read our own data self['table'] = self._form.table.data @@ -542,12 +565,12 @@ class UploadTableForm(UploadNullFileForm): ''' Produces the dict pyramid will use to render the form. ''' - response = super(UploadTableForm, self).write(result, errors) + response = super().write(result, errors) response['table'] = self['table'] - return response + return super().write_response(response) -class UploadDoubleTableForm(UploadTableForm): +class UploadDoubleTableForm(UploadDoubleFileFormMixin, UploadTableForm): ''' Acts like a dict, but with extra methods. @@ -559,29 +582,21 @@ class UploadDoubleTableForm(UploadTableForm): ''' def __init__(self, uh, fc=UploadTableWTForm, data={}, **kwargs): data.update(kwargs) - super(UploadDoubleTableForm, self).__init__(uh, fc, data) + super().__init__(uh, fc, data) def read(self): ''' Read form data from the client ''' - # Read parent's data - super(UploadDoubleTableForm, self).read() - - # Read our own data - post = self.uh.request.POST - if 'last_key' in post: - self['last_key'] = post['last_key'] - else: - self['last_key'] = '' + # Read parents' data + super().read() def write(self, result, errors): ''' Produces the dict pyramid will use to render the form. ''' - response = super(UploadDoubleTableForm, self).write(result, errors) - response['last_key'] = self['last_key'] - return response + response = super().write(result, errors) + return super().write_response(response) # Utility functions @@ -983,7 +998,7 @@ class DataLineProcessor(object): udl An UploadDataLine instance ''' - raise core_ex.NotImplementedError + raise NotImplementedError class NoOpProcessor(DataLineProcessor): @@ -1068,13 +1083,13 @@ class DBHandler(object): Return an instantiation of the upload form needed by the upload handler. ''' - raise core_ex.NotImplementedError + raise NotImplementedError def get_data(self): ''' Put something that will go into the db into the 'data' attribute. ''' - raise core_ex.NotImplementedError + raise NotImplementedError def val_input(self): ''' -- 2.34.1