1.2.1.2. models

This module provides the models for the data stored in the database as well as functionality for defining and managing the models themselves.

1.2.1.2.1. Data

database_tables[source]

A dictionary which contains all the database tables and their MetaTable instances.

SCHEMA_VERSION[source]

The schema version of the database, used for compatibility checks.

1.2.1.2.2. Functions

current_timestamp(*args, **kwargs)[source]

The function used for creating the timestamp used by database objects.

Returns:The current timestamp.
Return type:datetime.datetime
get_tables_with_column_id(column_id)[source]

Get all tables which contain a column named column_id.

Parameters:column_id (str) – The column name to get all the tables of.
Returns:The list of matching tables.
Return type:set
register_table(table)[source]

Register a database table. This will populate the information provided in DATABASE_TABLES dictionary. This also forwards signals to the appropriate listeners within the server.signal module.

Parameters:table (cls) – The table to register.
sql_null()[source]

Return a constant Null construct.

1.2.1.2.3. Classes

class BaseRowCls[source]

Bases: object

The base class from which other database table objects inherit from. Provides a standard __repr__ method and default permission checks which are to be overridden as desired by subclasses.

Warning

Subclasses should not directly override the session_has_*_access methods. These contain wrapping logic to do things like checking if the session is an administrator, etc. Instead subclasses looking to control access should override the individual private variants _session_has_*_access. Each of these use the same call signature as their public counterparts.

assert_session_has_permissions(*args, **kwargs)[source]

A convenience function which wraps session_has_permissions() and raises a KingPhisherPermissionError if the session does not have the specified permissions.

is_private = False[source]

Whether the table is only allowed to be accessed by the server or not.

classmethod metatable()[source]

Generate a MetaTable instance for this model class.

Returns:The appropriate metadata for the table represented by this model.
Return type:MetaTable
classmethod session_has_create_access(session, instance=None)[source]

Check that the authenticated session has access to create the specified model instance.

Parameters:
  • session – The authenticated session to check access for.
  • instance – The optional model instance to inspect.
Returns:

Whether the session has the desired permissions.

Return type:

bool

classmethod session_has_delete_access(session, instance=None)[source]

Check that the authenticated session has access to delete the specified model instance.

Parameters:
  • session – The authenticated session to check access for.
  • instance – The optional model instance to inspect.
Returns:

Whether the session has the desired permissions.

Return type:

bool

session_has_permissions(access, session)[source]

Check that the authenticated session has the permissions specified in access. The permissions in access are abbreviated with the first letter of create, read, update, and delete. For example, to check for read and update permissions, access would be 'ru'.

Note

This will always return True for sessions which are for administrative users. To maintain this logic, this method should not be overridden in subclasses. Instead override the specific _session_has_*_access methods as necessary.

Parameters:
  • access (str) – The desired permissions.
  • session – The authenticated session to check access for.
Returns:

Whether the session has the desired permissions.

Return type:

bool

classmethod session_has_read_access(session, instance=None)[source]

Check that the authenticated session has access to read the specified model instance.

Parameters:
  • session – The authenticated session to check access for.
  • instance – The optional model instance to inspect.
Returns:

Whether the session has the desired permissions.

Return type:

bool

classmethod session_has_read_prop_access(session, prop, instance=None)[source]

Check that the authenticated session has access to read the property of the specified model instance. This allows models to only explicitly control which of their attributes can be read by a particular session.

Parameters:
  • session – The authenticated session to check access for.
  • instance – The optional model instance to inspect.
Returns:

Whether the session has the desired permissions.

Return type:

bool

classmethod session_has_update_access(session, instance=None)[source]

Check that the authenticated session has access to update the specified model instance.

Parameters:
  • session – The authenticated session to check access for.
  • instance – The optional model instance to inspect.
Returns:

Whether the session has the desired permissions.

Return type:

bool

class MetaTable(column_names, model, name, table)[source]

Bases: tuple

Metadata describing a table and its various attributes.

column_names[source]

A tuple of strings representing the table’s column names.

model[source]

The SQLAlchemy model class associated with this table.

name[source]

The name of this table.