def update(dashboard): """ Updates a dashboard in the repository with the values of the provided dashboard. :param dashboard: The Dashboard instance to update its counterpart in the repository with. """ # Update item itself: transaction.commit() # Update indexes for item: for attempt in transaction.manager.attempts(): with attempt: database_root().dashboards.update_item(dashboard) transaction.begin()
def add(dashboard): """ Adds a dashboard to the repository. :param dashboard: The Dashboard instance to add to the repository. :raises (KeyError, DuplicateIndexError): When a dashboard with the same id already exists in the repository. """ try: transaction.begin() database_root().dashboards.add(dashboard) transaction.commit() except (KeyError, DuplicateIndexError): transaction.abort() raise
def delete(dashboard): """ Deletes a dashboard from the repository. :param dashboard: The Dashboard instance to delete from the repository. :raises KeyError: When the given dashboard does not exist within the repository. """ try: transaction.begin() database_root().dashboards.remove(dashboard) transaction.commit() except KeyError: transaction.abort() raise
def _delete(user): """ Removes the provided User-entity from the repository. :param user: The User-entity to remove. :raises KeyError: if the user does not exist in the repository. """ try: transaction.begin() database_root().users.remove(user) transaction.commit() except KeyError: transaction.abort() raise
def find_by_name(name): """ Returns a single User-entity with the given `name`, or None if it could not be found. :param name: A string denoting the name of the user we hope to find. """ return database_root().users.get('name', name, default=None)
def find_by_verification_code(verification_code): """ Returns a single User-entity with the given `verification_code`, or None if it could not be found. The latter case might indicate that the user does not exist, or that the verification code has expired. :param verification_code: A VerificationCode instance denoting the verification code of the user we hope to find. """ return database_root().users.get('_verification_code', verification_code, default=None)
def find(user_id): """ Finds a user in the database. :param user_id: UUID for the user to be retrieved. :return: User object or None if no user could be found. """ # Ensure that also callable with strings or integers: if not isinstance(user_id, uuid.UUID): user_id = uuid.UUID(user_id) return database_root().users['id', user_id]
def update(user): """ Updates a user in the repository with the values of the provided user. :param user: The User instance to update its counterpart in the repository with. >>> gandalf = User("GandalfTheGrey", "pass", '*****@*****.**') >>> add(gandalf) >>> gandalf.name = "GandalfTheWhite" >>> update(gandalf) >>> find_by_name("GandalfTheGrey") == gandalf False >>> find_by_name("GandalfTheWhite") == gandalf True """ transaction.commit() for attempt in transaction.manager.attempts(): with attempt: database_root().users.update_item(user) transaction.begin()
def add(user): """ Adds the User-entity to the repository. :param user: The User-entity to add. :raises (KeyError, DuplicateIndexError): If a user with one of the same indexes already exists within the repository. >>> list(all()) [] >>> gandalf = User("Gandalf", "pass", '*****@*****.**') >>> dumbledore = User("Dumbledore", "secret", '*****@*****.**') >>> add(gandalf) >>> add(dumbledore) >>> sorted([user.name for user in all()]) ['Dumbledore', 'Gandalf'] """ try: transaction.begin() database_root().users.add(user) transaction.commit() except (KeyError, DuplicateIndexError): transaction.abort() raise
def all(): """ Returns a (lazy) collection of all users (in no guaranteed order). >>> list(all()) [] >>> gandalf = User("Gandalf", "pass", '*****@*****.**') >>> dumbledore = User("Dumbledore", "secret", '*****@*****.**') >>> add(gandalf) >>> add(dumbledore) >>> sorted([user.name for user in all()]) ['Dumbledore', 'Gandalf'] >>> clear_all() >>> sorted([user.name for user in all()]) [] """ return database_root().users.values()
def clear_all(): """ Flushes the repository. >>> gandalf = User("Gandalf", "pass", '*****@*****.**') >>> dumbledore = User("Dumbledore", "secret", '*****@*****.**') >>> add(gandalf) >>> add(dumbledore) >>> sorted([user.name for user in all()]) ['Dumbledore', 'Gandalf'] >>> clear_all() >>> list(all()) [] """ transaction.begin() database_root().users = MultiIndexedPersistentCollection({'id', 'name', '_verification_code'}) transaction.commit()
def find(dashboard_id): """ Find and retrieve the dashboard from the repository with id `dashboard_id`. :param dashboard_id: A string, integer or UUID instance representing the id of the dashboard in question. :return: A Dashboard instance if it is in the repository. :raises KeyError: If no dashboard with id `dashboard_id` is found in the repository. :raises Exception: If some internal error occurred. """ # Ensure that this is also callable with strings or integers: if not isinstance(dashboard_id, uuid.UUID): dashboard_id = uuid.UUID(dashboard_id) logger.info(f"Starting to look for dashboard {dashboard_id}") try: res = database_root().dashboards['id', dashboard_id] logger.info(f"FOUND DASHBOARD in find_dashboard: {res}") return res except Exception as e: logger.error(f"EXCEPTION: {e}") raise
def clear_all(): """Clears the entire repository.""" transaction.begin() database_root().dashboards = MultiIndexedPersistentCollection({'id'}) transaction.commit()
>>> delete(dashboard) >>> delete(dashboard) Traceback (most recent call last): ... KeyError """ import uuid import transaction import pydash_logger from pydash_database import database_root, MultiIndexedPersistentCollection from multi_indexed_collection import DuplicateIndexError logger = pydash_logger.Logger(__name__) if not hasattr(database_root(), 'dashboards'): print("CREATING DASHBOARDS OBJECT") transaction.begin() database_root().dashboards = MultiIndexedPersistentCollection({'id'}) transaction.commit() def find(dashboard_id): """ Find and retrieve the dashboard from the repository with id `dashboard_id`. :param dashboard_id: A string, integer or UUID instance representing the id of the dashboard in question. :return: A Dashboard instance if it is in the repository. :raises KeyError: If no dashboard with id `dashboard_id` is found in the repository. :raises Exception: If some internal error occurred. """ # Ensure that this is also callable with strings or integers:
def all_unverified(): """Returns a collection of all unverified users (in no guaranteed order).""" return database_root().users.values('_verification_code')
def all(): """Returns an iterable collection of all dashboards in the repository (in no guaranteed order).""" return database_root().dashboards.values()
It handles a subset of the following tasks (specifically, it only actually contains functions for the tasks the application needs in its current state!): - Creating new entities of the specified type - Finding them based on certain attributes - Persisting updated versions of existing entities. - Deleting entities from the persistence layer. """ import uuid import transaction from pydash_database import database_root, MultiIndexedPersistentCollection from multi_indexed_collection import DuplicateIndexError from .entity import User if not hasattr(database_root(), 'users'): transaction.begin() database_root().users = MultiIndexedPersistentCollection({'id', 'name', '_verification_code'}) transaction.commit() def find(user_id): """ Finds a user in the database. :param user_id: UUID for the user to be retrieved. :return: User object or None if no user could be found. """ # Ensure that also callable with strings or integers: if not isinstance(user_id, uuid.UUID): user_id = uuid.UUID(user_id)