Ejemplo n.º 1
0
class Prediction(db.Model):
    __tablename__ = 'prediction'
    id = db.Column(db.Integer, primary_key=True)
    game_id = db.Column(db.Integer, db.ForeignKey('game.id'))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    winning_team_id = db.Column(db.Integer, db.ForeignKey('team.id'))
    game = db.relationship('Game', backref='predictions')
Ejemplo n.º 2
0
class Vote(db.Model):
    __tablename__ = 'vote'
    id = db.Column(db.Integer, primary_key=True)
    ballot_id = db.Column(db.Integer,
                          db.ForeignKey('ballot.id', ondelete='CASCADE'))
    team_id = db.Column(db.Integer, db.ForeignKey('team.id'))
    rank = db.Column(db.SmallInteger)
    reason = db.Column(db.String(140))

    def __repr__(self):
        return '<Vote %r on Ballot %r>' % (self.rank, self.ballot_id)
Ejemplo n.º 3
0
class Game(db.Model):
    __tablename__ = 'game'
    id = db.Column(db.Integer, primary_key=True)
    conference_id = db.Column(db.Integer, db.ForeignKey('conference.id'))
    point_value = db.Column(db.Float)
    home_team_id = db.Column(db.Integer, db.ForeignKey('team.id'), nullable=True)
    away_team_id = db.Column(db.Integer, db.ForeignKey('team.id'), nullable=True)
    next_game_id = db.Column(db.Integer, db.ForeignKey('game.id'), nullable=True)
    winner_is_home = db.Column(db.Boolean, nullable=True)
    is_championship = db.Column(db.Boolean)
    __table_args__ = (
        UniqueConstraint('next_game_id', 'winner_is_home', name='one_winner'),
        {})
    result = db.relationship('Result', uselist=False, back_populates='game')
    home_team = db.relationship('Team', foreign_keys=[home_team_id])
    away_team = db.relationship('Team', foreign_keys=[away_team_id])
Ejemplo n.º 4
0
class VoterApplication(db.Model):
    __tablename__ = 'voter_application'
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    primary_team_id = db.Column(db.Integer, db.ForeignKey('team.id'))
    primary_team = db.relationship('Team')
    other_teams = db.relationship('Team', secondary=other_teams_table)
    approach = db.Column(db.Text, nullable=False)
    consumption_tags = db.relationship('ConsumptionTag',
                                       secondary=application_tags_table)
    other_comments = db.Column(db.Text, nullable=True)
    will_participate = db.Column(db.Boolean, nullable=False)
    updated = db.Column(db.DateTime, default=datetime.utcnow)
    season = db.Column(db.Integer, nullable=False)

    def __repr__(self):
        return '<Application %r>' % self.user.nickname
Ejemplo n.º 5
0
class Ballot(db.Model):
    __tablename__ = 'ballot'
    id = db.Column(db.Integer, primary_key=True)
    updated = db.Column(db.DateTime, default=datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id',
                                                  ondelete='CASCADE'))
    poll_id = db.Column(db.Integer, db.ForeignKey('poll.id',
                                                  ondelete='CASCADE'))
    votes = db.relationship('Vote',
                            backref='fullballot',
                            lazy='dynamic',
                            cascade="all, delete-orphan",
                            passive_deletes=True)

    __table_args__ = (UniqueConstraint('user_id', 'poll_id',
                                       name='oneballot'), {})

    @property
    def is_provisional(self):
        return not self.voter.was_voter_at(self.fullpoll.closeTime)

    @is_provisional.setter
    def is_provisional(self, value):
        raise AttributeError('is_provisional is not a settable field')

    def __repr__(self):
        return '<Ballot %r>' % self.id

    def __str__(self):
        if self.fullpoll.week:
            return "%s's Ballot for Week %s of %s-%s" % (
                self.voter.nickname, int(
                    self.fullpoll.week), int(self.fullpoll.season - 1),
                int(self.fullpoll.season - 2000))
        else:
            return "%s's Ballot for Preseason of %s-%s" % (
                self.voter.nickname, int(self.fullpoll.season - 1),
                int(self.fullpoll.season - 2000))
Ejemplo n.º 6
0
class VoterEvent(db.Model):
    __tablename__ = 'voter_event'
    id = db.Column(db.Integer, primary_key=True)
    timestamp = db.Column(db.DateTime)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    is_voter = db.Column(db.Boolean)

    def __repr__(self):
        return '<VoterEvent %r>' % self.id

    def __commit_insert__(self):
        if self.is_voter:
            subj = 'You have been approved for voting on the /r/CollegeBasketball Poll'
            template = 'pm_voter_granted'
        else:
            subj = 'Your voting privilege has been revoked from the /r/CollegeBasketball Poll'
            template = 'pm_voter_revoked'
        send_reddit_pm(self.user.nickname, subj, template, user=self.user)
Ejemplo n.º 7
0
class Result(db.Model):
    __tablename = 'result'
    id = db.Column(db.Integer, primary_key=True)
    game_id = db.Column(db.Integer, db.ForeignKey('game.id'))
    winning_team_id = db.Column(db.Integer, db.ForeignKey('team.id'))
    game = db.relationship('Game', back_populates='result')
Ejemplo n.º 8
0
class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer,
                   primary_key=True,
                   autoincrement=True)

    nickname = db.Column(db.String(20), index=True)
    email = db.Column(db.String(120), index=True)
    emailConfirmed = db.Column(db.Boolean, default=False)
    role = db.Column(db.Enum('u', 'a'), default='u')
    accessToken = db.Column(db.String(30))
    refreshToken = db.Column(db.String(30))
    refreshAfter = db.Column(db.DateTime)
    emailReminders = db.Column(db.Boolean, default=False)
    pmReminders = db.Column(db.Boolean, default=False)
    applicationFlag = db.Column(db.Boolean, default=False)
    flair = db.Column(db.Integer, db.ForeignKey('team.id'))
    ballots = []

    voterEvents = []

    voterApplication = {}

    @property
    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    @property
    def is_anonymous(self):
        return False

    def is_admin(self):
        return self.role == 'a'

    @property
    def team(self):
        if self.flair_team:
            return self.flair_team
        return None

    @property
    def conference(self):
        if self.team:
            return self.team.conference
        return None

    @hybrid_property
    def remind_viaEmail(self):
        return self.emailConfirmed & self.emailReminders

    def get_id(self):
        return unicode(self.id)

    def generate_confirmation_token(self, expiration=3600, email=email):
        s = Serializer(app.config['SECRET_KEY'], expiration)
        return s.dumps({'confirm': self.id, 'email': email})

    def confirm(self, token):
        s = Serializer(app.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except:
            return False
        if data.get('confirm') != self.id:
            return False
        if data.get('email') == self.email and self.emailConfirmed:
            # Avoid a database write, but don't want to give an error to user.
            return True
        self.email = data.get('email')
        self.emailConfirmed = True
        db.session.add(self)
        db.session.commit()
        return True


    @hybrid_property
    def remind_viaRedditPM(self):
        return False

    @hybrid_property
    def is_voter(self):
        return False

    @is_voter.expression
    def is_voter(cls):
        return False

    @is_voter.setter
    def is_voter(self, value):
        pass

    @hybrid_method
    def was_voter_at(self, timestamp):
        return False

    @was_voter_at.expression
    def was_voter_at(cls, timestamp):
        return False

    def name_with_flair(self, size=30):
        team = self.team
        if not team:
            return str(self.nickname)
        return "%s%s" % (team.logo_html(size), self.nickname)

    def __repr__(self):
        return '<User %r>' % self.nickname

    def __str__(self):
        return str(self.nickname)
Ejemplo n.º 9
0
class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)

    nickname = db.Column(db.String(20), index=True)
    email = db.Column(db.String(120), index=True)
    emailConfirmed = db.Column(db.Boolean, default=False)
    role = db.Column(db.Enum('u', 'a'), default='u')
    accessToken = db.Column(db.String(30))
    refreshToken = db.Column(db.String(30))
    refreshAfter = db.Column(db.DateTime)
    emailReminders = db.Column(db.Boolean, default=False)
    pmReminders = db.Column(db.Boolean, default=False)
    applicationFlag = db.Column(db.Boolean, default=False)

    flair = db.Column(db.Integer, db.ForeignKey('team.id'))
    ballots = db.relationship('Ballot',
                              backref='voter',
                              lazy='dynamic',
                              cascade="all, delete-orphan",
                              passive_deletes=True)

    voterEvents = db.relationship('VoterEvent',
                                  backref='user',
                                  lazy='dynamic',
                                  order_by='desc(VoterEvent.timestamp)')

    voterApplication = db.relationship('VoterApplication',
                                       uselist=False,
                                       backref='user')

    @property
    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    @property
    def is_anonymous(self):
        return False

    def is_admin(self):
        return self.role == 'a'

    def submitted_ballot_to(self, poll_id):
        return self.ballots.filter_by(poll_id=poll_id).first()

    @property
    def team(self):
        if self.voterApplication:
            return self.voterApplication.primary_team
        elif self.flair_team:
            return self.flair_team
        return None

    @property
    def conference(self):
        if self.team:
            return self.team.conference
        return None

    @hybrid_property
    def remind_viaEmail(self):
        return self.emailConfirmed & self.emailReminders

    @hybrid_property
    def remind_viaRedditPM(self):
        return (self.is_voter == True) | (self.role == 'a') | (self.pmReminders
                                                               == True)

    @hybrid_property
    def is_voter(self):
        return self.was_voter_at(datetime.utcnow())

    @is_voter.expression
    def is_voter(cls):
        return select([VoterEvent.is_voter]).\
            where(VoterEvent.user_id == cls.id).\
            order_by(desc("timestamp")).\
            limit(1).as_scalar()

    @is_voter.setter
    def is_voter(self, value):
        event = VoterEvent(timestamp=(datetime.utcnow() -
                                      timedelta(seconds=1)),
                           user_id=self.id,
                           is_voter=value)
        db.session.add(event)
        db.session.commit()

    @hybrid_method
    def was_voter_at(self, timestamp):
        most_recent = VoterEvent.query.filter_by(user=self) \
            .having(VoterEvent.timestamp < timestamp) \
            .order_by(VoterEvent.timestamp.desc()) \
            .first()
        return most_recent and most_recent.is_voter

    @was_voter_at.expression
    def was_voter_at(cls, timestamp):
        return select([VoterEvent.is_voter]).\
            where(VoterEvent.user_id == cls.id).\
            where(VoterEvent.timestamp < timestamp).\
            order_by(desc("timestamp")).\
            limit(1).as_scalar()

    def get_id(self):
        return unicode(self.id)

    def generate_confirmation_token(self, expiration=3600, email=email):
        s = Serializer(app.config['SECRET_KEY'], expiration)
        return s.dumps({'confirm': self.id, 'email': email})

    def confirm(self, token):
        s = Serializer(app.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except:
            return False
        if data.get('confirm') != self.id:
            return False
        if data.get('email') == self.email and self.emailConfirmed:
            # Avoid a database write, but don't want to give an error to user.
            return True
        self.email = data.get('email')
        self.emailConfirmed = True
        db.session.add(self)
        db.session.commit()
        return True

    def name_with_flair(self, size=30):
        team = self.team
        if not team:
            return str(self.nickname)
        return "%s%s" % (team.logo_html(size), self.nickname)

    def __repr__(self):
        return '<User %r>' % self.nickname

    def __str__(self):
        return str(self.nickname)
Ejemplo n.º 10
0
        return '<VoterEvent %r>' % self.id

    def __commit_insert__(self):
        if self.is_voter:
            subj = 'You have been approved for voting on the /r/CollegeBasketball Poll'
            template = 'pm_voter_granted'
        else:
            subj = 'Your voting privilege has been revoked from the /r/CollegeBasketball Poll'
            template = 'pm_voter_revoked'
        send_reddit_pm(self.user.nickname, subj, template, user=self.user)


other_teams_table = db.Table(
    'other_teams',
    db.Column('application_id', db.Integer,
              db.ForeignKey('voter_application.id')),
    db.Column('team_id', db.Integer, db.ForeignKey('team.id')))

application_tags_table = db.Table(
    'application_tags',
    db.Column('application_id', db.Integer,
              db.ForeignKey('voter_application.id')),
    db.Column('tag_id', db.Integer, db.ForeignKey('consumption_tags.id')))


class VoterApplication(db.Model):
    __tablename__ = 'voter_application'
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    primary_team_id = db.Column(db.Integer, db.ForeignKey('team.id'))
    primary_team = db.relationship('Team')