From 1594f827802e41823ce04b3c0e5c441d9c91762c Mon Sep 17 00:00:00 2001 From: RomainLibert Date: Thu, 4 Apr 2019 16:58:12 +0200 Subject: [PATCH] [IMP] hr_attendance: Adapt module with new hr.employee.public model --- addons/hr_attendance/models/hr_employee.py | 51 ++++++++++--------- addons/hr_attendance/models/res_users.py | 5 +- .../tests/test_hr_attendance_constraints.py | 21 +++----- .../models/hr_employee.py | 5 +- 4 files changed, 37 insertions(+), 45 deletions(-) diff --git a/addons/hr_attendance/models/hr_employee.py b/addons/hr_attendance/models/hr_employee.py index ac2f23b1eb8..48a1561e4a5 100644 --- a/addons/hr_attendance/models/hr_employee.py +++ b/addons/hr_attendance/models/hr_employee.py @@ -1,13 +1,14 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import models, fields, api, exceptions, _, SUPERUSER_ID + from datetime import datetime from dateutil.relativedelta import relativedelta +from odoo import models, fields, api, exceptions, _, SUPERUSER_ID -class HrEmployee(models.Model): - _inherit = "hr.employee" - _description = "Employee" + +class HrEmployeeBase(models.AbstractModel): + _inherit = "hr.employee.base" attendance_ids = fields.One2many('hr.attendance', 'employee_id', help='list of attendances for the employee') last_attendance_id = fields.Many2one('hr.attendance', compute='_compute_last_attendance_id', store=True) @@ -44,35 +45,37 @@ class HrEmployee(models.Model): """ Receive a barcode scanned from the Kiosk Mode and change the attendances of corresponding employee. Returns either an action or a warning. """ - employee = self.search([('barcode', '=', barcode)], limit=1) - return employee and employee.attendance_action('hr_attendance.hr_attendance_action_kiosk_mode') or \ - {'warning': _('No employee corresponding to barcode %(barcode)s') % {'barcode': barcode}} + employee = self.sudo().search([('barcode', '=', barcode)], limit=1) + if employee: + return employee._attendance_action('hr_attendance.hr_attendance_action_kiosk_mode') + return {'warning': _('No employee corresponding to barcode %(barcode)s') % {'barcode': barcode}} @api.multi def attendance_manual(self, next_action, entered_pin=None): self.ensure_one() - if not (entered_pin is None) or self.env['res.users'].browse(SUPERUSER_ID).has_group('hr_attendance.group_hr_attendance_use_pin') and (self.user_id and self.user_id.id != self._uid or not self.user_id): + if not (entered_pin is None) or self.env.user.has_group('hr_attendance.group_hr_attendance_use_pin') and (self.user_id and self.user_id == self.env.user): if entered_pin != self.pin: return {'warning': _('Wrong PIN')} - return self.attendance_action(next_action) + return self._attendance_action(next_action) @api.multi - def attendance_action(self, next_action): + def _attendance_action(self, next_action): """ Changes the attendance of the employee. Returns an action to the check in/out message, next_action defines which menu the check in/out message should return to. ("My Attendances" or "Kiosk Mode") """ self.ensure_one() + employee = self.sudo() action_message = self.env.ref('hr_attendance.hr_attendance_action_greeting_message').read()[0] - action_message['previous_attendance_change_date'] = self.last_attendance_id and (self.last_attendance_id.check_out or self.last_attendance_id.check_in) or False - action_message['employee_name'] = self.name - action_message['barcode'] = self.barcode + action_message['previous_attendance_change_date'] = employee.last_attendance_id and (employee.last_attendance_id.check_out or employee.last_attendance_id.check_in) or False + action_message['employee_name'] = employee.name + action_message['barcode'] = employee.barcode action_message['next_action'] = next_action - if self.user_id: - modified_attendance = self.sudo(self.user_id.id).attendance_action_change() + if employee.user_id: + modified_attendance = employee.sudo(employee.user_id.id).attendance_action_change() else: - modified_attendance = self.sudo().attendance_action_change() + modified_attendance = employee.attendance_action_change() action_message['attendance'] = modified_attendance.read()[0] return {'action': action_message} @@ -82,8 +85,7 @@ class HrEmployee(models.Model): Check In: create a new attendance record Check Out: modify check_out field of appropriate attendance record """ - if len(self) > 1: - raise exceptions.UserError(_('Cannot perform check in or check out on multiple employees.')) + self.ensure_one() action_date = fields.Datetime.now() if self.attendance_state != 'checked_in': @@ -92,11 +94,10 @@ class HrEmployee(models.Model): 'check_in': action_date, } return self.env['hr.attendance'].create(vals) + attendance = self.env['hr.attendance'].search([('employee_id', '=', self.id), ('check_out', '=', False)], limit=1) + if attendance: + attendance.check_out = action_date else: - attendance = self.env['hr.attendance'].search([('employee_id', '=', self.id), ('check_out', '=', False)], limit=1) - if attendance: - attendance.check_out = action_date - else: - raise exceptions.UserError(_('Cannot perform check out on %(empl_name)s, could not find corresponding check in. ' - 'Your attendances have probably been modified manually by human resources.') % {'empl_name': self.name, }) - return attendance + raise exceptions.UserError(_('Cannot perform check out on %(empl_name)s, could not find corresponding check in. ' + 'Your attendances have probably been modified manually by human resources.') % {'empl_name': self.sudo().name, }) + return attendance diff --git a/addons/hr_attendance/models/res_users.py b/addons/hr_attendance/models/res_users.py index cf0a329a028..8b2bc7c3a3e 100644 --- a/addons/hr_attendance/models/res_users.py +++ b/addons/hr_attendance/models/res_users.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import models, fields, api, _ +from odoo import models, fields class User(models.Model): @@ -19,7 +19,6 @@ class User(models.Model): 'hours_last_month', 'attendance_state', ] - init_res = super(User, self).__init__(pool, cr) + super(User, self).__init__(pool, cr) # duplicate list to avoid modifying the original reference type(self).SELF_READABLE_FIELDS = type(self).SELF_READABLE_FIELDS + attendance_readable_fields - return init_res diff --git a/addons/hr_attendance/tests/test_hr_attendance_constraints.py b/addons/hr_attendance/tests/test_hr_attendance_constraints.py index d02acad4bdb..8b00197cd60 100644 --- a/addons/hr_attendance/tests/test_hr_attendance_constraints.py +++ b/addons/hr_attendance/tests/test_hr_attendance_constraints.py @@ -1,8 +1,9 @@ # -*- coding: utf-8 -*- -from odoo.tests.common import TransactionCase import time +from odoo.tests.common import TransactionCase + class TestHrAttendance(TransactionCase): """Tests for attendance date ranges validity""" @@ -12,6 +13,10 @@ class TestHrAttendance(TransactionCase): self.attendance = self.env['hr.attendance'] self.test_employee = self.browse_ref('hr.employee_qdp') # demo data contains set up for self.test_employee + self.open_attendance = self.attendance.create({ + 'employee_id': self.test_employee.id, + 'check_in': time.strftime('%Y-%m-10 10:00'), + }) def test_attendance_in_before_out(self): # Make sure check_out is before check_in @@ -24,10 +29,6 @@ class TestHrAttendance(TransactionCase): def test_attendance_no_check_out(self): # Make sure no second attandance without check_out can be created - self.attendance.create({ - 'employee_id': self.test_employee.id, - 'check_in': time.strftime('%Y-%m-10 10:00'), - }) with self.assertRaises(Exception): self.attendance.create({ 'employee_id': self.test_employee.id, @@ -68,10 +69,6 @@ class TestHrAttendance(TransactionCase): }) def test_attendance_5(self): - self.attendance.create({ - 'employee_id': self.test_employee.id, - 'check_in': time.strftime('%Y-%m-10 10:00'), - }) with self.assertRaises(Exception): self.attendance.create({ 'employee_id': self.test_employee.id, @@ -86,11 +83,7 @@ class TestHrAttendance(TransactionCase): 'check_in': time.strftime('%Y-%m-10 11:00'), 'check_out': time.strftime('%Y-%m-10 12:00'), }) - open_attendance = self.attendance.create({ - 'employee_id': self.test_employee.id, - 'check_in': time.strftime('%Y-%m-10 10:00'), - }) with self.assertRaises(Exception): - open_attendance.write({ + self.open_attendance.write({ 'check_out': time.strftime('%Y-%m-10 11:30'), }) diff --git a/addons/hr_attendance_presence/models/hr_employee.py b/addons/hr_attendance_presence/models/hr_employee.py index 3f99535da87..c1dc644cac7 100644 --- a/addons/hr_attendance_presence/models/hr_employee.py +++ b/addons/hr_attendance_presence/models/hr_employee.py @@ -2,9 +2,8 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. import logging -from odoo import fields, models, _ -from odoo.exceptions import UserError -from odoo.fields import Datetime + +from odoo import models _logger = logging.getLogger(__name__)