def get_default_setting(cls, app_name, setting_name, post_safe=False): """ Get default setting value from an app plugin. :param app_name: App name (string, must correspond to "name" in app plugin) :param setting_name: Setting name (string) :param post_safe: Whether a POST safe value should be returned (bool) :return: Setting value (string, integer or boolean) :raise: KeyError if nothing is found with setting_name """ app_plugin = get_app_plugin(app_name) if setting_name in app_plugin.app_settings: if app_plugin.app_settings[setting_name]['type'] == 'JSON': if not app_plugin.app_settings[setting_name].get('default'): return {} if post_safe: return json.dumps( app_plugin.app_settings[setting_name]['default']) return app_plugin.app_settings[setting_name]['default'] raise KeyError('Setting "{}" not found in app plugin "{}"'.format( setting_name, app_name))
def test_get_project_list_value(self): """Test get_project_list_value()""" app_plugin = get_app_plugin('filesfolders') # TODO: Refactor column system so that function returns link URL as # TODO: second return value instead of HTML self.assertEqual( tags.get_project_list_value(app_plugin, 'files', self.project), 0)
def test_is_app_link_visible_category(self): """Test is_app_link_visible() with a category""" app_plugin = get_app_plugin('filesfolders') self.assertEqual( tags.is_app_link_visible(app_plugin, self.category, self.user), False, )
def _make_setting( cls, app_name, name, setting_type, value, value_json={}, user_modifiable=True, project=None, user=None, ): """Make and save a AppSetting""" values = { 'app_plugin': get_app_plugin(app_name).get_model(), 'project': project, 'name': name, 'type': setting_type, 'value': value, 'value_json': value_json, 'user_modifiable': user_modifiable, 'user': user, } setting = AppSetting(**values) setting.save() return setting
def get_setting_def(cls, name, plugin=None, app_name=None): """ Return definition for a single app setting, either based on an app name or the plugin object. :param name: Setting name :param plugin: Plugin object extending ProjectAppPluginPoint :param app_name: Name of the app plugin (string) :return: Dict :raise: ValueError if neither app_name or plugin are set or if setting is not found in plugin """ if not plugin and not app_name: raise ValueError('Plugin and app name both unset') elif not plugin: plugin = get_app_plugin(app_name) if not plugin: raise ValueError( 'Plugin not found with app name "{}"'.format(app_name)) if name not in plugin.app_settings: raise ValueError( 'App setting not found in app "{}" with name "{}"'.format( plugin.name, name)) setting_def = plugin.app_settings[name] cls._check_type(setting_def['type']) return setting_def
def get_setting_defs(cls, scope, plugin=False, app_name=False, user_modifiable=False): """ Return app setting definitions of a specific scope from a plugin. :param scope: PROJECT, USER or PROJECT_USER :param plugin: project app plugin object extending ProjectAppPluginPoint :param app_name: Name of the app plugin (string) :param user_modifiable: Only return modifiable settings if True (boolean) :return: Dict :raise: ValueError if scope is invalid or if if neither app_name or plugin are set """ if not plugin and not app_name: raise ValueError('Plugin and app name both unset') if not plugin: plugin = get_app_plugin(app_name) if not plugin: raise ValueError( 'Plugin not found with app name "{}"'.format(app_name)) cls._check_scope(scope) return { k: v for k, v in plugin.app_settings.items() if ('scope' in v and v['scope'] == scope and ( not user_modifiable or ('user_modifiable' not in v or v['user_modifiable'] is True))) }
def test_is_app_hidden_hide(self): """Test is_app_hidden() with a hidden app and normal/superuser""" app_plugin = get_app_plugin('filesfolders') superuser = self.make_user('superuser') superuser.is_superuser = True superuser.save() self.assertEqual(tags.is_app_hidden(app_plugin, self.user), True) self.assertEqual(tags.is_app_hidden(app_plugin, superuser), False)
def test_get_app_link_state(self): """Test get_app_link_state()""" app_plugin = get_app_plugin('filesfolders') # TODO: Why does this also require app_name? self.assertEqual( tags.get_app_link_state(app_plugin, 'filesfolders', 'list'), 'active', ) self.assertEqual( tags.get_app_link_state(app_plugin, 'filesfolders', 'NON_EXISTING_URL_NAME'), '', )
def test_initialization(self): """Test AppSetting initialization""" expected = { 'id': self.setting_str.pk, 'app_plugin': get_app_plugin(EXAMPLE_APP_NAME).get_model().pk, 'project': None, 'name': 'str_setting', 'type': 'STRING', 'user': self.user.pk, 'value': 'test', 'value_json': {}, 'user_modifiable': True, 'sodar_uuid': self.setting_str.sodar_uuid, } self.assertEqual(model_to_dict(self.setting_str), expected)
def test_initialization_json(self): """Test initialization with JSON value""" expected = { 'id': self.setting_json.pk, 'app_plugin': get_app_plugin(EXAMPLE_APP_NAME).get_model().pk, 'project': self.project.pk, 'name': 'json_setting', 'type': 'JSON', 'user': None, 'value': None, 'value_json': {'Testing': 'good'}, 'user_modifiable': True, 'sodar_uuid': self.setting_json.sodar_uuid, } self.assertEqual(model_to_dict(self.setting_json), expected)
def test_initialization_integer(self): """Test initialization with integer value""" expected = { 'id': self.setting_int.pk, 'app_plugin': get_app_plugin(EXAMPLE_APP_NAME).get_model().pk, 'project': None, 'name': 'int_setting', 'type': 'INTEGER', 'user': self.user.pk, 'value': '170', 'value_json': {}, 'user_modifiable': True, 'sodar_uuid': self.setting_int.sodar_uuid, } self.assertEqual(model_to_dict(self.setting_int), expected)
def get_default_setting(cls, app_name, setting_name): """ Get default setting value from an app plugin. :param app_name: App name (string, must correspond to "name" in app plugin) :param setting_name: Setting name (string) :return: Setting value (string, integer or boolean) :raise: KeyError if nothing is found with setting_name """ app_plugin = get_app_plugin(app_name) if setting_name in app_plugin.app_settings: return app_plugin.app_settings[setting_name]['default'] raise KeyError('Setting "{}" not found in app plugin "{}"'.format( setting_name, app_name))
def test_is_app_link_visible_category_enabled(self): """Test is_app_link_visible() with category_enable=True""" app_plugin = get_app_plugin('timeline') self.assertEqual( tags.is_app_link_visible(app_plugin, self.category, self.user), True )
def test_is_app_hidden(self): """Test is_app_hidden()""" app_plugin = get_app_plugin('filesfolders') self.assertEqual(tags.is_app_hidden(app_plugin, self.user), False)
def test_is_app_link_visible(self): """Test is_app_link_visible()""" app_plugin = get_app_plugin('filesfolders') self.assertEqual( tags.is_app_link_visible(app_plugin, self.project, self.user), True )
def set_app_setting( cls, app_name, setting_name, value, project=None, user=None, validate=True, ): """ Set value of an existing project or user settings. Creates the object if not found. :param app_name: App name (string, must correspond to "name" in app plugin) :param setting_name: Setting name (string) :param value: Value to be set :param project: Project object (can be None) :param user: User object (can be None) :param validate: Validate value (bool, default=True) :return: True if changed, False if not changed :raise: ValueError if validating and value is not accepted for setting type :raise: ValueError if neither project nor user are set :raise: KeyError if setting name is not found in plugin specification """ cls._check_project_and_user(project, user) try: setting = AppSetting.objects.get( app_plugin__name=app_name, name=setting_name, project=project, user=user, ) if setting.value == value: return False if validate: cls.validate_setting(setting.type, value) setting.value = value setting.save() return True except AppSetting.DoesNotExist: app_plugin = get_app_plugin(app_name) if setting_name not in app_plugin.app_settings: raise KeyError( 'Setting "{}" not found in app plugin "{}"'.format( setting_name, app_name)) s_def = app_plugin.app_settings[setting_name] s_type = s_def['type'] s_mod = (bool(s_def['user_modifiable']) if 'user_modifiable' in s_def else True) if validate: cls.validate_setting(s_type, value) setting = AppSetting( app_plugin=app_plugin.get_model(), project=project, user=user, name=setting_name, type=s_type, value=value, user_modifiable=s_mod, ) setting.save() return True
def set_app_setting( cls, app_name, setting_name, value, project=None, user=None, validate=True, ): """ Set value of an existing project or user settings. Creates the object if not found. :param app_name: App name (string, must correspond to "name" in app plugin) :param setting_name: Setting name (string) :param value: Value to be set :param project: Project object (can be None) :param user: User object (can be None) :param validate: Validate value (bool, default=True) :return: True if changed, False if not changed :raise: ValueError if validating and value is not accepted for setting type :raise: ValueError if neither project nor user are set :raise: KeyError if setting name is not found in plugin specification """ if not project and not user: raise ValueError('Project and user are both unset') try: setting = AppSetting.objects.get( app_plugin__name=app_name, name=setting_name, project=project, user=user, ) if cls._compare_value(setting, value): return False if validate: cls.validate_setting(setting.type, value) if setting.type == 'JSON': setting.value_json = cls._get_json_value(value) else: setting.value = value setting.save() return True except AppSetting.DoesNotExist: app_plugin = get_app_plugin(app_name) if setting_name not in app_plugin.app_settings: raise KeyError( 'Setting "{}" not found in app plugin "{}"'.format( setting_name, app_name)) s_def = app_plugin.app_settings[setting_name] s_type = s_def['type'] s_mod = (bool(s_def['user_modifiable']) if 'user_modifiable' in s_def else True) cls._check_scope(s_def['scope']) cls._check_project_and_user(s_def['scope'], project, user) if validate: v = cls._get_json_value(value) if s_type == 'JSON' else value cls.validate_setting(s_type, v) s_vals = { 'app_plugin': app_plugin.get_model(), 'project': project, 'user': user, 'name': setting_name, 'type': s_type, 'user_modifiable': s_mod, } if s_type == 'JSON': s_vals['value_json'] = cls._get_json_value(value) else: s_vals['value'] = value AppSetting.objects.create(**s_vals) return True