From 86a50a4f6df9ece982743a3b7ca510846d248909 Mon Sep 17 00:00:00 2001 From: leonoverweel Date: Tue, 24 Jan 2017 12:05:15 +0100 Subject: Initial commit --- opendc/models/user.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 opendc/models/user.py (limited to 'opendc/models/user.py') diff --git a/opendc/models/user.py b/opendc/models/user.py new file mode 100644 index 00000000..9c716470 --- /dev/null +++ b/opendc/models/user.py @@ -0,0 +1,49 @@ +import json + +from opendc.models.model import Model + +class User(Model): + + JSON_TO_PYTHON_DICT = { + 'User': { + 'id': 'id', + 'googleId': 'google_id', + 'email': 'email', + 'givenName': 'given_name', + 'familyName': 'family_name' + } + } + + TABLE_NAME = 'users' + COLUMNS = ['id', 'google_id', 'email', 'given_name', 'family_name'] + COLUMNS_PRIMARY_KEY = ['id'] + + @classmethod + def from_google_id(cls, google_id): + """Initialize a User by fetching them by their google id.""" + + user = cls._from_database('SELECT * FROM users WHERE google_id = ?', (google_id,)) + + if user is not None: + return user + + return User() + + @classmethod + def from_email(cls, email): + """Initialize a User by fetching them by their email.""" + + user = cls._from_database('SELECT * FROM users WHERE email = ?', (email,)) + + if user is not None: + return user + + return User() + + def google_id_has_at_least(self, google_id, authorization_level): + """Return True if the User has at least the given auth level over this User.""" + + if authorization_level in ['EDIT', 'OWN']: + return google_id == self.google_id + + return True -- cgit v1.2.3