-
Notifications
You must be signed in to change notification settings - Fork 1
/
timeclock.py
173 lines (145 loc) · 5.78 KB
/
timeclock.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
from square.client import Client as squareClient
from employeeAPI import Employee, ApiRequestError, apiKey
from datetime import datetime, timezone
from PySide2.QtWidgets import QApplication, QHBoxLayout, QVBoxLayout, QPushButton, QLabel, QWidget, QLineEdit
from PySide2.QtCore import Qt, QSize
from PySide2.QtGui import QPixmap, QMovie, QFont
import os, time, threading
logo = 'logo.png'
class InactiveEmployeeError(Exception):
def __init__(self, message):
super().__init__(message)
class EmployeeNotFoundError(Exception):
def __init__(self, message):
super().__init__(message)
Prompts_En = {
'startup':'Hello!\nPlease enter your first and last name.',
'button':'Punch!',
'punch_in':'Success! Punched in at: ',
'punch_out':'Success! Punched out at: ',
'error':'Error: ',
'translate':'Español',
'not_found':'Employee not found',
'inactive':'Employee Inactive'
}
Prompts_Es = {
'startup':'Bienvenido!\nPor favor, escribe tu primer nombre y apellido abajo.',
'button':'Punchear!',
'punch_in':'Perfecto! Entró a las: ',
'punch_out':'Perfecto! Salió a las: ',
'error':'Error: ',
'translate':'English',
'not_found':'Empleado no encontrado',
'inactive':'Empleado no activo'
}
# TODO: Cache Employee IDs and only update on startup of program
def getEmployeeID(fname, lname, location=None):
_client = squareClient(access_token=apiKey, environment='production')
_client = _client.employees
if location == None: _employees = _client.list_employees().body['employees']
else: _employees = _client.list_employees(location_id=location).body['employees']
for e in _employees:
if e['first_name'] == fname and e['last_name'] == lname:
if e['status'] == 'ACTIVE':
location = e['location_ids']
employeeID = e['id']
return e
else: raise InactiveEmployeeError('%s, %s'%(lname, fname))
raise EmployeeNotFoundError(('%s, %s'%(lname, fname)))
class Timeclock(QWidget):
def __init__(self):
super().__init__()
# Set master settings
self.setAutoFillBackground(True)
_p = self.palette()
_p.setColor(self.backgroundRole(), Qt.white)
self.setPalette(_p)
# Set Language
self._span = False
self._prompts = Prompts_En
# Initialize widgets
self.text = QLabel(self._prompts['startup'])
self.button = QPushButton(self._prompts['button'])
self.language = QPushButton(self._prompts['translate'])
self.logo = QLabel()
self.logo.setPixmap(QPixmap(os.getcwd()+'/%s'%logo))
self.buttons = QHBoxLayout()
self.name = QVBoxLayout()
self.fname = QLineEdit()
self.lname = QLineEdit()
# Set widget alignment
self.logo.setAlignment(Qt.AlignCenter)
self.text.setAlignment(Qt.AlignCenter)
self.name.setAlignment(Qt.AlignCenter)
self.fname.setAlignment(Qt.AlignCenter)
self.lname.setAlignment(Qt.AlignCenter)
# Set Font Size
self.text.setFont(QFont('Calibri', 14))
# Create button layout
self.buttons.addWidget(self.button)
self.buttons.addWidget(self.language)
# Create Name layout
self.name.addWidget(self.fname)
self.name.addWidget(self.lname)
# Set master layout
self.layout = QVBoxLayout()
self.layout.addWidget(self.logo)
self.layout.addWidget(self.text)
self.layout.addLayout(self.name)
self.layout.addLayout(self.buttons)
self.setLayout(self.layout)
# Set Actions
self.button.clicked.connect(self.submit)
self.language.clicked.connect(self.translate)
def submit(self):
_movie = QMovie(os.getcwd()+'/loading.gif')
_movie.setScaledSize(QSize(50, 50))
self.text.setMovie(_movie)
_movie.start()
t = threading.Thread(target=self.processEmployee)
t.start()
def processEmployee(self):
try:
# Formatting First and Last Name
_fname = self.fname.text()[0].upper() + self.fname.text()[1:].lower()
_lname = self.lname.text()[0].upper() + self.lname.text()[1:].lower()
# Clearing Text Fields
self.fname.clear()
self.lname.clear()
_user = getEmployeeID(_fname, _lname)
_id = _user['id']
_loc = _user['location_ids'][0]
except EmployeeNotFoundError:
self.text.setText('%s: %s, %s'%(self._prompts['not_found'], _lname, _fname))
return
except InactiveEmployeeError:
self.text.setText('%s: %s, %s'%(self._prompts['inactive'], _lname, _fname))
return
except Exception as e:
self.text.setText('%s%s'%(self._prompts['error'], e))
return
_user = Employee(_id, _loc)
try:
_time, _punch_in = _user.punch()
if _punch_in: self.text.setText(self._prompts['punch_in'] + _time)
else: self.text.setText(self._prompts['punch_out'] + _time)
time.sleep(5)
self.text.setText(self._prompts['startup'])
except ApiRequestError as e: self.text.setText('Error: API Request Error: %s'%e)
except Exception as e: self.text.setText('Error: %s'%e)
def translate(self):
if self._span:
self._span = False
self._prompts = Prompts_En
else:
self._span = True
self._prompts = Prompts_Es
self.text.setText(self._prompts['startup'])
self.button.setText(self._prompts['button'])
self.language.setText(self._prompts['translate'])
if __name__ == "__main__":
app = QApplication([])
window = Timeclock()
#window.resize(400, 600)
window.show()
app.exec_()