-
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] hr_attendance_ip_check: pre-commit auto fixes
- Loading branch information
sonhd91
committed
Aug 13, 2024
1 parent
77ac3cf
commit 74c1652
Showing
6 changed files
with
60 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from . import allowed_cidrs | ||
from . import hr_employee | ||
from . import hr_attendance | ||
from . import hr_attendance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
return super()._check_validity() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,49 @@ | ||
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 | ||
|
||
|
||
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, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters