-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud.py
201 lines (93 loc) · 5.55 KB
/
crud.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
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
190
191
192
193
194
195
196
197
198
199
200
201
"""CRUD operations"""
from model import db, Country, CovidRecord, User, CurrCountryStat, connect_to_db
if __name__ == '__main__':
from server import app
connect_to_db(app)
############## Country Functions ##############
def create_country(name):
"""Create and return a new country."""
country = Country(country=name)
return country
def show_all_countries():
"""Return all countries"""
return Country.query.all()
def get_id_by_country_name(name):
"""Get the id of a country by its name."""
first_country_instance = Country.query.filter_by(country=name).first()
return first_country_instance.country_id
############## CovidRecord Functions ##############
def create_covid_record(country_id_given, date_given, total_cases_given, total_deaths_given):
"""Create a new covid record instance."""
covid_record = CovidRecord(country_id=country_id_given, date=date_given, total_cases=total_cases_given, total_deaths=total_deaths_given)
return covid_record
def get_list_of_days():
"""Grabs all unique days in the database from first recorded day to present."""
list_of_days = CovidRecord.query.with_entities(CovidRecord.date).distinct().order_by(CovidRecord.date.asc()).all()
# print(f"\n\n\n\n{list_of_days[0][0]}\n\n\n\n")
date_list = []
for day in list_of_days:
date_list.append(str(day[0]))
return date_list
def get_country_cases_by_date(user_date):
"""Returns cases for each country by chosen date."""
country_date_cases = db.session.query(Country.country, CovidRecord.total_cases).join(CovidRecord).filter(CovidRecord.date==user_date).all()
# for instance in country_date_cases:
# print(instance)
return country_date_cases
def get_country_deaths_by_date(user_date):
"""Returns cases for each country by chosen date."""
country_date_deaths = db.session.query(Country.country, CovidRecord.total_deaths).join(CovidRecord).filter(CovidRecord.date==user_date).all()
# for instance in country_date_cases:
# print(instance)
return country_date_deaths
############## User Functions ##############
def create_user_instance(get_first_name, get_last_name, get_email, get_password):
"""Returns a user instance with an id, first_name, last_name, email, and password."""
user_instance = User(first_name=get_first_name, last_name=get_last_name, email=get_email, password=get_password)
db.session.add(user_instance)
db.session.commit()
return user_instance
def check_if_user_in_system(check_email, check_password):
"""Checks if a user exists using email, and password provided at login."""
user_in_system_check = User.query.filter_by(email=check_email, password=check_password).first()
return user_in_system_check
def save_recent_date(get_id, get_most_recent_date):
"""Saves the user's last chosen date on slider, so that it's the first date they see upon login."""
find_user_by_id = User.query.filter_by(user_id=get_id).first()
find_user_by_id.recent_date = get_most_recent_date
db.session.commit()
find_user_by_id = User.query.filter_by(user_id=get_id).first()
print(find_user_by_id)
return find_user_by_id
def check_if_user_has_recent_date(given_id):
"""Uses the user's id to check if they have a value saved under recent_date column."""
check_if_user_id_exists = User.query.filter_by(user_id=given_id).first()
greeting_no_date = (f"Welcome, " + check_if_user_id_exists.first_name + "! You have no recent chosen dates.")
greeting_with_date = (f"Welcome, " + check_if_user_id_exists.first_name + "!" + " Your most recent chosen date is: " + str(check_if_user_id_exists.recent_date))
print(str(check_if_user_id_exists.recent_date) == 'None')
if str(check_if_user_id_exists.recent_date) != 'None':
return greeting_with_date
else:
return greeting_no_date
############## Country Statistics Functions ##############
def create_country_stats_instance(country_name_stat, population_stat, total_cases, \
cases_1m_stat, active_cases_stat, total_deaths, \
deaths_1m_stat, total_tests_stat, tests_1m_stat):
country_stats_record = CurrCountryStat(country_name=country_name_stat, population=population_stat, total_cases_stats=total_cases, \
cases_1m=cases_1m_stat, active_cases=active_cases_stat, total_deaths_stats=total_deaths, \
deaths_1m=deaths_1m_stat, total_tests=total_tests_stat, tests_1m=tests_1m_stat)
return country_stats_record
def stats_per_country(country):
"""Returns all current stats for searched country."""
country_stats = CurrCountryStat.query.filter_by(country_name=country).first()
country_stats_dict = country_stats.__dict__
country_stats_dict.pop('_sa_instance_state', None)
return country_stats_dict
############## Line Chart Functions ##############
def cases_and_deaths_for_chosen_country(country_provided):
"""Queries the database for all existing records of cases and deaths in order to update the line chart."""
country_line_graph_stats = db.session.query(Country.country, CovidRecord.date, CovidRecord.total_cases, CovidRecord.total_deaths).join(CovidRecord).filter(Country.country==country_provided).all()
# country_line_graph_stats_dict = country_line_graph_stats.__dict__
# country_line_graph_stats_dict.pop('_sa_instance_state', None)
# print(country_line_graph_stats)
return country_line_graph_stats