rattail.db.batch

Basic Models for Batches

Actually the classes in this module are not true models but rather are mixins, which provide the common columns etc. for batch tables.

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

Mixin for all (new-style) batch classes.

Note

This is all still very experimental.

class rattail.db.batch.model.FileBatchMixin[source]

Mixin for all (new-style) batch classes which involve a file upload as their first step.

Note

This is all still very experimental.

absolute_filepath(config)[source]

Return the absolute path where the data file resides. This is the path returned by filedir() with the batch filename joined to it.

delete_data(config)[source]

Delete the data file and folder for the batch.

filedir(config)[source]

Returns the absolute path to the folder in which the data file resides. The config object determines the root path for such files, e.g.:

[rattail]
batch.files = /path/to/batch/files

Within this root path, a more complete path is generated using the BatchMixin.key and the BatchMixin.uuid values.

filepath(config)

Return the absolute path where the data file resides. This is the path returned by filedir() with the batch filename joined to it.

filesize(config)[source]

Returns the size of the data file in bytes.

relative_filepath(config)[source]

Returns the path for the data file, relative to the root folder for batch file storage. This includes the batch key as the first segment.

write_file(config, contents)[source]

Save a data file for the batch to the location specified by filepath().

class rattail.db.batch.model.BatchRowMixin[source]

Mixin for all (new-style) batch row classes.

Note

This is all still very experimental.

class rattail.db.batch.model.ProductBatchRowMixin[source]

Mixin for all row classes of (new-style) batches which pertain to products.

Note

This is all still very experimental.

Basic Batch Handler

class rattail.db.batch.handler.BatchHandler(config)[source]

Base class for all batch handlers. This isn’t really useful by itself but it is expected that other batches will derive from it.

batch_model_class

Reference to the data model class of the batch type for which this handler is responsible.

cognize_row(session, row)[source]

This method should further populate the row’s fields, using database lookups and business rules etc. as needed. Each handler class must define this method.

Parameters:
  • session – SQLAlchemy database session object.
  • row – A batch row instance, whose fields reflect the initial source data only, e.g. that which was parsed from a file.
Returns:

Typically this method needn’t return anything. However if it returns False then the row will not be added to the batch.

executable(batch)[source]

This method should return a boolean indicating whether or not execution should be allowed for the batch, given its current condition. The default simply returns True but you may override as needed.

Note that this (currently) only affects the enabled/disabled state of the Execute button within the Tailbone batch view.

get_execute_title(batch)[source]

Get a human-friendly string describing the execution step for a batch. Most handlers should probably override this to provide something more useful than the default, which is just “Execute this batch”.

make_batch(session, **kwargs)[source]

Create a new batch instance and return it. All keyword arguments are passed to the batch model constructor. Note that some keyword arguments may be required, depending on the type of batch.

make_rows(session, batch, data, progress=None)[source]

Create batch rows from the given data set.

refresh_data(session, batch, progress=None)[source]

Refresh all data for the batch.

class rattail.db.batch.handler.FileBatchHandler(config)[source]

Base class for all file-based batch handlers. Adds some conveniences for managing data file storage.

Note

Current implementation only supports one data file per batch.

data_path(batch)[source]

Returns the full path to the batch’s one and only data file. As with datadir(), this method does not guarantee the existence of the file.

datadir(batch)[source]

Returns the absolute path of the folder in which the batch’s source data file(s) resides. Note that the batch must already have been persisted to the database. The structure of the path returned is as follows:

/{root_datadir}/{uuid[:2]}/{uuid[2:]}
  • {root_datadir} - Value returned by root_datadir().
  • {uuid[:2]} - First two characters of batch UUID.
  • {uuid[2:]} - All batch UUID characters after the first two.

Note

While it is likely that the data folder returned by this method already exists, this method does not guarantee any such thing. It is typically assumed that the path will have been created by a previous call to make_batch() however.

make_batch(session, path, **kwargs)[source]

Create a new batch as per usual, plus save a copy of the data file (at path) to the configured batch storage folder.

root_datadir

The absolute path of the root folder in which data for this particular type of batch is stored. The structure of this path is as follows:

/{root_batch_data_dir}/{batch_type_key}
  • {root_batch_data_dir} - Value of the ‘batch.files’ option in the [rattail] section of config file.
  • {batch_type_key} - Unique key for the type of batch it is.

Note

While it is likely that the data folder returned by this method already exists, this method does not guarantee it.

set_data_file(batch, path)[source]

Assign the data file found at path to the batch. This overwrites the batch’s filename attribute and places a copy of the data file in the batch’s data folder.

class rattail.db.batch.handler.MakeFileBatch(config)[source]

Filemon action for making new file-based batches.

__call__(path, handler=u'', user=u'')[source]

Make a batch from the given file path.

Parameters:
  • path – Path to the source data file for the batch.
  • handler – Spec string for the batch handler class, e.g. 'rattail.db.batch.vendorcatalog.handler:VendorCatalogHandler'.
  • user – Username of the user which is to be credited with creating and cognizing the batch.