2. Database Model

Database Model

2.1. Chemicals

This module contains the Chemical database model. Chemicals represent the stimuli that the collected samples are exposed to.

class ptmd.database.models.chemical.Chemical(**kwargs)

Bases: Model

The chemical creator.

Parameters
  • common_name – The base class for the creator.

  • cas – The name hash id.

  • formula – The chemical formula.

  • ptx_code – The PTX code.

cas: str
chemical_id: int
common_name: Column
formula: str
metadata = MetaData()
ptx_code: int
query: t.ClassVar[Query]

A SQLAlchemy query for a model. Equivalent to db.session.query(Model). Can be customized per-model by overriding query_class.

Warning

The query interface is considered legacy in SQLAlchemy. Prefer using session.execute(select()) instead.

query_class

alias of Query

registry = <sqlalchemy.orm.decl_api.registry object>
used_in_files

2.2. Files

This module contains the File database model. A file represents a pointer to an Excel file stored on Google Drive. The file table contains general information about the exposure and collection, such as the number of replicates, controls, blanks, the organism, the vehicle, etc.

class ptmd.database.models.file.File(gdrive_id, name, batch, replicates, controls, blanks, organisation_name, user_id, organism_name, vehicle_name, start_date, end_date, doses=None, chemicals=None, timepoints=None)

Bases: Model

File database creator.

Parameters
  • gdrive_id – Google Drive file identifier.

  • name – File name.

  • batch – Batch name.

  • replicates – Number of replicates.

  • controls – Number of controls.

  • blanks – Number of blanks (empty tubes).

  • organisation_name – Organisation name.

  • user_id – User identifier.

  • organism_name – Organism name.

  • batch – Batch name.

  • doses – List of doses.

  • chemicals – List of chemicals.

  • timepoints – List of timepoints.

  • start_date – Start date of the experiment.

  • end_date – End date of the experiment.

author
author_id: int
batch: str
blanks: int
chemicals
controls: int
doses
end_date: datetime
file_id: int
gdrive_id: str
metadata = MetaData()
name: str
organisation
organisation_id: int
organism
organism_id: int
query: t.ClassVar[Query]

A SQLAlchemy query for a model. Equivalent to db.session.query(Model). Can be customized per-model by overriding query_class.

Warning

The query interface is considered legacy in SQLAlchemy. Prefer using session.execute(select()) instead.

query_class

alias of Query

receive_date: datetime
received: bool
registry = <sqlalchemy.orm.decl_api.registry object>
remove() None

Remove the file from the database.

replicates: int
ship_date: datetime
ship_samples(at: str | None = None) None

Ship the samples to the user.

Parameters

at – the date the samples were shipped

shipment_was_received(at: str | None = None) None

Update the shipment status of the file. This method is called when the shipment is received.

Parameters

at – the date the shipment was received

shipped: bool
start_date: datetime
timepoints
validated: str
vehicle
vehicle_id: int

2.3. Organisations

This module contains the organisation table. This table represent the consortium partners.

class ptmd.database.models.organisation.Organisation(**kwargs)

Bases: Model

Organisation creator.

Parameters
  • name – The base class for the creator.

  • gdrive_id – The Google Drive ID of the corresponding folder.

gdrive_id: str
longname: str
metadata = MetaData()
name: str
organisation_id: int
query: t.ClassVar[Query]

A SQLAlchemy query for a model. Equivalent to db.session.query(Model). Can be customized per-model by overriding query_class.

Warning

The query interface is considered legacy in SQLAlchemy. Prefer using session.execute(select()) instead.

query_class

alias of Query

registry = <sqlalchemy.orm.decl_api.registry object>

2.4. Organisms (species)

This module contains the organism model. This table represent the species that are used in the project.

class ptmd.database.models.organism.Organism(**kwargs)

Bases: Model

Organism creator.

Parameters
  • ptox_biosystem_name – pretox biosystem name

  • scientific_name – scientific name

  • ptox_biosystem_code – pretox biosystem code

metadata = MetaData()
organism_id: int
ptox_biosystem_code: str
ptox_biosystem_name: str
query: t.ClassVar[Query]

A SQLAlchemy query for a model. Equivalent to db.session.query(Model). Can be customized per-model by overriding query_class.

Warning

The query interface is considered legacy in SQLAlchemy. Prefer using session.execute(select()) instead.

query_class

alias of Query

registry = <sqlalchemy.orm.decl_api.registry object>
scientific_name: str

2.5. Samples

This module contains the Sample database model. The data is stored a JSON string in the ‘data’ column.

class ptmd.database.models.sample.Sample(sample_id, data, file_id)

Bases: Model

The sample creator.

data: str
file
file_id: int
metadata = MetaData()
query: t.ClassVar[Query]

A SQLAlchemy query for a model. Equivalent to db.session.query(Model). Can be customized per-model by overriding query_class.

Warning

The query interface is considered legacy in SQLAlchemy. Prefer using session.execute(select()) instead.

query_class

alias of Query

registry = <sqlalchemy.orm.decl_api.registry object>
sample_id: str

2.6. Timepoints

This module implements the Timepoint database model. Each timepoint contains a label, a value and a unit.

class ptmd.database.models.timepoint.Timepoint(**kwargs)

Bases: Model

This class implements the Timepoint model.

files
label: str
metadata = MetaData()
query: t.ClassVar[Query]

A SQLAlchemy query for a model. Equivalent to db.session.query(Model). Can be customized per-model by overriding query_class.

Warning

The query interface is considered legacy in SQLAlchemy. Prefer using session.execute(select()) instead.

query_class

alias of Query

registry = <sqlalchemy.orm.decl_api.registry object>
timepoint_id: int
unit: str
value: int

2.7. Users and tokens

This module contains the user database model.

class ptmd.database.models.user.User(username, password, email, role='disabled', organisation_id=None)

Bases: Model

User creator.

Parameters
  • username – the username

  • password – the password

  • email – the user email

  • organisation_id – the organisation id the user belongs to

activation_token
activation_token_id: int | None
change_password(old_password: str, new_password: str) bool

Change the user password.

Parameters
  • old_password – the old password

  • new_password – the new password

Returns

True if the password was changed, False otherwise

email: str
id: int
metadata = MetaData()
organisation
organisation_id: int | None
password: str
query: t.ClassVar[Query]

A SQLAlchemy query for a model. Equivalent to db.session.query(Model). Can be customized per-model by overriding query_class.

Warning

The query interface is considered legacy in SQLAlchemy. Prefer using session.execute(select()) instead.

query_class

alias of Query

registry = <sqlalchemy.orm.decl_api.registry object>
reset_token
reset_token_id: int | None
role: str
set_password(password: str) None

Set the user password. Helper function to avoid code repetition.

Parameters

password – the new password

Raises

PasswordPolicyError – if the password does not match the password policy

set_role(role: str) None

Set the user role. Helper function to avoid code repetition.

username: str
validate_password(password: str) bool

Checks if a user password is valid

Parameters

password – the password to check

Returns

True if the password is valid, False otherwise

This module contains the Token database model. It is used to store account creation and password reset tokens.

class ptmd.database.models.token.Token(token_type, user)

Bases: Model

A table to store tokens such as the email confirmation token and the password reset token.

Parameters
  • token_type – the type of the token

  • user – the User the token belongs to

expires_on: datetime
metadata = MetaData()
query: t.ClassVar[Query]

A SQLAlchemy query for a model. Equivalent to db.session.query(Model). Can be customized per-model by overriding query_class.

Warning

The query interface is considered legacy in SQLAlchemy. Prefer using session.execute(select()) instead.

query_class

alias of Query

registry = <sqlalchemy.orm.decl_api.registry object>
token: str
token_id: int
token_type: str

This module contains the Token block list database model. It is used to store expired JWT.

class ptmd.database.models.token_blocklist.TokenBlocklist(jti)

Bases: Model

Token block list required to log-out users

created_at
id
jti
metadata = MetaData()
query: t.ClassVar[Query]

A SQLAlchemy query for a model. Equivalent to db.session.query(Model). Can be customized per-model by overriding query_class.

Warning

The query interface is considered legacy in SQLAlchemy. Prefer using session.execute(select()) instead.

query_class

alias of Query

registry = <sqlalchemy.orm.decl_api.registry object>
ptmd.database.models.token_blocklist.check_if_token_revoked(jwt_header: dict, jwt_payload: dict) bool

Check if a JWT exists in the database blocklist

Parameters
  • jwt_header – JWT header

  • jwt_payload – JWT payload