class PriceModel(db.Model): __tablename__: str = "price" uuid: db.Column = db.Column(db.String(32), primary_key=True, unique=True) name: db.Column = db.Column(db.String(255), nullable=False) description: db.Column = db.Column(db.String(255), nullable=False) value: db.Column = db.Column(db.Float(), nullable=False) @property def serialize(self): dict = self.__dict__ key = '_sa_instance_state' if key in dict: del dict[key] return dict @staticmethod def create(name, description, value): uuid = str(uuid4()).replace('-', '') price: PriceModel = PriceModel(uuid=uuid, name=name, description=description, value=value) db.session.add(price) db.session.commit() return price
class ApplicationAdNetworkModel(db.Model): __tablename__ = 'application_adnetwork' application_id = db.Column(UUID(as_uuid=True), db.ForeignKey('application.id', onupdate="CASCADE", ondelete="CASCADE"), primary_key=True) ad_network_id = db.Column(UUID(as_uuid=True), db.ForeignKey('ad_network.id', onupdate="CASCADE", ondelete="CASCADE"), primary_key=True) score = db.Column(db.Float(), primary_key=True, default=0.0) ad_network = db.relationship('AdNetworkModel') last_update = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) @classmethod def find_by_ids(cls, application_network_id, ad_network_id): app_adn = cls.query.filter_by(application_id=application_network_id, ad_network_id=ad_network_id).first() return app_adn def update_score(self, score): self.score = score self.last_update = datetime.utcnow() try: db.session.commit() except Exception as e: app.logger.error('Could not update score : {e}'.format(e=e)) db.session.rollback()
class Transacoes(db.Model): id_transacao = db.Column(db.Integer, primary_key=True) id_conta = db.Column(db.Integer, db.ForeignKey('contas.id_conta'), nullable=False) valor = db.Column(db.Float(precision=2), nullable=False) data_transacao = db.Column(db.DateTime, server_default=sqlalchemy.sql.func.now()) def __init__(self, id_conta, valor): self.id_conta = id_conta self.valor = valor
class Contas(db.Model): id_conta = db.Column(db.Integer, primary_key=True) id_pessoa = db.Column(db.Integer, db.ForeignKey('pessoas.id_pessoa'), nullable=False) saldo = db.Column( db.Float(precision=2), nullable=False) # monetario - limitar negativo ? deposito negativo limite_saque_diario = db.Column( db.Float(precision=2), nullable=False) # monetario - limitar negativo ? flag_ativo = db.Column(db.Boolean, default=True) tipo_conta = db.Column(db.Integer, nullable=False) data_criacao = db.Column(db.DateTime, server_default=sqlalchemy.sql.func.now()) def __init__(self, id_pessoa, saldo, limite_saque_diario, flag_ativo, tipo_conta): self.id_pessoa = id_pessoa self.saldo = saldo self.limite_saque_diario = limite_saque_diario self.flag_ativo = flag_ativo self.tipo_conta = tipo_conta
class CrawledDataListEntity(db.Model): __tablename__ = 'CrawledDataListentity' cid = db.Column(db.String(255), primary_key=True) content = db.Column(db.Text()) timestamp = db.Column(db.Float()) def __repr__(self): return '<CrawledDataListEntity {0} {1} {2}>'.format(self.cid, self.content, str(self.timestamp)) def __eq__(self, other): if isinstance(other, CrawledDataListEntity): return self.cid == other.cid and \ self.content == other.content and \ self.timestamp == other.timestamp return False
class HikeRoute(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False) length = db.Column(db.Float(precision=2), nullable=False) difficulty = db.Column(db.String(20), nullable=False) national_park_id = db.Column(db.Integer, db.ForeignKey('national_park.id')) def save(self): db.session.add(self) def delete(self): db.session.delete(self) @staticmethod def get_all(): return HikeRoute.query.all() def __repr__(self): return f'<HikeRoute {self.name} | Length {self.length}>'
class Link(db.Model, Serializer): # The links table comprises of the machine learning prediction results # along with the details who’ve submitted the URL and their corresponding feedback. id = db.Column(db.Integer, primary_key=True) url = db.Column(db.String(128)) platform = db.Column(db.String(20)) text = db.Column(db.String(200)) sentiment = db.Column(db.String(50)) date_added = db.Column(db.DateTime, default=datetime.now) fraud = db.Column(db.String(20)) fraud_probability = db.Column(db.Float(precision='2,1')) f_deleted = db.Column(db.Boolean, default=False) username_submitted = db.Column(db.String(64), default='Guest') feedback = db.Column(db.String(64)) @classmethod def get_past_records(cls, start=1, records=5): # This module essentially fetches and returns a serialized and paginated list of past historical records that has been analysed. # The default parameters for the start page, and number of records are specified which can be overridden. # The module returns only records without the f_deleted variable and sorts them by descending order. records = cls.query.filter(cls.f_deleted != True).order_by( cls.date_added.desc()).paginate(start, records, False).items return Link.serialize_list(records) def add_link(url, platform, text, sentiment, fraud, fraud_probability, username): # This module commits the resulting parameters values into the table. # The parameter values are fed from the two ML models as well as the user inputted values. _link = Link(url = url, text = text, platform = platform, sentiment = sentiment,\ fraud = fraud, fraud_probability = fraud_probability, username_submitted = username) db.session.add(_link) db.session.commit() return _link @classmethod def add_feedback(cls, id, feedback_string, username='******'): # This module commits the feedback to the database based on the id variable. # Default parameters are specified for the username to factor for users that are not logged-in. # Note that the module only commits if the username and the id corresponds to the user who added the link, # otherwise it will not be committed since the ability to provide feedback are only limited to the user # who’ve added the link. _feedback = cls.query.filter( and_(cls.id == id, cls.username_submitted == username)).first() if _feedback is not None: _feedback.feedback = feedback_string db.session.commit() return _feedback @classmethod def get_summarised_records(cls): # This function returns the summarised count of the fake and real news from the fraud variable. # Note that only records without f_deleted = True will be tabulated. # The results which will be returned are: the platform variable, and the tabulated fake and real count. real_news = func.sum(case([(cls.fraud == 'Real', 1)], else_=0)).label('real_news') fake_news = func.sum(case([(cls.fraud == 'Fake', 1)], else_=0)).label('fake_news') summary = cls.query.with_entities( cls.platform, real_news, fake_news).group_by( cls.platform).filter(cls.f_deleted != True).all() return summary @classmethod def get_user_past_records(cls, username, start=1, records=5): # This module fetches and returns a serialized and paginated list of past historical records that has been analysed. # The default parameters for the start page, and number of records are specified which can be overridden. # A username has to be specified as well to filter records that are pertinent to the user. # The module returns only records without f_deleted = True which is the indicator if the record should # be shown or not, and resulting results are sorted and returned by descending order. records = cls.query.filter(and_(cls.f_deleted != True, cls.username_submitted == username))\ .order_by(cls.date_added.desc()).paginate(start, records, False).items return Link.serialize_list(records) @classmethod def get_past_content(cls, platform, search_string): # This module serves as a search function for the links that were submitted. # The possibilities of the search include the platform, user, text, and its various permutations. # Again, only records without f_deleted = True will be returned. if platform == 'All': records = cls.query.filter( and_(cls.f_deleted != True, cls.text.like(f'%{search_string}%'))).all() elif platform == 'User': records = cls.query.filter( and_(cls.f_deleted != True, cls.username_submitted == search_string)).all() else: records = cls.query.filter( and_(cls.f_deleted != True, cls.text.like(f'%{search_string}%'), cls.platform == platform)).all() return Link.serialize_list(records) @classmethod def get_trending(cls, records=5): # This module returns the top 5 records which has the highest number of count # based on the number of times the link has been submitted. # The default parameters are specified for the number of records as it serves to limit # how many records are shown since the results are sorted by descending order. trending = cls.query\ .with_entities(cls.id, cls.platform, cls.url, func.count(cls.url).label('count'), cls.date_added, cls.sentiment, cls.fraud, cls.fraud_probability)\ .group_by(cls.url)\ .order_by(func.count().desc())\ .limit(records)\ .all() return trending