DbManager update
This commit is contained in:
parent
a59a496bb5
commit
edd8663961
471
DBManager.py
471
DBManager.py
@ -1,5 +1,7 @@
|
|||||||
import json
|
import json
|
||||||
import os.path
|
from pathlib import Path
|
||||||
|
from dateHelper import format_date, get_date, format_time, get_end_time
|
||||||
|
import datetime
|
||||||
|
|
||||||
# PRZYKLADOWY SCHEMAT DB
|
# PRZYKLADOWY SCHEMAT DB
|
||||||
# informacje o dacie dnia dzisiejszego oraz nazwie tego dnia (pon, wt) będzie trzeba podawać przy inicjalizacji klasy
|
# informacje o dacie dnia dzisiejszego oraz nazwie tego dnia (pon, wt) będzie trzeba podawać przy inicjalizacji klasy
|
||||||
@ -33,370 +35,155 @@ import os.path
|
|||||||
# }
|
# }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class calender_db:
|
class calender_db:
|
||||||
def __init__(self, date="07.06.2021", name="poniedziałek", start_day_time="8:00", end_day_time="20:00", file_path="data-base/calender.json"):
|
def __init__(self, db_path="data-base/calendar.json"):
|
||||||
self.DB = {}
|
self.db = {}
|
||||||
self.start_day_time = start_day_time
|
self.db_file_path = Path(db_path)
|
||||||
self.end_day_time = end_day_time
|
if self.db_file_path.is_file():
|
||||||
self.today_date = date
|
with open(self.db_file_path, 'r+') as f:
|
||||||
self.today_day_name = name
|
self.db = json.load(f)
|
||||||
self.today_day, self.today_month, self.today_year = self.get_day_month_year(self.today_date)
|
|
||||||
if os.path.isfile(file_path):
|
|
||||||
with open(file_path, 'r+') as input:
|
|
||||||
self.DB = json.load(input)
|
|
||||||
|
|
||||||
def save_db(self):
|
def save_db(self):
|
||||||
with open(file_path, 'w+') as output:
|
with open(self.db_file_path, 'w+') as f:
|
||||||
self.DB = json.dumps(input)
|
json.dump(self.db, f)
|
||||||
|
|
||||||
# CRUD methods
|
# CRUD methods
|
||||||
|
|
||||||
def create_meeting(self, meeting_dict):
|
def create_meeting(self, meeting_dict):
|
||||||
date = meeting_dict['date'].lower()
|
date_time = get_date(
|
||||||
if date == "dziś":
|
meeting_dict['date'].lower(), meeting_dict['time'].lower())
|
||||||
date = self.today_date
|
|
||||||
elif is_date_day_name(date):
|
if format_date(date_time) in self.db.keys():
|
||||||
offset = get_days_difference(date)
|
if not self.is_collision(format_date(date_time), meeting_dict['time']):
|
||||||
date = calculate_date_with_offset(offset)
|
self.db[format_date(date_time)].append(meeting_dict)
|
||||||
#TODO chceck date format
|
self.save_db()
|
||||||
if date in self.DB.keys():
|
|
||||||
self.DB[date].append(meeting_dict)
|
|
||||||
self.save_db()
|
|
||||||
else:
|
else:
|
||||||
self.DB[date] = [meeting_dict]
|
self.db[format_date(date_time)] = [meeting_dict]
|
||||||
self.save_db()
|
self.save_db()
|
||||||
|
|
||||||
def is_collision(self, date, time):
|
def delete_meeting(self, date, time):
|
||||||
# TODO zaimplementować oferowanie innego czasu w razie kolizji
|
date_time = get_date(date, time)
|
||||||
if date in self.DB.keys():
|
|
||||||
meetings = DB[date]
|
|
||||||
for m in meetings:
|
|
||||||
meeting_time = m['time'].split("-")
|
|
||||||
start = meeting_time[0]
|
|
||||||
# meeting has start and end time
|
|
||||||
if len(meeting_time > 1):
|
|
||||||
end = meeting_time[1]
|
|
||||||
seeking_hour, seeking_min = self.get_hour_minutes(time)
|
|
||||||
seeking_hour_s, seeking_min_s = self.get_hour_minutes(start)
|
|
||||||
seeking_hour_e, seeking_min_e = self.get_hour_minutes(end)
|
|
||||||
if seeking_hour < seeking_hour_s:
|
|
||||||
return False
|
|
||||||
if seeking_hour == seeking_hour_s and seeking_min < seeking_min_s:
|
|
||||||
return False
|
|
||||||
if seeking_hour == seeking_hour_e and seeking_min >= seeking_min_e:
|
|
||||||
return False
|
|
||||||
if seeking_hour > seeking_hour_e:
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
# meeting has only start time
|
|
||||||
else:
|
|
||||||
seeking_hour, seeking_min = self.get_hour_minutes(time)
|
|
||||||
seeking_hour_s, seeking_min_s = self.get_hour_minutes(start)
|
|
||||||
if seeking_hour == seeking_hour_s and seeking_min == seeking_min_s:
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def is_collision(self, date, time_start, time_end):
|
if format_date(date_time) in self.db.keys():
|
||||||
# TODO zaimplementować oferowanie innego czasu w razie kolizji
|
meetings = self.db[format_date(date_time)]
|
||||||
if date in self.DB.keys():
|
for key, meeting in enumerate(meetings):
|
||||||
meetings = DB[date]
|
if format_time(meeting['time']) == format_time(time):
|
||||||
for m in meetings:
|
meetings.remove(meeting)
|
||||||
meeting_time = m['time'].split("-")
|
|
||||||
start = meeting_time[0]
|
self.db[format_date(date_time)] = meetings
|
||||||
# meeting has start and end time
|
self.save_db()
|
||||||
if len(meeting_time > 1):
|
|
||||||
end = meeting_time[1]
|
def update_meeting(self, meeting_dict):
|
||||||
hour_s, min_s = self.get_hour_minutes(time_start)
|
date_time = get_date(
|
||||||
hour_e, min_e = self.get_hour_minutes(time_end)
|
meeting_dict['date'].lower(), meeting_dict['time'].lower())
|
||||||
seeking_hour_s, seeking_min_s = self.get_hour_minutes(start)
|
|
||||||
seeking_hour_e, seeking_min_e = self.get_hour_minutes(end)
|
if format_date(date_time) in self.db.keys():
|
||||||
if hour_s < seeking_hour_s and hour_e < seeking_hour_s:
|
meetings = self.db[format_date(date_time)]
|
||||||
return False
|
for key, meeting in enumerate(meetings):
|
||||||
if hour_s < seeking_hour_s and hour_e == seeking_hour_s and min_e <= seeking_min_s:
|
if format_time(meeting['time']) == format_time(meeting_dict['time'].lower()):
|
||||||
return False
|
meetings.remove(meeting)
|
||||||
if hour_s == seeking_hour_s and min_s < seeking_min_s and hour_e == seeking_hour_s and min_e <= seeking_min_s:
|
|
||||||
return False
|
meetings.append(meeting_dict)
|
||||||
if hour_s == seeking_hour_e and min_s >= seeking_min_e:
|
self.db[format_date(date_time)] = meetings
|
||||||
return False
|
self.save_db()
|
||||||
if hour_s > seeking_hour_e:
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
# meeting has only start time
|
|
||||||
else:
|
|
||||||
seeking_hour, seeking_min = self.get_hour_minutes(time)
|
|
||||||
seeking_hour_s, seeking_min_s = self.get_hour_minutes(start)
|
|
||||||
if seeking_hour == seeking_hour_s and seeking_min == seeking_min_s:
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def find_meeting(self, date, time):
|
def find_meeting(self, date, time):
|
||||||
if date in self.DB.keys():
|
if date in self.db.keys():
|
||||||
meetings = DB[date]
|
meetings = self.db[date]
|
||||||
for m in meetings:
|
for m in meetings:
|
||||||
if time == m['time']:
|
# Czas może być w formacie (13, 13:00, 13:00-13:30), trzeba dopasować obie strony i porównywać tylko czas startu
|
||||||
|
if format_time(time) == format_time(m['time']):
|
||||||
return m
|
return m
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_meetings(self, date_list):
|
def get_meetings(self, date_list):
|
||||||
|
# lista dat dla których chcemy znaleźć spotkania
|
||||||
meetings = []
|
meetings = []
|
||||||
for date in date_list:
|
for date in date_list:
|
||||||
if date in self.DB.keys():
|
if date in self.db.keys():
|
||||||
date_meetings = self.DB[date]
|
meetings += self.db[date]
|
||||||
for m in date_meetings:
|
|
||||||
meetings.append(m)
|
|
||||||
return meetings
|
return meetings
|
||||||
|
|
||||||
def get_freetime(self, date_list):
|
def is_collision(self, date, time):
|
||||||
freetime_dict = {}
|
meetings = self.db[date]
|
||||||
for date in date_list:
|
start_time = format_time(time)
|
||||||
time_str = self.start_day_time + "-" + self.end_day_time
|
end_time = get_end_time(time)
|
||||||
freetime_dict[date] = [time_str]
|
|
||||||
# Change free time if there are meetings at this day
|
|
||||||
if date in self.DB.keys():
|
|
||||||
for meeting in self.DB[date]:
|
|
||||||
meeting_time = meeting['time'].split("-")
|
|
||||||
start = meeting_time[0]
|
|
||||||
# meeting has start and end time
|
|
||||||
if len(meeting_time > 1):
|
|
||||||
end = meeting_time[1]
|
|
||||||
meet_start_h, meet_start_m = self.get_hour_minutes(start)
|
|
||||||
meet_end_h, meet_end_m = self.get_hour_minutes(end)
|
|
||||||
for slot in freetime_dict[date]:
|
|
||||||
slot_time = slot.split("-")
|
|
||||||
slot_start_h, slot_start_m = self.get_hour_minutes(slot_time[0])
|
|
||||||
slot_end_h, slot_end_m = self.get_hour_minutes(slot_time[1])
|
|
||||||
# if meeting time have collision with slot time - change / split slot
|
|
||||||
|
|
||||||
# meeting starts inside slot
|
for meeting in meetings:
|
||||||
if meet_start_h == slot_start_h and meet_start_m > slot_start_m and meet_start_h < slot_end_h:
|
if self.overlap(start_time, end_time, format_time(meeting['time']), get_end_time(meeting['time'])):
|
||||||
pass
|
return True
|
||||||
elif meet_start_h == slot_start_h and meet_start_m > slot_start_m and meet_start_h == slot_end_h and meet_start_m < slot_end_m:
|
if start_time == format_time(meeting['time']) or end_time == get_end_time(meeting['time']):
|
||||||
pass
|
return True
|
||||||
elif meet_start_h > slot_start_h and meet_start_h < slot_end_h:
|
|
||||||
pass
|
|
||||||
elif meet_start_h > slot_start_h and meet_start_h == slot_end_h and meet_start_m < slot_end_m:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# starts before slot, ends inside slot
|
|
||||||
elif (meet_end_h > slot_start_h) or (meet_end_h == slot_start_h and meet_end_m > slot_start_h):
|
|
||||||
# meeting in whole slot - delete this slot
|
|
||||||
if meet_end_h == slot_end_h and meet_end_m == slot_end_m:
|
|
||||||
pass
|
|
||||||
# cut slot from left side
|
|
||||||
else:
|
|
||||||
pass
|
|
||||||
# if meeting starts and ends before slot start time - do nothing
|
|
||||||
# meeting has only start time
|
|
||||||
else:
|
|
||||||
m_start_h, m_start_m = self.get_hour_minutes(start)
|
|
||||||
|
|
||||||
def update_meeting(self, date, time, new_meeting_dict):
|
|
||||||
# TODO nie testowane
|
|
||||||
if date in self.DB.keys():
|
|
||||||
meetings = DB[date]
|
|
||||||
to_update = next(m for m in meetings if m['time'] == time)
|
|
||||||
if type(to_update) is dict:
|
|
||||||
to_update = new_meeting_dict
|
|
||||||
self.save_db()
|
|
||||||
|
|
||||||
def delete_meeting(self, date, time):
|
|
||||||
# TODO nie testowane
|
|
||||||
if date in self.DB.keys():
|
|
||||||
meetings = DB[date]
|
|
||||||
to_delete = next(m for m in meetings if m['time'] == time)
|
|
||||||
if type(to_delete) is dict:
|
|
||||||
del to_delete
|
|
||||||
self.save_db()
|
|
||||||
|
|
||||||
# other methods
|
|
||||||
|
|
||||||
def get_hour_minutes(self, time):
|
|
||||||
temp = time.split(":")
|
|
||||||
return int(temp[0]), int(temp[1])
|
|
||||||
|
|
||||||
def get_day_month_year(self, date):
|
|
||||||
temp = date.split(".")
|
|
||||||
return int(temp[0]), int(temp[1]), int(temp[2])
|
|
||||||
|
|
||||||
def calculate_date_with_offset(self, offset):
|
|
||||||
last_month_day = self.month_last_day(self.today_month, self.today_year)
|
|
||||||
new_day = self.today_day + offset
|
|
||||||
if new_day > last_month_day:
|
|
||||||
while True:
|
|
||||||
difference = new_day - last_month_day
|
|
||||||
if self.today_month == 12:
|
|
||||||
new_month = 1
|
|
||||||
new_year = self.today_year+1
|
|
||||||
else:
|
|
||||||
new_month = self.today_month+1
|
|
||||||
new_year = self.today_year
|
|
||||||
last_month_day = self.month_last_day(new_month, new_year)
|
|
||||||
new_day = difference
|
|
||||||
if new_day <= last_month_day:
|
|
||||||
date_str = ""
|
|
||||||
if new_day < 10:
|
|
||||||
date_str += "0"
|
|
||||||
date_str += str(new_day)
|
|
||||||
date_str += "."
|
|
||||||
if new_month < 10:
|
|
||||||
date_str += "0"
|
|
||||||
date_str += str(new_month)
|
|
||||||
date_str += "."
|
|
||||||
date_str += str(new_year)
|
|
||||||
return date_str
|
|
||||||
else:
|
|
||||||
date_str = ""
|
|
||||||
if new_day < 10:
|
|
||||||
date_str += "0"
|
|
||||||
date_str += str(new_day)
|
|
||||||
date_str += "."
|
|
||||||
if self.today_month < 10:
|
|
||||||
date_str += "0"
|
|
||||||
date_str += str(self.today_month)
|
|
||||||
date_str += "."
|
|
||||||
date_str += str(self.today_year)
|
|
||||||
return date_str
|
|
||||||
|
|
||||||
def month_last_day(self, month, year):
|
|
||||||
last_month_day = 30
|
|
||||||
if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
|
|
||||||
last_month_day = 31
|
|
||||||
elif month == 2:
|
|
||||||
if year % 4 == 0:
|
|
||||||
last_month_day = 29
|
|
||||||
else:
|
|
||||||
last_month_day = 28
|
|
||||||
return last_month_day
|
|
||||||
|
|
||||||
def is_date_day_name(self, date):
|
|
||||||
if date == "poniedziałek":
|
|
||||||
return True
|
|
||||||
if date == "wtorek":
|
|
||||||
return True
|
|
||||||
if date == "środa":
|
|
||||||
return True
|
|
||||||
if date == "czwartek":
|
|
||||||
return True
|
|
||||||
if date == "piątek":
|
|
||||||
return True
|
|
||||||
if date == "sobota":
|
|
||||||
return True
|
|
||||||
if date == "niedziela":
|
|
||||||
return True
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def get_days_difference(self, date):
|
def overlap(self, checking_start, checking_end, base_start, base_end):
|
||||||
if date == "poniedziałek":
|
interval1 = [datetime.datetime.strptime(checking_start, '%H:%M'),
|
||||||
if date == "poniedziałek":
|
datetime.datetime.strptime(checking_end, '%H:%M')]
|
||||||
return 7
|
interval2 = [datetime.datetime.strptime(base_start, '%H:%M'),
|
||||||
if date == "wtorek":
|
datetime.datetime.strptime(base_end, '%H:%M')]
|
||||||
return 1
|
results = []
|
||||||
if date == "środa":
|
for timestamp in interval1:
|
||||||
return 2
|
results.append(interval2[0] < timestamp < interval2[1])
|
||||||
if date == "czwartek":
|
for timestamp in interval2:
|
||||||
return 3
|
results.append(interval1[0] < timestamp < interval1[1])
|
||||||
if date == "piątek":
|
return True in results
|
||||||
return 4
|
|
||||||
if date == "sobota":
|
# def get_freetime(self, date_list):
|
||||||
return 5
|
# freetime_dict = {}
|
||||||
if date == "niedziela":
|
# for date in date_list:
|
||||||
return 6
|
# time_str = self.start_day_time + "-" + self.end_day_time
|
||||||
if date == "wtorek":
|
# freetime_dict[date] = [time_str]
|
||||||
if date == "poniedziałek":
|
# # Change free time if there are meetings at this day
|
||||||
return 6
|
# if date in self.DB.keys():
|
||||||
if date == "wtorek":
|
# for meeting in self.DB[date]:
|
||||||
return 7
|
# meeting_time = meeting['time'].split("-")
|
||||||
if date == "środa":
|
# start = meeting_time[0]
|
||||||
return 1
|
# # meeting has start and end time
|
||||||
if date == "czwartek":
|
# if len(meeting_time > 1):
|
||||||
return 2
|
# end = meeting_time[1]
|
||||||
if date == "piątek":
|
# meet_start_h, meet_start_m = self.get_hour_minutes(
|
||||||
return 3
|
# start)
|
||||||
if date == "sobota":
|
# meet_end_h, meet_end_m = self.get_hour_minutes(end)
|
||||||
return 4
|
# for slot in freetime_dict[date]:
|
||||||
if date == "niedziela":
|
# slot_time = slot.split("-")
|
||||||
return 5
|
# slot_start_h, slot_start_m = self.get_hour_minutes(
|
||||||
if date == "środa":
|
# slot_time[0])
|
||||||
if date == "poniedziałek":
|
# slot_end_h, slot_end_m = self.get_hour_minutes(
|
||||||
return 5
|
# slot_time[1])
|
||||||
if date == "wtorek":
|
# # if meeting time have collision with slot time - change / split slot
|
||||||
return 6
|
|
||||||
if date == "środa":
|
# # meeting starts inside slot
|
||||||
return 7
|
# if meet_start_h == slot_start_h and meet_start_m > slot_start_m and meet_start_h < slot_end_h:
|
||||||
if date == "czwartek":
|
# pass
|
||||||
return 1
|
# elif meet_start_h == slot_start_h and meet_start_m > slot_start_m and meet_start_h == slot_end_h and meet_start_m < slot_end_m:
|
||||||
if date == "piątek":
|
# pass
|
||||||
return 2
|
# elif meet_start_h > slot_start_h and meet_start_h < slot_end_h:
|
||||||
if date == "sobota":
|
# pass
|
||||||
return 3
|
# elif meet_start_h > slot_start_h and meet_start_h == slot_end_h and meet_start_m < slot_end_m:
|
||||||
if date == "niedziela":
|
# pass
|
||||||
return 4
|
|
||||||
if date == "czwartek":
|
# # starts before slot, ends inside slot
|
||||||
if date == "poniedziałek":
|
# elif (meet_end_h > slot_start_h) or (meet_end_h == slot_start_h and meet_end_m > slot_start_h):
|
||||||
return 4
|
# # meeting in whole slot - delete this slot
|
||||||
if date == "wtorek":
|
# if meet_end_h == slot_end_h and meet_end_m == slot_end_m:
|
||||||
return 5
|
# pass
|
||||||
if date == "środa":
|
# # cut slot from left side
|
||||||
return 6
|
# else:
|
||||||
if date == "czwartek":
|
# pass
|
||||||
return 7
|
# # if meeting starts and ends before slot start time - do nothing
|
||||||
if date == "piątek":
|
# # meeting has only start time
|
||||||
return 1
|
# else:
|
||||||
if date == "sobota":
|
# m_start_h, m_start_m = self.get_hour_minutes(start)
|
||||||
return 2
|
|
||||||
if date == "niedziela":
|
|
||||||
return 3
|
# Tests
|
||||||
if date == "piątek":
|
|
||||||
if date == "poniedziałek":
|
# db = calender_db()
|
||||||
return 3
|
# db.create_meeting({"date": "16.06.2021", "time": "15:00",
|
||||||
if date == "wtorek":
|
# "description": "chuj"})
|
||||||
return 4
|
# db.create_meeting({"date": "14.06.2021", "time": "13:00-18:00"})
|
||||||
if date == "środa":
|
# db.delete_meeting("16.06.2021", "15:00")
|
||||||
return 5
|
# print(db.find_meeting("16.06.2021", "13:00-14:00"))
|
||||||
if date == "czwartek":
|
# print(db.get_meetings(["16.06.2021", "14.06.2021"]))
|
||||||
return 6
|
# print(db.is_collision("16.06.2021", "12:30-13"))
|
||||||
if date == "piątek":
|
|
||||||
return 7
|
|
||||||
if date == "sobota":
|
|
||||||
return 1
|
|
||||||
if date == "niedziela":
|
|
||||||
return 2
|
|
||||||
if date == "sobota":
|
|
||||||
if date == "poniedziałek":
|
|
||||||
return 2
|
|
||||||
if date == "wtorek":
|
|
||||||
return 3
|
|
||||||
if date == "środa":
|
|
||||||
return 4
|
|
||||||
if date == "czwartek":
|
|
||||||
return 5
|
|
||||||
if date == "piątek":
|
|
||||||
return 6
|
|
||||||
if date == "sobota":
|
|
||||||
return 7
|
|
||||||
if date == "niedziela":
|
|
||||||
return 1
|
|
||||||
if date == "niedziela":
|
|
||||||
if date == "poniedziałek":
|
|
||||||
return 1
|
|
||||||
if date == "wtorek":
|
|
||||||
return 2
|
|
||||||
if date == "środa":
|
|
||||||
return 3
|
|
||||||
if date == "czwartek":
|
|
||||||
return 4
|
|
||||||
if date == "piątek":
|
|
||||||
return 5
|
|
||||||
if date == "sobota":
|
|
||||||
return 6
|
|
||||||
if date == "niedziela":
|
|
||||||
return 7
|
|
||||||
|
Loading…
Reference in New Issue
Block a user