Skip to content

record

django_spire.contrib.sync.database.record

SyncRecord dataclass

key instance-attribute

data instance-attribute

timestamps instance-attribute

sequence = field(default=0, compare=False) class-attribute instance-attribute

origin_node = field(default='', compare=False) class-attribute instance-attribute

received_at = field(default=0, compare=False) class-attribute instance-attribute

sync_field_last_modified property

__post_init__

Source code in django_spire/contrib/sync/database/record.py
def __post_init__(self) -> None:
    if self.key == '':
        message = 'SyncRecord key must be a non-empty string'
        raise InvalidParameterError(message)

    if self.received_at < 0:
        message = (
            f'received_at must be non-negative, '
            f'got {self.received_at}'
        )

        raise InvalidParameterError(message)

    if self.sequence < 0:
        message = (
            f'sequence must be non-negative, '
            f'got {self.sequence}'
        )

        raise InvalidParameterError(message)

from_dict classmethod

Source code in django_spire/contrib/sync/database/record.py
@classmethod
def from_dict(cls, key: str, data: dict[str, Any]) -> SyncRecord:
    if key == '':
        message = 'SyncRecord key must be a non-empty string'
        raise RecordFieldError(message)

    record_data = data.get('data', {})
    record_timestamps = data.get('timestamps', {})
    record_received_at = data.get('received_at', 0)
    record_sequence = data.get('sequence', 0)
    record_origin_node = data.get('origin_node', '')

    if not isinstance(record_data, dict):
        message = (
            f"Record {key!r}: 'data' must be a dict, "
            f'got {type(record_data).__name__}'
        )

        raise RecordFieldError(message)

    if not isinstance(record_timestamps, dict):
        message = (
            f"Record {key!r}: 'timestamps' must be a "
            f'dict, got {type(record_timestamps).__name__}'
        )

        raise RecordFieldError(message)

    record_received_at = _coerce_int(
        record_received_at,
        "'received_at'",
        key,
    )

    if record_received_at < 0:
        message = (
            f"Record {key!r}: 'received_at' must be "
            f'non-negative, got {record_received_at}'
        )

        raise RecordFieldError(message)

    record_sequence = _coerce_int(
        record_sequence,
        "'sequence'",
        key,
    )

    if record_sequence < 0:
        message = (
            f"Record {key!r}: 'sequence' must be "
            f'non-negative, got {record_sequence}'
        )

        raise RecordFieldError(message)

    if not isinstance(record_origin_node, str):
        message = (
            f"Record {key!r}: 'origin_node' must be a string, "
            f'got {type(record_origin_node).__name__}'
        )

        raise RecordFieldError(message)

    sanitized_timestamps: dict[str, int] = {}

    for timestamp_key, timestamp_value in record_timestamps.items():
        if not isinstance(timestamp_key, str):
            message = (
                f'Record {key!r}: timestamp key '
                f'{timestamp_key!r} must be a string'
            )

            raise RecordFieldError(message)

        coerced = _coerce_int(
            timestamp_value,
            f'timestamp for {timestamp_key!r}',
            key,
        )

        if coerced < 0:
            message = (
                f'Record {key!r}: timestamp for '
                f'{timestamp_key!r} must be non-negative, '
                f'got {coerced}'
            )

            raise RecordFieldError(message)

        sanitized_timestamps[timestamp_key] = coerced

    return cls(
        key=key,
        data=record_data,
        timestamps=sanitized_timestamps,
        sequence=record_sequence,
        origin_node=record_origin_node,
        received_at=record_received_at,
    )

to_dict

Source code in django_spire/contrib/sync/database/record.py
def to_dict(self) -> dict[str, Any]:
    return {
        'data': self.data,
        'origin_node': self.origin_node,
        'sequence': self.sequence,
        'timestamps': self.timestamps,
    }

RecordContext dataclass

model instance-attribute

key instance-attribute

sync_record instance-attribute

field_data instance-attribute

sequence instance-attribute

origin_node instance-attribute