5.1.2.1. Database Models

Contains documentation for all database model classes

5.1.2.1.1. Projects

Contains model classes related to managing projects on the Omicron server

class database.models.projects.Project(project_name, description, date_created=datetime.datetime(2016, 2, 22, 2, 44, 10, 672115), owner=None)[source]

Models a project for a given user. The project constructor requires the following

Variables:
  • project_name (str) – The name of the project
  • description (str) – A description of the project
  • date_created (datetime.datetime) – The date at which the project was created, defaults to the current datetime
  • owner (User) – The user who owns this project
__init__(project_name, description, date_created=datetime.datetime(2016, 2, 22, 2, 44, 10, 672115), owner=None)

Instantiates the variables described above

date_created_isoformat

Returns the project date created as an ISO 8601 - compliant string :return: The date string :rtype: str

get

Returns a dictionary representing a summary of the project. :return: A summary of the project :rtype: dict

5.1.2.1.2. Users

Contains all model classes relevant to management of users

class database.models.users.Administrator(username, password, email, date_created=datetime.datetime(2016, 2, 22, 2, 44, 10, 676934))[source]

Represents a “superuser” type. The administrator will be able to oversee all projects, revoke anyone’s token, and approve new users into the system, when user approval is complete.

class database.models.users.Token(token_string, expiration_date=None, owner=None)[source]

Contains methods for manipulating user tokens.

static hash_token(token_string)[source]

Takes a token string and hashes it using SHA256.

revoke()[source]

Expire the token by setting the expiration date equal to the current date

verify_token(token)[source]

Checks if the provided token string matches the token hash in the DB, and that the token is not expired :param str token: The token to verify :return: True if the token is valid, False if not

class database.models.users.User(username, password, email, date_created=datetime.datetime(2016, 2, 22, 2, 44, 10, 676934))[source]

Base class for a User.

__eq__(other)[source]
Parameters:other (User) – The user against which to compare
__ne__(other)[source]

Check if two users are not equal :param other:

classmethod from_session(username, session)[source]

Provides an alternate “constructor” by using the supplied session and returning the user matching the given username. The method also asserts that a user was returned by the query. If it did not happen, something horrible has happened.

Parameters:
  • username (str) – The username of the user to get
  • session (ContextManagedSession) – The session to use for retrieving the user
Returns:

The user to be retrieved

generate_auth_token(expiration=600, session=ContextManagedSession(bind=Engine(sqlite:////home/docs/checkouts/readthedocs.org/user_builds/omicron-server/checkouts/latest/test_db.sqlite3), expire_on_commit=True))[source]

Generates a token for the user. The user’s token is a UUID 1, randomly generated in this function. A Token is created with this randomly-generated UUID, the UUID token is hashed, and stored in the database.

Warning

The string representation of the generated token is returned only once, and is not recoverable from the data stored in the database. The returned token is also a proxy for the user’s password, so treat it as such.

Parameters:
  • expiration (int) – The lifetime of the token in seconds.
  • session (ContextManagedSession) – The database session with which this method will interact, in order to produce a token. By default, this is a ContextManagedSession that will point to conf.DATABASE_ENGINE, but for the purposes of unit testing, it can be repointed.
Returns:

A tuple containing the newly-created authentication token, and the expiration date of the new token. The expiration date is an object of type Datetime

Return type:

tuple(str, Datetime)

static hash_password(password)[source]

Hash the user’s password :param str password: The password to hash :return:

verify_auth_token(token_string)[source]

Loads the user’s current token, and uses that token to check whether the supplied token string is correct

Parameters:token_string (str) – The token to validate
Returns:True if the token is valid and False if not
verify_password(password)[source]

Verify the user’s password :param str password: The password to verify :return: True if the password is correct, else False :rtype: bool