""" MIT License Copyright (C) 2023 ROCKY4546 https://github.com/rocky4546 This file is part of Cabernet Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. """ from lib.web.pages.templates import web_templates from lib.common.decorators import getrequest from lib.common.decorators import postrequest @getrequest.route('/api/configform') def get_configform_html(_webserver): if 'area' in _webserver.query_data: configform = ConfigFormHTML() form = configform.get(_webserver.plugins.config_obj.defn_json.get_defn( _webserver.query_data['area']), _webserver.query_data['area'], _webserver.plugins.config_obj.data) _webserver.do_mime_response(200, 'text/html', form) else: _webserver.do_mime_response(404, 'text/html', web_templates['htmlError'].format('404 - Area Not Found')) @postrequest.route('/api/configform') def post_configform_html(_webserver): if _webserver.config['web']['disable_web_config']: _webserver.do_mime_response( 501, 'text/html', web_templates['htmlError'] .format('501 - Config pages disabled. ' 'Set [web][disable_web_config] to False in the config file to enable')) else: # Take each key and make a [section][key] to store the value config_changes = {} area = _webserver.query_data['area'][0] del _webserver.query_data['area'] del _webserver.query_data['name'] del _webserver.query_data['instance'] for key in _webserver.query_data: key_pair = key.split('-', 1) if key_pair[0] not in config_changes: config_changes[key_pair[0]] = {} config_changes[key_pair[0]][key_pair[1]] = _webserver.query_data[key] results = _webserver.plugins.config_obj.update_config(area, config_changes) _webserver.do_mime_response(200, 'text/html', results) class ConfigFormHTML: def __init__(self): self.area = None self.config_defn = None self.config = None def get(self, _config_defn, _area, _config): self.area = _area self.config = _config self.config_defn = _config_defn return ''.join([self.header, self.body]) @property def header(self): return ''.join([ '', '', '', 'Settings Editor', '', '', '', '']) @property def title(self): return ''.join([ '
', '

Settings Editor - ', self.config_defn['label'], '

', self.config_defn['description'], '
' ]) @property def tabs(self): active_tab = ' activeTab' area_html = ''.join(['']) return area_html @property def forms(self): area_html = '' for section in self.config_defn['sections'].keys(): area_html = ''.join([area_html, self.get_form(section)]) return area_html def get_form(self, _section): input_html = "*** UNKNOWN INPUT TYPE ***" section_data = self.config_defn['sections'][_section] form_html = ''.join([ '
', section_data['description'], '
' ]) section_html = '' subsection = None is_section_new = False plugin_image = '' for setting, setting_data in section_data['settings'].items(): if setting_data['level'] == 4: continue title = '' readonly = '' new_section = None if '-' in setting: new_section = setting.split('-', 1)[0] if new_section != subsection and new_section is not None: is_section_new = True subsection = new_section background_color = '#F0F0F0' if setting_data['help'] is not None: title = ''.join([' title="', setting_data['help'], '"']) if 'writable' in setting_data and not setting_data['writable']: readonly = ' readonly' background_color = '#C0C0C0' if setting_data['type'] == 'string' \ or setting_data['type'] == 'path': input_html = ''.join([ '']) if setting_data['type'] == 'password': input_html = ''.join([ '']) elif setting_data['type'] == 'integer' \ or setting_data['type'] == 'float': input_html = ''.join([ '']) elif setting_data['type'] == 'boolean': if 'writable' in setting_data and not setting_data['writable']: readonly = ' disabled' input_html = ''.join([ '' '' ]) elif setting_data['type'] == 'list': dlsetting = '' if section_data['name'] == 'display' and setting == 'display_level': dlsetting = ' class="dlsetting" ' option_html = ''.join(['']) for value in setting_data['values']: option_html += ''.join([ '']) input_html = ''.join([ '']) elif setting_data['type'] == 'image': if setting != 'plugin_image': input_html = ''.join([ '']) else: input_html = None img_size = self.lookup_config_size() plugin_image = ''.join([ '
', '', '
' ]) if is_section_new: is_section_new = False section_html = ''.join([section_html, '']) if input_html: section_html = ''.join([section_html, '']) return ''.join([ form_html, section_html, '

', subsection.upper(), '

', input_html, '
', plugin_image, '
', '
']) def lookup_config_size(self): size_text = self.config['channels']['thumbnail_size'] if size_text == 'None': return 0 elif size_text == 'Tiny(16)': return 16 elif size_text == 'Small(48)': return 48 elif size_text == 'Medium(128)': return 128 elif size_text == 'Large(180)': return 180 elif size_text == 'X-Large(270)': return 270 elif size_text == 'Full-Size': return None else: return None @property def body(self): if self.config_defn is None: return ''.join([ '
', '
']) else: return ''.join([ self.title, self.tabs, self.forms, '
', '
'])