Skip to content

Commit

Permalink
[FIX] project and add new wizard to add person in local access, acces…
Browse files Browse the repository at this point in the history
…s point
  • Loading branch information
archetipo committed Dec 6, 2024
1 parent 424f784 commit 49c6526
Show file tree
Hide file tree
Showing 16 changed files with 250 additions and 33 deletions.
7 changes: 7 additions & 0 deletions inrim_anagrafiche/models/ca_tag_persona.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,10 @@ def get_current_by_tag(self, tag):
('ca_tag_id', '=', tag.id),
('state', '=', 'to_give_back')
], limit=1)

@api.model
def get_current_by_pesona(self, persona):
return self.env['ca.tag_persona'].search([
('ca_persona_id', '=', persona.id),
('state', '=', 'to_give_back')
], limit=1)
2 changes: 1 addition & 1 deletion inrim_anagrafiche/models/ca_work_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class CaWorkInfo(models.Model):
def _check_date(self):
for record in self:
if record.date_end and record.date_start:
if record.date_end <= record.date_start:
if record.date_end < record.date_start:
raise UserError(
_('Data fine deve essere maggiore della data di inizio'))

Expand Down
1 change: 1 addition & 0 deletions inrim_controllo_accessi/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'wizard/ca_registra_persona_views.xml',
'wizard/ca_registra_doc_persona_views.xml',
'wizard/ca_ritira_badge_views.xml',
'wizard/ca_abilita_persona_locale_views.xml',
'views/ca_tag_lettore_views.xml',
'views/ca_lettore_persona_views.xml',
'views/punto_accesso_category_view.xml',
Expand Down
7 changes: 7 additions & 0 deletions inrim_controllo_accessi/models/ca_anag_registro_accesso.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class CaAnagRegistroAccesso(models.Model):
system_error = fields.Boolean(
related="ca_lettore_id.system_error", store=True,
readonly=True)
access_conflict = fields.Boolean(
default=False,
help="Access Conflict Tag Access realted to a person that is not present")
type = fields.Selection([
('manual', 'Manual'),
('auto', 'Auto')
Expand All @@ -74,12 +77,16 @@ def aggiungi_riga_accesso(
):
if not tz:
tz = self._context.get('tz')
access_conflict = False
if self.ca_punto_accesso_id.typology == 'local_access':
access_conflict = ca_tag_persona_id.ca_persona_id.present == 'no'
vals = {
'ca_punto_accesso_id': ca_punto_accesso_id.id,
'ca_tag_persona_id': ca_tag_persona_id.id,
'datetime_event': datetime_event,
'type': type,
'access_allowed': access_allowed,
'access_conflict': access_conflict,
'tz': tz
}
res = self.create(vals)
Expand Down
7 changes: 5 additions & 2 deletions inrim_controllo_accessi/models/ca_lettore_persona.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ class CaLettorePersona(models.Model):
ca_tag_lettore_id = fields.Many2one(
'ca.tag_lettore', required=True, readonly=True)
ca_lettore_id = fields.Many2one(
related="ca_tag_lettore_id.ca_lettore_id", store=True, auto_join=True)
related="ca_tag_lettore_id.ca_lettore_id", store=True, index=True)
ca_punto_accesso_id = fields.Many2one(
related="ca_tag_lettore_id.ca_punto_accesso_id", store=True, index=True)
ca_tag_persona = fields.Many2one(
'ca.tag_persona', ondelete='cascade', required=True,
readonly=True)
Expand Down Expand Up @@ -106,7 +108,8 @@ def check_update_state(self):
self.state = 'expired'

def check_update_by_date_valididty(self):
for person_reader in self.env['ca.lettore_persona'].search([]):
for person_reader in self.env['ca.lettore_persona'].search(
[('state', 'not in', ['expired'])]):
if person_reader:
person_reader.check_update_state()

Expand Down
65 changes: 57 additions & 8 deletions inrim_controllo_accessi/models/ca_punto_accesso.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ class CaPuntoAccesso(models.Model):
('local_access', 'Local Access')
], required=True)
ca_persona_id = fields.Many2one('ca.persona', string="Referent")
type_ids = fields.Many2many('ca.tipo_persona', compute="_compute_type_ids")
type_ids = fields.Many2many(
'ca.tipo_persona', compute="_compute_type_ids")
last_update_reader = fields.Datetime()
last_reading_events = fields.Datetime()
available_events = fields.Integer(
Expand All @@ -100,9 +101,9 @@ class CaPuntoAccesso(models.Model):
ca_tag_lettore_ids = fields.One2many(
'ca.tag_lettore', 'ca_punto_accesso_id')
ca_tag_lettore_persona_ids = fields.One2many(
'ca.lettore_persona', 'ca_lettore_id')
'ca.lettore_persona', 'ca_punto_accesso_id')
ca_tag_lettore_persona_view = fields.One2many(
'ca.lettore_persona', 'ca_lettore_id',
'ca.lettore_persona', 'ca_punto_accesso_id',
domain=[("state", "in", ["active", "scheduled"])]
)
remote_update = fields.Boolean(readonly=True)
Expand Down Expand Up @@ -224,13 +225,18 @@ def local_access_detach(self, tag_persona):
rimuove link tag - lettore
sync
"""

logger.info("local_access_detach")
lettore_persona = self.ca_tag_lettore_persona_ids.filtered(
lambda x: x.ca_persona_id.id == tag_persona.ca_persona_id.id
)

lettore_persona.ca_tag_lettore_id.detach()
lettore_persona.unlink()
logger.info(f"set {lettore_persona.ca_tag_lettore_id} state {lettore_persona.ca_tag_lettore_id.state} ")
logger.info(f"set {lettore_persona.ca_persona_id.name} set expired")
lettore_persona.state = 'expired'
lettore_persona.active = False
self.env['ca.lettore_persona'].elabora_persone(self.ca_lettore_id)
return True

def local_access_attach(self, tag):
"""
Expand Down Expand Up @@ -260,20 +266,48 @@ def local_access_attach(self, tag):
return True
return False

def local_access_attach_person(self, pesona):
"""
Deve esistere un record Tag/Persona
Aggiungo Tag - Lettore
Aggiungo Tag-Persona --> Da restituire
Aggiungo Lettore-Persona
:return:
"""
self.ensure_one()
tag_persona_id = self.env['ca.tag_persona'].get_current_by_tag(pesona)
if tag_persona_id:
tag = tag_persona_id.ca_tag_id
tag_lettore = self.env['ca.tag_lettore'].search(
[
('ca_tag_id', '=', tag.id),
('ca_lettore_id', "=", self.ca_lettore_id.id)
], limit=1)
if not tag_lettore:
self.env['ca.tag_lettore'].create({
'ca_lettore_id': self.ca_lettore_id.id,
'ca_tag_id': tag.id,
'date_start': self.date_start,
'date_end': self.date_end,
'ca_punto_accesso_id': self.id
})
self.env['ca.lettore_persona'].elabora_persone(self.ca_lettore_id)
return True
return False

def stamping_detach(self, tag_persona):
"""
Impost Tag_persona --> restituito o scaduto
Rimuovo Lettore-Persona
:return:
"""

lettore_persona = self.ca_tag_lettore_persona_ids.filtered(
lambda x: x.ca_persona_id.id == tag_persona.ca_persona_id.id
)
logger.info(f"set {lettore_persona.ca_persona_id.name} expired")
lettore_persona.state = 'expired'
lettore_persona.active = False
self.env['ca.lettore_persona'].elabora_persone(self.ca_lettore_id)

def stamping_attach(self):
"""
Expand Down Expand Up @@ -315,7 +349,7 @@ def check_and_attach(self):
def check_and_detach(self, tag_persona):
if self.typology == 'stamping':
self.stamping_detach(tag_persona)
elif self.typology == 'accesss':
elif self.typology == 'local_access':
self.local_access_detach(tag_persona)

def sposta_punto_accesso(self, ca_spazio_id):
Expand Down Expand Up @@ -364,3 +398,18 @@ def update_readers_data(self):

def update_clock(self):
return True

def action_abilita_persona_locale(self):
return {
'name': _('Add person in local'),
'type': 'ir.actions.act_window',
'res_model': 'ca.abilita_persona_locale',
'view_id': self.env.ref(
'inrim_controllo_accessi.ca_abilita_persona_locale_form').id,
'target': 'new',
'view_mode': "form",
'context': {
'default_punto_accesso_id': self.id
},

}
7 changes: 5 additions & 2 deletions inrim_controllo_accessi/models/ca_tag_lettore.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ def unlink(self):

def detach(self):
self.ensure_one()
self.active = False
self.state = 'expired'
if self.ca_punto_accesso_id:
self.ca_punto_accesso_id.remote_update = True
self.state = 'expired'
self.active = False


@api.onchange('ca_lettore_id')
def _onchange_ca_lettore_id(self):
Expand Down Expand Up @@ -113,6 +114,8 @@ def _compute_scheduled(self):
def check_update_state(self):
now = fields.Date.today()
self.ensure_one()
if not self.date_start and not self.date_end:
return
if self.date_start <= now <= self.date_end:
self.state = 'active'
elif self.date_start > now:
Expand Down
6 changes: 5 additions & 1 deletion inrim_controllo_accessi/security/ir.model.access.csv
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ access_ca_restituisci_badge_config,access_ca_restituisci_badge_config,model_ca_r

access_ca_registra_doc_persona_base,access_ca_registra_doc_persona_base,model_ca_registra_doc_persona,controllo_accessi.ca_base,1,0,0,0
access_ca_registra_doc_persona_ca,access_ca_registra_doc_persona_ca,model_ca_registra_doc_persona,controllo_accessi.ca_ca,1,1,1,0
access_ca_registra_doc_persona_config,access_ca_registra_doc_persona_config,model_ca_registra_doc_persona,controllo_accessi.ca_config,1,1,1,1
access_ca_registra_doc_persona_config,access_ca_registra_doc_persona_config,model_ca_registra_doc_persona,controllo_accessi.ca_config,1,1,1,1

access_ca_abilita_persona_locale_base,access_ca_abilita_persona_locale_base,model_ca_abilita_persona_locale,controllo_accessi.ca_base,1,0,0,0
access_ca_abilita_persona_locale_ca,access_ca_abilita_persona_locale_ca,model_ca_abilita_persona_locale,controllo_accessi.ca_ca,1,1,1,0
access_ca_abilita_persona_locale_config,access_ca_abilita_persona_locale_config,model_ca_abilita_persona_locale,controllo_accessi.ca_config,1,1,1,1
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<field name="direction"/>
<field name="access_allowed"/>
<field name="system_error"/>
<field name="access_conflict"/>

</tree>
</field>
Expand Down Expand Up @@ -60,7 +61,8 @@
<field name="typology"/>
<field name="direction"/>
<field name="access_allowed" readonly="True"/>
<field name="system_error"/>
<field name="system_error" readonly="True"/>
<field name="access_conflict" readonly="True"/>
<field name="type" readonly="True"/>
</group>
</group>
Expand Down
9 changes: 6 additions & 3 deletions inrim_controllo_accessi/views/ca_punto_accesso_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@
groups="controllo_accessi.ca_tech"
/>
<button
name="persone_abilitate"
string="Persone Abilitate"
name="action_abilita_persona_locale"
type="object"
icon="fa-id-badge"
string="Add person in local"
groups="controllo_accessi.ca_ca"
invisible="typology=='stamping'"
/>
<button
name="accessi_oggi"
Expand Down Expand Up @@ -122,7 +125,7 @@
domain="[('type_ids', 'in', type_ids)]"/>
<notebook>
<page name="people" string="People">
<field name="ca_tag_lettore_persona_view" options="{'no_create': True}">
<field name="ca_tag_lettore_persona_view" >
<tree default_order="ca_persona_id asc, date_end asc" options="{'no_create': True}">
<field name="ca_lettore_id" column_invisible="1"/>
<field name="ca_persona_id"/>
Expand Down
3 changes: 2 additions & 1 deletion inrim_controllo_accessi/wizard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
from . import ca_aggiungi_movimento_accesso
from . import ca_registra_persona
from . import ca_registra_doc_persona
from . import ca_restituisci_badge
from . import ca_restituisci_badge
from . import ca_abilita_persona_locale
74 changes: 74 additions & 0 deletions inrim_controllo_accessi/wizard/ca_abilita_persona_locale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import logging

from odoo import models, fields, api, _
from odoo.exceptions import UserError

logger = logging.getLogger(__name__)


class CaAbilitaPersonaLocale(models.TransientModel):
_name = 'ca.abilita_persona_locale'
_description = 'Abilita Persona locale'

punto_accesso_id = fields.Many2one(
"ca.punto_accesso", required=True,
domain=[('typology', '=', 'local_access')]
)
persona_id = fields.Many2one("ca.persona")
referente_id = fields.Many2one(
'ca.persona', string='Reference person', index=True,
domain=[('is_internal', '=', True), ("present", "=", "yes")]
)
ca_tag_persona_id = fields.Many2one(
'ca.tag_persona', compute="_compute_tag_persona")
allowed_date_start = fields.Datetime(
related="ca_tag_persona_id.date_start", readonly=True)
allowed_date_end = fields.Datetime(
related="ca_tag_persona_id.date_end", readonly=True)
date_start = fields.Datetime(required=True, default=fields.Datetime.now)
date_end = fields.Datetime(required=True)

@api.onchange('persona_id')
def _compute_tag_persona(self):
for record in self:
if not record.persona_id:
return
ca_tag_persona_id = self.env['ca.tag_persona'].get_current_by_pesona(
record.persona_id
)
lettore_persona = self.env['ca.tag_lettore'].search([
('ca_lettore_id', "=", self.punto_accesso_id.ca_lettore_id.id),
('ca_tag_id', '=', ca_tag_persona_id.ca_tag_id.id,)
], limit=1)
if lettore_persona:
raise UserError(
_('Person alredy allowed to access'))
if ca_tag_persona_id:
record.ca_tag_persona_id = ca_tag_persona_id

@api.constrains('date_start', 'date_end')
def _check_date(self):
for record in self:
if record.date_end and record.date_start:
if record.date_end < record.date_start:
raise UserError(
_('Date end must be after date start'))
if (
record.date_end < record.allowed_date_start or
record.date_start > record.allowed_date_end or
record.date_end > record.allowed_date_end
):
raise UserError(
_('Date start and date end must be beetween a valid period'))

def action_confirm(self):
self.env['ca.tag_lettore'].create({
'ca_lettore_id': self.punto_accesso_id.ca_lettore_id.id,
'ca_tag_id': self.ca_tag_persona_id.ca_tag_id.id,
'date_start': self.date_start,
'date_end': self.date_end,
'ca_punto_accesso_id': self.punto_accesso_id.id
})
self.env['ca.lettore_persona'].elabora_persone(
self.punto_accesso_id.ca_lettore_id)
return {'type': 'ir.actions.act_window_close'}
Loading

0 comments on commit 49c6526

Please sign in to comment.