From 6731de0c1e1cf0ddb051fdd71c798a0676a0b0a0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 20 Aug 2021 14:30:03 +0200
Subject: [PATCH] misc: add option to disable some fields (#56118)

---
 tests/test_fields.py | 101 +++++++++++++++++++++++++++++++++++++++++++
 wcs/fields.py        |   4 ++
 2 files changed, 105 insertions(+)

diff --git a/tests/test_fields.py b/tests/test_fields.py
index 949854f4..0fa4fb10 100644
--- a/tests/test_fields.py
+++ b/tests/test_fields.py
@@ -1,3 +1,4 @@
+import os
 import re
 import shutil
 
@@ -626,3 +627,103 @@ def test_file_convert_from_anything():
     )
     assert value.base_filename == 'test.txt'
     assert value.get_file_pointer().read() == b'hello'
+
+
+def test_new_field_type_options():
+    pub.load_site_options()
+    if not pub.site_options.has_section('options'):
+        pub.site_options.add_section('options')
+    pub.site_options.set('options', 'disabled-fields', '')
+    with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
+        pub.site_options.write(fd)
+
+    assert fields.get_field_options(blacklisted_types=[]) == [
+        ('string', 'Text (line)', 'string'),
+        ('text', 'Long Text', 'text'),
+        ('email', 'Email', 'email'),
+        ('bool', 'Check Box (single choice)', 'bool'),
+        ('file', 'File Upload', 'file'),
+        ('date', 'Date', 'date'),
+        ('item', 'List', 'item'),
+        ('items', 'Multiple choice list', 'items'),
+        ('table', 'Table', 'table'),
+        ('table-select', 'Table of Lists', 'table-select'),
+        ('tablerows', 'Table with rows', 'tablerows'),
+        ('map', 'Map', 'map'),
+        ('ranked-items', 'Ranked Items', 'ranked-items'),
+        ('password', 'Password', 'password'),
+        ('', '—', ''),
+        ('title', 'Title', 'title'),
+        ('subtitle', 'Subtitle', 'subtitle'),
+        ('comment', 'Comment', 'comment'),
+        ('page', 'Page', 'page'),
+        ('', '—', ''),
+        ('computed', 'Computed Data', 'computed'),
+    ]
+    assert fields.get_field_options(blacklisted_types=['password', 'page']) == [
+        ('string', 'Text (line)', 'string'),
+        ('text', 'Long Text', 'text'),
+        ('email', 'Email', 'email'),
+        ('bool', 'Check Box (single choice)', 'bool'),
+        ('file', 'File Upload', 'file'),
+        ('date', 'Date', 'date'),
+        ('item', 'List', 'item'),
+        ('items', 'Multiple choice list', 'items'),
+        ('table', 'Table', 'table'),
+        ('table-select', 'Table of Lists', 'table-select'),
+        ('tablerows', 'Table with rows', 'tablerows'),
+        ('map', 'Map', 'map'),
+        ('ranked-items', 'Ranked Items', 'ranked-items'),
+        ('', '—', ''),
+        ('title', 'Title', 'title'),
+        ('subtitle', 'Subtitle', 'subtitle'),
+        ('comment', 'Comment', 'comment'),
+        ('', '—', ''),
+        ('computed', 'Computed Data', 'computed'),
+    ]
+
+    pub.site_options.set('options', 'disabled-fields', 'table, password, ')
+    with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
+        pub.site_options.write(fd)
+
+    assert fields.get_field_options(blacklisted_types=[]) == [
+        ('string', 'Text (line)', 'string'),
+        ('text', 'Long Text', 'text'),
+        ('email', 'Email', 'email'),
+        ('bool', 'Check Box (single choice)', 'bool'),
+        ('file', 'File Upload', 'file'),
+        ('date', 'Date', 'date'),
+        ('item', 'List', 'item'),
+        ('items', 'Multiple choice list', 'items'),
+        ('table-select', 'Table of Lists', 'table-select'),
+        ('tablerows', 'Table with rows', 'tablerows'),
+        ('map', 'Map', 'map'),
+        ('ranked-items', 'Ranked Items', 'ranked-items'),
+        ('', '—', ''),
+        ('title', 'Title', 'title'),
+        ('subtitle', 'Subtitle', 'subtitle'),
+        ('comment', 'Comment', 'comment'),
+        ('page', 'Page', 'page'),
+        ('', '—', ''),
+        ('computed', 'Computed Data', 'computed'),
+    ]
+    assert fields.get_field_options(blacklisted_types=['password', 'page']) == [
+        ('string', 'Text (line)', 'string'),
+        ('text', 'Long Text', 'text'),
+        ('email', 'Email', 'email'),
+        ('bool', 'Check Box (single choice)', 'bool'),
+        ('file', 'File Upload', 'file'),
+        ('date', 'Date', 'date'),
+        ('item', 'List', 'item'),
+        ('items', 'Multiple choice list', 'items'),
+        ('table-select', 'Table of Lists', 'table-select'),
+        ('tablerows', 'Table with rows', 'tablerows'),
+        ('map', 'Map', 'map'),
+        ('ranked-items', 'Ranked Items', 'ranked-items'),
+        ('', '—', ''),
+        ('title', 'Title', 'title'),
+        ('subtitle', 'Subtitle', 'subtitle'),
+        ('comment', 'Comment', 'comment'),
+        ('', '—', ''),
+        ('computed', 'Computed Data', 'computed'),
+    ]
diff --git a/wcs/fields.py b/wcs/fields.py
index 9d76d8b4..b3a45211 100644
--- a/wcs/fields.py
+++ b/wcs/fields.py
@@ -3482,11 +3482,15 @@ def get_field_types():
 
 def get_field_options(blacklisted_types):
     widgets, non_widgets = [], []
+    disabled_fields = (get_publisher().get_site_option('disabled-fields') or '').split(',')
+    disabled_fields = [f.strip() for f in disabled_fields if f.strip()]
     for klass in field_classes:
         if klass is ComputedField:
             continue
         if klass.key in blacklisted_types:
             continue
+        if klass.key in disabled_fields:
+            continue
         if issubclass(klass, WidgetField):
             widgets.append((klass.key, klass.description, klass.key))
         else:
-- 
2.30.2

