def __getattr__(self, attr): if attr not in self.defaults: raise AttributeError("Invalid API setting: '%s'" % attr) try: # Check if present in user settings val = self.user_settings[attr] except KeyError: # Fall back to defaults val = self.defaults[attr] # Coerce import strings into classes if attr in self.import_strings: val = perform_import(val, attr) # Validations for attrs if attr == 'BACKUP_CODES_CHARACTERS' and ',' in val: raise ImproperlyConfigured( 'Cannot use , as a character for {}.'.format(attr), ) if attr == 'MFA_METHODS': for method_name, method_config in val.items(): if 'HANDLER' not in method_config: raise ImproperlyConfigured( 'Missing handler in {} configuration.'.format( method_name), ) method_config['HANDLER'] = perform_import( method_config.get('HANDLER'), 'HANDLER', ) # Cache the result self._cached_attrs.add(attr) setattr(self, attr, val) return val
def _validate(self, attribute: str, value: Any) -> None: if attribute == self._FIELD_MFA_METHODS: for method_name, method_config in value.items(): if self._FIELD_HANDLER not in method_config: raise MethodHandlerMissingError(method_name=method_name) for k, v in self.defaults[ self._FIELD_MFA_METHODS][method_name].items(): method_config[k] = method_config.get(k, v) method_config[self._FIELD_HANDLER] = perform_import( method_config[self._FIELD_HANDLER], self._FIELD_HANDLER)
def __new__(mcls, name, bases, attrs): if 'permission_classes' not in attrs: settings_key = attrs.get( 'settings_permission_key', 'FORMIDABLE_DEFAULT_PERMISSION' ) # If the settings key define is not present in the settings # The ``NoOne`` permission is loaded. modules = getattr(settings, settings_key, getattr(settings, 'FORMIDABLE_DEFAULT_PERMISSION', # noqa ['formidable.permissions.NoOne'])) # noqa permissions_classes = perform_import(modules, None) attrs['permission_classes'] = permissions_classes return super(MetaClassView, mcls).__new__(mcls, name, bases, attrs)
def __getattr__(self, attr): if attr not in self.defaults: raise AttributeError("Invalid setting: '%s'" % attr) # pragma: no cover try: # Check if present in user settings val = self.user_settings[attr] except KeyError: # Fall back to defaults val = self.defaults[attr] # Coerce import strings into classes if attr in self.import_strings: val = perform_import(val, attr) return val
def __getattr__(self, attr): if attr not in self.defaults.keys(): raise AttributeError("Invalid registration setting: '%s'" % attr) try: # Check if present in user settings val = self.user_settings[attr] except KeyError: # Fall back to defaults val = self.defaults[attr] # Coerce import strings into classes if attr in self.import_strings: val = perform_import(val, attr) # Cache the result setattr(self, attr, val) return val
def __new__(mcls, name, bases, attrs): if 'permission_classes' not in attrs: settings_key = attrs.get('settings_permission_key', 'FORMIDABLE_DEFAULT_PERMISSION') # If the settings key define is not present in the settings # The ``NoOne`` permission is loaded. modules = getattr( settings, settings_key, getattr( settings, 'FORMIDABLE_DEFAULT_PERMISSION', # noqa ['formidable.permissions.NoOne'])) # noqa permissions_classes = perform_import(modules, None) attrs['permission_classes'] = permissions_classes return super().__new__(mcls, name, bases, attrs)
def __getattr__(self, attr: str) -> Any: if attr not in self.defaults.keys(): raise AttributeError( "Invalid {self.root_setting_name} setting: '{attr}'".format( self=self, attr=attr)) try: # Check if present in user settings val = self.user_settings[attr] except KeyError: # Fall back to defaults val = self.defaults[attr] # Coerce import strings into classes if attr in self.import_strings: val = perform_import(val, attr) # Cache the result setattr(self, attr, val) return val
from django.conf import settings from rest_framework import generics from rest_framework import permissions from rest_framework import views from rest_framework import viewsets from rest_framework.settings import perform_import GALAXY_EXCEPTION_HANDLER = perform_import( settings.GALAXY_EXCEPTION_HANDLER, 'GALAXY_EXCEPTION_HANDLER' ) GALAXY_AUTHENTICATION_CLASSES = perform_import( settings.GALAXY_AUTHENTICATION_CLASSES, 'GALAXY_AUTHENTICATION_CLASSES' ) GALAXY_PAGINATION_CLASS = perform_import( settings.GALAXY_PAGINATION_CLASS, 'GALAXY_PAGINATION_CLASS' ) class _MustImplementPermission(permissions.BasePermission): def has_permission(self, request, view): raise NotImplementedError("subclass must implement permission_classes") class LocalSettingsMixin: authentication_classes = GALAXY_AUTHENTICATION_CLASSES pagination_class = GALAXY_PAGINATION_CLASS
def get_token_model(): return perform_import( oauth2_client_settings.OAUTH2_CLIENT_TOKEN_MODEL, oauth2_client_settings.OAUTH2_CLIENT_TOKEN_MODEL_NAME)
if PANDAS_RENDERERS is None: PANDAS_RENDERERS = ( "rest_pandas.renderers.PandasCSVRenderer", "rest_pandas.renderers.PandasTextRenderer", "rest_pandas.renderers.PandasJSONRenderer", "rest_pandas.renderers.PandasExcelRenderer", "rest_pandas.renderers.PandasOldExcelRenderer", "rest_pandas.renderers.PandasPNGRenderer", "rest_pandas.renderers.PandasSVGRenderer", ) if "rest_pandas" in settings.INSTALLED_APPS: DEFAULT_TEMPLATE = True PANDAS_RENDERERS = ( "rest_pandas.renderers.PandasHTMLRenderer", ) + PANDAS_RENDERERS PANDAS_RENDERERS = perform_import(PANDAS_RENDERERS, "PANDAS_RENDERERS") class PandasMixin(object): pandas_serializer_class = PandasSerializer def with_list_serializer(self, cls): meta = getattr(cls, 'Meta', object) if getattr(meta, 'list_serializer_class', None): return cls class SerializerWithListSerializer(cls): class Meta(meta): list_serializer_class = self.pandas_serializer_class return SerializerWithListSerializer
def init_checkers(): checker_classes = perform_import(TOKEN_CHECKERS, 'TOKEN_CHECKERS') return map(lambda klass: klass(), checker_classes)
SimpleSerializer, PandasSerializer, USE_LIST_SERIALIZERS ) PANDAS_RENDERERS = getattr(settings, "PANDAS_RENDERERS", None) if PANDAS_RENDERERS is None: PANDAS_RENDERERS = ( "rest_pandas.renderers.PandasCSVRenderer", "rest_pandas.renderers.PandasTextRenderer", "rest_pandas.renderers.PandasJSONRenderer", "rest_pandas.renderers.PandasExcelRenderer", "rest_pandas.renderers.PandasOldExcelRenderer", "rest_pandas.renderers.PandasPNGRenderer", "rest_pandas.renderers.PandasSVGRenderer", ) PANDAS_RENDERERS = perform_import(PANDAS_RENDERERS, "PANDAS_RENDERERS") class PandasMixin(object): renderer_classes = PANDAS_RENDERERS pandas_serializer_class = PandasSerializer paginate_by = None # DRF 2 pagination_class = None # DRF 3 def with_list_serializer(self, cls): if not USE_LIST_SERIALIZERS: # Django REST Framework 2 used the instance serializer for lists class SerializerWithListSerializer( self.pandas_serializer_class, cls): pass
"unicef_rest_export.renderers.ExportCSVRenderer", "unicef_rest_export.renderers.ExportJSONRenderer", "unicef_rest_export.renderers.ExportOpenXMLRenderer", "unicef_rest_export.renderers.ExportExcelRenderer", "unicef_rest_export.renderers.ExportPDFRenderer", "unicef_rest_export.renderers.ExportPDFTableRenderer", "unicef_rest_export.renderers.ExportDocxRenderer", "unicef_rest_export.renderers.ExportDocxTableRenderer", ) if "unicef_rest_export" in settings.INSTALLED_APPS: DEFAULT_TEMPLATE = True EXPORT_RENDERERS = ( "unicef_rest_export.renderers.ExportHTMLRenderer", ) + EXPORT_RENDERERS EXPORT_RENDERERS = perform_import(EXPORT_RENDERERS, "EXPORT_RENDERERS") class ExportMixin(object): export_serializer_class = ExportSerializer def with_list_serializer(self, cls): meta = getattr(cls, 'Meta', object) if getattr(meta, 'list_serializer_class', None): return cls class SerializerWithListSerializer(cls): class Meta(meta): list_serializer_class = self.export_serializer_class return SerializerWithListSerializer
def init_checkers(): checker_classes = perform_import(token_settings.TOKEN_CHECKERS, 'TOKEN_CHECKERS') return list(map(lambda klass: klass(), checker_classes))