예제 #1
0
def create(user_email: str, session_id: bytes) -> None:
    """Store the session id in the database.

    Args:
        user_email: User's email in Cognito.
        session_id: New session id.

    Raises:
        `db.ConditionalCheckFailedError` if the session id already exists.
        `db.DatabasError` if there was an error connecting to the database.

    """
    attributes: SessionAttributes = {
        'ExpiresAt': _get_session_ttl()
    }

    sess_hash = _hex_hash(session_id)
    pk = db.PartitionKey(ent.Session, sess_hash)
    sk_session = db.SortKey(ent.Session, sess_hash)
    sk_user = db.SortKey(ent.User, user_email)

    # We create a session entity in the database while creating a
    # SESSION-USER relation to make sure that there can be no duplicate
    # session ids in the database. Can not use conditions for this purpose,
    # as those require knowing the primary (composite) key which we don't
    # without querying the database.
    get_table().transact_write_items([
        db.InsertArg(pk, sk_session, attributes=attributes),
        db.InsertArg(pk, sk_user, attributes=attributes)
    ])
예제 #2
0
def _get_trial_end_op(user_email: str, project_domain: str, trial_days: int) \
        -> db.InsertArg:
    trial_end = get_trial_end_date(trial_days)
    pk = db.PartitionKey(ent.TrialEnd, trial_end)
    # Concatenation of values ensures uniqueness of item.
    sk = db.SortKey(ent.TrialEnd, f'{user_email}|{project_domain}')
    return db.InsertArg(pk, sk)
예제 #3
0
def _get_user_create_op(user_email: str, project_domain: str) -> db.InsertArg:
    pk = db.PartitionKey(ent.User, user_email)
    sk = db.SortKey(ent.Sub, project_domain)
    attr: SubAttributes = {
        'IsActive': True
    }
    return db.InsertArg(pk, sk, attr)
예제 #4
0
sk_order1 = db.SortKey(Order, '2020-02-21|order-1')
sk_order2 = db.SortKey(Order, '2020-02-21|order-2')

logging.info('Testing insert')
table.insert(pk_alice, sk_alice)

logging.info('Testing update_attributes')
table.update_attributes(pk_alice, sk_alice, {'MyJson': {'A': 1}})

logging.info('Testing get_item')
res = table.get(pk_alice, sk_alice, attributes=['MyJson'], consistent=True)
assert res['MyJson']['A'] == 1, res

logging.info('Testing transact_write_items')
table.transact_write_items([
    db.InsertArg(pk_alice, sk_order1),
    db.InsertArg(pk_book, sk_order1),
    db.InsertArg(pk_alice, sk_order2),
    db.InsertArg(pk_book, sk_order2)
])

logging.info('Testing batch_get')
res = table.batch_get([
    db.PrimaryKey(pk_alice, sk_order1),
    db.PrimaryKey(pk_book, sk_order1),
    db.PrimaryKey(pk_alice, sk_order2),
],
                      consistent=True)
assert len(res.items) == 3, res

logging.info('Testing query_prefix')
예제 #5
0
def _get_group_create_op(group_name: str, project_domain: str) -> db.InsertArg:
    pk_gr = db.PartitionKey(ent.Group, group_name)
    sk_gr = db.SortKey(ent.GroupSub, project_domain)
    attr: GroupSubAttributes = {'IsActive': True}
    return db.InsertArg(pk_gr, sk_gr, attributes=attr)