database module

Module for interacting with database.

class database.User(*args, **kwargs)

Bases: pymodm.base.models.MongoModel

exception DoesNotExist

Bases: pymodm.errors.DoesNotExist

exception MultipleObjectsReturned

Bases: pymodm.errors.MultipleObjectsReturned

objects

The default manager used for MongoModel instances.

This implementation of BaseManager uses QuerySet as its QuerySet class.

This Manager class (accessed via the objects attribute on a MongoModel) is used by default for all MongoModel classes, unless another Manager instance is supplied as an attribute within the MongoModel definition.

Managers have two primary functions:

  1. Construct QuerySet instances for use when querying or working with MongoModel instances in bulk.
  2. Define collection-level functionality that can be reused across different MongoModel types.

If you created a custom QuerySet that makes certain queries easier, for example, you will need to create a custom Manager type that returns this queryset using the from_queryset() method:

class UserQuerySet(QuerySet):
    def active(self):
        '''Return only active users.'''
        return self.raw({"active": True})

class User(MongoModel):
    active = fields.BooleanField()
    # Add our custom Manager.
    users = Manager.from_queryset(UserQuerySet)

In the above example, we added a users attribute on User so that we can use the active method on our new QuerySet type:

active_users = User.users.active()

If we wanted every method on the QuerySet to examine active users only, we can do that by customizing the Manager itself:

class UserManager(Manager):
    def get_queryset(self):
        # Override get_queryset, so that every QuerySet created will
        # have this filter applied.
        return super(UserManager, self).get_queryset().raw(
            {"active": True})

class User(MongoModel):
    active = fields.BooleanField()
    users = UserManager()

active_users = User.users.all()
original_image

A field that stores unicode strings.

password

A field that stores unicode strings.

processed_image

A field that stores unicode strings.

username

A field that stores email addresses.

database.add_user(username, password)

Creates new user if user does not exist in the mongo database

Parameters:
  • username – user email as string type which serves as user id
  • password – user password as string type
Returns:

updates user information in mongo database

database.delete_image(name)

Deletes image stored in server :param name: name (uuid) of an image stored in the VM server

database.delete_user(username)

Deletes user from mongo database :param username: user email as string type which serves as user id

database.get_original_image(username)

Gets the original image uuid for a user :param username: user email as string type which serves as user id :returns: uuid of user’s original image as a string

database.get_processed_image(username)

Gets the processed image uuid for a user :param username: user email as string type which serves as user id :returns: uuid of user’s processed image as a string

database.get_user(username)

Gets user by unique username :param username: user email as string type which serves as user id :returns: user information

database.login_user(username, password)

Returns true if user exists and has the correct password :param username: user email as string type which serves as user id :param password: user password as string type :returns: True if password is correct, False if incorrect

database.remove_images(username)

Removes all images associated with a user :param username: user email as string type which serves as user id

database.save_original_image_uuid(username, uuid)

Updates existing user by adding the uuid of a user-uploaded image :param username: user email as string type which serves as user id :param uuid: UUID4 of user-uploaded image :returns: adds uuid of user-uploaded image to mongo database

database.save_processed_image_uuid(username, uuid)

Updates existing user by adding the uuid of the processed image :param username: user email as string type which serves as user id :param uuid: UUID4 of processed image :returns: adds uuid of processed image to mongo database