Skip to content

Commit

Permalink
[IMP] hr_attendance_ip_check: pre-commit auto fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sonhd91 committed Aug 13, 2024
1 parent 77ac3cf commit 74c1652
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 37 deletions.
2 changes: 1 addition & 1 deletion hr_attendance_ip_check/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion hr_attendance_ip_check/models/__init__.py
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
26 changes: 16 additions & 10 deletions hr_attendance_ip_check/models/allowed_cidrs.py
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."))
6 changes: 3 additions & 3 deletions hr_attendance_ip_check/models/hr_attendance.py
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()
40 changes: 26 additions & 14 deletions hr_attendance_ip_check/models/hr_employee.py
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,
}
)
21 changes: 13 additions & 8 deletions hr_attendance_ip_check/views/attendance_cidr.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<record id="view_tree" model="ir.ui.view">
Expand All @@ -23,7 +23,10 @@
<group>
<field name="employee_ids" widget="many2many_tags" />
<field name="cidr" placeholder="192.168.2.0/24" />
<field name="single_check" attrs="{'readonly': [('employee_ids', '=', [])]}"/>
<field
name="single_check"
attrs="{'readonly': [('employee_ids', '=', [])]}"
/>
</group>
</group>
</sheet>
Expand All @@ -37,10 +40,12 @@
<field name="view_mode">tree,form</field>
</record>

<menuitem name="Attendance IP Check"
id="menu_ir_attendance_cidr"
action="ir_attendance_cidr_act"
parent="base.menu_security"
sequence="15" />
<menuitem
name="Attendance IP Check"
id="menu_ir_attendance_cidr"
action="ir_attendance_cidr_act"
parent="base.menu_security"
sequence="15"
/>

</odoo>
</odoo>

0 comments on commit 74c1652

Please sign in to comment.