-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathmodels.py
111 lines (98 loc) · 3.41 KB
/
models.py
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
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
"""
init SQLAlchemy
"""
engine = create_engine('postgresql://postgres:[email protected]/olympics_dev', echo=False)
Base = declarative_base(engine)
def loadSession():
metadata = Base.metadata
Session = sessionmaker(bind=engine)
session = Session()
return session
def execute_search(or_search, and_search, database=engine):
return list(database.connect().execute("""
SELECT 'or', ARRAY[ts_headline(athlete_name,q), athlete_id::text],
ARRAY[ts_headline(sport_name,q), sport_id::text],
ARRAY[ts_headline(event_name,q), event_id::text],
ARRAY[ts_headline(olympic_year::text,q), olympic_id::text],
ARRAY[ts_headline(city_name,q), olympic_id::text],
ARRAY[ts_headline(country_rep,q), country_rep_id::text],
ARRAY[ts_headline(country_host,q), country_host_id::text]
FROM complete, to_tsquery('{0}:*') q WHERE tsv @@ q
-- LIMIT 10
UNION ALL
SELECT 'and', ARRAY[ts_headline(athlete_name,q), athlete_id::text],
ARRAY[ts_headline(sport_name,q), sport_id::text],
ARRAY[ts_headline(event_name,q), event_id::text],
ARRAY[ts_headline(olympic_year::text,q), olympic_id::text],
ARRAY[ts_headline(city_name,q), olympic_id::text],
ARRAY[ts_headline(country_rep,q), country_rep_id::text],
ARRAY[ts_headline(country_host,q), country_host_id::text]
FROM complete, to_tsquery('{1}:*') q WHERE tsv @@ q
-- LIMIT 10
""".format(or_search, and_search)))
"""
models
"""
class Athlete(Base):
"""
id
first_name
last_name
gender
medal_id: relation to medal model
"""
__tablename__ = 'athletes'
__table_args__ = {'autoload':True}
class Event(Base):
"""
id
name
sport_id: relation to sport model
"""
__tablename__ = 'events'
__table_args__ = {'autoload':True}
class Sport(Base):
"""
id
name
"""
__tablename__ = 'sports'
__table_args__ = {'autoload':True}
class City(Base):
"""
id
name
country_id: relation to countries table
"""
__tablename__ = 'cities'
__table_args__ = {'autoload':True}
class Country(Base):
"""
id
name
noc: Name of countries National Olympic Committee
"""
__tablename__ = 'countries'
__table_args__ = {'autoload':True}
class Olympics(Base):
"""
id
year
season
city_id: Relation to the cities table where the games were hosted
"""
__tablename__ = 'olympics'
__table_args__ = {'autoload':True}
class Medal(Base):
"""
id
athlete_id: Relation to athlete model who won the medal
country_id: Relation to the country model which was being represented by the athlete
event_id: Relation to the event model which the medal was one in
olympic_id: Relation to the olympic model containing the year and season the medal was won in
"""
__tablename__ = 'medals'
__table_args__ = {'autoload':True}