Пример #1
0
    def in_bulk_any_type(self, *args, **kwargs):
        '''Works like in_bulk() but the ids passed to the function
            have a prefix with the type of the TextUnit.
            e.g. ['Clause:11:address', 'Title:11:Sheriff']

            We assumed that there are no clashes in IDs among different
            types of units. 11:address will never be another type than Clause.
        '''
        from digipal.views.faceted_search.settings import FacettedType

        aids = self.options['aids'] = args[0]

        # split into types {'Clause': ['11:address']}
        models = {}
        for maid in aids:
            maid = maid.split(':')
            if maid[0] not in models:
                models[maid[0]] = []
            models[maid[0]].append(':'.join(maid[1:]))

        ret = {}
        for name, ids in models.iteritems():
            ct = FacettedType.fromModelName(name)
            if not ct: continue
            model = ct.getModelClass()
            if not model: continue
            recs = model.objects.in_bulk(ids)
            ret.update(recs)
        return ret
Пример #2
0
    def in_bulk_any_type(self, *args, **kwargs):
        '''Works like in_bulk() but the ids passed to the function
            have a prefix with the type of the TextUnit.
            e.g. ['Clause:11:address', 'Title:11:Sheriff']

            We assumed that there are no clashes in IDs among different
            types of units. 11:address will never be another type than Clause.
        '''
        from digipal.views.faceted_search.settings import FacettedType

        aids = self.options['aids'] = args[0]

        # split into types {'Clause': ['11:address']}
        models = {}
        for maid in aids:
            maid = maid.split(':')
            if maid[0] not in models:
                models[maid[0]] = []
            models[maid[0]].append(':'.join(maid[1:]))

        ret = {}
        for name, ids in models.iteritems():
            ct = FacettedType.fromModelName(name)
            if not ct:
                continue
            model = ct.getModelClass()
            if not model:
                continue
            recs = model.objects.in_bulk(ids)
            ret.update(recs)
        return ret
Пример #3
0
def read_config(file_format):
    models_list = []
    if file_format == "json":
        models = json.loads(file(MODELS_CONFIG_FILE).read())
    else:
        models = load(file(MODELS_CONFIG_FILE), Loader=Loader)

    for model_name, model in models.iteritems():
        fields = {}

        for field in model['fields']:

            kwargs = {
                'verbose_name': field['title'],
                'blank': True,
                'null': True,
            }

            fields.update({
                field['id']: set_field_type(field['type'], **kwargs)
            })

        models_list.append({
            'name': model_name,
            'fields': fields,
            'verbose_name': model['title'],
        })

    return models_list
Пример #4
0
 def all_concrete_models():
     R = [].append
     M = apps.get_models()
     for app_label, models in apps.all_models.iteritems():
         models = [model for name,model in models.iteritems() if model in M and not model._meta.abstract]
         if models:
             R((app_label,models))
     return R.__self__
Пример #5
0
def get_cached_models():
    """Yields app_label and model from the CACHETREE setting.
    """
    for app_label, models in cachetree_settings.CACHETREE.iteritems():
        for model_name, cache_settings in models.iteritems():
            model = get_model(app_label, model_name)
            if model is None:
                raise ImproperlyConfigured(
                    "CACHETREE defines model %s.%s, which does not exist." %
                    (app_label, model_name))

            yield app_label, model
Пример #6
0
def get_cached_models():
    """Yields app_label and model from the CACHETREE setting.
    """
    for app_label, models in cachetree_settings.CACHETREE.iteritems():
        for model_name, cache_settings in models.iteritems():
            model = get_model(app_label, model_name)
            if model is None:
                raise ImproperlyConfigured(
                    "CACHETREE defines model %s.%s, which does not exist." % (
                        app_label, model_name))
            
            yield app_label, model
Пример #7
0
    def _map_element(self, element, models, get_id=None):
        """Maps an element to several models.

        Args:
            element: an XML element
            models: the models to mapped
            get_id: the function to use to calculate the ID of the element to identify it amongst the other.

        Returns:
            The number of Models created in the DB for the passed element.
        """
        elem_id = '(id:%s) ' % (self._resolve_get_id(get_id)(element),)

        status = {k: '[KO]' for k in models.keys()}
        nb_created = 0
        for app_model, fields in models.iteritems():
            try:
                ins = self._map_to_model(element, app_model, fields)
                status[app_model] = 'pk=%s' % (ins.pk)
                nb_created = nb_created + 1
                logger.info('%s - Mapping the element %sto the Model %s with fields %s => object created, pk=%s [0K]' % (
                        self.log_desc,
                        elem_id,
                        app_model,
                        fields,
                        ins.pk,
                    )
                )
            except Exception as err:
                logger.error('%s - Mapping the element %sto the Model %s with fields %s => Cannot be mapped. [K0]\n%s' % (
                        self.log_desc,
                        elem_id,
                        app_model,
                        fields,
                        err,
                    )
                )

        logger.info('%s - Element %smapped to %s Models => %s' % (
                self.log_desc,
                elem_id,
                len(models),
                ' ; '.join(['%s: %s' % (k, v) for (k, v) in status.items()]),
            )
        )

        return nb_created