ResearchNotes.database_transactions package

Module for database transactions split into different files following the naming convention of the database module.

Every complex database transaction like updating and creating records goes here. Queries and “one-liner” transactions should be left out of it.

To avoid conflict with existing functions and make them easy to identify this module will be imported completely as dbt.

We will import all functions to __initi__ to maker we them import to the other sub-modules without changes.

Submodules

ResearchNotes.database_transactions.basics module

Contains all database transaction functions for the basic moduls - User, Groups, Locations and init_db

class ResearchNotes.database_transactions.basics.Transaction(database: SQLAlchemy)[source]

Bases: object

Database transaction context manager.

Allows with as statement for our database operations.

ResearchNotes.database_transactions.basics.create_roles(database: SQLAlchemy) None[source]

Create the Admin, Supervisor, Student, and ExStudent roles. Also, location and Mtypes.

Some basic roles as well as needed basic entries for Location and Measurement Types) are created.

Parameters:

database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

Return type:

None.

ResearchNotes.database_transactions.basics.create_admin(database: SQLAlchemy) None[source]

Create a generic admin user.

Parameters:

database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

Return type:

None.

ResearchNotes.database_transactions.basics.create_index(database: SQLAlchemy, label: str, user_info: dict) None[source]

Create the first index of the Documents tree.

It is created for the group using the scheme group_name+’_index’. These pages are protected through the database - mainly, one cannot rename them. As pages are only deletable, without children, the main page cannot be deleted, when it has some pages linked from it.

Parameters:
  • database (FlaskSQLAlchemy) – Database handler that the Flask APP is using.

  • label (str) – Label to be given to the document entry

  • user_info (dict) – Information of user creating the index

Return type:

None.

ResearchNotes.database_transactions.basics.update_session_metadata(database: SQLAlchemy, user: User, IP_address: Optional[str] = None, login: bool = True) None[source]

Update user metadata.

Updates last login metadata, that being what IP address it was made from and at what time.

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • user (User) – The user whose last login metadata is updated.

  • IP_address (str, optional) – String representation of the IP address of the user. The default is None.

  • login (bol, optional) – Mark if this is called at login or logout. The default is True.

Return type:

None.

ResearchNotes.database_transactions.basics.create_user(database: SQLAlchemy, user_info: dict) None[source]

Create user entry.

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • user_info (dict) – Info dict containing the needed user information

ResearchNotes.database_transactions.basics.update_user(database: SQLAlchemy, user: User, user_info: dict) None[source]

Update user data based on information passed.

Parameters:
  • database (FlaskSQLAlchemy class.) – Database handler that the Flask APP is using

  • user (User) – Entry of User object to be updated.

  • user_info (Dict)

Return type:

None.

ResearchNotes.database_transactions.basics.deactivate_user(database: SQLAlchemy, user: User) None[source]

Deactivate user.

Registers a user deactivation in the database, by changing its role to ExStudent, updating its “active” status, and resetting its password

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • user (User) – Entry to User whose deactivation is registered.

Return type:

None.

ResearchNotes.database_transactions.basics.activate_user(database: SQLAlchemy, user: User) None[source]

Reactivates user, if it was inactive (and makes it a Student).

Registers a user activation in the database, by changing its role to Student and updating its “active” status

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • user (User) – Entry to User whose activation is registered.

Return type:

None.

ResearchNotes.database_transactions.basics.rename_group(database: SQLAlchemy, group: Groups, name: str) None[source]

Renames a group Changes the name of a group and relabels documents indexed to it

Parameters:
  • database (SQLAlchemy) – Database handler that the Flask APP is using.

  • group (Groups.) – Group to update name.

  • name (str) – New name for group.

Return type:

None.

ResearchNotes.database_transactions.basics.delete_group(database: SQLAlchemy, group: Groups) None[source]

Deletes a group

Removes the group itself, without removing any information associated with it. Instead, all its members, samples, documents, mtypes, and locations are moved to the orphan group None (gid = 1)

Parameters:
  • database (SQLAlchemy) – database

  • group (Groups) – group

Return type:

None

ResearchNotes.database_transactions.basics.create_parent_location(database: SQLAlchemy, location_info: dict) None[source]

Creates a location in the database

Parameters:
  • database (SQLAlchemy) – database

  • location_info (dict) – Information needed to initialize a Locations object

Return type:

None.

ResearchNotes.database_transactions.basics.create_location(database: SQLAlchemy, location_info: dict) None[source]

Creates a location in the database

Parameters:
  • database (SQLAlchemy) – database

  • location_info (dict) – Information needed to initialize a Locations object

Return type:

None.

ResearchNotes.database_transactions.basics.create_mtype(database: SQLAlchemy, mtype_info: dict) None[source]

Creates a measurement in the database

Parameters:
  • database (SQLAlchemy) – The database/session object to write to.

  • mtype_info (dict) – Information needed to initialize a Measurement Type object.

Return type:

None.

ResearchNotes.database_transactions.database_transactions module

Module for database transactions.

Every complex database transaction like updating and creating records goes here. Queries and “one-liner” transactions should be left out of it.

To avoid conflict with existing functions and make them easy to identify this module will be imported completely as dbt.

ResearchNotes.database_transactions.documents module

All database transactions for the Documents go here.

ResearchNotes.database_transactions.documents.create_document(database: SQLAlchemy, info_doc: dict, parent_id: int) None[source]

Create a new document.

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • info_doc (dict.) – Dict to contain the document information.

  • parent_id (int) – Document id of the parent document

Return type:

None.

ResearchNotes.database_transactions.documents.update_document(database: SQLAlchemy, document: Documents, doc_data: dict) None[source]

Update an existing document.

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • document (Document) – Entry of Document to be updated.

  • doc_data (dict) – Information of document to be updated.

Return type:

None.

ResearchNotes.database_transactions.ess_ppm_report module

All database transactions for the ESS, PPM and Reports go here.

We also include related templates to not separate them from their parent things.

ResearchNotes.database_transactions.ess_ppm_report.create_measurement(database: SQLAlchemy, info_measurement: dict) int[source]

Create a new measurement.

We will also update the sample updated time to mark the change to the sample related information.

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • info_measurement (dict.) – Entry of Measurements to be created.

Returns:

ID of the created measurement.

Return type:

int

ResearchNotes.database_transactions.ess_ppm_report.update_measurement(database: SQLAlchemy, measurement: Measurements, info_measurement: dict) None[source]

Update Measurement record in database.

Parameters:
  • database (FlaskSQLAlchemy class)

  • measurement (Measurements) – Entry of Measurements to be updated.

  • info_measurement (Dict)

Return type:

None

ResearchNotes.database_transactions.ess_ppm_report.create_report(database: SQLAlchemy, info_report: dict) int[source]

Add a report to the database and updates relevant timestamps.

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • info_report (dict) – Entry to Reports to be added to the database.

Returns:

ID of the new created report.

Return type:

int.

ResearchNotes.database_transactions.ess_ppm_report.update_report(database: SQLAlchemy, report: Reports, report_data: dict) None[source]

Update the title, long description, and timestamps of a report.

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • report (Reports) – Entry in Reports to be updated.

  • report_data (Dict.) – Dict that contains the updated content of the report.

Return type:

None.

ResearchNotes.database_transactions.ess_ppm_report.create_sample(database: SQLAlchemy, sample_data: dict) int[source]

Create a new ESS entry.

Parameters:
  • database (SQLAlchemy) – The database class we want the sample created in.

  • sample_data (dict) – Record to contain info for sample creation.

Returns:

ID of the newly created sample.

Return type:

int

ResearchNotes.database_transactions.ess_ppm_report.update_sample(database: SQLAlchemy, sample: Samples, sample_data: dict) None[source]

Update the metadata associated with a sample.

Pass a dict with the needed information (identifier, origin, long description, and short description).

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • sample (Sample) – Entry of Sample object to be updated.

  • sample_data (dict) – Containing the updated fields.

Return type:

None.

ResearchNotes.database_transactions.ess_ppm_report.create_template_sample(database: SQLAlchemy, temp_info: dict) None[source]

Create a template sample based on the data of a web form

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • temp_info (dict) – Information needed to initialize a TemplateSamples object

Return type:

None.

ResearchNotes.database_transactions.ess_ppm_report.update_template_sample(database: SQLAlchemy, template: TemplateSamples, temp_info: dict) None[source]

Update a template sample based on the data of a web form.

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • template (TemplateSamples)

  • temp_info (Dict)

Return type:

None.

ResearchNotes.database_transactions.ess_ppm_report.create_template_measurement(database: SQLAlchemy, temp_info: dict) None[source]

Creates a template measurement based on the data of a web form.

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • temp_info (Dict) – Information needed to initialize a TemplateMeasurements object

Return type:

None.

ResearchNotes.database_transactions.ess_ppm_report.update_template_measurement(database: SQLAlchemy, template: TemplateMeasurements, temp_info: dict) None[source]

Update a template measurement based on the data of a web form.

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • template (TemplateMeasurements)

  • temp_info (Dict)

Return type:

None.

ResearchNotes.database_transactions.instrument_journal module

All database tramsactions related to the instrumentattion and instrumentation journal go here.

Again, related templates for instruments and JEntres are inside this modul.

ResearchNotes.database_transactions.instrument_journal.create_instrumentation_journal_entry(database: SQLAlchemy, entry_info: dict) int[source]

Create an instrumentation journal entry.

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • entry_info (Dict) – Information needed to initialize an Instrumentation Journal Entry object

Returns:

ID of the newly created jentry.

Return type:

int

ResearchNotes.database_transactions.instrument_journal.update_instrumentation_journal_entry(database: SQLAlchemy, entry: InstrumentationJournalEntry, entry_info: dict) None[source]

Update an instrumentation journal entry

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • entry_info (Dict) – Updated fields of an Instrumentation Journal Entry object

Return type:

None.

ResearchNotes.database_transactions.instrument_journal.create_etype(database: SQLAlchemy, etype_info: dict) None[source]
Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • etype_info (Dict) – Instrumentation journal entry type information

Return type:

None

ResearchNotes.database_transactions.instrument_journal.update_etype(database: SQLAlchemy, etype: EntryType, etype_info: dict) None[source]

Updates existing entry types.

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using

  • etype (EntryType) – Instrumentation journal entry type to update

  • etype_info (Dict) – Instrumentation journal entry type information

Return type:

None

ResearchNotes.database_transactions.instrument_journal.create_instrument(database: SQLAlchemy, instrument_info: dict) int[source]

Creates an instrument based on the data of a web form.

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • instrument_info (Dict) – Information needed to initialize an Instrument object

Returns:

New instrument ID.

Return type:

int

ResearchNotes.database_transactions.instrument_journal.update_instrument(database: SQLAlchemy, instrument: Instrument, instrument_info: dict[str | int | None]) None[source]

Update the metadata associated with an instrument.

Pass a dict with the needed information (identifier, description, and location_id).

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • sample (Sample) – Entry of Sample object to be updated.

  • sample_data (dict) – Containing the updated fields.

Return type:

None.

ResearchNotes.database_transactions.instrument_journal.deactivate_instrument(database: SQLAlchemy, instrument: Instrument) None[source]

Deactivate an instrument.

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • instrument (Instrument) – Instrument to be deactivated.

Return type:

None.

ResearchNotes.database_transactions.instrument_journal.activate_instrument(database: SQLAlchemy, instrument: Instrument) None[source]

Activate an instrument.

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • instrument (Instrument) – Instrument to be activated.

Return type:

None.

ResearchNotes.database_transactions.instrument_journal.update_default_instrument_templates(database: SQLAlchemy, instrument: Instrument, default_ess: None | int, default_ppm: None | int, default_jentry: None | int) None[source]

Update the default templates of the instrument.

Update the default templates for ESS, PPM and JEntry. If set to 0 or None, we set things to None in the database. This should resolve the problems seen with more advanced databases and foreign key referring.

Parameters:
  • database – SQLAlchemy Database object to update.

  • instrument – Table to update (here Instruments)

  • default_ess (None|int) – ID of the default EES template or None

  • default_ppm (None|int) – ID of the default PPM template or None

  • default_jentry (None|int) – ID of the default JEntry template or None

ResearchNotes.database_transactions.instrument_journal.create_template_instrument(database: SQLAlchemy, template_info: dict) None[source]

Create a template instrument.

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • template_info (Dict) – Information needed to create a TemplateInstrument object

Return type:

None.

ResearchNotes.database_transactions.instrument_journal.update_template_instrument(database: SQLAlchemy, template: TemplateInstrument, template_info: dict) None[source]

Update a instrument template based on the data of a web form.

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • template (TemplateSamples)

  • temp_info (Dict)

Return type:

None.

ResearchNotes.database_transactions.instrument_journal.create_template_entry(database: SQLAlchemy, template_info: dict) None[source]

Create a template instrumentation journal entry.

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • template_info (Dict) – Information needed to create a TemplateInstrumentationJournalEntry object

Return type:

None.

ResearchNotes.database_transactions.instrument_journal.update_template_entry(database: SQLAlchemy, template: TemplateInstrumentationJournalEntry, template_info: dict) None[source]

Update a instrumentation journal entry template based on the data of a web form.

Parameters:
  • database (FlaskSQLAlchemy class) – Database handler that the Flask APP is using.

  • template (TemplateInstrumentationJournalEntry)

  • template_info (Dict)

Return type:

None.

ResearchNotes.database_transactions.inventory module

All database transactions for the inventory go here

ResearchNotes.database_transactions.inventory.create_itemtype(database: SQLAlchemy, itype_info: dict) None[source]

Creates an item type in the database.

Parameters:
  • database (SQLAlchemy) – The database/session object to write to.

  • itype_info (dict) – Information needed to initialize a Measurement Type object.

Return type:

None.

ResearchNotes.database_transactions.inventory.create_item(database: SQLAlchemy, info_dict: dict) int[source]

Database transaction to create a new item record.

Parameters:
  • database (SQLAlchemy) – Database record to write to (table)

  • info_dict (dict) – The information of the record to create.

Returns:

ID of te newly created item.

Return type:

int

ResearchNotes.database_transactions.inventory.update_item(database: SQLAlchemy, item: Items, info_dict: dict) None[source]

Database transaction to update an itme record.

Parameters:
  • database (SQLAlchemy) – datebase object to find tale of item.

  • item (Items) – Table entry to update

  • info_dict (dict) – Information with the updated information.

Return type:

None

ResearchNotes.database_transactions.inventory.checkout_item(database: SQLAlchemy, item: Items, amount: float, user: User) None[source]

Enters amount of item checked out by user into database.

Parameters:
  • database (SQLAlchemy)

  • item (Items) – The item to check out.

  • amount (float) – Amount of item checked out.

  • user (User) – who checks out the item.

Return type:

None

ResearchNotes.database_transactions.inventory.create_template_from_item(database: SQLAlchemy, info_dict: dict) None[source]

Database transaction to create a new item record.

Parameters:
  • database (SQLAlchemy) – Database record to write to (table)

  • info_dict (dict) – The information of the record to create.

Returns:

ID of te newly created item.

Return type:

int

ResearchNotes.database_transactions.inventory.update_item_template(database: SQLAlchemy, item_template: ItemsTemplate, info_dict: dict) None[source]

Database transaction to create a new item record.

Parameters:
  • database (SQLAlchemy) – Database record to write to (table).

  • item_template (ItemsTemplate) – Template to update.

  • info_dict (dict) – The information of the record to create.

Returns:

ID of te newly created item.

Return type:

int