class Password_reset(db.Model): _serialize_blacklist = ['token', 'email'] token = db.Column(db.String(44), primary_key=True) email = db.Column(db.String(120)) created_at = db.Column(db.DateTime(), default=datetime.utcnow) def __init__(self, token, email): self.token = token self.email = email
class Authorize(db.Model, Serializable): _serialize_blacklist = ['oauth_id', 'email', 'deleted_at'] id = db.Column(db.BigInteger, primary_key=True) oauth_id = db.Column(db.String(100), default=None, nullable=True, index=True) name = db.Column(db.String(100), default=None, nullable=True) gender = db.Column(db.String(100), default=None, nullable=True) picture = db.Column(db.String(512), default=None, nullable=True) locale = db.Column(db.String(100), default=None, nullable=True) verified = db.Column(db.Boolean(), default=True) friends = db.Column(db.BigInteger, default=None, nullable=True) email = db.Column(db.String(120), default=None, nullable=True) network = db.Column(db.String(20), index=True) user_id = db.Column(db.BigInteger, db.ForeignKey('user.id'), nullable=False) created_at = db.Column(db.DateTime(), default=datetime.utcnow) updated_at = db.Column(db.DateTime(), default=datetime.utcnow, onupdate=datetime.utcnow) deleted_at = db.Column(db.DateTime(), default=None) def __init__(self, oauth_id, name, gender, locale, verified, friends, email, network, user_id): self.oauth_id = oauth_id self.name = name self.gender = gender self.locale = locale self.verified = verified self.friends = friends self.email = email self.network = network self.user_id = user_id
class User(db.Model): """This class represents the customers and drivers table""" __tablename__ = 'users' user_id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(50), nullable=False) last_name = db.Column(db.String(50), nullable=False) credit_card = db.Column(db.String(20), nullable=False) email = db.Column(db.String(100), unique=True, nullable=False) driver = db.Column(db.Boolean) password = db.Column(db.String(25), nullable=False) date_created = db.Column(db.String(50)) date_modified = db.Column(db.String(50)) def __init__(self, first_name, last_name, email, username, password, credit_card, driver, date_created, date_modified): """Iniitalize with user info""" self.first_name = first_name self.last_name = last_name self.email = email self.username = username self.password = password self.credit_card = credit_card self.driver = driver self.date_created = date_created self.date_modified = date_modified def save(self): """Add user to database""" db.session.add(self) db.session.commit() def delete(self): """Delete user from database""" db.session.delete(self) db.session.commit() def __repr__(self): """Represent user by name""" return "{} {}".format(self.first_name, self.last_name) def tojson(self): """Represent user data as JSON object""" return { 'first_name': self.first_name, 'last_name': self.last_name, 'email': self.email, 'driver': self.driver } def is_driver(self): """ Returns True if self is a driver False if self is a customer """ return self.driver def has_incompleted_ride(self): """ Check if the user has incompleted ride Return false if the user does not have any incompleted ride (means all rides are completed) true if user has incompleted ride """ rides = Rides.find_ride_by_email(self.email) for ride in rides: if ride.is_completed() is False: return True return False def generate_token(self, user_id): """Generates the access token to be used as the Authorization header""" try: # set up a payload with an expiration time payload = { 'exp': datetime.utcnow() + timedelta(hours=24), 'iat': datetime.utcnow(), 'sub': user_id } # create the byte string token using the payload and the SECRET key jwt_string = jwt.encode(payload, current_app.config.get('SECRET'), algorithm='HS256') return jwt_string except Exception as e: # return an error in string format if an exception occurs return str(e) @staticmethod def find_user_by_user_id(user_id): """Find one user by user_id (Primary Key)""" try: return User.query.filter_by(user_id=user_id).first() except Exception as e: # return an error in string format if an exception occurs return str(e)
class User(db.Model, UserMixin, Serializable): _serialize_blacklist = [ 'password', 'email', 'phone', 'deleted_at', 'updated_at' ] id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(50), default=None, nullable=True) email = db.Column(db.String(120), unique=True, nullable=True, default=None) password = db.Column(db.String(120), default=None, nullable=True) phone = db.Column(db.String(60), default=None, nullable=True) picture = db.Column(db.String(30), default=None, nullable=True) locale = db.Column(db.String(4), default=None, nullable=True) created_at = db.Column(db.DateTime(), default=datetime.utcnow) updated_at = db.Column(db.DateTime(), default=datetime.utcnow, onupdate=datetime.utcnow) deleted_at = db.Column(db.DateTime(), default=None) def __init__(self, username, email, password): self.username = username self.email = email self.set_password(password) def set_password(self, password): if password is None: self.password = None else: self.password = generate_password_hash(password) def check_password(self, value): return check_password_hash(self.password, value) def is_authenticated(self): if isinstance(self, AnonymousUserMixin): return False else: return True def is_deleted(self): if self.deleted_at: return True else: return False def is_active(self): return True def is_admin(self): if self.id in current_app.config['ADMIN_USER_IDS']: return True return False def is_anonymous(self): if isinstance(self, AnonymousUserMixin): return True else: return False def get_id(self): return self.id def __repr__(self): return '<User %r>' % self.username