-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
255 lines (219 loc) · 7.17 KB
/
main.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
from datetime import datetime
from db import create_connection
from dataclasses import dataclass
today = datetime.today().date()
conn = create_connection()
@dataclass
class Person:
name: str
email: str
phone: int
def get_room_avaibility(room_id, start_time, end_time):
"""Get information about room avaibility to reserve.
Args:
room_id (int): Number of reserving room
start_time (str): Start reservation time
end_time (str): End of reservation time
Returns:
int, str: It returns avaibility o froom
"""
if check_giving_arguments(room_id, start_time, end_time):
request_ = request_to_databse(room_id, start_time, end_time)
if request_.fetchone():
request_.close()
return get_reserved_person(room_id, start_time, end_time)
else:
return (
f"You can reserve office {room_id}" f"\nfrom {start_time} to {end_time}"
)
else:
return (
"Please be sure that you"
+ "\ngiving room_id as int and"
+ "\nstart_time and end_time as str."
)
def check_giving_arguments(room_id, start_time, end_time):
"""Checking giving arguments to room avaibility"""
if (
isinstance(room_id, int)
and isinstance(start_time, str)
and isinstance(end_time, str)
):
return True
else:
return False
def request_to_databse(room_id, start_time, end_time):
"""Requesting to database. Is there any available room.
Args:
room_id (int): Number of reserving room
start_time (str): Start reservation time
end_time (str): End of reservation time
Returns:
sqlite_cursor: Returns respons from database
"""
if check_giving_arguments(room_id, start_time, end_time):
cursor = conn.cursor()
query_response = cursor.execute(
"""SELECT number
FROM office
WHERE number = ?
AND (start_time = ?
OR end_time = ?);""",
(
room_id,
start_time,
end_time,
),
)
return query_response
else:
return False
def get_reserved_person(room_id, start_time, end_time):
"""Returns person by whom reserved"""
cursor = conn.cursor()
query_response = cursor.execute(
"""SELECT name, start_time, end_time
FROM person
INNER JOIN office
ON person.id = office.person_id
WHERE office.number = ?
AND (office.start_time = ?
OR office.end_time =?)""",
(
room_id,
start_time,
end_time,
),
)
all_responses = query_response.fetchone()
if all_responses:
(
name,
resp_start_time,
resp_end_time,
) = all_responses
return (
f"Sorry, you can't reserve office {room_id}."
f"\nBecause office reserved by {name}"
f"\nfrom {resp_start_time} to {resp_end_time}"
)
conn.commit()
conn.close()
def reserve(person, room_id, start_time, end_time):
"""Reserving a room by giving arguments
Args:
person (class): Instance of a class Person
room_id (int): Number of reserving room
start_time (str): Start reservation time
end_time (str): End of reservation time
Returns:
int, str: Informing about reservation
"""
if check_giving_arguments(room_id, start_time, end_time):
cursor = conn.cursor()
room_id_query = request_to_databse(room_id, start_time, end_time)
get_room_id = room_id_query.fetchall()
if get_room_id:
return print(get_reserved_person(room_id, start_time, end_time))
elif get_room_id is None:
reserve_is_none_update(person, room_id, start_time, end_time)
else:
cursor.execute(
"INSERT INTO person VALUES (?, ?, ?, ?)",
(
None,
person.name,
person.email,
person.phone,
),
)
cursor.execute(
"""INSERT INTO office
VALUES (?, ?, (SELECT id
FROM person WHERE name = ?), ?, ?, ?)""",
(
None,
room_id,
person.name,
start_time,
end_time,
today,
),
)
print(
f"{person.name} you successfully reserved"
f"\noffice №{room_id} from {start_time}"
f"\nto {end_time}."
)
else:
return (
"Please be sure that you"
+ "\ngiving room_id as int and"
+ "\nstart_time and end_time as str."
)
conn.commit()
conn.close()
def reserve_is_none_update(person, room_id, start_time, end_time):
"""If room value doesn't exist updating"""
cursor = conn.cursor()
cursor.execute(
"""INSERT INTO person
VALUES (?, ?, ?, ?)""",
(
None,
person.name,
person.email,
person.phone,
),
)
cursor.execute(
"""UPDATE office
SET number = ? person_id = (SELECT id
FROM person
WHERE name = ?),
start_time = ?, end_time = ?, date = ?""",
(
room_id,
person.name,
start_time,
end_time,
today,
),
)
conn.commit()
conn.close()
def send_email(person):
"""Returning an information to person"""
if isinstance(person, Person):
cursor = conn.cursor()
query = cursor.execute(
"""SELECT date, start_time,
end_time, number, email, phone
FROM person
INNER JOIN office
ON person.id = office.person_id
WHERE office.person_id = (SELECT id
FROM person
WHERE name = ?);""",
(person.name,),
)
date_, start_time, end_time, room_number, email, phone = query.fetchone()
print(
f"To {email} and {phone}:"
f"\nYou reserved office {room_number}"
f"\nfrom {start_time} to {end_time}"
f"\nat {date_}"
)
conn.commit()
conn.close()
else:
raise TypeError("Giving argument must be instance of a class Person")
if __name__ == "__main__":
print(get_room_avaibility("1", "08:00", "09:00"))
reserve(person7, 1, "08:00", "09:00")
send_email(person7)