def process_step(self, form): """Dynamically add forms from provider's setup_ui""" if form.__class__ == CredentialForm: # Import provider based on form # also check in form if class exists and is subclass of BaseProvider _form_class = path_to_class( form.cleaned_data.get('credential_type')) if issubclass(_form_class, ModelForm): # pylint: disable=no-member self.form_list.update( {str(int(self.steps.current) + 1): _form_class}) return self.get_form_step_data(form)
def related_models(context, model_path): """Return list of models which have a Relationship to current user""" request = context.get('request', None) if not request: # No Request -> no user -> return empty return [] user = request.user model = path_to_class(model_path) if not issubclass(model, UserAcquirable): # model_path is not actually a module # so we can't assume that it's usable return [] return model.objects.filter(users__in=[user])
def run(self, action: ProviderAction, model: str, model_pk, **kwargs): """Main Provider handler. This function is called as a task from `post_save`, `pre_delete` and `m2m_changed`. Function uses `ProviderMultiplexer` Args: action (ProviderAction): [description] model (str): [description] model_pk ([type]): [description] """ from supervisr.core.providers.tasks import get_instance self.prepare(**kwargs) model_class = path_to_class(model) instance = get_instance(model_class, model_pk) LOGGER.debug("provider_signal_handler %r", action) task_group = group(self.get_model_handlers(action, instance, **kwargs)) return task_group()
def clean_credentials(self): """Import Provider and check if credentials are compatible""" # Import provider based on form valid_providers = get_providers(path=True) if self.cleaned_data.get('provider_path') not in valid_providers: LOGGER.warning('selected provider not in valid_providers list') raise Http404 # also check in form if class exists and is subclass of BaseProvider _class = path_to_class(self.cleaned_data.get('provider_path')) # Get credentials if self.cleaned_data.get('credentials').owner != self.request.user: LOGGER.warning( "selected credentials owner doesn't match request user") raise Http404 credentials = self.cleaned_data.get('credentials').cast() # Check if credentials work with provider prov_inst = _class(credentials) LOGGER.info("About to provider.check_credentials") prov_inst.check_credentials(credentials) return self.cleaned_data.get('credentials')
def provider_resolve_helper(provider_pk: int, model_path: str, model_pk) -> Iterable: """Helper function to do all the actual work in tasks, except for error handling""" # multiplexer is used to get responsible translator multiplexer = ProviderMultiplexer() # Convert to actual python class since we only pass the path # so we don't have to use pickle serialization model_class = path_to_class(model_path) # Get root provider to get translator from root_provider = get_instance(ProviderInstance, provider_pk).provider # Lookup model instance from DB model_instance = get_instance(model_class, model_pk) # Do the actual translator lookup translator = multiplexer.get_translator(model_instance, root_provider) if not translator: return [] # Convert to provider_object, this fuction wraps the errors # and converts them to SupervisrProviderException try: return translator.to_external(model_instance) except Exception as exc: # noqa raise SupervisrProviderException from exc
def get_instance(self) -> QuerySet: query_set = self.model.objects.filter(uuid=self.kwargs.get('uuid'), owner=self.request.user) # Get form class from credential instance self.form = path_to_class(query_set.first().cast().form) return query_set