Example #1
0
def url_mptt(url_string, name, settings):
    '''
    This wrapper creates and returns a usual url() object, and additionally sets get_absolute_url() methods for all models mentioned in settings
    '''

    leaf_model = import_by_path(settings['leaf']['model'])
    node_model = import_by_path(settings['node']['model'])

    # ---- Set up get_absolute_url() for nodes and leaves ----
    def get_absolute_url(instance):
        slug_list = []
        if isinstance(instance, leaf_model):
            # Model is leaf
            slug_list.append(getattr(instance, settings['leaf'].get('slug_field', 'slug')))
            instance = getattr(instance, settings['leaf'].get('parent', 'parent'))
        while instance is not None:
            slug_list.append(getattr(instance, settings['node'].get('slug_field', 'slug')))
            instance = getattr(instance, settings['node'].get('parent', 'parent'))

        return reverse(name, kwargs={'url': '/'.join(reversed(slug_list))})

    leaf_model.get_absolute_url = get_absolute_url
    node_model.get_absolute_url = get_absolute_url

    return url(url_string, 'mptt_urls.views.process_url', {'settings': settings}, name=name)
Example #2
0
    def handle(self, *args, **kwargs):
        # Initialize pubsub helper.
        pubsub = PubSub()

        if DIRECTOR_ENABLED:
            logger.info('Starting director.')
            pubsub.init_director()

        if FORWARDER_ENABLED:
            logger.info('Starting forwarder.')
            pubsub.init_forwarder()

        # Get factories for connection and tornado webapp.
        authenticator_factory = import_by_path(AUTHENTICATOR_FACTORY)
        connection_factory = import_by_path(CONNECTION_FACTORY)
        webapp_factory = import_by_path(WEBAPP_FACTORY)

        # Create app and listen on SEVER_PORT
        app = webapp_factory(
            connection_factory(authenticator_factory(), pubsub))
        app.listen(SERVER_PORT)

        loop = ioloop.IOLoop().instance()
        try:
            logger.info('Starting omnibusd.')
            loop.start()
        except KeyboardInterrupt:
            logger.info('Received KeyboardInterrup, stopping omnibusd.')
            loop.stop()
Example #3
0
def _create_cache(backend, **kwargs):
    try:
        # Try to get the CACHES entry for the given backend name first
        try:
            conf = settings.CACHES[backend]
        except KeyError:
            try:
                # Trying to import the given backend, in case it's a dotted path
                import_by_path(backend)
            except ImproperlyConfigured as e:
                raise InvalidCacheBackendError("Could not find backend '%s': %s" % (
                    backend, e))
            location = kwargs.pop('LOCATION', '')
            params = kwargs
        else:
            params = conf.copy()
            params.update(kwargs)
            backend = params.pop('BACKEND')
            location = params.pop('LOCATION', '')
        backend_cls = import_by_path(backend)
    except (AttributeError, ImportError, ImproperlyConfigured) as e:
        raise InvalidCacheBackendError(
            "Could not find backend '%s': %s" % (backend, e))
    cache = backend_cls(location, params)
    # Some caches -- python-memcached in particular -- need to do a cleanup at the
    # end of a request cycle. If not implemented in a particular backend
    # cache.close is a no-op
    signals.request_finished.connect(cache.close)
    return cache
Example #4
0
 def __init__(self, *args, **kwargs):
     super(DefaultConfig, self).__init__(*args, **kwargs)
     
     # At this point Sellmo will begin to load
     pre_init.send(self)
     
     if celery.enabled:
         import sellmo.celery.integration
         sellmo.celery.integration.setup()
     
     if caching.enabled:
         import sellmo.caching.integration
         sellmo.caching.integration.setup()
     
     # At this point Django models can begin to load
     # by Sellmo's core modules.
     params.loader = Loader()
     
     # Also at this point, chains can be registered
     # by Sellmo's core modules.
     params.chainer = Chainer()
     
     # Load core modules
     for module in self.core_modules:
         import_by_path(module)
         
     # Remaining models will now be imported by Django
     # This does not happen in the desired order. Because
     # we want to enable a correct Django Template order, Sellmo
     # apps need to be configured in a reverse manner. As
     # we don't control the loading of models.py modules, we need
     # to adjust Sellmo's loading mechanism.
     params._loader = params.loader
     params.loader = DummyLoader()
Example #5
0
def _create_cache(backend, **kwargs):
    try:
        # Try to get the CACHES entry for the given backend name first
        try:
            conf = settings.CACHES[backend]
        except KeyError:
            try:
                # Trying to import the given backend, in case it's a dotted path
                import_by_path(backend)
            except ImproperlyConfigured as e:
                raise InvalidCacheBackendError(
                    "Could not find backend '%s': %s" % (backend, e))
            location = kwargs.pop('LOCATION', '')
            params = kwargs
        else:
            params = conf.copy()
            params.update(kwargs)
            backend = params.pop('BACKEND')
            location = params.pop('LOCATION', '')
        backend_cls = import_by_path(backend)
    except (AttributeError, ImportError, ImproperlyConfigured) as e:
        raise InvalidCacheBackendError("Could not find backend '%s': %s" %
                                       (backend, e))
    cache = backend_cls(location, params)
    # Some caches -- python-memcached in particular -- need to do a cleanup at the
    # end of a request cycle. If not implemented in a particular backend
    # cache.close is a no-op
    signals.request_finished.connect(cache.close)
    return cache
Example #6
0
 def from_model(cls, model):
     """
     Feed me a model, get a ModelState representing it out.
     """
     # Deconstruct the fields
     fields = []
     for field in model._meta.local_fields:
         name, path, args, kwargs = field.deconstruct()
         field_class = import_by_path(path)
         try:
             fields.append((name, field_class(*args, **kwargs)))
         except TypeError as e:
             raise TypeError("Couldn't reconstruct field %s on %s.%s: %s" %
                             (
                                 name,
                                 model._meta.app_label,
                                 model._meta.object_name,
                                 e,
                             ))
     for field in model._meta.local_many_to_many:
         name, path, args, kwargs = field.deconstruct()
         field_class = import_by_path(path)
         try:
             fields.append((name, field_class(*args, **kwargs)))
         except TypeError as e:
             raise TypeError("Couldn't reconstruct m2m field %s on %s: %s" %
                             (
                                 name,
                                 model._meta.object_name,
                                 e,
                             ))
     # Extract the options
     options = {}
     for name in DEFAULT_NAMES:
         # Ignore some special options
         if name in ["apps", "app_label"]:
             continue
         elif name in model._meta.original_attrs:
             if name == "unique_together":
                 ut = model._meta.original_attrs["unique_together"]
                 options[name] = set(normalize_unique_together(ut))
             else:
                 options[name] = model._meta.original_attrs[name]
     # Make our record
     bases = tuple(
         ("%s.%s" %
          (base._meta.app_label,
           base._meta.model_name) if hasattr(base, "_meta") else base)
         for base in model.__bases__
         if (not hasattr(base, "_meta") or not base._meta.abstract))
     if not bases:
         bases = (models.Model, )
     return cls(
         model._meta.app_label,
         model._meta.object_name,
         fields,
         options,
         bases,
     )
    def test_import_error_traceback(self):
        """Test preserving the original traceback on an ImportError."""
        try:
            import_by_path("test_module.bad_module.content")
        except ImproperlyConfigured:
            traceback = sys.exc_info()[2]

        self.assertIsNotNone(traceback.tb_next.tb_next, "Should have more than the calling frame in the traceback.")
    def test_import_error_traceback(self):
        """Test preserving the original traceback on an ImportError."""
        try:
            import_by_path('test_module.bad_module.content')
        except ImproperlyConfigured:
            traceback = sys.exc_info()[2]

        self.assertIsNotNone(traceback.tb_next.tb_next,
            'Should have more than the calling frame in the traceback.')
Example #9
0
def mail():
    # Get inbound mail
    path = getattr(settings, u'EMAIL_INBOUND_TRANSPORT', None)
    if path:
        klass = import_by_path(path)
        with klass() as transport:
            messages = transport.get_messages()
            while True:
                try:
                    with transaction.atomic():
                        message = next(messages)
                        nop()  # To let tests raise testing exception here.
                    cron_logger.info(u'Received email: %s' % repr(message))
                except StopIteration:
                    break
                except Exception:
                    cron_logger.error(u'Receiving emails failed:\n%s' %
                                      traceback.format_exc())
                    break

    # Process inbound mail; At most 10 messages in one batch
    messages = (Message.objects.inbound().not_processed().order_by_pk().
                prefetch_related(Message.prefetch_recipients()))[:10]
    for message in messages:
        try:
            with transaction.atomic():
                message.processed = utc_now()
                message.save(update_fields=[u'processed'])
                message_received.send(sender=None, message=message)
                nop()  # To let tests raise testing exception here.
            cron_logger.info(u'Processed received email: %s' % repr(message))
        except Exception:
            cron_logger.error(u'Processing received email failed: %s\n%s' %
                              (repr(message), traceback.format_exc()))

    # Send outbound mail; At most 10 messages in one batch
    path = getattr(settings, u'EMAIL_OUTBOUND_TRANSPORT', None)
    if path:
        messages = (Message.objects.outbound().not_processed().order_by_pk(
        ).prefetch_related(Message.prefetch_recipients()).prefetch_related(
            Message.prefetch_attachments()))[:10]
        if messages:
            klass = import_by_path(path)
            with klass() as transport:
                for message in messages:
                    try:
                        with transaction.atomic():
                            transport.send_message(message)
                            message.processed = utc_now()
                            message.save(update_fields=[u'processed'])
                            message_sent.send(sender=None, message=message)
                            nop()  # To let tests raise testing exception here.
                        cron_logger.info(u'Sent email: %s' % repr(message))
                    except Exception:
                        cron_logger.error(
                            u'Seding email failed: %s\n%s' %
                            (repr(message), traceback.format_exc()))
Example #10
0
def bind_referrer_to_member(request, service):
    app = service.app
    if service.project_name_slug == 'playground':
        referrer_bind_callback = import_by_path(
            'playground.views.referee_registration_callback')
        referrer_bind_callback(request, service=service)
    elif app.referrer_bind_callback:
        referrer_bind_callback = import_by_path(app.referrer_bind_callback)
        referrer_bind_callback(request, service=service)
Example #11
0
 def from_model(cls, model):
     """
     Feed me a model, get a ModelState representing it out.
     """
     # Deconstruct the fields
     fields = []
     for field in model._meta.local_fields:
         name, path, args, kwargs = field.deconstruct()
         field_class = import_by_path(path)
         try:
             fields.append((name, field_class(*args, **kwargs)))
         except TypeError as e:
             raise TypeError("Couldn't reconstruct field %s on %s.%s: %s" % (
                 name,
                 model._meta.app_label,
                 model._meta.object_name,
                 e,
             ))
     for field in model._meta.local_many_to_many:
         name, path, args, kwargs = field.deconstruct()
         field_class = import_by_path(path)
         try:
             fields.append((name, field_class(*args, **kwargs)))
         except TypeError as e:
             raise TypeError("Couldn't reconstruct m2m field %s on %s: %s" % (
                 name,
                 model._meta.object_name,
                 e,
             ))
     # Extract the options
     options = {}
     for name in DEFAULT_NAMES:
         # Ignore some special options
         if name in ["app_cache", "app_label"]:
             continue
         elif name in model._meta.original_attrs:
             if name == "unique_together":
                 ut = model._meta.original_attrs["unique_together"]
                 options[name] = set(normalize_unique_together(ut))
             else:
                 options[name] = model._meta.original_attrs[name]
     # Make our record
     bases = tuple(
         ("%s.%s" % (base._meta.app_label, base._meta.object_name.lower()) if hasattr(base, "_meta") else base)
         for base in model.__bases__
         if (not hasattr(base, "_meta") or not base._meta.abstract)
     )
     if not bases:
         bases = (models.Model, )
     return cls(
         model._meta.app_label,
         model._meta.object_name,
         fields,
         options,
         bases,
     )
Example #12
0
    def test_import_by_path(self):
        cls = import_by_path("django.utils.module_loading.import_by_path")
        self.assertEqual(cls, import_by_path)

        # Test exceptions raised
        for path in ("no_dots_in_path", "unexistent.path", "tests.regressiontests.utils.unexistent"):
            self.assertRaises(ImproperlyConfigured, import_by_path, path)

        with self.assertRaises(ImproperlyConfigured) as cm:
            import_by_path("unexistent.module.path", error_prefix="Foo")
        self.assertTrue(str(cm.exception).startswith("Foo"))
    def test_import_by_path(self):
        cls = import_by_path('django.utils.module_loading.import_by_path')
        self.assertEqual(cls, import_by_path)

        # Test exceptions raised
        for path in ('no_dots_in_path', 'unexistent.path', 'utils_tests.unexistent'):
            self.assertRaises(ImproperlyConfigured, import_by_path, path)

        with self.assertRaises(ImproperlyConfigured) as cm:
            import_by_path('unexistent.module.path', error_prefix="Foo")
        self.assertTrue(str(cm.exception).startswith('Foo'))
Example #14
0
 def filters(self):
     filter_name = self.model.__name__ + "FilterSet"
     try:
         FilterSet = import_by_path(get_app().__name__[:-6] + "filtersets.%s" % filter_name)
     except: pass
     try:
         FilterSet = import_by_path(get_app().__name__[:-6] + "controls.%s" % filter_name)
     except:
         class FilterSet(django_filters.FilterSet):
             class Meta:
                 model = self.model
     model_filters = FilterSet(self.request.GET, queryset=self.object_list)
     self.object_list = model_filters.qs
     return model_filters
def stats_container(clazz):
    if not issubclass(clazz, models.Model):
        return clazz

    if not hasattr(clazz, 'processors'):
        clazz.processors = list()
        for processor in CONTENT_INTERACTIONS_STATS_PROCESSORS:
            processor_class = import_by_path(processor)
            if not issubclass(processor_class, BaseProcessor):
                continue
            processor = import_by_path(processor)(clazz)
            clazz = processor.stats_clazz
            clazz.processors.append(processor)

    return clazz
Example #16
0
 def __new__(cls, name, bases, attrs):
     bases = tuple(
         import_by_path(sgm)
         for sgm in settings.CASCADE_SEGMENTATION_MIXINS) + bases
     new_class = super(SegmentationAdminMetaclass,
                       cls).__new__(cls, name, bases, attrs)
     return new_class
def get_backends():
    backends = {}
    for backend_name, backend_path in settings.SUBSCRIPTION_BACKENDS.items():
        backends[backend_name] = import_by_path(backend_path)
    if not backends:
        raise ImproperlyConfigured('No subscription backends have been defined. Does SUBSCRIPTION_BACKENDS contain anything?')
    return backends
Example #18
0
 def load_tour_class(self):
     """
     Imports and returns the tour class.
     :return: The tour class instance determined by `tour_class`
     :rtype: BaseTour
     """
     return import_by_path(self.tour_class)(self)
Example #19
0
def translate_tasks_by_id_list(model_path, id_list):
    """Takes a model path and a list of ids and generates translation tasks"""
    model_cls = import_by_path(model_path)

    objs = list(model_cls.objects.filter(id__in=id_list))
    for obj in objs:
        create_translation_tasks(obj)
Example #20
0
def get_plugin():
    """
    Return an instance of a dj-stripe plugin.
    
    """
    mod = import_by_path(DJSTRIPE_CUSTOMER_RELATED_MODEL_PLUGIN)    
    return mod()
Example #21
0
    def load_middleware(self):
        """
        Populate middleware lists from settings.MIDDLEWARE_CLASSES.

        Must be called after the environment is fixed (see __call__ in subclasses).
        """
        self._view_middleware = []
        self._template_response_middleware = []
        self._response_middleware = []
        self._exception_middleware = []

        request_middleware = []
        for middleware_path in settings.MIDDLEWARE_CLASSES:
            mw_class = import_by_path(middleware_path)
            try:
                mw_instance = mw_class()
            except MiddlewareNotUsed:
                continue

            if hasattr(mw_instance, 'process_request'):
                request_middleware.append(mw_instance.process_request)
            if hasattr(mw_instance, 'process_view'):
                self._view_middleware.append(mw_instance.process_view)
            if hasattr(mw_instance, 'process_template_response'):
                self._template_response_middleware.insert(0, mw_instance.process_template_response)
            if hasattr(mw_instance, 'process_response'):
                self._response_middleware.insert(0, mw_instance.process_response)
            if hasattr(mw_instance, 'process_exception'):
                self._exception_middleware.insert(0, mw_instance.process_exception)

        # We only assign to this when initialization is complete as it is used
        # as a flag for initialization being complete.
        self._request_middleware = request_middleware
Example #22
0
    def __init__(self, form, **kwargs):
        import inspect
        if inspect.isclass(form) and issubclass(form, forms.Form):
            form_class = form
        elif callable(form):
            form_class = form()
            self.form = form_class()
        elif isinstance(form, six.string_types):
            from django.utils import module_loading
            if hasattr(module_loading, 'import_by_path'):
                form_class = module_loading.import_by_path(form)
            else:
                form_class = module_loading.import_string(form)
        self.form = form_class()

        # Set the widget and initial data
        kwargs['widget'] = FormFieldWidget([f for f in self.form])
        kwargs['initial'] = [f.field.initial for f in self.form]
        # The field it self should not be required, this allows us to
        # have optional fields in a sub form
        kwargs['required'] = False

        self.max_length = kwargs.pop('max_length', None)

        super(FormField, self).__init__(**kwargs)

        self.fields = [f.field for f in self.form]
Example #23
0
    def template_engine_class(self):
        if self.template_engine == self.DJANGO:
            return DjangoTemplate
        elif self.template_engine == self.JINJA2:

            return module_loading.import_by_path(
                dotted_path=settings.JINJA2_TEMPLATE_CLASS)
Example #24
0
 def from_model(cls, model):
     """
     Feed me a model, get a ModelState representing it out.
     """
     # Deconstruct the fields
     fields = []
     for field in model._meta.local_fields:
         name, path, args, kwargs = field.deconstruct()
         field_class = import_by_path(path)
         fields.append((name, field_class(*args, **kwargs)))
     # Extract the options
     options = {}
     for name in DEFAULT_NAMES:
         # Ignore some special options
         if name in ["app_cache", "app_label"]:
             continue
         elif name in model._meta.original_attrs:
             if name == "unique_together":
                 options[name] = set(model._meta.original_attrs["unique_together"])
             else:
                 options[name] = model._meta.original_attrs[name]
     # Make our record
     bases = tuple(model for model in model.__bases__ if (not hasattr(model, "_meta") or not model._meta.abstract))
     if not bases:
         bases = (models.Model, )
     return cls(
         model._meta.app_label,
         model._meta.object_name,
         fields,
         options,
         bases,
     )
Example #25
0
 def get_all_modifiers(self):
     """
     Returns all registered modifiers of this shop instance.
     """
     if not self.USE_CACHE or not self._modifiers_list:
         self._modifiers_list = [import_by_path(mc)() for mc in settings.CART_MODIFIERS]
     return self._modifiers_list
Example #26
0
    def __init__(self, backends=None):
        if not backends:
            backends = meho_settings.MEHO_PUBLISHER_BACKENDS
        self.backends = {}

        for name, backend in backends.items():
            self.backends[name] = import_by_path(backend)
Example #27
0
    def load_filters(self):
        for filter_name in self.filter_tray:
            filter_obj = self.filter_tray[filter_name]
            if not isinstance(filter_obj, Filter):
                kwargs = filter_obj.setdefault('kwargs', {})
                kwargs.update({
                    'name': filter_name
                })
                filter_obj = import_by_path(filter_obj['type'])(
                    *filter_obj.setdefault('args', []), **kwargs
                )
                self.filter_tray[filter_name] = self.catalog_config['FILTER_TRAY'][filter_name] = filter_obj

            if isinstance(filter_obj, MultipleArgumentFilterMixin):
                children = filter_obj.children
                children_values = {}
                for child, role in children.iteritems():
                    child_value = self.complete_request_dict.get(child)
                    if child_value:
                        children_values[role] = child_value
                if len(children_values) == len(filter_obj.required_args):
                    filter_obj.set_args(**children_values)
                    self.filters.add_filter(filter_obj)
            else:
                value = self.complete_request_dict.getlist(filter_name)
                value = [val for val in value if val]
                if value:
                    value = ",".join(value)
                    filter_obj.set_args(value)
                    self.filters.add_filter(filter_obj)
def get_all_remote_methods(resolver=None, ns_prefix=''):
    """
    Returns a dictionary to be used for calling ``djangoCall.configure()``, which itself extends the
    Angular API to the client, offering him to call remote methods.
    """
    if not resolver:
        resolver = get_resolver(get_urlconf())
    result = {}
    for name in resolver.reverse_dict.keys():
        if not isinstance(name, six.string_types):
            continue
        try:
            url = reverse(ns_prefix + name)
            resmgr = resolve(url)
            ViewClass = import_by_path('{0}.{1}'.format(
                resmgr.func.__module__, resmgr.func.__name__))
            if isclass(ViewClass) and issubclass(ViewClass, JSONResponseMixin):
                result[name] = _get_remote_methods_for(ViewClass, url)
        except (NoReverseMatch, ImproperlyConfigured):
            pass
    for namespace, ns_pattern in resolver.namespace_dict.items():
        sub_res = get_all_remote_methods(ns_pattern[1],
                                         ns_prefix + namespace + ':')
        if sub_res:
            result[namespace] = sub_res
    return result
def stop_workflow(workflow_dict, task=None):
    LOG.info("Running undo...")

    if 'steps' not in workflow_dict:
        return False

    if 'exceptions' not in workflow_dict:
        workflow_dict['exceptions'] = {}
        workflow_dict['exceptions']['traceback'] = []
        workflow_dict['exceptions']['error_codes'] = []

    workflow_dict['total_steps'] = len(workflow_dict['steps'])
    if 'step_counter' not in workflow_dict:
        workflow_dict['step_counter'] = len(workflow_dict['steps'])
    workflow_dict['msgs'] = []
    workflow_dict['created'] = False

    try:

        for step in workflow_dict['steps'][::-1]:

            my_class = import_by_path(step)
            my_instance = my_class()

            time_now = str(time.strftime("%m/%d/%Y %H:%M:%S"))

            msg = "\n%s - Rollback Step %i of %i - %s" % (
                time_now, workflow_dict['step_counter'],
                workflow_dict['total_steps'], str(my_instance))

            LOG.info(msg)

            workflow_dict['step_counter'] -= 1

            if task:
                workflow_dict['msgs'].append(msg)
                task.update_details(persist=True, details=msg)

            my_instance.undo(workflow_dict)

            if task:
                task.update_details(persist=True, details="DONE!")

        return True
    except Exception as e:
        LOG.info("Exception: {}".format(e))

        if not workflow_dict['exceptions']['error_codes'] or not workflow_dict[
                'exceptions']['traceback']:
            traceback = full_stack()
            workflow_dict['exceptions']['error_codes'].append(DBAAS_0001)
            workflow_dict['exceptions']['traceback'].append(traceback)

        LOG.warn("\n".join(
            ": ".join(error)
            for error in workflow_dict['exceptions']['error_codes']))
        LOG.warn("\nException Traceback\n".join(
            workflow_dict['exceptions']['traceback']))

        return False
Example #30
0
    def geturl(ref, url_name=None, user=None):
        if isinstance(ref, Flow):
            url_ref = "{}:{}".format(ref.namespace, url_name if url_name else "index")
            return reverse(url_ref)
        elif isinstance(ref, AbstractProcess):
            kwargs, url_ref = {}, "{}:{}".format(ref.flow_cls.instance.namespace, url_name if url_name else "index")
            if url_name in ["details", "cancel"]:
                kwargs["process_pk"] = ref.pk
            return reverse(url_ref, kwargs=kwargs)
        elif isinstance(ref, AbstractTask):
            return ref.flow_task.get_task_url(ref, url_type=url_name if url_name else "guess", user=user)
        else:
            try:
                app_label, flow_cls_path = ref.split("/")
            except ValueError:
                raise TemplateSyntaxError(
                    "Flow reference string should  looks like 'app_label/FlowCls' but '{}'".format(ref)
                )

            app_package = get_app_package(app_label)
            if app_package is None:
                raise TemplateSyntaxError("{} app not found".format(app_label))

            flow_cls = import_by_path("{}.flows.{}".format(app_package, flow_cls_path))
            url_ref = "{}:{}".format(flow_cls.instance.namespace, url_name if url_name else "index")
            return reverse(url_ref)
Example #31
0
    def geturl(ref, url_name=None, user=None):
        if isinstance(ref, Flow):
            url_ref = '{}:{}'.format(ref.namespace,
                                     url_name if url_name else 'index')
            return reverse(url_ref)
        elif isinstance(ref, AbstractProcess):
            kwargs, url_ref = {}, '{}:{}'.format(
                ref.flow_cls.instance.namespace,
                url_name if url_name else 'index')
            '''
            And I thought my code was hacky.
            '''
            if url_name in ['details', 'cancel']:
                kwargs['process_pk'] = ref.pk
            return reverse(url_ref, kwargs=kwargs)
        elif isinstance(ref, AbstractTask):
            return ref.flow_task.get_task_url(
                ref, url_type=url_name if url_name else 'guess', user=user)
        else:
            try:
                app_label, flow_cls_path = ref.split('/')
            except ValueError:
                raise TemplateSyntaxError(
                    "Flow reference string should  looks like 'app_label/FlowCls' but '{}'"
                    .format(ref))

            app_package = get_app_package(app_label)
            if app_package is None:
                raise TemplateSyntaxError("{} app not found".format(app_label))

            flow_cls = import_by_path('{}.flows.{}'.format(
                app_package, flow_cls_path))
            url_ref = '{}:{}'.format(flow_cls.instance.namespace,
                                     url_name if url_name else 'index')
            return reverse(url_ref)
Example #32
0
 def __new__(cls, name, bases, attrs):
     if name in cls.plugins_with_extrafields:
         ExtraFieldsMixin.media = media_property(ExtraFieldsMixin)
         bases = (ExtraFieldsMixin, ) + bases
     if name in cls.plugins_with_sharables:
         SharableGlossaryMixin.media = media_property(SharableGlossaryMixin)
         bases = (SharableGlossaryMixin, ) + bases
         attrs['fields'] += (
             ('save_shared_glossary', 'save_as_identifier'),
             'shared_glossary',
         )
         attrs['sharable_fields'] = cls.plugins_with_sharables[name]
         base_model = SharableCascadeElement
     else:
         base_model = CascadeElement
     if name == 'SegmentPlugin':
         # SegmentPlugin shall additionally inherit from configured mixin classes
         bases = tuple(
             import_by_path(mc)
             for mc in settings.CASCADE_SEGMENTATION_MIXINS) + bases
     model_mixins = attrs.pop('model_mixins', ())
     attrs['model'] = CascadePluginBaseMetaclass.create_model(
         name, model_mixins, base_model)
     return super(CascadePluginBaseMetaclass,
                  cls).__new__(cls, name, bases, attrs)
Example #33
0
 def setUpClass(cls):
     try:
         cls.selenium = import_by_path(cls.webdriver_class)()
     except Exception as e:
         raise unittest.SkipTest('Selenium webdriver "%s" not installed or '
                                 'not operational: %s' % (cls.webdriver_class, str(e)))
     super(JavascriptI18nTests, cls).setUpClass()
Example #34
0
    def _get_auth_handler(self, name, request):
        auth_scheme = request.headers['WWW-Authenticate'].split(' ')[0].lower()
        credentials = self.credentials(name)
        origin = self.netloc(name)

        # format credentials into kwargs style if provided within the file name
        if credentials is not None:
            credentials = {'username': credentials[0], 'password': credentials[1]}

        # if credentials are not provided within the file name, we try to get
        # them from the credential manager
        else:
            try:
                print 
                credentials = self._credential_set.get(scheme=auth_scheme, origin=origin).data
            except Credentials.DoesNotExist:
                credentials = None

        auth_class = meho_settings.MEHO_AUTH_BACKENDS.get(auth_scheme, None)
        if credentials and auth_class:
            return import_by_path(auth_class)(**credentials)
        else:
            raise ImproperlyConfigured(
                'No authentication credentials for "%(origin)s" with scheme '
                '"%(scheme)s". Either provide credentials within the file '
                'name or set credentials for (%(origin)s, %(scheme)s).' % {
                    'origin': origin,
                    'scheme': auth_scheme
                })
Example #35
0
def forms_widget_autocomplete(request):
    u'''
    Поставляет данные для поля - автоподстановка
    Забирает данные у функции-обработчика
    и отдает их для списка автодополнения
    '''

    if not request.is_ajax():
        return HttpResponseForbidden(u'Возможно обращение только по ajax')

    params = request.GET

    term = params.get('q')
    handler_path = params.get('handler_path')

    # Чистим лишние параметры для передачи в обработчик
    _params = {}
    for k in params.keys():
        if k in ('q', 'handler_path', '_'):
            continue
        _params[k] = params.get(k)

    handle_function = import_by_path(handler_path)
    res = handle_function(request=request, term=term, params=_params)

    return res
    def save(self, *args, **kwargs):
        """
        Builds the objects managed by the template before saving the template.
        """
        super(SmartManager, self).save(*args, **kwargs)

        smart_manager = import_by_path(self.smart_manager_class)(self.template)
        primary_built_obj = smart_manager.build()

        # Do an update of the primary object type and id after it has been built. We use an update since
        # you can't call save in a save method. We may want to put this in post_save as well later.
        if primary_built_obj:
            self.primary_obj_type = ContentType.objects.get_for_model(primary_built_obj)
            self.primary_obj_id = primary_built_obj.id
            SmartManager.objects.filter(id=self.id).update(
                primary_obj_type=self.primary_obj_type, primary_obj_id=primary_built_obj.id)

        # Sync all of the objects from the built template
        sync(self.smartmanagerobject_set.all(), [
            SmartManagerObject(
                smart_manager=self,
                model_obj_id=built_obj.id,
                model_obj_type=ContentType.objects.get_for_model(built_obj, for_concrete_model=False),
            )
            for built_obj in smart_manager.built_objs
        ], ['smart_manager_id', 'model_obj_id', 'model_obj_type_id'])
Example #37
0
    def geturl(ref, url_name=None, user=None):
        if isinstance(ref, Flow):
            url_ref = '{}:{}'.format(ref.namespace, url_name if url_name else 'index')
            return reverse(url_ref)
        elif isinstance(ref, AbstractProcess):
            kwargs, url_ref = {}, '{}:{}'.format(ref.flow_cls.instance.namespace, url_name if url_name else 'index')
            '''
            And I thought my code was hacky.
            '''
            if url_name in ['details', 'cancel']:
                kwargs['process_pk'] = ref.pk
            return reverse(url_ref, kwargs=kwargs)
        elif isinstance(ref, AbstractTask):
            return ref.flow_task.get_task_url(ref, url_type=url_name if url_name else 'guess', user=user)
        else:
            try:
                app_label, flow_cls_path = ref.split('/')
            except ValueError:
                raise TemplateSyntaxError(
                    "Flow reference string should  looks like 'app_label/FlowCls' but '{}'".format(ref))

            app_package = get_app_package(app_label)
            if app_package is None:
                raise TemplateSyntaxError("{} app not found".format(app_label))

            flow_cls = import_by_path('{}.flows.{}'.format(app_package, flow_cls_path))
            url_ref = '{}:{}'.format(flow_cls.instance.namespace, url_name if url_name else 'index')
            return reverse(url_ref)
Example #38
0
 def run_creation_hook(self):
     creation_hook_name = self.get_creation_hook_name()
     if creation_hook_name:
         logger.info("Running creation hook %s for new job",
                     creation_hook_name)
         creation_hook_function = import_by_path(creation_hook_name)
         creation_hook_function(self)
 def populate(self):
     menu = self.toolbar.get_or_create_menu('segmentation', _("Segmentation"))
     for sgm in settings.CASCADE_SEGMENTATION_MIXINS:
         SegmentationMixin = import_by_path(sgm[1])
         populate_handler = getattr(SegmentationMixin, 'populate_toolbar', None)
         if callable(populate_handler):
             populate_handler(menu, self.request)
Example #40
0
 def setUpClass(cls):
     try:
         cls.selenium = import_by_path(cls.webdriver_class)()
     except Exception as e:
         raise SkipTest('Selenium webdriver "%s" not installed or not '
                        'operational: %s' % (cls.webdriver_class, str(e)))
     super(AdminSeleniumWebDriverTestCase, cls).setUpClass()
Example #41
0
    def handle(self, *args, **options):
        file_ids = args or settings.EXTERNAL_FILES.keys()
        updated = False

        def printout(msg, ending=None):
            if not (options['quiet'] or options['status']):
                self.stdout.write(msg, ending=ending)

        for fid in file_ids:
            try:
                finfo = settings.EXTERNAL_FILES[fid]
            except KeyError:
                raise CommandError('No external file configuration for ' + fid)
            klass = import_by_path(finfo.get('type', DEFAULT_CLASS))
            printout('updating {0}... '.format(fid), ending='')
            result = klass(fid).update(options['force'])
            if result is None:
                printout('already up-to-date')
            else:
                updated = True
                printout('done')

        if options['status']:
            if updated:
                self.stdout.write('updated')
            else:
                self.stdout.write('up-to-date')
Example #42
0
    def render(self, context):
        # resolve flow reference
        flow_path = self.flow_ref.resolve(context)

        try:
            app_label, flow_cls_path = flow_path.split('/')
        except ValueError:
            raise TemplateSyntaxError(
                "Flow action should looks like app_label/FlowCls")

        app_config = apps.get_app_config(app_label)
        if app_config is None:
            raise TemplateSyntaxError("{} app not found".format(app_label))

        flow_cls = import_by_path('{}.flows.{}'.format(
            app_config.module.__package__, flow_cls_path))

        # resolve url name
        url = self.url_name.resolve(context)

        kwargs = {
            key: value.resolve(context)
            for key, value in self.kwargs.items()
        }

        return reverse(url,
                       current_app=flow_cls._meta.namespace,
                       kwargs=kwargs)
Example #43
0
def find_template_loader(loader):
    if isinstance(loader, (tuple, list)):
        loader, args = loader[0], loader[1:]
    else:
        args = []
    if isinstance(loader, six.string_types):
        TemplateLoader = import_by_path(loader)

        if hasattr(TemplateLoader, "load_template_source"):
            func = TemplateLoader(*args)
        else:
            # Try loading module the old way - string is full path to callable
            if args:
                raise ImproperlyConfigured(
                    "Error importing template source loader %s - can't pass arguments to function-based loader."
                    % loader
                )
            func = TemplateLoader

        if not func.is_usable:
            import warnings

            warnings.warn(
                "Your TEMPLATE_LOADERS setting include %r, but your Python installation doesn't support that type of template loading. Consider removing that line from TEMPLATE_LOADERS."
                % loader
            )
            return None
        else:
            return func
    else:
        raise ImproperlyConfigured('Loader does not define a "load_template" callable template source loader')
def execute(step, workflow_dict, is_rollback, task):
    my_class = import_by_path(step)
    my_instance = my_class()

    time_now = str(time.strftime("%m/%d/%Y %H:%M:%S"))

    kind_of = "Rollback " if is_rollback else ""

    msg = "\n%s - %sStep %i of %i - %s" % (
        time_now, kind_of, workflow_dict['global_step_counter'],
        workflow_dict['total_steps'], str(my_instance))

    LOG.info(msg)

    if task:
        workflow_dict['msgs'].append(msg)
        task.update_details(persist=True, details=msg)

    if is_rollback:
        my_instance.undo(workflow_dict)
    else:
        if not my_instance.do(workflow_dict):
            workflow_dict['status'] = 0
            raise Exception("We caught an error while executing the steps...")
        workflow_dict['status'] = 1

    if task:
        task.update_details(persist=True, details="DONE!")
Example #45
0
    def __init__(self):
        """Setup mako variables and lookup object"""
        from django.conf import settings

        # Set all mako variables based on django settings
        directories = getattr(
            settings,
            'MAKO_TEMPLATE_DIRS',
            settings.TEMPLATE_DIRS
        )
        module_directory = getattr(settings, 'MAKO_MODULE_DIR', None)
        if module_directory is None:
            module_directory = tempfile.mkdtemp()

        output_encoding = getattr(settings, 'MAKO_OUTPUT_ENCODING', 'utf-8')
        encoding_errors = getattr(settings, 'MAKO_ENCODING_ERRORS', 'replace')
        template_class = getattr(settings, 'MAKO_TEMPLATE_CLASS', Template)
        if isinstance(template_class, basestring):
            template_class = import_by_path(template_class)

        global lookup
        lookup = TemplateLookup(
            directories=directories,
            module_directory=module_directory,
            output_encoding=output_encoding,
            encoding_errors=encoding_errors,
            template_class=template_class,
        )

        import djangomako
        djangomako.lookup = lookup
Example #46
0
def translate_tasks_by_id_list(model_path, id_list):
    """Takes a model path and a list of ids and generates translation tasks"""
    model_cls = import_by_path(model_path)

    objs = list(model_cls.objects.filter(id__in=id_list))
    for obj in objs:
        create_translation_tasks(obj)
Example #47
0
def get_caller():
    caller_text = getattr(
        settings,
        'SIMPLEIMAGES_TRANSFORM_CALLER',
        'simpleimages.callers.default'
    )
    return import_by_path(caller_text)
Example #48
0
 def __new__(cls, name, bases, attrs):
     if name in cls.plugins_with_extrafields:
         ExtraFieldsMixin.media = media_property(ExtraFieldsMixin)
         bases = (ExtraFieldsMixin, ) + bases
     if name in cls.plugins_with_sharables:
         SharableGlossaryMixin.media = media_property(SharableGlossaryMixin)
         bases = (SharableGlossaryMixin, ) + bases
         attrs['fields'] += (
             ('save_shared_glossary', 'save_as_identifier'),
             'shared_glossary',
         )
         attrs['sharable_fields'] = cls.plugins_with_sharables[name]
         base_model = SharableCascadeElement
     else:
         base_model = CascadeElement
     if name in getattr(
             settings,
             'CASCADE_PLUGINS_WITH_EXTRA_RENDER_TEMPLATES').keys():
         RenderTemplateMixin.media = media_property(RenderTemplateMixin)
         bases = (RenderTemplateMixin, ) + bases
     model_mixins = attrs.pop('model_mixins', ())
     if name == 'SegmentPlugin':
         # SegmentPlugin shall additionally inherit from configured mixin classes
         model_mixins += tuple(
             import_by_path(mc[0])
             for mc in settings.CASCADE_SEGMENTATION_MIXINS)
     attrs['model'] = create_proxy_model(name, model_mixins, base_model)
     if is_installed('reversion'):
         import reversion
         reversion.register(attrs['model'])
     return super(CascadePluginBaseMetaclass,
                  cls).__new__(cls, name, bases, attrs)
Example #49
0
    def load_middleware(self):
        """
        Populate middleware lists from settings.MIDDLEWARE_CLASSES.

        Must be called after the environment is fixed (see __call__ in subclasses).
        """
        self._view_middleware = []
        self._template_response_middleware = []
        self._response_middleware = []
        self._exception_middleware = []

        request_middleware = []
        for middleware_path in settings.MIDDLEWARE_CLASSES:
            mw_class = import_by_path(middleware_path)
            try:
                mw_instance = mw_class()
            except MiddlewareNotUsed:
                continue

            if hasattr(mw_instance, 'process_request'):
                request_middleware.append(mw_instance.process_request)
            if hasattr(mw_instance, 'process_view'):
                self._view_middleware.append(mw_instance.process_view)
            if hasattr(mw_instance, 'process_template_response'):
                self._template_response_middleware.insert(0, mw_instance.process_template_response)
            if hasattr(mw_instance, 'process_response'):
                self._response_middleware.insert(0, mw_instance.process_response)
            if hasattr(mw_instance, 'process_exception'):
                self._exception_middleware.insert(0, mw_instance.process_exception)

        # We only assign to this when initialization is complete as it is used
        # as a flag for initialization being complete.
        self._request_middleware = request_middleware
Example #50
0
def get_context():
    """
    Returns context for config generation
    """
    context = {}

    # Current project settings
    django_project_settings = django_settings._wrapped.__dict__

    # Built-in Django settings taken from django.conf.global_settings
    django_default_settings = django_project_settings['default_settings'].__dict__

    # Adds into context built-in settings
    context.update(**django_default_settings)

    # Override some of them with project settings
    context.update(**django_project_settings)

    # And cleans private attributes out
    context = {k: v for k, v in context.items() if k.isupper()}

    # Modify data with context processors
    for context_processor in settings.CONTEXT_PROCESSORS:
        try:
            items = import_by_path(context_processor)()
            assert isinstance(items, dict)
        except ImproperlyConfigured:
            raise
        except AssertionError:
            raise ImproperlyConfigured('context processor must return dict')
        context.update(**items)
    return context
Example #51
0
 def setUpClass(cls):
     try:
         cls.selenium = import_by_path(cls.webdriver_class)()
     except Exception as e:
         raise SkipTest('Selenium webdriver "%s" not installed or not '
                        'operational: %s' % (cls.webdriver_class, str(e)))
     super(AdminSeleniumWebDriverTestCase, cls).setUpClass()
Example #52
0
def find_template_loader(loader):
    if isinstance(loader, (tuple, list)):
        loader, args = loader[0], loader[1:]
    else:
        args = []
    if isinstance(loader, six.string_types):
        TemplateLoader = import_by_path(loader)

        if hasattr(TemplateLoader, 'load_template_source'):
            func = TemplateLoader(*args)
        else:
            # Try loading module the old way - string is full path to callable
            if args:
                raise ImproperlyConfigured(
                    "Error importing template source loader %s - can't pass arguments to function-based loader."
                    % loader)
            func = TemplateLoader

        if not func.is_usable:
            import warnings
            warnings.warn(
                "Your TEMPLATE_LOADERS setting includes %r, but your Python installation doesn't support that type of template loading. Consider removing that line from TEMPLATE_LOADERS."
                % loader)
            return None
        else:
            return func
    else:
        raise ImproperlyConfigured(
            'Loader does not define a "load_template" callable template source loader'
        )
Example #53
0
def push_changes_to_external_layers(node, external_layer, operation):
    """
    Sync other applications through their APIs by performing updates, adds or deletes.
    This method is designed to be performed asynchronously, avoiding blocking the user
    when he changes data on the local DB.

    :param node: the node which should be updated on the external layer.
    :type node: Node model instance
    :param operation: the operation to perform (add, change, delete)
    :type operation: string
    """
    # putting the model inside prevents circular imports
    # subsequent imports go and look into sys.modules before reimporting the module again
    # so performance is not affected
    from nodeshot.core.nodes.models import Node
    # get node because for some reason the node instance object is not passed entirely,
    # pheraphs because objects are serialized by celery or transport/queuing mechanism
    if not isinstance(node, basestring):
        node = Node.objects.get(pk=node.pk)
    # import synchronizer
    Synchronizer = import_by_path(external_layer.synchronizer_path)
    # create instance
    instance = Synchronizer(external_layer.layer)
    # call method only if supported
    if hasattr(instance, operation):
        getattr(instance, operation)(node)
Example #54
0
def send_delayed_notifications():
    from datetime import timedelta  #DO NOT REMOVE!!!
    from .models import Transport, Notification, MultipleNotificationTemplateConfig, Subscription

    transports = Transport.objects.filter(allows_freq_config=True)
    users = User.objects.annotate(
        count_notifications=Count('notifications')).filter(
            count_notifications__gt=0)

    for transport in transports:
        for user in users:
            try:
                subscription = Subscription.objects.get(user=user,
                                                        transport=transport)
                delta = eval(subscription.frequency.delta)
            except:
                subscription = None
                delta = 0

            if not subscription or (subscription.last_sent <=
                                    timezone.now() - delta):
                template_config = MultipleNotificationTemplateConfig.objects.get(
                    transport=transport)
                transport_class = import_by_path(transport.cls)
                send_notification_method = getattr(
                    transport_class, 'send_multiple_notification')
                user_notifications = Notification.objects.filter(
                    user=user, transport=transport, sent=False)
                send_notification_method(user, user_notifications,
                                         template_config,
                                         transport.delete_sent)

                if subscription:
                    subscription.last_sent = timezone.now()
                    subscription.save()
Example #55
0
    def geturl(ref, url_name=None, user=None):
        if isinstance(ref, Flow):
            url_ref = 'viewflow:{}'.format(url_name if url_name else 'index')
            return reverse(url_ref, current_app=ref._meta.namespace)
        elif isinstance(ref, AbstractProcess):
            kwargs, url_ref = {}, 'viewflow:{}'.format(
                url_name if url_name else 'index')
            if url_name == 'details':
                kwargs['process_pk'] = ref.pk
            return reverse(url_ref,
                           current_app=ref.flow_cls._meta.namespace,
                           kwargs=kwargs)
        elif isinstance(ref, AbstractTask):
            return ref.get_absolute_url(user=user, url_type=url_name)
        else:
            try:
                app_label, flow_cls_path = ref.split('/')
            except ValueError:
                raise TemplateSyntaxError(
                    "Flow reference string should  looks like 'app_label/FlowCls' but '{}'"
                    .format(ref))

            app_package = get_app_package(app_label)
            if app_package is None:
                raise TemplateSyntaxError("{} app not found".format(app_label))

            flow_cls = import_by_path('{}.flows.{}'.format(
                app_package, flow_cls_path))
            url_ref = 'viewflow:{}'.format(url_name if url_name else 'index')
            return reverse(url_ref, current_app=flow_cls._meta.namespace)
Example #56
0
    def sso_api_list():
        """
        return sso related API
        """
        ssourls = []

        def collect(u, prefixre, prefixname):
            _prefixname = prefixname + [
                u._regex,
            ]
            urldisplayname = " ".join(_prefixname)

            if hasattr(u.urlconf_module, "_MODULE_MAGIC_ID_") \
                    and getattr(u.urlconf_module, "_MODULE_MAGIC_ID_") == MAGIC_ID:  # find aap name sso
                ssourls.append(urldisplayname)

        rooturl = import_by_path(settings.ROOT_URLCONF + ".urlpatterns")
        # traverse url matching tree to find the url statement including sso app,
        # should be 1 element in the ssourls unless you assigned 1+ prefix url for sso app
        traverse_urls(rooturl, resolverFunc=collect)

        finalQ = Q()  # filter to get full url of all registered sso api
        for prefix in ssourls:
            finalQ |= Q(name__startswith=prefix)
        return APIEntryPoint.objects.filter(finalQ)
Example #57
0
 def post(self, request, *args, **kwargs):
     context = self.get_context_data(**kwargs)
     payment_mean = context['payment_mean']
     if getattr(settings, 'UNIT_TESTING', False):
         signature = 'dumb_signature'
     else:
         signature = ''.join([
             random.SystemRandom().choice(string.ascii_letters +
                                          string.digits) for _ in range(16)
         ])
     request.session['signature'] = signature
     payments_conf = getattr(settings, 'PAYMENTS', None)
     if payments_conf:
         conf = request.POST.get('payment_conf', 'default').lower().strip()
         request.session['payment_conf'] = conf
         path = payments_conf[conf]['before']
     else:
         path = getattr(settings, 'MOMO_BEFORE_CASH_OUT')
     momo_before_checkout = import_by_path(path)
     http_resp = momo_before_checkout(request, payment_mean, *args,
                                      **kwargs)
     if http_resp:
         return http_resp
     if payment_mean.slug == ORANGE_MONEY:
         return init_web_payment(request, *args, **kwargs)
     if payment_mean.slug == YUP:
         return init_yup_web_payment(request, *args, **kwargs)
     if payment_mean.slug == UBA:
         return init_uba_web_payment(request, *args, **kwargs)
     context['amount'] = request.session['amount']
     return render(request, self.template_name, context)