summaryrefslogtreecommitdiff
path: root/f1elo/model.py
blob: fb9c8c4301e6511a146fe02040bdffb840c94b23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from sqlalchemy import Column, ForeignKey, Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import backref, relationship, sessionmaker
from sqlalchemy.types import Boolean, Date, Float, Integer, String

Base = declarative_base()

class Country(Base):
    __tablename__ = 'countries'

    id = Column(Integer, primary_key=True)
    name = Column(String(255), index=True)

    drivers = relationship(
        'Driver',
        back_populates='country',
        cascade="all",
        passive_deletes=True)

    @staticmethod
    def fetch(name, session):
        country = session.query(Country).filter(Country.name == name).first()
        if not country:
            country = Country()
            country.name = name
            session.add(country)
        return country

class Driver(Base):
    __tablename__ = 'drivers'

    id = Column(Integer, primary_key=True)
    driver = Column(String(255), index=True)

    _country = Column(
        Integer,
        ForeignKey(
            'countries.id',
            onupdate="CASCADE",
            ondelete="CASCADE"))
    country = relationship(
        'Country',
        back_populates='drivers',
        order_by='Driver.id')

    rankings = relationship(
        'Ranking',
        order_by='Ranking.rank_date,Ranking.id',
        back_populates='driver',
        cascade="all",
        passive_deletes=True)

    def __repr__(self):
        return (u"<%s (#%d)>" % (self.driver, self.id)).encode('utf8')

    def get_ranking(self, rank_date=None):
        ranks = self.rankings
        if rank_date is not None:
            ranks = [r for r in ranks if r.rank_date <= rank_date]
        if len(ranks):
            return ranks[-1]
        return None

    @staticmethod
    def fetch(name, country, session):
        driver = session.query(Driver).filter(Driver.driver == name).first()
        if not driver:
            driver = Driver()
            driver.driver = name
            driver.country = Country.fetch(country, session)
            session.add(driver)
        return driver

driver_entry = Table('driver_entries', Base.metadata,
                     Column(
                     '_driver',
                     Integer,
                     ForeignKey(
                         'drivers.id',
                         onupdate="CASCADE",
                         ondelete="CASCADE")),
                     Column(
                     '_entry',
                     Integer,
                     ForeignKey(
                         'entries.id',
                         onupdate="CASCADE",
                         ondelete="CASCADE")),
                     Column('id', Integer, primary_key=True))


class Entry(Base):
    __tablename__ = 'entries'

    id = Column(Integer, primary_key=True)
    result = Column(String(255))
    car_no = Column(String(255))
    result_group = Column(Integer, index=True)

    _race = Column(
        Integer,
        ForeignKey(
            'races.id',
            onupdate="CASCADE",
            ondelete="CASCADE"))
    race = relationship(
        'Race',
        back_populates='entries',
        order_by=result_group)

    drivers = relationship(
        'Driver',
        secondary=driver_entry,
        cascade="all",
        passive_deletes=True)

    def __repr__(self):
        return ('#%s (%s) %s[%d]' % (self.car_no, ', '.join([driver.__repr__().decode('utf8') for driver in self.drivers]), self.result, self.result_group)).encode('utf8')


class Race(Base):
    __tablename__ = 'races'

    id = Column(Integer, primary_key=True)
    race = Column(String(1024))
    date = Column(Date, index=True)
    ranked = Column(Boolean, nullable=False, server_default='0', default=False, index=True)

    _type = Column(
        Integer,
        ForeignKey(
            'race_types.id',
            onupdate="CASCADE",
            ondelete="CASCADE"))
    type = relationship(
        'RaceType',
        back_populates='races',
        order_by='Race.date')

    entries = relationship(
        'Entry',
        back_populates='race',
        order_by='Entry.result_group',
        cascade="all",
        passive_deletes=True)

    def __repr__(self):
        return ('%s (%s)' % (self.race, self.date)).encode('utf8')


class RaceType(Base):
    __tablename__ = 'race_types'

    id = Column(Integer, primary_key=True)
    code = Column(String(255), index=True)
    description = Column(String(1024))

    races = relationship(
        'Race',
        back_populates='type',
        cascade="all",
        passive_deletes=True)

    def __repr__(self):
        return ('%s (%s)' % (self.description, self.code)).encode('utf8')


class Ranking(Base):
    __tablename__ = 'rankings'

    id = Column(Integer, primary_key=True)
    rank_date = Column(Date, index=True)
    ranking = Column(Float, index=True)

    _driver = Column(
        Integer,
        ForeignKey(
            'drivers.id',
            onupdate="CASCADE",
            ondelete="CASCADE"))
    driver = relationship(
        'Driver',
        back_populates='rankings',
     order_by=rank_date)

    def __repr__(self):
        return ("%s: %0.2f (%s)" % (self.driver.__repr__().decode('utf8'), self.ranking, self. rank_date)).encode('utf8')

__all__ = ['Country', 'Driver', 'Entry', 'Ranking', 'Race', 'RaceType']