Add a setting for uploaded file format
authorKarl O. Pinc <kop@karlpinc.com>
Sat, 23 Jan 2021 22:13:51 +0000 (16:13 -0600)
committerKarl O. Pinc <kop@karlpinc.com>
Sat, 23 Jan 2021 22:13:51 +0000 (16:13 -0600)
src/pgwui_upload_core/check_settings.py
src/pgwui_upload_core/exceptions.py
src/pgwui_upload_core/views/upload.py

index 04eb1a227ffdd0be14271daf261d22ffc7af8834..60c5118322f844f90cd8ba43ef935255d116ab36 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2020 The Meme Factory, Inc.  http://www.karlpinc.com/
+# Copyright (C) 2020, 2021 The Meme Factory, Inc.  http://www.karlpinc.com/
 
 # This file is part of PGWUI_Upload_Core.
 #
 # Karl O. Pinc <kop@karlpinc.com>
 
 from pgwui_common import checkset
+from pgwui_upload_core import exceptions as upload_core_ex
 
 
 UPLOAD_SETTINGS = ['menu_label',
                    'literal_column_headings',
                    'trim',
                    'null',
+                   'file_format'
                    ]
 REQUIRED_SETTINGS = []
 BOOLEAN_SETTINGS = []
@@ -34,6 +36,15 @@ BOOLEAN_CHOICE_SETTINGS = ['literal_column_headings',
                            'null']
 
 
+def validate_file_format(component, errors, settings):
+    '''Make sure the values are those allowed
+    '''
+    value = settings.get('file_format')
+    if value not in ('csv', 'tab'):
+        errors.append(
+            upload_core_ex.BadFileFormatError(component, value))
+
+
 def check_settings(
         component, all_setngs, required_setngs, boolean_setngs,
         boolean_choice_setngs, component_config):
index 662b7f634015d7fde366d9dce5929e22250cf8fc..58c8e7d8737afd8f00f7ccb98d3df8bbbdb7b80d 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2020 The Meme Factory, Inc.  http://www.karlpinc.com/
+# Copyright (C) 2020, 2021 The Meme Factory, Inc.  http://www.karlpinc.com/
 
 # This file is part of PGWUI_Upload.
 #
@@ -28,6 +28,13 @@ class UploadError(core_ex.Error):
     pass
 
 
+class BadFileFormatError(UploadError):
+    def __init__(self, component, value):
+        super().__init__(
+            f'The "pgwui:{component}:file_format" PGWUI setting is ({value}) '
+            'it must be either "csv" or "tab" or the setting be omitted')
+
+
 class NoTableError(UploadError):
     '''No table uploaded'''
     def __init__(self, e, descr='', detail=''):
index 7dbab4eaafae95c2d55976793a5e5b15d14effb9..7c90a332dec851b1f1367e069a11d734ac84c038 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2015, 2018, 2020 The Meme Factory, Inc.
+# Copyright (C) 2015, 2018, 2020, 2021 The Meme Factory, Inc.
 # http://www.karlpinc.com/
 
 # This file is part of PGWUI_Upload_Core.
@@ -38,6 +38,10 @@ from pgwui_core.core import (
     UploadData,
     doublequote,
 )
+from pgwui_core.constants import (
+    CSV,
+    TAB,
+)
 
 from pgwui_upload_core import exceptions as upload_ex
 
@@ -62,9 +66,16 @@ class UploadCoreInitialPost(UploadNullFileInitialPost):
         # otherwise the result is never displayed on the page.
         return (val == 'yes-always' or val == 'choice-yes')
 
+    def find_upload_fmt(self, upload_settings):
+        if upload_settings['file_format'] == 'csv':
+            return CSV
+        else:
+            return TAB
+
     def build(self, settings={}):
         super().build(settings)
         upload_settings = settings['pgwui'][self.component]
+        self.upload_fmt = self.find_upload_fmt(upload_settings)
         self.upload_null = self.boolean_choice(upload_settings, 'null')
         self.trim_upload = self.boolean_choice(upload_settings, 'trim')
         self.literal_col_headings = self.boolean_choice(