Exemplo n.º 1
0
    def get_model(self, cache=True, data_table=None):
        # TODO: the current dynamic model registry is handled by Brasil.IO's
        # code but it needs to be delegated to dynamic_models.

        data_table = data_table or self.data_table
        db_table = data_table.db_table_name

        # TODO: limit the max number of items in DYNAMIC_MODEL_REGISTRY
        cache_key = (self.id, db_table)
        if cache and cache_key in DYNAMIC_MODEL_REGISTRY:
            return DYNAMIC_MODEL_REGISTRY[cache_key]

        # TODO: unregister the model in Django if already registered (cache_key
        # in DYNAMIC_MODEL_REGISTRY and not cache)
        fields = {field.name: field.field_class for field in self.fields}
        fields["search_data"] = SearchVectorField(null=True)
        ordering = self.ordering or []
        filtering = self.filtering or []
        search = self.search or []
        indexes = []
        # TODO: add has_choices fields also
        if ordering:
            indexes.append(
                django_indexes.Index(
                    name=make_index_name(db_table, "order", ordering),
                    fields=ordering,
                ))
        if filtering:
            for field_name in filtering:
                if ordering == [field_name]:
                    continue
                indexes.append(
                    django_indexes.Index(name=make_index_name(
                        db_table, "filter", [field_name]),
                                         fields=[field_name]))
        if search:
            indexes.append(
                pg_indexes.GinIndex(name=make_index_name(
                    db_table, "search", ["search_data"]),
                                    fields=["search_data"]))

        managers = self.get_dynamic_model_managers()
        mixins = self.get_dynamic_model_mixins()
        meta = {"ordering": ordering, "indexes": indexes, "db_table": db_table}

        Model = dynamic_models.create_model_class(
            name=self.model_name,
            module="core.models",
            fields=fields,
            mixins=mixins,
            meta=meta,
            managers=managers,
        )
        Model.extra = {
            "filtering": filtering,
            "ordering": ordering,
            "search": search,
        }
        DYNAMIC_MODEL_REGISTRY[cache_key] = Model
        return Model
Exemplo n.º 2
0
    def get_model(self, cache=True):
        if cache and self.id in DYNAMIC_MODEL_REGISTRY:
            return DYNAMIC_MODEL_REGISTRY[self.id]

        # TODO: unregister the model in Django if already registered (self.id
        # in DYNAMIC_MODEL_REGISTRY and not cache)
        # TODO: may use Django's internal registry instead of
        # DYNAMIC_MODEL_REGISTRY
        name = self.dataset.slug + "-" + self.name.replace("_", "-")
        model_name = "".join([word.capitalize() for word in name.split("-")])
        fields = {field.name: field.field_class for field in self.fields}
        fields["search_data"] = SearchVectorField(null=True)
        ordering = self.ordering or []
        filtering = self.filtering or []
        search = self.search or []
        indexes = []
        # TODO: add has_choices fields also
        if ordering:
            indexes.append(
                django_indexes.Index(
                    name=make_index_name(name, "order", ordering),
                    fields=ordering,
                ))
        if filtering:
            for field_name in filtering:
                if ordering == [field_name]:
                    continue
                indexes.append(
                    django_indexes.Index(name=make_index_name(
                        name, "filter", [field_name]),
                                         fields=[field_name]))
        if search:
            indexes.append(
                pg_indexes.GinIndex(name=make_index_name(
                    name, "search", ["search_data"]),
                                    fields=["search_data"]))

        Options = type(
            "Meta",
            (object, ),
            {
                "ordering": ordering,
                "indexes": indexes,
                "db_table": self.db_table,
            },
        )
        Model = type(
            model_name,
            (
                DynamicModelMixin,
                models.Model,
            ),
            {
                "__module__": "core.models",
                "Meta": Options,
                "objects": DynamicModelQuerySet.as_manager(),
                **fields,
            },
        )
        Model.extra = {
            "filtering": filtering,
            "ordering": ordering,
            "search": search,
        }
        DYNAMIC_MODEL_REGISTRY[self.id] = Model
        return Model
Exemplo n.º 3
0
    def get_model(self, cache=True):
        if cache and self.id in DYNAMIC_MODEL_REGISTRY:
            return DYNAMIC_MODEL_REGISTRY[self.id]

        # TODO: unregister the model in Django if already registered (self.id
        # in DYNAMIC_MODEL_REGISTRY and not cache)
        # TODO: may use Django's internal registry instead of
        # DYNAMIC_MODEL_REGISTRY
        name = self.dataset.slug + '-' + self.name.replace('_', '-')
        model_name = ''.join([word.capitalize() for word in name.split('-')])
        fields = {field.name: field.field_class for field in self.fields}
        fields['search_data'] = SearchVectorField(null=True)
        ordering = self.ordering or []
        filtering = self.filtering or []
        search = self.search or []
        indexes = []
        # TODO: add has_choices fields also
        if ordering:
            indexes.append(
                django_indexes.Index(
                    name=make_index_name(name, 'order', ordering),
                    fields=ordering,
                ))
        if filtering:
            for field_name in filtering:
                if ordering == [field_name]:
                    continue
                indexes.append(
                    django_indexes.Index(name=make_index_name(
                        name, 'filter', [field_name]),
                                         fields=[field_name]))
        if search:
            indexes.append(
                pg_indexes.GinIndex(name=make_index_name(
                    name, 'search', ['search_data']),
                                    fields=['search_data']))

        Options = type(
            'Meta',
            (object, ),
            {
                'ordering': ordering,
                'indexes': indexes,
                'db_table': self.db_table,
            },
        )
        Model = type(
            model_name,
            (
                DynamicModelMixin,
                models.Model,
            ),
            {
                '__module__': 'core.models',
                'Meta': Options,
                'objects': DynamicModelQuerySet.as_manager(),
                **fields,
            },
        )
        Model.extra = {
            'filtering': filtering,
            'ordering': ordering,
            'search': search,
        }
        DYNAMIC_MODEL_REGISTRY[self.id] = Model
        return Model
Exemplo n.º 4
0
    def get_model(self, cache=True, data_table=None):
        # TODO: the current dynamic model registry is handled by Brasil.IO's
        # code but it needs to be delegated to dynamic_models.

        data_table = data_table or self.data_table
        db_table = data_table.db_table_name

        # TODO: limit the max number of items in DYNAMIC_MODEL_REGISTRY
        cache_key = (self.id, db_table)
        if cache and cache_key in DYNAMIC_MODEL_REGISTRY:
            return DYNAMIC_MODEL_REGISTRY[cache_key]

        # TODO: unregister the model in Django if already registered (cache_key
        # in DYNAMIC_MODEL_REGISTRY and not cache)
        fields = {field.name: field.field_class for field in self.fields}
        fields["search_data"] = SearchVectorField(null=True)
        ordering = self.ordering or []
        filtering = self.filtering or []
        search = self.search or []
        indexes = []

        if ordering and ordering != ["id"]:
            indexes.append(
                django_indexes.Index(
                    name=make_index_name(db_table, "order", ordering),
                    fields=ordering,
                ))
        if filtering:
            for field_name in filtering:
                if ordering and field_name == ordering[0]:
                    # This index is not needed, since it's covered by compound
                    # index from `ordering`. More info at:
                    # <https://github.com/gregnavis/active_record_doctor#removing-extraneous-indexes>
                    continue
                indexes.append(
                    django_indexes.Index(name=make_index_name(
                        db_table, "filter", [field_name]),
                                         fields=[field_name]))
        if search:
            indexes.append(
                pg_indexes.GinIndex(name=make_index_name(
                    db_table, "search", ["search_data"]),
                                    fields=["search_data"]))

        managers = self.get_dynamic_model_managers()
        mixins = self.get_dynamic_model_mixins()
        meta = {"ordering": ordering, "indexes": indexes, "db_table": db_table}

        Model = dynamic_models.create_model_class(
            name=self.model_name,
            module="core.models",
            fields=fields,
            mixins=mixins,
            meta=meta,
            managers=managers,
        )
        Model.extra = {
            "filtering": filtering,
            "ordering": ordering,
            "search": search,
            "table": self,
        }
        DYNAMIC_MODEL_REGISTRY[cache_key] = Model
        return Model