Пример #1
0
def pull_process_and_push_data(settings, device, device_attendance_logs=None):
	attendance_success_log_file = '_'.join(["attendance_success_log", device.device_id])
	attendance_failed_log_file = '_'.join(["attendance_failed_log", device.device_id])
	attendance_success_logger = setup_logger(attendance_success_log_file, '/'.join([logs_directory, attendance_success_log_file])+'.log')
	attendance_failed_logger = setup_logger(attendance_failed_log_file, '/'.join([logs_directory, attendance_failed_log_file])+'.log')
	if not device_attendance_logs:
		device_attendance_logs = fetch_attendance(device)
		if not device_attendance_logs:
			return
	index_of_last = -1
	last_line = get_last_line_from_file('/'.join([logs_directory, attendance_success_log_file])+'.log')
	import_start_date = _safe_convert_date(settings.import_start_date, "%Y%m%d")
	if last_line or import_start_date:
		last_user_id = None
		last_timestamp = None
		if last_line:
			last_user_id, last_timestamp = last_line.split("\t")[4:6]
			last_timestamp = datetime.datetime.fromtimestamp(float(last_timestamp))
		if import_start_date:
			if last_timestamp:
				if last_timestamp < import_start_date:
					last_timestamp = import_start_date
					last_user_id = None
			else:
				last_timestamp = import_start_date
		for i, x in enumerate(device_attendance_logs):
			if last_user_id and last_timestamp:
				if last_user_id == str(x['user_id']) and last_timestamp == x['timestamp']:
					index_of_last = i
					break
			elif last_timestamp:
				if x['timestamp'] >= last_timestamp:
					index_of_last = i
					break

	for device_attendance_log in device_attendance_logs[index_of_last+1:]:
		punch_direction = device.punch_direction
		if punch_direction == 'AUTO':
			if device_attendance_log['punch'] in device_punch_values_OUT:
				punch_direction = 'OUT'
			elif device_attendance_log['punch'] in device_punch_values_IN:
				punch_direction = 'IN'
			else:
				punch_direction = None

		employee = frappe.db.get_value('Employee',{'attendance_device_id': device_attendance_log['user_id']})
		checkin_record = frappe.db.get_value('Employee Checkin',
						{
							'employee':employee,
							'time':device_attendance_log['timestamp'],
							'device_id': device.device_id,
							'log_type': punch_direction
						})
		if not checkin_record:
			add_log_based_on_employee_field(device_attendance_log['user_id'], device_attendance_log['timestamp'], device.device_id, punch_direction)
Пример #2
0
	def test_add_log_based_on_employee_field(self):
		employee = make_employee("*****@*****.**")
		employee = frappe.get_doc("Employee", employee)
		employee.attendance_device_id = '3344'
		employee.save()

		time_now = now_datetime().__str__()[:-7]
		employee_checkin = add_log_based_on_employee_field('3344', time_now, 'mumbai_first_floor', 'IN')
		self.assertEqual(employee_checkin.employee, employee.name)
		self.assertEqual(employee_checkin.time, time_now)
		self.assertEqual(employee_checkin.device_id, 'mumbai_first_floor')
		self.assertEqual(employee_checkin.log_type, 'IN')