fietsboek.models.user module¶
User models for fietsboek.
- fietsboek.models.user.FINGERPRINT_SHAKE_BYTES = 10¶
Number of bytes to include in the SHAKE digest of the fingerprint.
- class fietsboek.models.user.FriendRequest(**kwargs)¶
Bases:
Base
Represents a request of friendship between two
User
s.- Variables:
- date¶
- id¶
- recipient_id¶
- sender_id¶
- exception fietsboek.models.user.PasswordMismatch¶
Bases:
Exception
Exception that is raised if the passwords mismatch.
Using an exception forces the handling of the password mismatch and makes it impossible to accidentally ignore the return value of the password verification.
- fietsboek.models.user.SESSION_SECRET_LENGTH = 32¶
Length of the secret bytes for the user’s session.
- fietsboek.models.user.TOKEN_LIFETIME = datetime.timedelta(days=1)¶
Maximum validity time of a token.
- class fietsboek.models.user.Token(**kwargs)¶
Bases:
Base
A token is something that a user can use to perform certain account related functions.
A token with type
TokenType.VERIFY_EMAIL
can be used by the user to verify their email address.A token with type
TokenType.RESET_PASSWORD
can be used by the user to reset their password.- Variables:
- date¶
- classmethod generate(user, token_type)¶
Generate a new token for the given user.
- id¶
- token_type¶
- user_id¶
- uuid¶
- class fietsboek.models.user.TokenType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)¶
Bases:
Enum
Type of tokens.
A token can be used either to verify the user’s email, or it can be used to reset the password.
- RESET_PASSWORD = 2¶
A token that can be used to reset a user’s password.
- VERIFY_EMAIL = 1¶
A token that can be used to verify a user’s email.
- class fietsboek.models.user.User(**kwargs)¶
Bases:
Base
A fietsboek user.
- Variables:
id (int) – Database ID.
name (str) – Name of the user.
password (bytes) – (Hashed) Password of the user. Note that you should use
set_password()
andcheck_password()
to interface with the password instead of accessing this field directly.salt (bytes) – Password salt.
email (str) – Email address of the user.
session_secret (bytes) – Secret bytes for the session fingerprint.
is_admin (bool) – Flag determining whether this user has admin access.
is_verified (bool) – Flag determining whether this user has been verified.
tracks (list[fietsboek.models.track.Track]) – Tracks owned by this user.
tagged_tracks (list[fietsboek.models.track.Track]) – Tracks in which this user is tagged.
uploads (list[fietsboek.models.track.Upload]) – Currently ongoing uploads by this user.
tokens (list[fietsboek.models.user.Token]) – List of tokens that this user can use.
comments (list[fietsboek.model.comment.Comment]) – List of comments left by this user.
- add_friend(friend)¶
Add the given user as a new friend.
- Parameters:
friend (User) – The user to befriend.
- all_tracks_query()¶
Returns a query that selects all the user’s tracks.
This includes the user’s own tracks, as well as any tracks the user has been tagged in.
The returned query can be further modified, however, you need to use
sqlalchemy.orm.aliased()
to access the correct objects:>>> from fietsboek.models.track import Track, TrackType >>> from sqlalchemy import select >>> from sqlalchemy.orm import aliased >>> user = retrieve_user() >>> query = user.all_tracks_query() >>> query = query.filter(query.c.type == TrackType.ORGANIC) >>> query = select(aliased(Track, query))
- Return type:
- authenticated_user_id()¶
Returns a string suitable to re-identify this user, e.g. in a cookie or session.
The returned ID contains a “fingerprint” to ensure that the re-identified user matches.
- Return type:
- Returns:
The string used to re-identify this user.
- autocomplete_tags()¶
Returns all tags the user has ever used, suitable for autocompletion lists.
- check_password(password)¶
Checks if the given password fits for the user.
Does nothing if the passwords match, raises an exception otherwise.
- Raises:
PasswordMismatch – When the password does not match the stored one.
- Parameters:
password (str) – The password to check.
- email¶
- classmethod factory(request)¶
Factory method to pass to a route definition.
This factory retrieves the user based on the
user_id
matched route parameter, and returns the user. If the user is not found,HTTPNotFound
is raised.
- classmethod get_by_authenticated_user_id(session, user_id)¶
Retrieves a user by their authenticated user ID (as returned by
authenticated_user_id()
).This method returns
None
on any of the following conditions:The user ID is malformed.
The user with the given ID was not found.
The fingerprint of the
user_id
and the database user do not match.
- get_friends()¶
Returns all friends of the user.
This is not a simple SQLAlchemy property because the friendship relation is complex.
- id¶
- incoming_requests¶
- is_admin¶
- is_verified¶
- name¶
- outgoing_requests¶
- password¶
- principals()¶
Returns all principals that this user fulfills.
This does not include the
Everyone
andAuthenticated
principals, as those have to be checked before accessing the user.
- classmethod query_by_email(email)¶
Returns a query that can be used to query a user by its email.
This properly ensures that the email is matched case-insensitively.
- Parameters:
email (str) – The email address to match.
- Returns:
The prepared query.
- Return type:
- remove_friend(friend)¶
Remove the friend relationship between two users.
- Parameters:
friend (User) – The befriended user.
- roll_session_secret()¶
Rolls a new session secret for the user.
This function automatically generates the right amount of random bytes from a secure source.
- salt¶
- session_secret¶
- set_password(new_password)¶
Sets a new password for the user.
This function automatically generates a new random salt and updates the stored password hash and salt value.
- Parameters:
new_password (str) – The new password of the user.
- toggle_favourite(track)¶
Toggles the favourite status for the given track.
- Parameters:
track (
Track
) – The track to (un)favour.
- classmethod visible_tracks_query(user=None)¶
Returns a query that selects all tracks visible to the given user.
The user might be
None
, in which case tracks for public users are returned.The returned query can be further modified, however, you need to use
sqlalchemy.orm.aliased()
to access the correct objects (see alsoall_tracks_query()
).- Parameters:
user (User) – The user for which the tracks should be searched.
- Return type: