ResearchNotes.database package

Database sub module

As the database growth, it will be split into files and submodules to make it easier to handel.

Submodules

ResearchNotes.database.basics module

Define all base classes. We define database schemes for

  • Groups

  • Roles

  • User

  • Locations

class ResearchNotes.database.basics.Role(**kwargs)[source]

Bases: Model

Define the different roles that Users can have.

Currently, we define 4 different roles: Admin, Student, Supervisor, Ex-Student.

The role is required for certain administrative tasks and used in the role_required decorator of the auth.py module (used mainly in the conf.py module).

Creates user.role_member as back reference.

id: int
name: str
description: str
members
class ResearchNotes.database.basics.Groups(**kwargs)[source]

Bases: Model

Database scheme to define the groups that a user will be member of.

The group has several one-to-many relationships that will make finding stuff easier.

members relating to Users (hence group. members should contain all group ids of the members) Creates field user.group_member

locations for samples locations Creates samples.group_locations

mtypes are the measurement types of the group Creates MeasurementType.group_mtype

id: int
name: str
members
samples
owned_instruments
locations
journal_entries
instrument_templates
journal_entry_templates
mtypes
documents
itypes
items
class ResearchNotes.database.basics.User(**kwargs)[source]

Bases: Model

Defines the user db Scheme.

User have an ID, name, a full name (UserName), password as password hash, marking if active or not. The field confirmed has any use up to now? We also track login dates (last login, current login, login IP and login Count).

Several one-to-many relationships as well as a many-to-many relationship are defined here

We get a group_id and a role_id

We do backref to samples, measurements and reports

Creates Sample.sample_owner, Measurements.measurement_owner, Report.report_owner

Back-refers to many-to-many relationship

samples.sharedsamples_owner

We have one function defined as properties that gives the id of the sharedsamples

id: int
name: str
UserName: str
email: str
password: str
active: bool
confirmed_at: datetime
last_login_at: datetime
current_login_at: datetime
last_login_ip: str
current_login_ip: str
login_count: int
group_id: int
role_id: int
samples
measurements
reports
created_instruments
created_entries
sharedsamples
shared_instruments
checked_out_items
property is_active: bool

Return True, if user is active.

Use database entry to set a property, if user is active or not.

Returns:

True if user is active.

Return type:

bool

property sharedsamplelist: set[int]

Give a set of ids in user.sharedsamples.

This should be the sample records shared to the user.

Returns:

Set of samples IDs with shared user.

Return type:

set

property samplelist: set[int]

Give a set of ids in user.samples.

This should be the sample owned by the user.

Returns:

Set of samples IDs with shared user.

Return type:

set

class ResearchNotes.database.basics.ParentLocations(**kwargs)[source]

Bases: Model

Database scheme for Locations.

We have id and name of location

One-to-many relation
  • Groups

Backref
  • Locations

pl_id: int
name: str
group_id: int
sublocations
class ResearchNotes.database.basics.Locations(**kwargs)[source]

Bases: Model

Database scheme for Locations.

We have id and name of location

One-to-many relation
  • Groups

Backref
  • Samples

  • Instruments

id: int
name: str
group_id: int
parent_location_id: int
samples
instruments
items

ResearchNotes.database.documents module

All document related database schemes go here

class ResearchNotes.database.documents.Documents(**kwargs)[source]

Bases: SearchableMixin, Model

Database structure for all the documents.

Here we store all our documentation and use the data here to create the wiki. Backref and the function for children and parents relate the documents to each other.

In praxis, we use the label to find the document and display title and body. Body can contain markdown and needs to be escaped before display.

id: int
label: str
title: str
body: str
created: datetime
updated: datetime
group_id: int
creator_id: int
updatetor_id: int
children
add_child(doc)[source]

Add document as a child to parent.

Parameters:

doc (Document) – Document, we become a child from.

Return type:

None.

remove_child(doc)[source]

Remove document form parent database entry.

Parameters:

doc (Document) – Remove this document as child from parent document.

Return type:

None.

is_child(doc) bool[source]

Test, if a document is a child of document passed as doc.

Parameters:

doc (Document) – Document to check, if we are already child from.

Returns:

True, if we are a child from.

Return type:

bool

property childrenlist: set[int]

Create a list of all child documents ids.

Returns:

Children of the document.

Return type:

list[int]

ResearchNotes.database.ess_ppm_report module

Every databse scheme for ESS including the templates

class ResearchNotes.database.ess_ppm_report.Samples(**kwargs)[source]

Bases: SearchableMixin, Model

Defines the Sample database scheme.

A sample has an id, identifier, origin, short description, long description and creator (Name or combination of names).

We note creation date and update name. Update date should be also updated, if measurement or report are updated.

One-to-many relation:

  • creator (user, how makes he record) as user id.

  • Group-id

  • Location

Backref:

  • Measurements

  • Reports

Many-to-many relation:

-SharedSamples

id: int
identifier: str
origin: str
short_dis: str
long_dis: str
creator: str
created: datetime
updated: datetime
creator_id: int
group_id: int
location_id: int
measurements
reports
sharedsamples
property sharedsamplelist: List[int]

Give a list of ids in samples.sharedsamples.

This should be the ids of the user the sample is shared to.

Returns:

List of user IDs to whom sample is shared.

Return type:

list

property is_shared: bool

Return true, if the sample is shared to someone.

Returns:

True if there is an entry in the sharedsamplelist, i.e. exits user.

Return type:

Bool

class ResearchNotes.database.ess_ppm_report.MeasurementType(**kwargs)[source]

Bases: Model

Defines the MeasurementType database scheme.

Type has an ID and a name. It relates to its group and the measurements attached to it.

id: int
name: str
group_id: int
measurements
class ResearchNotes.database.ess_ppm_report.Measurements(**kwargs)[source]

Bases: SearchableMixin, Model

Measurement database scheme.

Measurements have an id, short description, long description, creator, a creation date and an update date

One-to-many

  • Measurement type (mtype)

  • Sample ID

  • User ID of person who made measurement report (might not be the creator)

Backref

  • Reports

id: int
short_dis: str
long_dis: str
creator: str
created: datetime
updated: datetime
mtype_id: int
sample_id: int
creator_id: int
instrument_id: int
reports
class ResearchNotes.database.ess_ppm_report.Reports(**kwargs)[source]

Bases: SearchableMixin, Model

Defines the Report database scheme. A report has an ID, title and a long description.

We note creator (free name), creation date and updated date.

One-to-many relations:
  • Sample

  • User submitting report (User ID)

  • Measurement report is about

id: int
title: str
long_dis: str
creator: str
created: datetime
updated: datetime
sample_id: int
creator_id: int
measurement_id: int
class ResearchNotes.database.ess_ppm_report.TemplateSamples(**kwargs)[source]

Bases: Model

ESS template database structure.

The class is used for all templates related to create E/S/S entries. They can either be created from scratch or from existing samples.

id: int
tname: str
identifier: str
origin: str
short_dis: str
long_dis: str
creator_id: int
group_id: int
group_share: bool
created: datetime
updated: datetime
instruments_default
class ResearchNotes.database.ess_ppm_report.TemplateMeasurements(**kwargs)[source]

Bases: Model

PPM database structure.

Class for all PMM templates to come from exiting PMM or created from scratch.

id: int
tname: str
short_dis: str
long_dis: str
creator_id: int
group_id: int
group_share: bool
created: datetime
updated: datetime
mtype_id: int
creator: str
instruments_default
class ResearchNotes.database.ess_ppm_report.TemplateReports(**kwargs)[source]

Bases: Model

Report template database structure (unused).

This guy is currently not used as we have no report templates.

id: int
tname: str
title: str
long_dis: str
creator_id: int
group_id: int
group_share: bool
created: datetime
updated: datetime

ResearchNotes.database.instrument_journal module

All databases schemes for instrumentation and instrumentation journal entries (including templates) go here.

class ResearchNotes.database.instrument_journal.Instrument(**kwargs)[source]

Bases: SearchableMixin, Model

Define the Instrument database scheme.

An instrument has an id, an identifier, a description, and a creator. It also stores creation and update date.

One-to-many relations:

  • user_id

  • group_id

  • location_id

Backref:

  • InstrumentationJournalEntry

Many-to-many relation:

  • InstrumentSharedWithGroups

id: int
identifier: str
description: str
active: bool
created: datetime
updated: datetime
creator_id: int
group_id: int
location_id: int
default_ess_template_id: int
default_ppm_template_id: int
default_entry_template_id: int
journal_entries
measurements
etypes
guest_users
property guest_users_list: list[int]

Return a list with the IDs of all guest users

Returns:

The IDs of the users this instrument is shared with

Return type:

List[int]

property is_shared: bool

Return true, if the instrument is shared with some group.

Returns:

True if there is an entry in the shared_with_groups_list.

Return type:

Bool

class ResearchNotes.database.instrument_journal.TemplateInstrument(**kwargs)[source]

Bases: Model

Instrument template database scheme.

Class for instrument templates, that can be created from exiting instruments or made from scratch.

id: int
creator_id: int
group_id: int
tname: str
location_id: int
identifier: str
description: str
created: datetime
updated: datetime
class ResearchNotes.database.instrument_journal.EntryType(**kwargs)[source]

Bases: Model

Defines the EntryType database scheme.

Type has an ID and a name. It relates to its instrument.

id: int
name: str
instrument_id: int
entries
class ResearchNotes.database.instrument_journal.InstrumentationJournalEntry(**kwargs)[source]

Bases: SearchableMixin, Model

Define the Instrumentation Journal Entry database scheme.

An entry has an id, an identifier, a description, and a creator. It also stores creation and update date.

One-to-many relations:

  • user_id

  • group_id

  • location_id

id: int
identifier: str
description: str
created: datetime
updated: datetime
etype_id: int
creator_id: int
instrument_id: int
group_id: int
class ResearchNotes.database.instrument_journal.TemplateInstrumentationJournalEntry(**kwargs)[source]

Bases: Model

Instrumentation journal entry template database scheme.

Class for instrumentation journal entry templates, that can be created from exiting entries or made from scratch.

id: int
creator_id: int
group_id: int
tname: str
identifier: str
description: str
created: datetime
updated: datetime
etype_id: int
instruments_default

ResearchNotes.database.inventory module

All database entries for inventory tracking - items and item_types go here.

class ResearchNotes.database.inventory.ItemType(**kwargs)[source]

Bases: Model

Defines the ItemType database scheme.

itype_id: int
name: str
group_id: int
items
class ResearchNotes.database.inventory.Items(**kwargs)[source]

Bases: Model

Defines the Item database scheme.

item_id: int
identifier: str
name: str
vendor: str
catalog: str
description: str
amount: float
unite: str
amount_is_int: bool
is_permanent: bool
available: float
creator: str
created: datetime
updated_by: int
updated: datetime
itype_id: int
group_id: int
location_id
item_user
property is_int: bool

True if the amount should be an integer.

property user_list: set[int]

Set of user IDs who checked out the item.

item_by_user(user_id: int) int[source]

Return the ItemCheckOut entry for a given user of this item.

class ResearchNotes.database.inventory.ItemsCheckOut(**kwargs)[source]

Bases: Model

Association table between item and user.

item_id: int
user_id: int
item
user
amount: float
class ResearchNotes.database.inventory.ItemsTemplate(**kwargs)[source]

Bases: Model

Defines the item template database scheme.

tid: int
tname: str
identifier: str
name: str
vendor: str
catalog: str
description: str
unite: str
amount_is_int: bool
creator: str
created: datetime
updated_by: int
updated: datetime
group_id: int

ResearchNotes.database.mixins module

Define all mixin classes for later reimport

class ResearchNotes.database.mixins.SearchableMixin[source]

Bases: object

Mixin class for database to add search function in fulltext search engine.

We define some functions to index automatically some fields in a search engine database. Also, a search function is provided to retrieve information via a search query.

This works currently with Elasticsearch and Meilisearch.

id: int
classmethod search(expression: str) tuple[Any, int][source]

Search expression inside the index of a fulltext search engine.

Parameters:

expression (str) – String to be searched in the index. We call the query_index function as defined in the search submodule.

Return type:

tuple[SQLAlchemy database record, Int]

classmethod before_commit(session) None[source]

Remember objects of database session as changed before the commit to the database.

We need to remember our objects that changed (like new, updated or deleted) and add, update or delete them accordingly form our search index. Hence, the function is called before every commit of a session by SQLAlachemy (see below as registered as event.listen to db object).

Parameters:

session (SQLAlchemy class or object) – session of our database (should be db.session most of the time).

Return type:

None

classmethod after_commit(session) None[source]

Function to be called after the commit to the database.

After successful commit to the database, this function is called to add, update or delete entries in our search index.

Parameters:

session (SQLAlchemy.session) – Session for our database

Return type:

None

classmethod reindex()[source]

Reindex a database model in the fulltext search engine.

This will delete the index of the database model in the full text search engine database and rewrite the index for the db.Model adding all fields marked as searchable.

Needs to be run the first time or if the search index somehow lost the connection to the actual database entries, e.g. if the search engine was down and new entries have been committed.

Return type:

None

classmethod make_index() None[source]

Create the search index for the class.