Skip to content

base

django_spire.contrib.seeding.model.base

classproperty

Source code in django_spire/contrib/seeding/model/base.py
def __init__(self, fget: Any) -> None:
    self.fget = fget

fget = fget instance-attribute

__get__

Source code in django_spire/contrib/seeding/model/base.py
def __get__(self, instance: Any, owner: Any) -> Any:
    return self.fget(owner)

BaseModelSeeder

Bases: ABC

model_class = None class-attribute instance-attribute

fields = None class-attribute instance-attribute

field_config_class = FieldsConfig class-attribute instance-attribute

default_to = 'llm' class-attribute instance-attribute

cache_seed = True class-attribute instance-attribute

cache_name = 'model_seeder' class-attribute instance-attribute

cache_limit = 1000 class-attribute instance-attribute

override_class = FieldOverride class-attribute instance-attribute

override

Source code in django_spire/contrib/seeding/model/base.py
@classproperty
def override(cls) -> FieldOverride:
    return cls.override_class(seeder_class=cls)

get_field_config classmethod

Source code in django_spire/contrib/seeding/model/base.py
@classmethod
def get_field_config(cls) -> FieldsConfig:
    if cls._field_config is None:
        if cls.model_class is None:
            message = 'model_class must be defined before using seeder.'
            raise ValueError(message)

        raw_fields = cls.__dict__.get('fields', {})

        cls._field_config = cls.field_config_class(
            raw_fields=raw_fields,
            field_names=cls.field_names(),
            default_to=cls.default_to,
            model_class=cls.model_class,
        )

    return cls._field_config

resolved_fields classmethod

Source code in django_spire/contrib/seeding/model/base.py
@classmethod
def resolved_fields(cls) -> dict:
    return cls.get_field_config().fields

clear_cache classmethod

Source code in django_spire/contrib/seeding/model/base.py
@classmethod
def clear_cache(cls) -> None:
    SqliteCache.clear(cache_name=cls.cache_name)

field_names abstractmethod classmethod

Source code in django_spire/contrib/seeding/model/base.py
@classmethod
@abstractmethod
def field_names(cls) -> list[str]:
    pass

seed_data classmethod

Source code in django_spire/contrib/seeding/model/base.py
@classmethod
@recorder_to_html_file('model_seeder')
def seed_data(
    cls,
    count: int = 1,
    fields: dict | None = None,
) -> list[dict]:
    field_config = (
        cls.get_field_config().override(fields)
        if fields
        else cls.get_field_config()
    )

    if cls.cache_seed:
        cache_key = generate_cache_key(
            cls.seed_data, count=count, fields=field_config.fields
        )

        cache = SqliteCache(cache_name=cls.cache_name, limit=cls.cache_limit)
        formatted_seed_data = cache.get(cache_key)

        if formatted_seed_data:
            return formatted_seed_data

    seed_data = []

    for seeder_cls in cls._field_seeders:
        seeder = seeder_cls(field_config.fields, field_config.default_to)

        if len(seeder.seeder_fields) > 0:
            seed_data.append(seeder.seed(cls, count))

    formatted_seed_data = [
        {} for _ in range(max(len(sublist) for sublist in seed_data))
    ]
    for sublist in seed_data:
        for i, d in enumerate(sublist):
            formatted_seed_data[i].update(d)

    if cls.cache_seed:
        cache.set(cache_key, formatted_seed_data)

    return formatted_seed_data

seed classmethod

Source code in django_spire/contrib/seeding/model/base.py
@classmethod
def seed(
    cls,
    count: int = 1,
    fields: dict | None = None,
) -> list:
    return [
        cls.model_class(**seed_data) for seed_data in cls.seed_data(count, fields)
    ]