From 6bfb65172c7174d2090352dcdd17820d22afc26c Mon Sep 17 00:00:00 2001 From: TheAMM Date: Thu, 18 May 2017 15:28:51 +0300 Subject: [PATCH] Add helper functions to models.User --- nyaa/models.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/nyaa/models.py b/nyaa/models.py index 9ae3597..d82a013 100644 --- a/nyaa/models.py +++ b/nyaa/models.py @@ -343,6 +343,16 @@ class User(db.Model): def __repr__(self): return '' % self.username + def validate_authorization(self, password): + ''' Returns a boolean for whether the user can be logged in ''' + checks = [ + # Password must match + password == self.password_hash, + # Reject inactive and banned users + self.status == UserStatusType.ACTIVE + ] + return all(checks) + @classmethod def by_id(cls, id): return cls.query.get(id) @@ -357,6 +367,10 @@ class User(db.Model): user = cls.query.filter_by(email=email).first() return user + @classmethod + def by_username_or_email(cls, username_or_email): + return cls.by_username(username_or_email) or cls.by_email(username_or_email) + @property def is_admin(self): return self.level is UserLevelType.ADMIN or self.level is UserLevelType.SUPERADMIN