class File(db.Model): __tablename__ = 'files' id = db.Column(db.Integer, primary_key=True) qid = db.Column(db.String(60), nullable=False, server_default=db.FetchedValue()) path = db.Column(db.String(300), nullable=True, server_default=db.FetchedValue()) image = db.Column(db.LargeBinary(length=2048)) salt = db.Column(db.String(32), nullable=False, server_default=db.FetchedValue()) created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
class FlicketUser(Base): """ User model class """ __tablename__ = 'flicket_users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(user_field_size['username_max']), index=True, unique=True) name = db.Column(db.String(user_field_size['name_max'])) password = db.Column(db.LargeBinary(user_field_size['password_max'])) email = db.Column(db.String(user_field_size['email_max']), unique=True) date_added = db.Column(db.DateTime) date_modified = db.Column(db.DateTime, onupdate=datetime.datetime.now) job_title = db.Column(db.String(user_field_size['job_title'])) avatar = db.Column(db.String(user_field_size['avatar'])) def __init__(self, username, name, email, password, date_added, job_title=None): self.username = username self.name = name self.password = password self.email = email self.job_title = job_title self.date_added = date_added def __repr__(self): return '<User {}>'.format(self.username) @property def is_authenticated(self): return True @property def is_active(self): return True @property def is_anonymous(self): return False @property def is_admin(self): """ returns true if the user is a member of the 'flicket_admin' group""" user = FlicketUser.query.filter_by(id=self.id).first() for g in user.flicket_groups: if g.group_name == app.config['ADMIN_GROUP_NAME']: return True def get_id(self): return str(self.id)
class CommentImage(Base): __tablename__ = "image" name = db.Column(db.String(144), nullable=False) fileformat = db.Column(db.String(10), nullable=False) image_data = db.Column(db.LargeBinary(), nullable=False) def __init__(self, name): self.name = name def get_id(self): return self.id
class Image(db.Model): __tablename__ = 'images' id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, unique=True, nullable=False) path = db.Column(db.String) unit_features = db.Column(db.LargeBinary(length=2048)) magnitude = db.Column(db.Float) score = db.Column(db.Float) category_id = db.Column(UUID(as_uuid=True), db.ForeignKey('categories.id')) category = db.relationship("Category", back_populates="images")
class FlicketUser(PaginatedAPIMixin, Base): __tablename__ = 'flicket_users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(user_field_size['username_max']), index=True, unique=True) name = db.Column(db.String(user_field_size['name_max'])) password = db.Column(db.LargeBinary(user_field_size['password_max'])) email = db.Column(db.String(user_field_size['email_max']), unique=True) date_added = db.Column(db.DateTime) date_modified = db.Column(db.DateTime, onupdate=datetime.now) job_title = db.Column(db.String(user_field_size['job_title'])) avatar = db.Column(db.String(user_field_size['avatar'])) total_posts = db.Column(db.Integer, default=0) total_assigned = db.Column(db.Integer, default=0) token = db.Column(db.String(32), index=True, unique=True) token_expiration = db.Column(db.DateTime) locale = db.Column(db.String(10)) def __init__(self, username, name, email, password, date_added, job_title=None, locale='en'): """ :param username: username, must be unique. :param name: Full name. :param email: email address, must be unique. :param password: password. :param date_added: date added. :param job_title: job title / description. :param locale: locale 'en' = english. See app config for options. """ self.username = username self.name = name self.password = password self.email = email self.job_title = job_title self.date_added = date_added self.locale = locale @property def is_authenticated(self): """ :return: True if authenticated. """ return True @property def is_active(self): """ :return: True if active. """ return True @property def is_anonymous(self): """ :return: False if anonymous. """ return False @property def is_admin(self): """ Returns true if the user is a member of the 'flicket_admin' group. :return True if condition met. """ user = FlicketUser.query.filter_by(id=self.id).first() for g in user.flicket_groups: if g.group_name == app.config['ADMIN_GROUP_NAME']: return True else: return False def get_id(self): """ :return: self.id """ return str(self.id) def check_password(self, password): """ :param password: :return: True if password matches. """ result = FlicketUser.query.filter_by(username=self.username) if result.count() == 0: return False result = result.first() if bcrypt.hashpw(password.encode('utf-8'), result.password) != result.password: return False return True def to_dict(self): """ Returns a dictionary object about the user. :return: dict() """ data = { 'id': self.id, 'username': self.username, 'name': self.name, 'email': self.email, 'job_title': self.job_title if self.job_title else 'unknown', 'total_posts': self.total_posts, 'links': { 'self': url_for('bp_api_v2.get_user', id=self.id) } } return data def get_token(self, expires_in=36000): """ :param expires_in: :return: self.token """ now = datetime.utcnow() if self.token and self.token_expiration > now + timedelta(seconds=60): return self.token self.token = base64.b64encode(os.urandom(24)).decode('utf-8') self.token_expiration = now + timedelta(seconds=expires_in) db.session.add(self) return self.token def revoke_token(self): """ Method to expire the token. Typically used on logging out. :return: """ self.token_expiration = datetime.utcnow() - timedelta(seconds=1) @staticmethod def check_token(token): """ :param token: :return: None if token is expired otherwise returns self. """ user = FlicketUser.query.filter_by(token=token).first() if user is None or user.token_expiration < datetime.utcnow(): return None return user def __repr__(self): """ :return: str() with user details. """ return '<User {}>'.format(self.username)
class FlicketUser(PaginatedAPIMixin, UserMixin, Base): __tablename__ = 'flicket_users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(user_field_size['username_max']), index=True, unique=True) name = db.Column(db.String(user_field_size['name_max'])) password = db.Column(db.LargeBinary(user_field_size['password_max'])) email = db.Column(db.String(user_field_size['email_max']), unique=True) date_added = db.Column(db.DateTime) date_modified = db.Column(db.DateTime, onupdate=datetime.now) job_title = db.Column(db.String(user_field_size['job_title'])) avatar = db.Column(db.String(user_field_size['avatar'])) total_posts = db.Column(db.Integer, default=0) total_assigned = db.Column(db.Integer, default=0) token = db.Column(db.String(32), index=True, unique=True) token_expiration = db.Column(db.DateTime) locale = db.Column(db.String(10)) disabled = db.Column(db.Boolean, default=False) def __init__(self, username, name, email, password, date_added, job_title=None, locale='en', disabled=False): """ :param str() username: username, must be unique. :param str() name: Full name. :param str() email: email address, must be unique. :param str() password: password. :param str() date_added: date added. :param str() job_title: job title / description. :param str() locale: locale 'en' = english. See app config for options. """ self.username = username self.name = name self.password = password self.email = email self.job_title = job_title self.date_added = date_added self.locale = locale self.disabled = disabled @property def is_admin(self): """ Returns true if the user is a member of the 'flicket_admin' group. :return bool: """ user = FlicketUser.query.filter_by(id=self.id).first() for g in user.flicket_groups: if g.group_name == app.config['ADMIN_GROUP_NAME']: return True else: return False @property def is_super_user(self): """ Returns true if the user is a member of the 'super_user' group. :return bool: """ user = FlicketUser.query.filter_by(id=self.id).first() for g in user.flicket_groups: if g.group_name == app.config['SUPER_USER_GROUP_NAME']: return True else: return False def check_password(self, password): """ Returns True if password is validated. False if not or is user account is disabled. :param password: :return bool: """ users = FlicketUser.query.filter_by(username=self.username) if users.count() == 0: return False user = users.first() if user.disabled: return False if bcrypt.hashpw(password.encode('utf-8'), user.password) != user.password: return False return True @staticmethod def check_token(token): """ Returns True if token hasn't expired and user account isn't disabled. Otherwise False. :param token: :return bool: """ user = FlicketUser.query.filter_by(token=token).first() if user is None or user.token_expiration < datetime.utcnow(): return None if user.disabled: return None return user @staticmethod def generate_password(): """ A pseudo randomly generated password used for registered users wanting to reset their password. :return str(): """ characters = string.ascii_letters + string.digits password = ''.join(random.sample(characters, 12)) return password def get_token(self, expires_in=36000): """ :param expires_in: :return str(): self.token """ now = datetime.utcnow() if self.token and self.token_expiration > now + timedelta(seconds=60): return self.token self.token = base64.b64encode(os.urandom(24)).decode('utf-8') self.token_expiration = now + timedelta(seconds=expires_in) db.session.add(self) return self.token def revoke_token(self): """ Method to expire the token. Typically used on logging out. :return: """ self.token_expiration = datetime.utcnow() - timedelta(seconds=1) def to_dict(self): """ Returns a dictionary object about the user. :return dict: """ avatar_url = app.config['base_url'] + url_for('flicket_bp.static', filename='flicket_avatars/{}'.format("__default_profile.png")) if self.avatar: avatar_url = app.config['base_url'] + url_for('flicket_bp.static', filename='flicket_avatars/{}'.format(self.avatar)) data = { 'id': self.id, 'avatar': avatar_url, 'email': self.email, 'job_title': self.job_title if self.job_title else 'unknown', 'name': self.name, 'username': self.username, 'total_posts': self.total_posts, 'links': { 'self': app.config['base_url'] + url_for('bp_api.get_user', id=self.id), 'users': app.config['base_url'] + url_for('bp_api.get_users') } } return data def __repr__(self): """ :return: str() with user details. """ return '<User: id={}, username={}, email={}>'.format(self.id, self.username, self.email)