Add literal_column_headings setting
authorKarl O. Pinc <kop@karlpinc.com>
Fri, 6 Dec 2019 03:19:32 +0000 (21:19 -0600)
committerKarl O. Pinc <kop@karlpinc.com>
Fri, 6 Dec 2019 03:19:32 +0000 (21:19 -0600)
examples/development.ini
examples/pgwui.ini
src/pgwui_server/__init__.py
tests/test___init__.py

index 4ea2ce8b926f2515f7ccf0be3f5846643fdb2569..9d55a8e70aad5b87dbdf143e59b1fb17780adf0e 100644 (file)
@@ -77,6 +77,17 @@ pgwui.dry_run = False
 # vulnerabilties.  Validation is on by default.
 pgwui.validate_hmac = False
 
+# Take uploaded column headings literally?
+# The available choices are:
+#   on   The file's column headings, as typed, are the table's column names.
+#   off  The file's column headings are case-insensitive.
+#   ask  Put a "Take Column Headings Literally" checkbox on the upload screen.
+# Optional setting.  The default is "off".
+#
+# Caution: Non-ASCII column names, particularly in the Turkish locale,
+# are not guaranteed to be case-insensitive.
+pgwui.literal_column_headings = off
+
 
 #
 # Pyramid configuration
index 552dd3649c6a8dd51c09d28542f40fa9ef80cbd0..52b91090442218b0de7f7c854aa73d49cce54d8f 100644 (file)
@@ -76,6 +76,17 @@ pgwui.dry_run = False
 # vulnerabilties.  Validation is on by default.
 # pgwui.validate_hmac = True
 
+# Take uploaded column headings literally?
+# The available choices are:
+#   on   The file's column headings, as typed, are the table's column names.
+#   off  The file's column headings are case-insensitive.
+#   ask  Put a "Take Column Headings Literally" checkbox on the upload screen.
+# Optional setting.  The default is "off".
+#
+# Caution: Non-ASCII column names, particularly in the Turkish locale,
+# are not guaranteed to be case-insensitive.
+pgwui.literal_column_headings = off
+
 
 #
 # Pyramid configuration
index 68e43e80d10a7088921b2c16b8691063dd75bc04..0f39fc1076a0b0bde4aa566218ac96592fb3823f 100644 (file)
@@ -36,6 +36,7 @@ SETTINGS = set(
      'route_prefix',
      'routes',
      'validate_hmac',
+     'literal_column_headings',
      ])
 
 # Required length of HMAC value
@@ -66,6 +67,13 @@ class NotBooleanSettingError(Error):
             .format(key))
 
 
+class BadLiteralColumnHeadingsError(Error):
+    def __init__(self, value):
+        super().__init__(
+            'The "pgwui.literal_column_headings" PGWUI setting must be'
+            '"on", "off", "ask", or not present')
+
+
 class BadHMACError(Error):
     pass
 
@@ -125,6 +133,9 @@ def validate_setting_values(settings):
     # validate_hmac
     boolean_setting('pgwui.validate_hmac', settings)
 
+    # literal_column_headings
+    validate_literal_column_headings(settings)
+
 
 def do_validate_hmac(settings):
     '''True unless the user has specificly rejected hmac validation
@@ -145,6 +156,16 @@ def validate_hmac(settings):
         raise HMACLengthError()
 
 
+def validate_literal_column_headings(settings):
+    '''Make sure the values are those allowed
+    '''
+    value = settings.get('pgwui.literal_column_headings')
+    if value is None:
+        return
+    if value not in ('on', 'off', 'ask'):
+        raise BadLiteralColumnHeadingsError(value)
+
+
 def validate_settings(settings):
     '''Be sure all settings validate
     '''
index b7588e8b28688072479cc943a5cafb50a71d8e73..ca504059d4ddd64e07d81f274703cc64609eccd2 100644 (file)
@@ -197,6 +197,38 @@ def test_validate_hmac_length(monkeypatch):
         pgwui_server_init.validate_hmac({'session.secret': ''})
 
 
+# validate_literal_column_headings()
+
+def test_validate_literal_column_headings_nosetting():
+    '''No error is raised when there's no setting'''
+    pgwui_server_init.validate_literal_column_headings({})
+
+
+def test_validate_literal_column_headings_on():
+    '''No error is raised when the setting is "on"'''
+    pgwui_server_init.validate_literal_column_headings(
+        {'pgwui.literal_column_headings': 'on'})
+
+
+def test_validate_literal_column_headings_off():
+    '''No error is raised when the setting is "off"'''
+    pgwui_server_init.validate_literal_column_headings(
+        {'pgwui.literal_column_headings': 'off'})
+
+
+def test_validate_literal_column_headings_ask():
+    '''No error is raised when the setting is "ask"'''
+    pgwui_server_init.validate_literal_column_headings(
+        {'pgwui.literal_column_headings': 'ask'})
+
+
+def test_validate_literal_column_headings_bad():
+    '''Raises an error when given a bad value'''
+    with pytest.raises(pgwui_server_init.BadLiteralColumnHeadingsError):
+        pgwui_server_init.validate_literal_column_headings(
+            {'pgwui.literal_column_headings': 'bad'})
+
+
 # validate_settings()
 
 def test_validate_settings(monkeypatch):