rattail.db.util

Database Utilities

class rattail.db.util.CounterMagic(config)[source]

Provides magic counter values, to simulate PostgreSQL sequence.

next_value(session, key)[source]

Increment and return the next counter value for given key.

class rattail.db.util.PhoneValidator(error=False)[source]

Simple validator, used to ensure a phone number matches the general expected pattern.

class rattail.db.util.QuerySequence(query)[source]

Simple wrapper for a SQLAlchemy (or Django, or other?) query, to make it sort of behave like a normal sequence, as much as needed to e.g. make an importer happy.

rattail.db.util.format_phone_number(number)[source]

Returns a phone number in (XXX) XXX-XXXX format if possible; otherwise returns the argument unaltered.

rattail.db.util.get_fieldnames(config, obj, columns=True, proxies=True, relations=False)[source]

Produce a simple list of fieldnames for the given class, reflecting its table columns as well as any association proxies, and optionally, relationships.

Parameters:
  • obj – Either a class or instance of a class, which derives from the base model class.

  • columns – Whether or not to include simple columns.

  • relations – Whether or not to include fields which represent relationships to other models. If False (the default) then only “simple” fields will be included.

  • proxies – Whether or not to include association proxy fields.

rattail.db.util.make_full_description(brand_name, description, size)[source]

Combine the given field values into a complete description.

rattail.db.util.make_topo_sortkey(model, metadata=None)[source]

Returns a function suitable for use as a key kwarg to a standard Python sorting call. This key function will expect a single class mapper and return a sequence number associated with that model. The sequence is determined by SQLAlchemy’s topological table sorting.

rattail.db.util.maxlen(attr)[source]

Return the maximum length for the given attribute.

rattail.db.util.normalize_full_name(first_name, last_name)[source]

Normalize the given first and last name to a “full” name value. The fallback return value is an empty string.

rattail.db.util.normalize_phone_number(number)[source]

Normalize a phone number to digits only.

rattail.db.util.short_session(session=None, Session=None, commit=False, factory=None, config=None)[source]

Compatibility wrapper around wuttjamaican.db.sess.short_session.

Note that this wrapper is a function whereas the upsream version is a proper context manager (class). So calling this function will return a new instance of the upsream class.

You should always specify keyword arguments when calling this function, since the arg order is different between this function and the upstream class. And note that this function will eventually be deprecated and removed, so new code should call upstream directly.

rattail.db.util.validate_phone_number(number, error=False)[source]

Validate a single phone number.