-
Notifications
You must be signed in to change notification settings - Fork 0
/
Date_picker.py
108 lines (93 loc) · 4.52 KB
/
Date_picker.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
from calendar import Calendar
from datetime import datetime, date
from tkinter import *
from tkinter.ttk import Style
from tkcalendar import *
from requests import Session, exceptions
class DatePicker:
def __init__(self, session: Session) -> None:
self.root = Tk()
self.root.geometry('900x600')
self.root.title("citrusHR")
style = Style(self.root)
style.theme_use('clam')
self.calendar = Calendar(self.root,
selectmode='day',
selectmultiple=True,
font="Arial 19",
showweeknumbers=False,
date_pattern='dd/MM/yyyy',
background="#353935",
normalbackground ="#353935",
foreground="#F5F5F5",
normalforeground="#F5F5F5",
borderwidth=0,
bg_selected='red',
disabledforeground="red",
style="Custom.TCalendarDay")
self.selected_dates = set()
self.label = Label(self.root, text = "")
self.successful_dates = []
self.session = session
self.calendar.bind('<<CalendarSelected>>', self.select_date)
def render(self):
self.calendar.pack(pady=20)
# Pack buttons and label
Button(self.root, text = "Request WFH", command = self.request_wfh).pack(pady = 10)
Button(self.root, text="Clear Selection", command=self.clear_selection).pack(pady=10)
Button(self.root, text = "Quit", command = self.quit).pack(pady = 10)
self.label.pack(pady = 20)
self.root.mainloop()
def request_wfh(self):
user_id = self.session.headers.get('user_id')
print(f"User ID: {user_id}")
for selected_date in self.selected_dates:
print(f"Selected Date: {selected_date}")
url = f'https://system.citrushr.com/LocationBooking/CreateHome?userId={user_id}'
payload = {
'Startdate': selected_date,
'StartPoint': 'StartOfDay',
'StartDateHours': '',
'EndDate': selected_date,
'EndPoint': 'EndOfDay',
'EndDateHours': '',
'SingleDayPeriod': 'AllDay',
'SingleDayHours': '',
'Details': ''
}
try:
response = self.session.post(url, data=payload)
print(f"Response status code: {response.status_code}")
if response.status_code == 200:
self.successful_dates.append(selected_date)
except exceptions.RequestException as e:
self.label.config(text=f'An https error occurred. Please try again')
except Exception as e:
self.label.config(text=f'Request failed with error: {e}')
if self.successful_dates:
successful_dates_str = ', '.join(self.successful_dates)
self.label.config(text=f'Work from home successfully requested on {successful_dates_str}')
self.label.after(10000, lambda: self.clear_dates())
def clear_dates(self):
self.label.config(text='')
self.successful_dates = []
def select_date(self, event):
selected_date = self.calendar.selection_get().strftime("%Y-%m-%d")
selected_date_format = datetime.strptime(selected_date, "%Y-%m-%d").date()
if selected_date in self.selected_dates:
self.selected_dates.remove(selected_date)
self.calendar.calevent_remove(selected_date_format)
self.calendar.tag_delete(selected_date)
else:
self.selected_dates.add(selected_date)
self.calendar.calevent_create(selected_date_format, 'Selected already', selected_date)
self.calendar.tag_config(selected_date, background='red', foreground='yellow')
print(f"Selected Dates: {self.selected_dates}")
def clear_selection(self):
for selected_date in self.selected_dates:
self.calendar.tag_delete(selected_date)
self.selected_dates.clear()
today_date = date.today()
self.calendar.selection_set(today_date)
def quit(self):
self.root.destroy()