archivebox.base_models.models

This file provides the Django ABIDField and ABIDModel base model to inherit from.

Module Contents

Classes

AutoDateTimeField

ModelWithReadOnlyFields

Base class for models that have some read-only fields enforced by .save().

ModelWithUUID

ModelWithSerializers

ABIDModel

Abstract Base Model for other models to depend on. Provides ArchiveBox ID (ABID) interface and other helper methods.

ModelWithNotes

Very simple Model that adds a notes field to any model.

ModelWithHealthStats

ModelWithConfig

Base Model that adds a config property to any ABIDModel. This config is retrieved by abx.pm.hook.get_scope_config(…) later whenever this model is used.

ModelWithOutputDir

Base Model that adds an output_dir property to any ABIDModel.

Functions

get_or_create_system_user_pk

Get or create a system user with is_superuser=True to be the default owner for new DB rows

find_all_abid_prefixes

Return the mapping of all ABID prefixes to their models. e.g. {‘tag_’: core.models.Tag, ‘snp_’: core.models.Snapshot, …}

find_prefix_for_abid

Find the correct prefix for a given ABID that may have be missing a prefix (slow). e.g. ABID(‘obj_01BJQMF54D093DXEAWZ6JYRPAQ’) -> ‘snp_’

find_model_from_abid_prefix

Return the Django Model that corresponds to a given ABID prefix. e.g. ‘tag_’ -> core.models.Tag

find_model_from_abid

Shortcut for find_model_from_abid_prefix(abid.prefix)

find_obj_from_abid_rand

This is a huge hack and should only be used for debugging, never use this in real code / expose this to users.

find_obj_from_abid

Find an object with a given ABID by filtering possible models for a matching abid/uuid/id (fast). e.g. ‘snp_01BJQMF54D093DXEAWZ6JYRPAQ’ -> Snapshot(‘snp_01BJQMF54D093DXEAWZ6JYRPAQ’)

Data

DEFAULT_ICON

ABIDField

API

archivebox.base_models.models.DEFAULT_ICON[source]

‘<img src=”data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAgCAYAAAAMq2gFAAAAAXNSR0IArs4c6QAAAIRl…’

archivebox.base_models.models.ABIDField[source]

‘partial(…)’

archivebox.base_models.models.get_or_create_system_user_pk(username='system')[source]

Get or create a system user with is_superuser=True to be the default owner for new DB rows

class archivebox.base_models.models.AutoDateTimeField[source]

Bases: django.db.models.DateTimeField

exception archivebox.base_models.models.ABIDError[source]

Bases: Exception

class archivebox.base_models.models.ModelWithReadOnlyFields[source]

Bases: django.db.models.Model

Base class for models that have some read-only fields enforced by .save().

read_only_fields: ClassVar[tuple[str, ...]][source]

()

class Meta[source]
abstract[source]

True

_fresh_from_db()[source]
diff_from_db(keys: Iterable[str] = ()) dict[str, tuple[Any, Any]][source]

Get a dictionary of the fields that have changed from the values in the database

save(*args, **kwargs) None[source]
class archivebox.base_models.models.ModelWithUUID[source]

Bases: archivebox.base_models.models.ModelWithReadOnlyFields, tags.models.ModelWithKVTags

read_only_fields[source]

(‘id’, ‘created_at’)

id[source]

‘UUIDField(…)’

created_at[source]

‘AutoDateTimeField(…)’

class Meta[source]

Bases: django_stubs_ext.db.models.TypedModelMeta

abstract[source]

True

default_json_keys: ClassVar[tuple[str, ...]][source]

(‘TYPE’, ‘id’, ‘abid’, ‘str’, ‘modified_at’, ‘created_at’, ‘created_by_id’, ‘status’, ‘retry_at’, ‘n…

classmethod from_dict(fields: dict[str, Any]) Self[source]
update(**kwargs) None[source]

Update the object’s properties from a dict

as_json(keys: Iterable[str] = ()) dict[source]

Get the object’s properties as a dict

TYPE() str[source]

Get the full Python dotted-import path for this model, e.g. ‘core.models.Snapshot’

property admin_change_url: str[source]

get the admin URL e.g. /admin/core/snapshot/abcd-1234-1234-asdfjkl23jsdf4/change/

class archivebox.base_models.models.ModelWithSerializers[source]

Bases: archivebox.base_models.models.ModelWithUUID

as_csv_row(keys: Iterable[str] = (), separator: str = ',') str[source]

Get the object’s properties as a csv string

as_jsonl_row(keys: Iterable[str] = (), **json_kwargs) str[source]

Get the object’s properties as a jsonl string

as_html_icon() str[source]

Get a representation of this object as a simple html tag or emoji

as_html_row() str[source]

Get a representation of this object as a static html table … string

as_html_embed() str[source]

Get a representation of this object suitable for embedding inside a roughly 400x300px iframe

as_html_fullpage() str[source]

Get a static html page representation of this object

class archivebox.base_models.models.ABIDModel(*args: Any, **kwargs: Any)[source]

Bases: archivebox.base_models.models.ModelWithReadOnlyFields, archivebox.base_models.models.ModelWithUUID

Abstract Base Model for other models to depend on. Provides ArchiveBox ID (ABID) interface and other helper methods.

Initialization

Overriden init method ensures we have a stable creation timestamp that fields can use within initialization code pre-saving to DB.

abid_prefix: str[source]

None

abid_ts_src[source]

‘self.created_at’

abid_uri_src[source]

‘None’

abid_subtype_src[source]

‘self.class.name

abid_rand_src[source]

self.id

abid_drift_allowed: bool[source]

False

abid_salt: str[source]

None

read_only_fields[source]

(‘id’, ‘abid’, ‘created_at’, ‘created_by’)

id[source]

‘UUIDField(…)’

abid[source]

‘ABIDField(…)’

created_at[source]

‘AutoDateTimeField(…)’

created_by[source]

‘ForeignKey(…)’

modified_at[source]

‘DateTimeField(…)’

_prefetched_objects_cache: Dict[str, Any][source]

None

class Meta[source]

Bases: django_stubs_ext.db.models.TypedModelMeta

abstract[source]

True

__str__() str[source]
classmethod check(**kwargs)[source]
clean(abid_drift_allowed: bool | None = None) None[source]
save(*args: Any, abid_drift_allowed: bool | None = None, **kwargs: Any) None[source]

Overriden save method ensures new ABID is generated while a new object is first saving.

classmethod id_from_abid(abid: str) str[source]
property ABID_SOURCES: Dict[str, str][source]

“Get the dict of fresh ABID component values based on the live object’s properties.

property ABID_FRESH_VALUES: Dict[str, Any][source]

“Get the dict of fresh ABID component values based on the live object’s properties.

property ABID_FRESH_HASHES: Dict[str, str][source]

“Get the dict of fresh ABID component hashes based on the live object’s properties.

property ABID_FRESH_DIFFS: Dict[str, Dict[str, Any]][source]

Get the dict of discrepancies between the existing saved ABID and a new fresh ABID computed based on the live object.

issue_new_abid(overwrite=False) archivebox.base_models.abid.ABID[source]

Issue a new ABID based on the current object’s properties, can only be called once on new objects (before they are saved to DB). TODO: eventually we should move this to a separate service that makes sure they’re all unique and monotonic perhaps it could be moved to a KVTag as well, and we could just use the KVTag service + Events to issue new ABIDs

property ABID: archivebox.base_models.abid.ABID[source]

Get the object’s existing ABID (from self.abid if it’s already saved to DB, otherwise generated fresh) e.g. -> ABID(ts=’01HX9FPYTR’, uri=’E4A5CCD9’, subtype=’00’, rand=’ZYEBQE’)

property api_url: str[source]

Compute the REST API URL to access this object. e.g. /api/v1/core/snapshot/snp_01BJQMF54D093DXEAWZ6JYRP

property api_docs_url: str[source]

Compute the REST API Documentation URL to learn about accessing this object. e.g. /api/v1/docs#/Core%20Models/api_v1_core_get_snapshots

class archivebox.base_models.models.ModelWithNotes[source]

Bases: django.db.models.Model

Very simple Model that adds a notes field to any model.

notes[source]

‘TextField(…)’

class Meta[source]
abstract[source]

True

class archivebox.base_models.models.ModelWithHealthStats[source]

Bases: django.db.models.Model

num_uses_failed[source]

‘PositiveIntegerField(…)’

num_uses_succeeded[source]

‘PositiveIntegerField(…)’

class Meta[source]
abstract[source]

True

increment_num_uses_failed() None[source]
increment_num_uses_succeeded() None[source]
reset_health_counts() None[source]
property health: int[source]
class archivebox.base_models.models.ModelWithConfig[source]

Bases: django.db.models.Model

Base Model that adds a config property to any ABIDModel. This config is retrieved by abx.pm.hook.get_scope_config(…) later whenever this model is used.

config[source]

‘JSONField(…)’

class Meta[source]
abstract[source]

True

class archivebox.base_models.models.ModelWithOutputDir(*args: Any, **kwargs: Any)[source]

Bases: ModelsWithSerializers, archivebox.base_models.models.ModelWithUUID, archivebox.base_models.models.ABIDModel

Base Model that adds an output_dir property to any ABIDModel.

It creates the directory on .save(with_indexes=True), automatically migrating any old data if needed. It then writes the indexes to the output_dir on .save(write_indexes=True). It also makes sure the output_dir is in sync with the model.

Initialization

Overriden init method ensures we have a stable creation timestamp that fields can use within initialization code pre-saving to DB.

class Meta[source]
abstract[source]

True

save(*args, write_indexes=False, **kwargs) None[source]
property output_dir_parent: str[source]

Get the model type parent directory name that holds this object’s data e.g. ‘archiveresults’

property output_dir_name: str[source]

Get the subdirectory name for the filesystem directory that holds this object’s data e.g. ‘snp_2342353k2jn3j32l4324’

property output_dir_str: str[source]

Get relateive the filesystem directory Path that holds that data for this object e.g. ‘snapshots/snp_2342353k2jn3j32l4324’

property OUTPUT_DIR: pathlib.Path[source]

Get absolute filesystem directory Path that holds that data for this object e.g. Path(‘/data/snapshots/snp_2342353k2jn3j32l4324’)

write_indexes()[source]

Write the Snapshot json, html, and merkle indexes to its output dir

save_merkle_index(**kwargs) None[source]

Write the ./.index.merkle file to the output dir

save_html_index(**kwargs) None[source]
save_json_index(**kwargs) None[source]

Save a JSON dump of the object to the output dir

Set up the symlink farm pointing to this object’s data

as_json(*keys) dict[source]

Get the object’s properties as a dict

as_html() str[source]

Get the object’s properties as a html string

archivebox.base_models.models.find_all_abid_prefixes() Dict[str, type[django.db.models.Model]][source]

Return the mapping of all ABID prefixes to their models. e.g. {‘tag_’: core.models.Tag, ‘snp_’: core.models.Snapshot, …}

archivebox.base_models.models.find_prefix_for_abid(abid: archivebox.base_models.abid.ABID) str[source]

Find the correct prefix for a given ABID that may have be missing a prefix (slow). e.g. ABID(‘obj_01BJQMF54D093DXEAWZ6JYRPAQ’) -> ‘snp_’

archivebox.base_models.models.find_model_from_abid_prefix(prefix: str) type[archivebox.base_models.models.ABIDModel] | None[source]

Return the Django Model that corresponds to a given ABID prefix. e.g. ‘tag_’ -> core.models.Tag

archivebox.base_models.models.find_model_from_abid(abid: archivebox.base_models.abid.ABID) type[django.db.models.Model] | None[source]

Shortcut for find_model_from_abid_prefix(abid.prefix)

archivebox.base_models.models.find_obj_from_abid_rand(rand: Union[archivebox.base_models.abid.ABID, str], model=None) List[archivebox.base_models.models.ABIDModel][source]

This is a huge hack and should only be used for debugging, never use this in real code / expose this to users.

Find an object corresponding to an ABID by exhaustively searching using its random suffix (slow). e.g. ‘obj_………………..JYRPAQ’ -> Snapshot(‘snp_01BJQMF54D093DXEAWZ6JYRPAQ’)

archivebox.base_models.models.find_obj_from_abid(abid: archivebox.base_models.abid.ABID, model=None, fuzzy=False) Any[source]

Find an object with a given ABID by filtering possible models for a matching abid/uuid/id (fast). e.g. ‘snp_01BJQMF54D093DXEAWZ6JYRPAQ’ -> Snapshot(‘snp_01BJQMF54D093DXEAWZ6JYRPAQ’)