Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 67 lines (51 sloc) 2.149 kb
079acb1 @skorokithakis Initial commit.
authored
1 from sqlalchemy.ext.declarative import declarative_base
2 from sqlalchemy.orm import sessionmaker, relation, backref
3 import sqlalchemy
4 try:
5 from database_settings import CONNECTION_STRING
6 except ImportError:
7 CONNECTION_STRING = "sqlite:///imdbapi.db"
8
9 Base = declarative_base()
10
11 class Show(Base):
12 __tablename__ = 'shows'
13 id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
14 name = sqlalchemy.Column(sqlalchemy.Unicode(200))
15 year = sqlalchemy.Column(sqlalchemy.Integer)
16
17 def __init__(self, name, year):
18 self.name = name
19 self.year = year
20
21 def __repr__(self):
22 return "<Show('%s','%s')>" % (self.name, self.year)
23
24 sqlalchemy.Index('idx_show_name_year', Show.name, Show.year)
25
26 class Episode(Base):
27 __tablename__ = 'episodes'
28 id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
29 show_id = sqlalchemy.Column(sqlalchemy.Integer, sqlalchemy.ForeignKey('shows.id'), index=True)
30 name = sqlalchemy.Column(sqlalchemy.Unicode(200), index=True)
31 season = sqlalchemy.Column(sqlalchemy.Integer)
32 number = sqlalchemy.Column(sqlalchemy.Integer)
33
34 show = relation(Show, backref='episodes', order_by=id)
35
36 def __init__(self, show, name, season, number):
37 self.show = show
38 self.name = name
39 self.season = season
40 self.number = number
41
42 def __repr__(self):
43 return "<Episode('%s.%s','%s')>" % (self.season, self.number, self.name)
44
45 class Stats(Base):
46 __tablename__ = 'stats'
47 key = sqlalchemy.Column(sqlalchemy.Unicode(200), index=True, primary_key=True)
48 value = sqlalchemy.Column(sqlalchemy.Integer)
49
50 def __init__(self, key, value):
51 self.key = key
52 self.value = value
53
54 def __repr__(self):
55 return "<Stats('%s','%s')>" % (self.key, self.value)
56
57
554ba4a Changed the import script.
Poromenos.org Server authored
58 def init_db(transactional=False):
079acb1 @skorokithakis Initial commit.
authored
59 engine = sqlalchemy.create_engine(CONNECTION_STRING)
60 Base.metadata.create_all(engine)
554ba4a Changed the import script.
Poromenos.org Server authored
61 Session = sessionmaker(bind=engine, transactional=transactional)
079acb1 @skorokithakis Initial commit.
authored
62 session = Session()
63 return session
64
65 if __name__ == "__main__":
66 init_db()
Something went wrong with that request. Please try again.