Change some of the form classes from inheritence to mixins
authorKarl O. Pinc <kop@karlpinc.com>
Wed, 23 Dec 2020 17:25:21 +0000 (11:25 -0600)
committerKarl O. Pinc <kop@karlpinc.com>
Mon, 28 Dec 2020 22:35:41 +0000 (16:35 -0600)
src/pgwui_core/core.py

index 6f60d67f2d06e8fb14bdbe71233a6023a5cf4c59..1e96f8721a7ddd418fc1358ec35957c6f6a92c22 100644 (file)
@@ -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):
         '''