wuttjamaican.db.model.batch

Batch data models

class wuttjamaican.db.model.batch.BatchMixin[source]

Mixin base class for data models which represent a batch.

See also BatchRowMixin which should be used for the row model.

For a batch model (table) to be useful, at least one batch handler must be defined, which is able to process data for that batch type.

batch_type

This is the canonical batch type for the batch model.

By default this will match the underlying table name for the batch, but the model class can set it explicitly to override.

__row_class__

Reference to the specific data model class used for the batch rows.

This will be a subclass of BatchRowMixin (among other classes).

When defining the batch model, you do not have to set this as it will be assigned automatically based on BatchRowMixin.__batch_class__.

id

Numeric ID for the batch, unique across all batches (regardless of type).

See also id_str.

description

Simple description for the batch.

notes

Arbitrary notes for the batch.

rows

List of data rows for the batch, aka. batch rows.

Each will be an instance of BatchRowMixin (among other base classes).

row_count

Cached row count for the batch, i.e. how many rows it has.

No guarantees perhaps, but this should ideally be accurate (it ultimately depends on the batch handler implementation).

STATUS

Dict of possible batch status codes and their human-readable names.

Each key will be a possible status_code and the corresponding value will be the human-readable name.

See also status_text for when more detail/subtlety is needed.

Typically each “key” (code) is also defined as its own “constant” on the model class. For instance:

from collections import OrderedDict
from wuttjamaican.db import model

class MyBatch(model.BatchMixin, model.Base):
    """ my custom batch """

    STATUS_INCOMPLETE = 1
    STATUS_EXECUTABLE = 2

    STATUS = OrderedDict([
        (STATUS_INCOMPLETE, "incomplete"),
        (STATUS_EXECUTABLE, "executable"),
    ])

    # TODO: column definitions...

And in fact, the above status definition is the built-in default. However it is expected for subclass to overwrite the definition entirely (in similar fashion to above) when needed.

Note

There is not any built-in logic around these integer codes; subclass can use any the developer prefers.

Of course, once you define one, if any live batches use it, you should not then change its fundamental meaning (although you can change the human-readable text).

It’s recommended to use OrderedDict (as shown above) to ensure the possible status codes are displayed in the correct order, when applicable.

status_code

Status code for the batch as a whole. This indicates whether the batch is “okay” and ready to execute, or (why) not etc.

This must correspond to an existing key within the STATUS dict.

See also status_text.

status_text

Text which may (briefly) further explain the batch status_code, if needed.

For example, assuming built-in default STATUS definition:

batch.status_code = batch.STATUS_INCOMPLETE
batch.status_text = "cannot execute batch because it is missing something"
created

When the batch was first created.

created_by

Reference to the User who first created the batch.

executed

When the batch was executed.

executed_by

Reference to the User who executed the batch.

property id_str

Property which returns the id as a string, zero-padded to 8 digits:

batch.id = 42
print(batch.id_str)  # => '00000042'
class wuttjamaican.db.model.batch.BatchRowMixin[source]

Mixin base class for data models which represent a batch row.

See also BatchMixin which should be used for the (parent) batch model.

__batch_class__

Reference to the data model for the parent batch class.

This will be a subclass of BatchMixin (among other classes).

When defining the batch row model, you must set this attribute explicitly! And then BatchMixin.__row_class__ will be set automatically to match.

batch

Reference to the parent batch to which the row belongs.

This will be an instance of BatchMixin (among other base classes).

sequence

Sequence (aka. line) number for the row, within the parent batch. This is 1-based so the first row has sequence 1, etc.

STATUS

Dict of possible row status codes and their human-readable names.

Each key will be a possible status_code and the corresponding value will be the human-readable name.

See also status_text for when more detail/subtlety is needed.

Typically each “key” (code) is also defined as its own “constant” on the model class. For instance:

from collections import OrderedDict
from wuttjamaican.db import model

class MyBatchRow(model.BatchRowMixin, model.Base):
    """ my custom batch row """

    STATUS_INVALID    = 1
    STATUS_GOOD_TO_GO = 2

    STATUS = OrderedDict([
        (STATUS_INVALID,    "invalid"),
        (STATUS_GOOD_TO_GO, "good to go"),
    ])

    # TODO: column definitions...

Whereas there is a built-in default for the BatchMixin.STATUS, there is no built-in default defined for the BatchRowMixin.STATUS. Subclass must overwrite the definition entirely, in similar fashion to above.

Note

There is not any built-in logic around these integer codes; subclass can use any the developer prefers.

Of course, once you define one, if any live batches use it, you should not then change its fundamental meaning (although you can change the human-readable text).

It’s recommended to use OrderedDict (as shown above) to ensure the possible status codes are displayed in the correct order, when applicable.

status_code

Current status code for the row. This indicates if the row is “good to go” or has “warnings” or is outright “invalid” etc.

This must correspond to an existing key within the STATUS dict.

See also status_text.

status_text

Text which may (briefly) further explain the row status_code, if needed.

For instance, assuming the example STATUS definition shown above:

row.status_code = row.STATUS_INVALID
row.status_text = "input data for this row is missing fields: foo, bar"
modified

Last modification time of the row. This should be automatically set when the row is first created, as well as anytime it’s updated thereafter.