This repository has been archived by the owner on Feb 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsign-out-all.py
66 lines (58 loc) · 2.66 KB
/
sign-out-all.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
from datetime import date, datetime, timedelta
from flask import Flask
import portero_config
import logging
app = Flask(__name__)
app.config.from_object('portero_config')
app.debug = app.config['DEBUG']
if app.debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
from logging.handlers import SMTPHandler
mail_handler = SMTPHandler(app.config['SMTP_HOST'], app.config['SMTP_USER'], app.config['ADMINS'], 'Portero Error')
mail_handler.setLevel(logging.ERROR)
app.logger.addHandler(mail_handler)
#file_handler = FileHandler(app.config['LOG_FILE'])
#file_handler.setLevel(logging.INFO)
#app.logger.addHandler(file_handler)
import openerplib
connection = openerplib.get_connection(hostname=app.config['ERP_HOST'], database=app.config['ERP_DB'], login=app.config['ERP_USER'], password=app.config['ERP_PASSWORD'])
employee_model = connection.get_model("hr.employee")
employees = employee_model.search_read([("active", "=", True)])
attendance_model = connection.get_model("hr.attendance")
timesheet_model = connection.get_model("hr_timesheet_sheet.sheet")
today = str(date.today().strftime('%Y-%m-%d'))
now = str(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'))
auto_signout_hours = int(app.config['AUTO_SIGNOUT_HOURS'])
employees_signed_in = [{'id': employee['id'], 'name': employee['name']} for employee in employees if employee['state'] == 'present']
#print employees_signed_in
for employee in employees_signed_in:
#print employee
current_timesheets = timesheet_model.search([("employee_id", "=", employee['id']), ("date_from", "=", today)])
if len(current_timesheets):
sheet = current_timesheets[0]
#Check to see if automatically-signed-out users are logged out "now", or get a pre-set number of hours credited
if auto_signout_hours > 0:
sign_in = attendance_model.search_read([("sheet_id", "=", sheet)])[0]
#Parse the sign-in time from the 'name' value of the attendance object
sign_in_time = datetime.strptime(sign_in['name'], '%Y-%m-%d %H:%M:%S')
#print sign_in_time
#Next, we'll create a 'timedelta' object containing the credited hours
time_diff = timedelta(hours=auto_signout_hours)
#print time_diff
#Finally, we add the credited hours to the sign-in time to generate a new sign-out time
sign_out_time = (sign_in_time + time_diff).strftime('%Y-%m-%d %H:%M:%S')
#print sign_out_time
else:
#If auto_signout_hours is set to zero, just sign users out at time script is run
sign_out_time = now
new_event = {
'employee_id' : employee['id'],
'name' : sign_out_time,
'day' : today,
'action' : 'sign_out',
'sheet_id' : int(sheet)
}
print new_event
event = attendance_model.create(new_event)