-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbooking_data.py
executable file
·209 lines (164 loc) · 6.44 KB
/
booking_data.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
#!/usr/bin/python3
from enum import Enum
from datetime import datetime, timedelta
import json
from pymongo import MongoClient
class States(Enum):
INITIAL = 1
ASK_FOR_NUMBER = 2
ASK_FOR_SERVICE = 3
ASK_FOR_DAY = 4
KNOWN = 5
ASK_FOR_NEED_BOOKING = 6
ASK_FOR_SERVICE_TYPE = 7
ASK_FOR_ANOTHER_SERV = 8
class Booking:
def __init__(self, for_user):
self.services = []
self.time = None
self.user_id = for_user
def __repr__(self):
return "Booking for user {} at {}. Services: {}.".format(self.user_id,
str(self.time), self.services)
class BookingStore:
def __init__(self):
self.booking_list = []
def check_time_free(self, time):
for booking in self.booking_list:
end_time = booking.time + timedelta(minutes = 15)
if time >= booking.time and time <= end_time:
return False
new_procedure_end_time = time + timedelta(minutes = 15)
if booking.time >= time and booking.time <= new_procedure_end_time:
return False
return True
def find_close_free_time(self, time):
next_time = prev_time = time
for _ in range(1, 60):
next_time = next_time + timedelta(minutes = 1)
if self.check_time_free(next_time):
return next_time
prev_time = prev_time - timedelta(minutes = 1)
if self.check_time_free(prev_time):
return prev_time
return None
def add_bookig(self, to_add):
if not self.check_time_free(to_add.time):
return False
self.booking_list.append(to_add)
return True
class DBBookingStore(BookingStore):
def __init__(self, db_path = ("localhost", 27017)):
self.db_url, self.db_port = db_path
super().__init__()
connection = MongoClient(self.db_url, self.db_port)
base = connection.booking_bot_base
booking_collection = base.booking_collection
for booking in booking_collection.find():
need_to_add = Booking(booking["user_id"])
# Use there standart time format
need_to_add.time = datetime.strptime(booking["time"], "%Y-%m-%d %H:%M:%S")
for service in booking["services"]:
need_to_add.services.append(service)
self.booking_list.append(need_to_add)
connection.close()
def add_bookig(self, to_add):
if not super().add_bookig(to_add):
return False
connection = MongoClient(self.db_url, self.db_port)
base = connection.booking_bot_base
booking_collection = base.booking_collection
booking_to_add = {"user_id": to_add.user_id, "time": str(to_add.time),
"services": to_add.services }
booking_collection.insert_one(booking_to_add)
connection.close()
return True
class User:
def __init__(self, user_id, user_name, phone = None,
state = States.INITIAL, gift = False):
self._user_id = user_id
self._first_name = user_name
# user_profile[0]['first_name']
self._phone = phone
self._state = state
self.gift_geted = gift
# Thats how we check that it's new user and we need some raection
if None == phone and States.INITIAL == self._state:
self.user_state_changed()
def user_state_changed(self):
pass
@property
def state(self):
return self._state
@state.setter
def state(self, state):
if state in [item for item in States]:
self._state = state
self.user_state_changed()
@property
def phone(self):
return self._phone
@phone.setter
def phone(self, phone_number):
self._phone = phone_number
self.user_state_changed()
@property
def user_id(self):
return self._user_id
@property
def first_name(self):
return self._first_name
def is_gifted(self):
return self.gift_geted
def __repr__(self):
return "User {} on state {}. Phone {}.".format(self._first_name,
self._state.name, self._phone)
class SavedToDBUser(User):
def __init__(self, user_id, user_name, phone = None,
state = States.INITIAL, gift = False,
db_path = ("localhost", 27017)):
self.db_url, self.db_port = db_path
super().__init__(user_id, user_name, phone, state, gift)
def user_state_changed(self):
# Just open connection and send all we need
connection = MongoClient(self.db_url, self.db_port)
base = connection.booking_bot_base
clients = base.clients_collection
if None == clients.find_one({"id": self._user_id}):
doc = {"id": self._user_id, "name": self._first_name,
"phone": self._phone, "state": self._state.name,
"gifted": self.gift_geted }
clients.insert_one(doc)
connection.close()
return
clients.update_one({"id": self._user_id},
{"$set": {"phone": self._phone,
"state": self._state.name} } )
connection.close()
def __repr__(self):
return super().__repr__() + " Saved to DataBase"
def extract_users_from_db(db_path = ("localhost", 27017)):
db_url, db_port = db_path
connection = MongoClient(db_url, db_port)
base = connection.booking_bot_base
clients = base.clients_collection
result = { }
for client in clients.find():
# Need to avoid saeing here
user = SavedToDBUser(client["id"], client["name"],
phone = client["phone"],
state = States[client["state"]],
gift = client["gifted"],
db_path = db_path)
# We don't remember prepare booking for now, so just drop this states on reboot
if user.state in [States.ASK_FOR_SERVICE, States.ASK_FOR_DAY,
States.ASK_FOR_SERVICE_TYPE, States.ASK_FOR_ANOTHER_SERV]:
user.state = States.KNOWN
result[client["id"]] = user
connection.close()
return result
# if __name__ == "__main__":
# user = User(1, "aaa", None)
# print(user.state)
# user.state = States.ASK_FOR_NEED_BOOKING
# print(user.state)