Python API Reference¶
Models¶
-
class
gridt.models.user.User(username, email, password, role='user')¶ Bases:
sqlalchemy.ext.declarative.api.ModelIntuitive representation of users in the database.
Parameters: Attribute password_hash: Hashed version of the users’s password.
Attribute follower_associations: All associations to movements where the follower is this user. Useful for determining the leaders of a user.
Attribute movements: List of all movements that the user is subscribed to.
Todo: Make a user.leaders dictionary attribute that has movements as the keys and lists of leaders as the values. Right now this is solved with the leaders method.
-
delete_from_db()¶ Delete this user from the database.
Warning: This is a permanent action and cannot be undone.
-
hash_password(password)¶ Hash password and set it as the password_hash. :param str password: Password that is to be hashed.
-
leaders(movement)¶ Find all the leaders of this user in the provided movement.
Parameters: movement (gridt.models.movement.Movement) – The movement that the user is to retrieve the leaders from.
-
save_to_db()¶ Save this user and all of the changes made to it to the database.
-
verify_password(password)¶ Verify that this password matches with the hashed version in the database. :rtype bool:
-
-
class
gridt.models.movement_user_association.MovementUserAssociation(movement=None, follower=None, leader=None)¶ Bases:
sqlalchemy.ext.declarative.api.ModelAssociation class that lies at the foundation of the network. Think of this class as the arrows that connect followers with leaders within their respective circle of the movement.
Parameters: - follower (model.user.User) – User that will be following.
- leader (model.user.User) – User that will lead.
- movement (model.movement.Movement) – Movement in which this relationship is happening.
Attribute leader: The leading user.
Attribute follower: The following user.
Attribute movement: The movement in which this connection happens.
-
delete_from_db()¶ Delete this association from the database.
Warning: This action is permanent and can not be undone.
-
save_to_db()¶ Save this association to the database.
-
class
gridt.models.movement.Movement(name, interval, short_description='', description='')¶ Bases:
sqlalchemy.ext.declarative.api.ModelIntuitive representation of movements in the database.
from datetime import timedelta flossing = Movement('flossing', timedelta(days=2)) robin = User.find_by_id(1) pieter = User.find_by_id(2) jorn = User.find_by_id(3) flossing.users = [robin, pieter, jorn] flossing.save_to_db()
Note: changes are only saved to the database when
Movement.save_to_db()is called.Parameters: - name (str) – Name of the movement
- interval (datetime.timedelta) – Interval in which the user is supposed to repeat the action.
- short_description (str) – Give a short description for your movement.
Attribute str description: More elaborate description of your movement.
Attribute users: All user that have been subscribed to this movement.
Attribute user_associations: All instances of
models.movement_user_association.MovementUserAssociationwith that link to this movement.-
add_user(user)¶ Add a new user to self.users and give it appropriate leaders. Find followers without leaders and the user as a leader.
Parameters: user (gridt.models.user.User) – the user that is to be subscribed to this movement Todo: Move find leader logic into private function.
-
delete_from_db()¶ Delete this movement from the database.
Warning: This is permanent and irrevocable.
-
dictify(user)¶ Return a dict version of this movement, ready for shipping to JSON.
Parameters: user – The user that requests the information.
-
classmethod
find_by_id(identifier)¶ Find a movement by it’s id. :param identifier: Id of movement that is being queried. :rtype: None or gridt.models.movement.Movement
-
classmethod
find_by_name(name)¶ Find a movement by it’s name. :param name: Name of movement that is being queried. :rtype: None or gridt.models.movement.Movement
-
find_leaders(user, exclude=[])¶ Private function to look for ids of leaders that this user could use.
Parameters: - user (gridt.models.user.User) – User that needs new leaders.
- exclude (list) – List of users (can be a user model or an id) to exclude from search.
Returns: A list of ids of users, or None if the user is not in this movement.
-
remove_user(user)¶ Remove any relationship this user previously had with this movement. Deleting any leader as well as follower relationship.
Parameters: user – user to be deleted.
-
save_to_db()¶ Store this movement in the database, making changes to the movement permanent.
-
swap_leader(user, leader)¶ Swap out the presented leader in the users leaders.
Parameters: - user – User who’s leader will be swapped.
- leader – The leader that will be swapped.
Returns: New leader or None
-
class
gridt.models.update.Update(leader, movement)¶ Bases:
sqlalchemy.ext.declarative.api.ModelRepresentation of updates in the database.
Attribute leader: The leader that created this update. Attribute movement: The movement that this update was created in.