Attach the initial post to my form class, not WTForms' class
authorKarl O. Pinc <kop@karlpinc.com>
Sat, 23 Jan 2021 04:48:30 +0000 (22:48 -0600)
committerKarl O. Pinc <kop@karlpinc.com>
Sat, 23 Jan 2021 04:48:30 +0000 (22:48 -0600)
src/pgwui_core/core.py

index 9d85129f7b5bd9e484a9b15869167984be621e70..8935d9f71c6e5cb6922b732f5b742f2a2bc8a8bc 100644 (file)
@@ -115,8 +115,6 @@ class UserWTForm(Form):
     user = StringField('User:')
     password = PasswordField('Password:')
 
-    ivals = UserInitialPost
-
 
 class AuthWTForm(UserWTForm):
     '''The wtform used to connect to any db and authenticate.'''
@@ -125,8 +123,6 @@ class AuthWTForm(UserWTForm):
     # just to keep my hand in.
     db = StringField('Database:')
 
-    ivals = UserInitialPost
-
 
 class UploadFileWTForm(AuthWTForm):
     '''The wtform used for uploading files.'''
@@ -140,8 +136,6 @@ class UploadFileWTForm(AuthWTForm):
     trim_upload = BooleanField('Trim Leading/Trailing Spaces:')
     literal_col_headings = BooleanField('Literal Uploaded Column Headings:')
 
-    ivals = UploadFileInitialPost
-
 
 class UploadNullFileWTForm(UploadFileWTForm):
     '''The wtform used for uploading files that may contain NULL.'''
@@ -151,15 +145,11 @@ class UploadNullFileWTForm(UploadFileWTForm):
     upload_null = BooleanField('Upload NULL Values:')
     null_rep = StringField('NULL Representation:')
 
-    ivals = UploadNullFileInitialPost
-
 
 class UploadTableWTForm(UploadNullFileWTForm):
     '''The wtform used for uploading arbitrary data into tables.'''
     table = StringField('Table or View:')
 
-    ivals = UploadTableInitialPost
-
 
 @attr.s
 class LoadedForm(collections.abc.MutableMapping):
@@ -178,14 +168,17 @@ class LoadedForm(collections.abc.MutableMapping):
       _form   Instantaiated html form object (WTForms)
       _fc     Class handling html form
     '''
+    fc_default = attr.ib(default=None)
+    ip_default = attr.ib(default=None)
     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):
+    def build(self, uh, fc=None, ip=None, data={}, **kwargs):
         '''Form initialization
+        ip is the instantiated initial post
         '''
         self.uh = uh
         if data == {}:
@@ -194,8 +187,9 @@ class LoadedForm(collections.abc.MutableMapping):
             store = dict(data)
             store.update(kwargs)
         self._store = store
-        self._fc = fc
-        self.ivals = fc.ivals().build(self.uh.request.registry.settings)
+        self._fc = (self.fc_default if fc is None else fc)
+        ip_used = (self.ip_default if ip is None else ip)
+        self.ivals = ip_used.build(self.uh.request.registry.settings)
         return self
 
     def __iter__(self):
@@ -253,13 +247,12 @@ class CredsLoadedForm(LoadedForm):
     Methods:
       read()  Load form from pyramid request object.
     '''
+    fc_default = attr.ib(default=UserWTForm)
+    ip_default = attr.ib(factory=UserInitialPost)
     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):
         '''
         Put data into the session.
@@ -381,15 +374,14 @@ class UploadFileForm(AuthLoadedForm):
     Methods:
       read()  Load form from pyramid request object.
     '''
+    fc_default = attr.ib(default=UploadFileWTForm)
+    ip_default = attr.ib(factory=UploadFileInitialPost)
     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 form data from the client
@@ -559,8 +551,8 @@ class UploadTableForm(UploadNullMixin, UploadFileForm):
     Methods:
       read()  Load form from pyramid request object.
     '''
-    def build(self, uh, fc=UploadTableWTForm, data={}, **kwargs):
-        return super().build(uh, fc, data, **kwargs)
+    fc_default = attr.ib(default=UploadTableWTForm)
+    ip_default = attr.ib(factory=UploadTableInitialPost)
 
     def read(self):
         '''