-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDB_Queries.py
More file actions
158 lines (106 loc) · 6.55 KB
/
DB_Queries.py
File metadata and controls
158 lines (106 loc) · 6.55 KB
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
from Cipher import _encrypt, _decrypt, CRYPTO_KEY, CRYPTO_IV
from sqlalchemy import create_engine
from sqlalchemy.sql import text
import random
import string
#------------------------------ below are the select queries for the tempoture database ------------------------------#
def Select_User_Query(email, password):
engine = create_engine(os.environ['DB_URI'])
cipher_text = _encrypt(password, CRYPTO_KEY, CRYPTO_IV)
with engine.connect() as connection:
select_query = """SELECT * FROM "User" WHERE email=:input_1 AND password=:input_2;"""
query_result = connection.execute(text(select_query), input_1 = email, input_2 = cipher_text ).fetchone()
return query_result
def Select_Cities_Query(state_id):
engine = create_engine(os.environ['DB_URI'])
with engine.connect() as connection:
select_query = """SELECT * FROM "Cities" WHERE state_id=:input;"""
query_result = connection.execute(text(select_query), input = state_id ).fetchone()
return query_result
def Select_States_Query(state):
engine = create_engine(os.environ['DB_URI'])
with engine.connect() as connection:
select_query = """SELECT * FROM "states" WHERE state_name=:input;"""
query_result = connection.execute(text(select_query), input = state ).fetchone()
return query_result
def Select_Countries_Query(country):
engine = create_engine(os.environ['DB_URI'])
with engine.connect() as connection:
select_query = """SELECT * FROM "countries" WHERE country_name=:input;"""
query_result = connection.execute(text(select_query), input = country ).fetchone()
return query_result
def Select_Daily_Forecasts_Query(city_id):
engine = create_engine(os.environ['DB_URI'])
with engine.connect() as connection:
select_query = """SELECT * FROM "daily_forecasts" WHERE city_id =:input;"""
query_result = connection.execute(text(select_query), input = city_id ).fetchone()
return query_result
#------------------------------ below are the insert queries for the tempoture database ------------------------------#
#---Temporary password Generator for User---#
def get_random_string(length):
#characters is all possible password characters
characters = string.ascii_lowercase
characters += string.ascii_uppercase
characters += string.digits
password = ''
#create random combination of 16 letters from the characters list
for i in range(16):
password += password.join(random.choice(characters))
return password
def Insert_User_Query(email, spotify_id, date, time, city_id):
generated_password = get_random_string(16)
engine = create_engine(os.environ['DB_URI'])
cipher_text = _encrypt(generated_password, CRYPTO_KEY, CRYPTO_IV)
with engine.connect() as connection:
connection.autocommit = True
insert_query = """INSERT INTO "users"(user_id, email, password, spotify_id, date_created, time_updated, city_id)
VALUES (DEFAULT, :input_1, :input_2, :input_3, :input_4, :input_5, :input_6);"""
connection.execute(text(insert_query), input_1 = email, input_2 = cipher_text, input_3 = spotify_id ,
input_4 = date, input_5 = time , input_6 = city_id )
#---This will be for after we have implemented User logins---#
"""
def Insert_User_Query(email, password, spotify_id, date, time, city_id):
engine = create_engine(os.environ['DB_URI'])
cipher_text = _encrypt(password, CRYPTO_KEY, CRYPTO_IV)
with engine.connect() as connection:
connection.autocommit = True
insert_query = ""INSERT INTO "users"(user_id, email, password, spotify_id, date_created, time_updated, city_id)
VALUES (DEFAULT, :input_1, :input_2, :input_3, :input_4, :input_5, :input_6);""
connection.execute(text(insert_query), input_1 = email, input_2 = cipher_text, input_3 = spotify_id ,
input_4 = date, input_5 = time , input_6 = city_id )
"""
def Insert_Cities_Query(city_name, state_id):
engine = create_engine(os.environ['DB_URI'])
with engine.connect() as connection:
connection.autocommit = True
insert_query = """INSERT INTO "cities"(city_id, city_name, state_id)
VALUES(DEFAULT, :input_1, :input_2);"""
connection.execute(text(insert_query), input_1 = city_name, input_2 = state_id)
return query_result
def Insert_States_Query(state_name, country_id):
engine = create_engine(os.environ['DB_URI'])
with engine.connect() as connection:
connection.autocommit = True
insert_query = """INSERT INTO "states"(state_id, state_name, country_id)
VALUES(DEFAULT, :input_1, :input_2);"""
connection.execute(text(insert_query), input_1 = state_name, input_2 = country_id )
return query_result
def Insert_Countries_Query(country_name):
engine = create_engine(os.environ['DB_URI'])
with engine.connect() as connection:
connection.autocommit = True
insert_query = """INSERT INTO "countries"(country_id, country_name) VALUES(DEFAULT, :input);"""
connection.execute(text(insert_query), input = country_name )
return query_result
def Insert_Daily_Forecasts_Query(weather_date, average_temp, pressure, humidity, wind_speed, wind_direction, cloudiness,
precipitation_volume, snow_volume, precipitation_probability, time_updated, city_id):
engine = create_engine(os.environ['DB_URI'])
with engine.connect() as connection:
connection.autocommit = True
insert_query = """INSERT INTO "daily_forecasts"(daily_forecast_id, weather_date, average_temp, pressure, humidity, wind_speed, wind_direction,
cloudiness, precipitation_volume, snow_volume, precipitation_probability, time_updated, city_id)
VALUES (DEFAULT, :input_1, :input_2, :input_3, :input_4, :input_5, :input_6, :input_7, :input_8,:input_9, :input_10, :input_11, :input_12);"""
connection.execute(text(insert_query), input_1 = weather_date, input_2 = average_temp, input_3 = pressure, input_4 = humidity, input_5 = wind_speed,
input_6 = wind_direction, input_7 = cloudiness, input_8 = precipitation_volume, input_9 = snow_volume,
input_10 = precipitation_probability, input_11 = time_updated, input_12 = city_id)
return query_result