Session (pydent.aqsession)

Session class for interacting with Trident and Aquarium. To initialize a new session, you’ll need your Aquarium login credentials and an Aquarium url:

from pydent import AqSession
session = AqSession('joeshmoe', 'pass123', 'http://myaquariumurl.org`)

Note

To login discretely with a hidden password, you may use the pydent.login method.

After initializing a session, models are accessible from the Session:

sample = session.Sample.one()
last50 = session.Item.last(50)
first10 = session.SampleType.first(10)
mysamples = session.Sample.last(10, query={'user_id': 66})

User Objects

The AqSession and Browser are the main interaction objects for querying the Aquarium server. The Browser class provides special methods for speeding up and caching results from the server, which is covered in Advanced Topics.

AqSession(login, password, aquarium_url[, …])

Holds an AqHTTP with login information.

Browser(session[, inherit_models])

A class for browsing models and Aquarium inventory.

Non-User Objects

These modules provide utility support for Trident. These are not relevant for most users.

SessionABC

Session abstract base class.

AqHTTP(login, password, aquarium_url)

Defines a Python to Aquarium server connection.

Interfaces

Interfaces govern how the session searches, loads, and dumps Aquarium models from the server.

BrowserInterface(model_name, aqhttp, session)

Initializer for SessionInterface.

CRUDInterface(aqhttp, session)

Create, Update, Delete interface.

QueryInterface(model_name, aqhttp, session)

Makes requests using AqHTTP that are model specific.

QueryInterfaceABC

Interface that is used by models to find other models.

SessionInterface(aqhttp, session)

Generic session interface.

UtilityInterface(aqhttp, session)

Miscellaneous and specialized requests for creating, updating, etc.

class pydent.aqsession.AqSession(login, password, aquarium_url, name=None, aqhttp=None)[source]

Bases: pydent.sessionabc.SessionABC

Holds an AqHTTP with login information. Creates SessionInterfaces for models.

session1 = AqSession(username, password, aquarium_url)
session1.User.find(1)
# <User(id=1,...)>

Initializes a new trident Session.

Parameters
  • login (str) – the Aquarium login for the user

  • password (str) – the password for the Aquarium login. This will not be stored anywhere. However, login headers for Aquarium will be stored in .__aqhttp. This may change in the future with changes authentication for the Aquarium API

  • aquarium_url (str) – the http formatted Aquarium url

  • name (str or None) – (optional) name for this session

_aqhttp = None

requests interface

_browser = None

the sessions browser

_initialize_interfaces()[source]

Initializes the session’s interfaces.

_log_to_aqhttp(msg)[source]

Sends a log message to the aqhttp’s logger.

static _swap_sessions(from_session, to_session)[source]

Moves models from one session to another.

property current_user

Returns the current User associated with this session.

Returns None if no user is found (as in cases where the Aquarium connection is down).

Return type

ModelBase

property interface_class

Returns the session’s interface class.

Return type

Type

logged_in()[source]

Returns whether the user is logged in. If the session is able to return the User model instance using the session’s login credentials, the user is considered to be logged in.

Returns

whether user is currently logged in

Return type

boolean

property login

Logs into aquarium, generating the necessary headers to perform requests to Aquarium.

Return type

str

model_interface(model_name, interface_class=None)[source]

Returns model interface by name.

Return type

SessionInterface

property models

Returns list of all models available.

Return type

List[str]

open()[source]

Open Aquarium in a web browser window.

ping(num=5)[source]

Performs a number of simple requests (pings) to estimate the speed of the server.

Displays a message about the average time each ping took.

Return type

Optional[int]

query(data, use_cache=False)[source]

Perform a complex query a complex JSON query object.

Check out the JSON Schema page for more information.

You may also checkout AqSession.query_schema to view the JSON schema that validates the input.

{
  "__model__": "Item",
  "__description__": "Get available primers in last 7 days",
  "query": {
    "object_type": {
      "query": {
        "sample_type": {
          "query": {
            "name": "Primer"
          }
        }
      }
    },
    "created_at": {
      "__time__": {
        "__days__": -7
      }
    },
    "location": {
        "__not__": "deleted"
    }
    "__options__": {
      "limit": 1
    }
  }
}
..versionadded:: 0.1.5a16

Added query method for complex queries

Changed in version 0.1.5a23: query key now changed to __query__

Parameters
  • data (Dict) – data query

  • use_cache (bool) – whether to inherit the cache from the provided session (default: False)

Return type

List[ModelBase]

Returns

list of models fitting the query

Raises

AquariumQueryLanguageValidationError if input data is invalid.

property session

Return self.

Return type

AqSession

set_timeout(timeout_in_seconds)[source]

Sets the request timeout.

property url

Returns the aquarium_url for this session.

property utils

Instantiates a utility interface.

with_cache(using_requests=None, using_models=False, timeout=None, verbose=None)[source]
Parameters
  • using_requests (Optional[bool]) – if False, ForbiddenRequest will be raised if requests are made using the session.

  • using_models (bool) – if True (default: False), derived session will inherit the current sessions model_cache

  • timeout (Optional[int]) – the requests timeout in seconds

  • verbose – if True, verbose mode will be activated for the derived session

Return type

AqSession

Returns