misc: add option to disable some fields (#56118)
gitea-wip/wcs/pipeline/head Build started... Details

This commit is contained in:
Lauréline Guérin 2021-08-20 14:30:03 +02:00
parent f85302d28b
commit de23297457
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 105 additions and 0 deletions

View File

@ -1,3 +1,4 @@
import os
import re
import pytest
@ -642,3 +643,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):
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'),
]

View File

@ -3499,11 +3499,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: