diff --git a/hr_attendance_ip_check/__manifest__.py b/hr_attendance_ip_check/__manifest__.py index f249271e..e8bdf49d 100644 --- a/hr_attendance_ip_check/__manifest__.py +++ b/hr_attendance_ip_check/__manifest__.py @@ -4,7 +4,7 @@ Allow attendance check in from specified ip networks only. """, "author": "Mint System GmbH, Odoo Community Association (OCA)", - "website": "https://www.mint-system.ch", + "website": "https://github.com/OCA/hr-attendance", "category": "Technical", "version": "15.0.1.1.0", "license": "AGPL-3", diff --git a/hr_attendance_ip_check/models/__init__.py b/hr_attendance_ip_check/models/__init__.py index 05b14ee1..a011f9a1 100644 --- a/hr_attendance_ip_check/models/__init__.py +++ b/hr_attendance_ip_check/models/__init__.py @@ -1,3 +1,3 @@ from . import allowed_cidrs from . import hr_employee -from . import hr_attendance \ No newline at end of file +from . import hr_attendance diff --git a/hr_attendance_ip_check/models/allowed_cidrs.py b/hr_attendance_ip_check/models/allowed_cidrs.py index cf1b7046..bf6b0af8 100644 --- a/hr_attendance_ip_check/models/allowed_cidrs.py +++ b/hr_attendance_ip_check/models/allowed_cidrs.py @@ -1,23 +1,29 @@ -from odoo import models, fields, api, _ import ipaddress -from odoo.exceptions import ValidationError import logging + +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError + _logger = logging.getLogger(__name__) class AttendanceCidr(models.Model): - _name = 'attendance.cidr' - _description = 'Allowed Attendance CIDR' - _rec_name = 'cidr' + _name = "attendance.cidr" + _description = "Allowed Attendance CIDR" + _rec_name = "cidr" - employee_ids = fields.Many2many('hr.employee',string='Employees', ondelete='restrict') - cidr = fields.Char(string='CIDR') - single_check = fields.Boolean(help='Check if CIDR should no be combined with other CIDRs.') + employee_ids = fields.Many2many( + "hr.employee", string="Employees", ondelete="restrict" + ) + cidr = fields.Char(string="CIDR") + single_check = fields.Boolean( + help="Check if CIDR should no be combined with other CIDRs." + ) - @api.constrains('cidr') + @api.constrains("cidr") def _validate_cidr(self): for rec in self: try: ipaddress.IPv4Network(rec.cidr) except: - raise ValidationError(_('This is not a valid CIDR.')) + raise ValidationError(_("This is not a valid CIDR.")) diff --git a/hr_attendance_ip_check/models/hr_attendance.py b/hr_attendance_ip_check/models/hr_attendance.py index 134e6d19..a76693da 100644 --- a/hr_attendance_ip_check/models/hr_attendance.py +++ b/hr_attendance_ip_check/models/hr_attendance.py @@ -1,10 +1,10 @@ -from odoo import fields, models, api +from odoo import api, models class HrAttendance(models.Model): _inherit = "hr.attendance" - @api.constrains('check_in', 'check_out', 'employee_id') + @api.constrains("check_in", "check_out", "employee_id") def _check_validity(self): self.employee_id._attendance_ip_check() - return super()._check_validity() \ No newline at end of file + return super()._check_validity() diff --git a/hr_attendance_ip_check/models/hr_employee.py b/hr_attendance_ip_check/models/hr_employee.py index 1bdbb773..9323892d 100644 --- a/hr_attendance_ip_check/models/hr_employee.py +++ b/hr_attendance_ip_check/models/hr_employee.py @@ -1,6 +1,8 @@ -from odoo import _, api, fields, models, exceptions -from odoo.http import request import logging + +from odoo import _, exceptions, fields, models +from odoo.http import request + _logger = logging.getLogger(__name__) import ipaddress @@ -8,30 +10,40 @@ class HrEmployee(models.Model): _inherit = "hr.employee" - attendance_cidr_ids = fields.Many2many('attendance.cidr', ondelete='restrict') + attendance_cidr_ids = fields.Many2many("attendance.cidr", ondelete="restrict") def _attendance_ip_check(self): """Return if client ip is not in totp cidrs.""" # Get remote ip - ip_address = ipaddress.IPv4Address(request.httprequest.environ['REMOTE_ADDR']) - + ip_address = ipaddress.IPv4Address(request.httprequest.environ["REMOTE_ADDR"]) + # Get cidrs from employee employee_cidrs = self.sudo().attendance_cidr_ids - + # Get single check cidrs - allowed_cidrs = employee_cidrs.filtered('single_check')[:1] + allowed_cidrs = employee_cidrs.filtered("single_check")[:1] # Combine global and employee cidrs if not allowed_cidrs: - allowed_cidrs = self.env['attendance.cidr'].search([('employee_ids', '=', False)]) + allowed_cidrs = self.env["attendance.cidr"].search( + [("employee_ids", "=", False)] + ) allowed_cidrs += employee_cidrs - + # Chech if ip is in allowed_cidrs - in_cidr = any(ip_address in cidr for cidr in allowed_cidrs.mapped(lambda r: ipaddress.IPv4Network(r.cidr))) + in_cidr = any( + ip_address in cidr + for cidr in allowed_cidrs.mapped(lambda r: ipaddress.IPv4Network(r.cidr)) + ) - # Raise exception if there is a cidr list and the client ip is not in cidr + # Raise exception if there is a cidr list and the client ip is not in cidr if allowed_cidrs and not in_cidr: - raise exceptions.ValidationError(_('Not allowed to create new attendance record for %(empl_name)s, the client ip is not in the list of allowed ip networks.') % { - 'empl_name': self.name, - }) + raise exceptions.ValidationError( + _( + "Not allowed to create new attendance record for %(empl_name)s, the client ip is not in the list of allowed ip networks." + ) + % { + "empl_name": self.name, + } + ) diff --git a/hr_attendance_ip_check/views/attendance_cidr.xml b/hr_attendance_ip_check/views/attendance_cidr.xml index 520a5c55..28201931 100644 --- a/hr_attendance_ip_check/views/attendance_cidr.xml +++ b/hr_attendance_ip_check/views/attendance_cidr.xml @@ -1,4 +1,4 @@ - + @@ -23,7 +23,10 @@ - + @@ -37,10 +40,12 @@ tree,form - + - \ No newline at end of file +