class Task(db.Model): __tablename__ = 'tasks' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(250), unique=True, nullable=False) description = db.Column(db.String) creation_date = db.Column(db.TIMESTAMP, server_default=db.func.current_timestamp(), nullable=False) status = db.Column(db.Integer, nullable=False, default=0) # Relationships users = db.relationship('User', secondary=task_users, backref=db.backref('tasks')) @hybrid_property def nr_users(self): return len(self.annotations) def update_users(self, user_ids): new_users = db.session.query(User).filter(User.id.in_(user_ids)).all() self.users = new_users db.session.commit() def __init__(self, name, status=0): self.name = name self.status = status
class Stock(db.Model, TimestampMixin): __tablename__ = "stocks" id = db.Column(db.Integer(), db.Sequence("stocks_id_seq"), primary_key=True) ticker = db.Column(db.String(15)) short_name = db.Column(db.String(255)) latest_market_data = db.Column(JSONB) company_info = db.Column(JSONB) __table_args__ = (UniqueConstraint("ticker", name="uq_stocks_ticker"), ) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @property def json(self): return { "id": self.id, "ticker": self.ticker, "short_name": self.short_name, "company_info": self.company_info, "latest_market_data": self.latest_market_data, } @property def json_short(self): return { "id": self.id, "ticker": self.ticker, "short_name": self.short_name, "latest_market_data": self.latest_market_data, }
class Role(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), unique=True) users = db.relationship('User', back_populates='role') permissions = db.relationship('Permission', secondary=roles_permissions, back_populates='roles') @staticmethod def init_role(): roles_permissions_map = { 'Locked': ['FOLLOW', 'COLLECT'], 'User': ['FOLLOW', 'COLLECT', 'COMMENT', 'UPLOAD'], 'Moderator': ['FOLLOW', 'COLLECT', 'COMMENT', 'UPLOAD', 'MODERATE'], 'Administrator': [ 'FOLLOW', 'COLLECT', 'COMMENT', 'UPLOAD', 'MODERATE', 'ADMINISTER' ] } for role_name in roles_permissions_map: role = Role.query.filter_by(name=role_name).first() if role is None: role = Role(name=role_name) db.session.add(role) role.permissions = [] for permission_name in roles_permissions_map[role_name]: permission = Permission.query.filter_by( name=permission_name).first() if permission is None: permission = Permission(name=permission_name) db.session.add(permission) role.permissions.append(permission) db.session.commit()
class UserRoles(db.Model): __tablename__ = "user_roles" id = db.Column(db.Integer(), primary_key=True) user_id = db.Column(UUID(as_uuid=True), db.ForeignKey("users.id", ondelete="CASCADE")) role_id = db.Column(db.Integer(), db.ForeignKey("roles.id", ondelete="CASCADE"))
class FinalFeedback(db.Model, CRUDMixin): id = db.Column(db.Integer, primary_key=True, autoincrement=True) motivation = db.Column(db.Enum(MotivationFeedback), nullable=False) user_uuid = db.Column(db.String(255), db.ForeignKey('user.uuid')) user = relationship("User", back_populates="feedback")
class Challenge(db.Model, CRUDMixin): """ Challenge model""" __tablename__ = "challenge" identifier = db.Column(db.String(255), primary_key=True) name = db.Column(db.String(255), nullable=False) description = db.Column(db.Text, nullable=False) help = db.Column(db.Text, nullable=True) external_link = db.Column(db.String(255), nullable=True) # Relations users = relationship("SolvedChallenges", back_populates="solved_challenge") def __repr__(self): return self.name or "" def to_dict(self): return dict(identifier=self.identifier, name=self.name, description=self.description, help=self.help, external_link=self.external_link) @classmethod def json_list(cls): return dict( list(map(lambda x: (x.identifier, x.to_dict()), cls.query.all())))
class RideshareAdditionalInfo(Base): """ Additional info that can be added to a tourister service """ rideshare_detail_id = db.Column(db.Integer, db.ForeignKey('rideshare_detail.id'), nullable=False) value = db.Column(db.String(100))
class TripPricing(Base): """ different pricing models for trips a tourister can provide """ trip_id = db.Column(db.Integer, db.ForeignKey('trips.id'), nullable=False) regular = db.Column(db.Float) tripDuration = db.Column(db.Float) overtimePrice = db.Column(db.Float)
class PaypalInfo(Base): """ information needed to use user's paypal """ tourister_id = db.Column(db.Integer, db.ForeignKey('touristers.id'), nullable=False) email = db.Column(db.String(100), nullable=False)
class ServicePerks(Base): """ perks that can be added to a tourister service """ service_id = db.Column(db.Integer, db.ForeignKey('services.id'), nullable=False) value = db.Column(db.String(100))
class CurrencyPair(db.Model, BaseModel): __tablename__ = 'currency_pair' symbol = db.Column(db.String(15), unique=True, nullable=False) from_currency_id = db.Column(db.Integer, db.ForeignKey('currency.id'), nullable=False) from_currency = db.relationship("Currency", foreign_keys=[from_currency_id], lazy='subquery') to_currency_id = db.Column(db.Integer, db.ForeignKey('currency.id'), nullable=False) to_currency = db.relationship("Currency", foreign_keys=[to_currency_id], lazy='subquery') @classmethod def get_by_symbol(cls, symbol): currency_pair = cls.query.filter_by(symbol=symbol).first() if not currency_pair: raise NoCurrencyPairBySymbolException(symbol) return currency_pair def __repr__(self): return self.symbol def get_market_repr(self, market_name): if market_name == "Binance": return self.binance_repr elif market_name == "Bitmex": return self.bitmex_repr return self.symbol def get_market_order_repr(self, market_name): if market_name == "Binance": return self.binance_order_repr elif market_name == "Bitmex": return self.bitmex_repr return self.symbol @property def bitmex_repr(self): return "{}/{}".format(self.to_currency.short_name.upper(), self.from_currency.short_name.upper()) @property def binance_repr(self): return "{}{}".format(self.to_currency.short_name.upper(), self.from_currency.short_name.upper()) @property def binance_order_repr(self): return "{}/{}".format(self.to_currency.short_name.upper(), self.from_currency.short_name.upper())
class Notification(db.Model): id = db.Column(db.Integer, primary_key=True) message = db.Column(db.Text, nullable=False) is_read = db.Column(db.Boolean, default=False) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) receiver_id = db.Column(db.Integer, db.ForeignKey('user.id')) receiver = db.relationship('User', back_populates='notifications')
class Education(Base): """ education of a tourister """ tourister_id = db.Column(db.Integer, db.ForeignKey('touristers.id'), nullable=False) school = db.Column(db.String(30)) degree = db.Column(db.String(30))
class TouristerImage(db.Model): """ used to associate many to many images to touristers """ __tablename__ = 'tourister_images' __table_args__ = (db.PrimaryKeyConstraint('tourister_id', 'image_id'), ) tourister_id = db.Column(db.Integer, db.ForeignKey('touristers.id'), nullable=False) image_id = db.Column(db.Integer, db.ForeignKey('image.id'), nullable=False)
class SocialNetwork(Base): """ different social networks of a tourister """ tourister_id = db.Column(db.Integer, db.ForeignKey('touristers.id'), nullable=False) type = db.Column(db.String(30)) src = db.Column(db.String(200))
class Appointment(ResourceMixin, db.Model): __tablename__ = 'appointments' # Properties id = db.Column(db.Integer, primary_key=True) scheduled_datetime = db.Column(db.Date, index=True) subject_matter = db.Column(db.String(1000), nullable=False) # Foreign Keys user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
class Testimonial(Base): """ testimonials given by touristees """ tourister_id = db.Column(db.Integer, db.ForeignKey('touristers.id'), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) user = db.relationship("User", uselist=False) story = db.Column(db.String(800), nullable=False)
class Language(Base): """ languages a tourister can speak/understand """ __tablename__ = 'languages' tourister_id = db.Column(db.Integer, db.ForeignKey('touristers.id'), nullable=False) name = db.Column(db.String(10), nullable=False) proficiency = db.Column(db.String(10), nullable=False)
class PortfolioStocks(db.Model): __tablename__ = "portfolio_stocks" portfolio_id = db.Column( db.Integer(), db.ForeignKey("portfolio.id", ondelete="CASCADE"), primary_key=True, ) stock_id = db.Column(db.Integer(), db.ForeignKey("stocks.id", ondelete="CASCADE"), primary_key=True)
class UserIdentifier(db.Model, CRUDMixin): __tablename__ = "user_identifier" id = db.Column(db.Integer, primary_key=True, autoincrement=True) ip_addr = db.Column(db.String(255), unique=False) user_agent = db.Column(db.String(2048), unique=False) # Relation users = relationship("User", secondary=user_identifier_association, back_populates="user_identifiers")
class Occupation(Base): """ Occupations touristers can exercise (to find matches) """ __tablename__ = 'occupations' tourister_id = db.Column(db.Integer, db.ForeignKey('touristers.id'), nullable=False) name = db.Column(db.String(30), nullable=False) affiliation = db.Column(db.String(30))
class Base(db.Model): """Base class for all the tables. Consists of two default columns `created_at` and `modified_at`. """ __abstract__ = True id = db.Column(db.Integer, primary_key=True, autoincrement=True) created_at = db.Column(db.DateTime, default=db.func.current_timestamp()) modified_at = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp())
class Activity(Base): """ activities a tourister has done """ tourister_id = db.Column(db.Integer, db.ForeignKey('touristers.id'), nullable=False) image_id = db.Column(db.Integer, db.ForeignKey('image.id'), nullable=False) detail = db.Column(db.String(300), nullable=False) image = db.relationship("Image", uselist=False)
class Service(Base): """ services a tourister can offer """ __tablename__ = 'services' trip_id = db.Column(db.Integer, db.ForeignKey('trips.id'), nullable=False) type = db.Column(db.String(30)) perks = db.relationship('ServicePerks', backref='service', lazy='dynamic') details = db.relationship('RideshareDetail', backref='service', uselist=False)
class Post(ResourceMixin, db.Model): __tablename__ = 'posts' # Relationships comments = db.relationship('Comment', backref=db.backref('post', uselist=False), passive_deletes=True, lazy='dynamic') # Properties id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), index=True, unique=True, nullable=False) body = db.Column(db.ARRAY(db.String), nullable=False) summary = db.Column(db.String(250), nullable=False) img_src = db.Column(db.String(200)) thumbnail_src = db.Column(db.String(200)) view_count = db.Column(db.Integer, server_default='0') # Foreign Keys author_id = db.Column(db.Integer, db.ForeignKey('staff.id')) practice_area_id = db.Column(db.Integer, db.ForeignKey('practice_areas.id')) def __init__(self, **kwargs): # Call Flask-SQLAlchemy's constructor. super(Post, self).__init__(**kwargs) @hybrid_property def slug(self): return slugify(self.title) @classmethod def find_by_title(cls, title): """ Find client by user ID. :param user_id: user ID :type title: str :return: Client instance """ return cls.query.filter(cls.title == title).first() def to_json(self): return { 'id': self.id, 'title': self.title, 'author': self.author.user.first_last_name, 'body': self.body, 'summary': self.summary, 'practiceArea': self.practice_area.area, 'imgSrc': self.img_src, 'thumbnailSrc': self.thumbnail_src, 'created': self.created_on, 'updated': self.updated_on, 'views': self.view_count, 'authorPhoto': self.author.user.photo, 'slug': self.slug }
class SolvedChallenges(db.Model): __tablename__ = 'solved_challenges' user_id = db.Column(db.String(255), db.ForeignKey('user.uuid'), primary_key=True) challenge_id = db.Column(db.String(255), db.ForeignKey('challenge.identifier'), primary_key=True) time_solved = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now) user = relationship("User", back_populates="solved_challenges") solved_challenge = relationship("Challenge", back_populates="users")
class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(250), unique=True, nullable=False) status = db.Column(db.Integer, nullable=False, default=0) creation_date = db.Column(db.TIMESTAMP, server_default=db.func.current_timestamp(), nullable=False) def __init__(self, name, status=None): self.name = name if status: self.status = status
class StockHistory(db.Model): __tablename__ = 'stocks_history' id = db.Column(db.Integer, primary_key=True) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) stock_info = db.Column(JSON) def __init__(self, timestamp, stock_info): self.timestamp = timestamp self.stock_info = stock_info def __repr__(self): return '<id {}>'.format(self.id)
class User(db.Model, TimestampMixin): __tablename__ = "users" id = db.Column(UUID(as_uuid=True), primary_key=True, nullable=False, default=uuid4) email = db.Column(db.String(255), nullable=False) first_name = db.Column(db.String(255), nullable=False) last_name = db.Column(db.String(255), nullable=False) password = db.Column(PasswordType(schemes=["bcrypt"]), nullable=False) is_active = db.Column(db.Boolean(), nullable=False, server_default="1") confirmed = db.Column(db.Boolean(), nullable=False, server_default="0") email_confirmed_at = db.Column(db.DateTime()) last_logged_in = db.Column(db.DateTime()) role = db.Column(db.String(100), nullable=True, server_default="user") __table_args__ = (UniqueConstraint("email", name="uq_users_email"),) def __init__(self, *args, **kwargs): super(User, self).__init__(*args, **kwargs) if not self.password: self.password = binascii.hexlify(os.urandom(24)).decode() @classmethod def auth(cls, **kwargs): email = kwargs.get("email") password = kwargs.get("password") if not email or not password: return None user = cls.query.filter_by(email=email).first() if not user or user.password != password: return None return user @property def json(self): return { "id": self.id, "first_name": self.first_name, "last_name": self.last_name, "email": self.email, # "portfolios": [portfolio.json for portfolio in self.portfolios], } def __repr__(self): return ( f"User(id={self.id}, email={self.email}, first_name={self.first_name}, " f"last_name={self.last_name}), confirmed={self.confirmed}" )
class User(Base): __tablename__ = "users" id = db.Column(db.Integer, primary_key=True, autoincrement=True) email = db.Column(db.String(255), unique=True, nullable=False) password = db.Column(db.String(255), nullable=False) admin = db.Column(db.Boolean, nullable=False, default=False) tourister = db.relationship('Tourister', backref='user', uselist=False) def __init__(self, email, password, admin=False): self.email = email self.password = bcrypt.generate_password_hash( password, app.config.get('BCRYPT_LOG_ROUNDS')).decode('utf-8') self.admin = admin