class Search(db.Model): """ Search model. """ # Columns. param = db.Column('param', db.String(), primary_key=True) create_datetime = \ db.Column('create_datetime', db.DateTime(), nullable=False) identity = db.Column('identity', JSONType, nullable=False)
class Vote(db.Model): """ Vote model. """ id = db.Column('id', db.Integer, primary_key=True) vote_pool_id = db.Column('vote_pool_id', db.Integer, db.ForeignKey('vote_pool.id'), nullable=False) create_datetime = \ db.Column('create_datetime', db.DateTime(), nullable=False) screen_name = \ db.Column('screen_name', db.String(), nullable=False, unique=True) email = db.Column('email', db.String(), nullable=False, unique=True) password = db.Column('password', db.String(), nullable=False) phone_number = db.Column('phone_number', db.String(), nullable=False) hit = db.Column('hit', db.Boolean) error = db.Column('error', db.Text())
class AvgTemp(db.Model): """ This model is a cache for the `/api/avg-temp` API endpoint. The cache expiration time is determined by the parameter `max_cache_time` of the app config. """ _app_config = Config() id = db.Column('id', db.Integer, primary_key=True) username = db.Column('username', db.String(), nullable=False, unique=True) repos_q = db.Column('repos_q', db.Integer, nullable=False) avg_temp = db.Column('avg_temp', db.Float, nullable=False) created_at = db.Column('created_at', db.DateTime(), nullable=False) @classmethod def get_from_cache(cls, username: str) -> Optional[AvgTemp]: """ Returns a record from the cache if its found and its not expired. """ min_datetime = datetime.datetime.now() \ - datetime.timedelta(seconds=cls._app_config.get_max_cache_time()) instance = cls.query.filter(cls.username == username, cls.created_at >= min_datetime).first() return instance @classmethod def save_or_update(cls, username: str, repos_q: int, avg_temp: float): """ Saves or updates a record. :param str username: :param int repos_q: Total number of repos. :param float avg_temp: The average temperatur in degrees Celsius. :returns: The saved instance. """ instance = cls.query.filter_by(username=username).first() if not instance: instance = AvgTemp() instance.username = username instance.repos_q = repos_q instance.avg_temp = avg_temp instance.created_at = datetime.datetime.utcnow() db.session.add(instance) db.session.commit() return instance
class Account(db.Model): """ Account model. Status will be blank for those accounts which were not tested. :const str STATUS_LOGGED_IN: Login was successful. :const str STATUS_WRONG_CREDENTIALS: Incorrect screen name or password. :const str STATUS_SUSPENDED: Suspended account. :const str STATUS_UNCONFIRMED_ACCESS: A manual confirmation is needed. :const str STATUS_UNDETERMINED: Unable to complete the login process. """ STATUS_LOGGED_IN = 'logged_in' STATUS_WRONG_CREDENTIALS = 'wrong_credentials' STATUS_SUSPENDED = 'suspended' STATUS_UNCONFIRMED_ACCESS = 'unconfirmed_access' STATUS_UNDETERMINED = 'undetermined' id = db.Column('id', db.Integer, primary_key=True) screen_name = \ db.Column('screen_name', db.String(), nullable=False, unique=True) email = db.Column('email', db.String(), nullable=False, unique=True) password = db.Column('password', db.String(), nullable=False) phone_number = db.Column('phone_number', db.String(), nullable=False) status = db.Column('status', db.String()) status_updated_at = db.Column('status_updated_at', db.DateTime()) cookies = db.Column('cookies', db.Text()) proxy = db.Column('proxy', db.String()) def update_status(self, status: str): """ Update status and status_updated_at fields. :param str status: New status. """ self.status = status self.status_updated_at = datetime.utcnow() db.session.commit() def set_cookies(self, cookies: Optional[str]): """ Set and save cookies. :param None | str cookies: Cookies. """ self.cookies = cookies db.session.commit()
class VotePool(db.Model): """ Vote pool model. :const str STATUS_RUNNING: The vote pool is running. :const str STATUS_FINISHED: The vote pool has finished. :const str STATUS_INTERRUPTED: The vote pool could not finish. """ STATUS_RUNNING = 'running' STATUS_FINISHED = 'finished' STATUS_INTERRUPTED = 'interrupted' # Columns. id = db.Column('id', db.Integer, primary_key=True) tweet_id = db.Column('tweet_id', db.Integer, nullable=False) option_index = db.Column('option_index', db.Integer, nullable=False) intended_hits = db.Column('intended_hits', db.Integer, nullable=False) max_tries = db.Column('max_tries', db.Integer, nullable=False) create_datetime = \ db.Column('create_datetime', db.DateTime(), nullable=False) status = db.Column('status', db.String(), nullable=False) def update_status(self, status: str): """ Update status. :param str status: New status. """ self.status = status db.session.commit() def get_basic_info(self) -> dict: """ Get the information of this model. :returns: A dictionary representation of this model. """ return { 'id': self.id, 'tweet_id': self.tweet_id, 'option_index': self.option_index, 'intended_hits': self.intended_hits, 'max_tries': self.max_tries, 'create_datetime': self.create_datetime.strftime("%Y-%m-%d %H:%M:%S"), 'status': self.status } def get_info(self) -> dict: """ Get the information from this model plus the its vote results. :returns: A dictionary that contains all the information. Please refer to the `/api/vote-pool-info` API endpoint section in the app README for more information. """ info = self.get_basic_info() # Count total tries. tries = Vote.query.filter(Vote.vote_pool_id == self.id).count() # Count hits. hits = Vote.query.\ filter(Vote.vote_pool_id == self.id).\ filter(Vote.hit == True).count() # Build error messages. error_votes = Vote.query.\ filter(Vote.vote_pool_id == self.id).\ filter(Vote.hit == False).all() errors = [] for vote in error_votes: error = {'screen_name': vote.screen_name, 'error': vote.error} errors.append(error) # Add extra info. info['tries'] = tries info['hits'] = hits info['errors'] = errors # Send info. return info