mirror of
https://github.com/bringout/oca-technical.git
synced 2026-04-19 09:51:59 +02:00
Initial commit: OCA Technical packages (595 packages)
This commit is contained in:
commit
2cc02aac6e
24950 changed files with 2318079 additions and 0 deletions
|
|
@ -0,0 +1,7 @@
|
|||
from . import iot_lock
|
||||
from . import iot_key
|
||||
from . import iot_rule
|
||||
from . import iot_key_action
|
||||
from . import res_partner
|
||||
from . import iot_device_input
|
||||
from . import iot_device
|
||||
30
odoo-bringout-oca-iot-iot_rule/iot_rule/models/iot_device.py
Normal file
30
odoo-bringout-oca-iot-iot_rule/iot_rule/models/iot_device.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
# Copyright 2019 Creu Blanca
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
from odoo import models
|
||||
|
||||
|
||||
class IotDevice(models.Model):
|
||||
_inherit = "iot.device"
|
||||
|
||||
def get_iot_keys(self, serial_of_input, type_of_key):
|
||||
iot_input = self.env["iot.device.input"].search(
|
||||
[("serial", "=", serial_of_input)], limit=1
|
||||
)
|
||||
if iot_input:
|
||||
if iot_input.lock_id:
|
||||
domain = []
|
||||
if type_of_key != "ALL":
|
||||
domain.append(("key_type", "=", type_of_key))
|
||||
return {"keys": iot_input.lock_id.get_virtual_keys(domain)}
|
||||
return {"error": "no input found"}
|
||||
|
||||
def write(self, vals):
|
||||
if vals.get("name", False):
|
||||
for device in self:
|
||||
for device_input in device.input_ids:
|
||||
if device_input.lock_id:
|
||||
input_name = device_input.name
|
||||
device_input.lock_id.write(
|
||||
{"name": vals.get("name") + " / " + input_name}
|
||||
)
|
||||
return super().write(vals)
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
# Copyright 2019 Creu Blanca
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class IotDeviceInput(models.Model):
|
||||
_inherit = "iot.device.input"
|
||||
|
||||
lock_id = fields.Many2one("iot.lock")
|
||||
|
||||
def call_lock(self, value):
|
||||
result = self.lock_id.check_access_unique_virtual_key(value)
|
||||
return {"access_granted": result}
|
||||
|
||||
def get_iot_keys(self, domain=None):
|
||||
if domain is None:
|
||||
domain = []
|
||||
return {"keys": self.lock_id.get_virtual_keys(domain)}
|
||||
|
||||
def generate_iot_lock(self):
|
||||
self.ensure_one()
|
||||
if not self.lock_id:
|
||||
device_name = self.device_id.name
|
||||
lock = self.env["iot.lock"].create(
|
||||
{"name": device_name + " / " + self.name}
|
||||
)
|
||||
self.lock_id = lock.id
|
||||
return {}
|
||||
122
odoo-bringout-oca-iot-iot_rule/iot_rule/models/iot_key.py
Normal file
122
odoo-bringout-oca-iot-iot_rule/iot_rule/models/iot_key.py
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
# Copyright 2019 Creu Blanca
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from odoo import _, api, fields, models
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
|
||||
class IotKey(models.Model):
|
||||
_name = "iot.key"
|
||||
_description = "IoT Key"
|
||||
_rec_name = "unique_virtual_key"
|
||||
|
||||
name = fields.Char()
|
||||
key_type = fields.Char()
|
||||
unique_virtual_key = fields.Char(
|
||||
readonly=True,
|
||||
default="/",
|
||||
required=True,
|
||||
)
|
||||
expiration_date = fields.Datetime()
|
||||
rule_ids = fields.Many2many("iot.rule", string="Rules")
|
||||
action_ids = fields.One2many("iot.key.action", inverse_name="key_id")
|
||||
active = fields.Boolean(default=True)
|
||||
res_id = fields.Integer(index=True)
|
||||
res_model = fields.Char(index=True)
|
||||
|
||||
_sql_constraints = [
|
||||
(
|
||||
"unique_virtual_key_uniq",
|
||||
"UNIQUE(unique_virtual_key)",
|
||||
"Key must be unique",
|
||||
),
|
||||
]
|
||||
|
||||
def _get_unique_virtual_key(self, vals):
|
||||
"""Hook that can be used to define the key according to needs"""
|
||||
return uuid.uuid4()
|
||||
|
||||
@api.model_create_multi
|
||||
def create(self, mvals):
|
||||
for vals in mvals:
|
||||
if vals.get("unique_virtual_key", "/") == "/":
|
||||
vals["unique_virtual_key"] = self._get_unique_virtual_key(vals)
|
||||
return super().create(mvals)
|
||||
|
||||
def view_actions(self):
|
||||
self.ensure_one()
|
||||
action = self.env.ref("iot_rule.iot_key_action_act_window").read()[0]
|
||||
action["domain"] = [("key_id", "=", self.id)]
|
||||
return action
|
||||
|
||||
def get_iot_rule_values(self):
|
||||
result = []
|
||||
for key in self:
|
||||
result.append(key._get_iot_rule_values())
|
||||
return result
|
||||
|
||||
def _get_iot_rule_values(self):
|
||||
if self.expiration_date:
|
||||
expiration_timestamp = int(datetime.timestamp(self.expiration_date))
|
||||
else:
|
||||
expiration_timestamp = False
|
||||
return {
|
||||
"key_type": self.key_type,
|
||||
"expiration_date": expiration_timestamp,
|
||||
"unique_virtual_key": self.unique_virtual_key,
|
||||
"id": self.id,
|
||||
}
|
||||
|
||||
def _get_unique_key_models(self):
|
||||
return []
|
||||
|
||||
@api.constrains("res_model", "res_id")
|
||||
def _constrain_key_model(self):
|
||||
unique_models = self._get_unique_key_models()
|
||||
for record in self:
|
||||
if record.res_model not in unique_models:
|
||||
continue
|
||||
if self.with_context(active_test=False).search(
|
||||
[
|
||||
("id", "!=", record.id),
|
||||
("res_id", "=", record.res_id),
|
||||
("res_model", "=", record.res_model),
|
||||
],
|
||||
limit=1,
|
||||
):
|
||||
raise ValidationError(_("Only one key can be assigned to this model"))
|
||||
|
||||
|
||||
class IotKeyMixin(models.AbstractModel):
|
||||
_name = "iot.key.mixin"
|
||||
_description = "Mixin for relation between record and Iot Keys"
|
||||
|
||||
iot_key_ids = fields.One2many(
|
||||
"iot.key",
|
||||
inverse_name="res_id",
|
||||
domain=lambda r: [("res_model", "=", r._name)],
|
||||
)
|
||||
iot_key_count = fields.Integer(compute="_compute_iot_key_count")
|
||||
|
||||
@api.depends("iot_key_ids")
|
||||
def _compute_iot_key_count(self):
|
||||
for record in self:
|
||||
record.iot_key_count = len(record.iot_key_ids)
|
||||
|
||||
def action_view_iot_key(self):
|
||||
self.ensure_one()
|
||||
action = self.env["ir.actions.act_window"]._for_xml_id(
|
||||
"iot_rule.iot_key_act_window"
|
||||
)
|
||||
action["domain"] = [
|
||||
("res_id", "=", self.id),
|
||||
("res_model", "=", self._name),
|
||||
]
|
||||
action["context"] = {
|
||||
"default_res_id": self.id,
|
||||
"default_res_model": self._name,
|
||||
}
|
||||
return action
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
# Copyright 2019 Creu Blanca
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class IotKeyAction(models.Model):
|
||||
|
||||
_name = "iot.key.action"
|
||||
_description = "IoT Key - Actions Log"
|
||||
_order = "timestamp DESC"
|
||||
|
||||
key_id = fields.Many2one("iot.key")
|
||||
key_name = fields.Char(related="key_id.name", string="Key's Name")
|
||||
unique_virtual_key = fields.Char()
|
||||
lock_id = fields.Many2one("iot.lock", required=True)
|
||||
result = fields.Char(required=True, default="undefined")
|
||||
timestamp = fields.Datetime(
|
||||
string="Time", default=fields.Datetime.now, required=True
|
||||
)
|
||||
66
odoo-bringout-oca-iot-iot_rule/iot_rule/models/iot_lock.py
Normal file
66
odoo-bringout-oca-iot-iot_rule/iot_rule/models/iot_lock.py
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
# Copyright 2019 Creu Blanca
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class IotLock(models.Model):
|
||||
|
||||
_name = "iot.lock"
|
||||
_description = "IoT Lock"
|
||||
|
||||
name = fields.Char(required=True)
|
||||
description = fields.Text()
|
||||
rule_ids = fields.Many2many("iot.rule", string="Rules")
|
||||
action_ids = fields.One2many("iot.key.action", inverse_name="lock_id")
|
||||
active = fields.Boolean(default=True, required=True)
|
||||
|
||||
def check_access_unique_virtual_key(self, unique_virtual_key):
|
||||
self.ensure_one()
|
||||
key = self.env["iot.key"].search(
|
||||
[("unique_virtual_key", "=", unique_virtual_key)], limit=1
|
||||
)
|
||||
if not key:
|
||||
self._missing_key(unique_virtual_key)
|
||||
return False
|
||||
return self.check_access(key)
|
||||
|
||||
def _missing_key(self, unique_virtual_key):
|
||||
return self.env["iot.key.action"].create(
|
||||
self._missing_key_action_vals(unique_virtual_key)
|
||||
)
|
||||
|
||||
def _missing_key_action_vals(self, unique_virtual_key):
|
||||
return {
|
||||
"lock_id": self.id,
|
||||
"unique_virtual_key": unique_virtual_key,
|
||||
"result": "refused",
|
||||
}
|
||||
|
||||
def check_access(self, key):
|
||||
result = self.rule_ids._check_access(key)
|
||||
self.create_action(key, result)
|
||||
return result
|
||||
|
||||
def create_action(self, key, result):
|
||||
return self.env["iot.key.action"].create(self._create_action_vals(key, result))
|
||||
|
||||
def _create_action_vals(self, key, result):
|
||||
return {
|
||||
"lock_id": self.id,
|
||||
"key_id": key.id,
|
||||
"result": "accepted" if result else "refused",
|
||||
}
|
||||
|
||||
def view_actions(self):
|
||||
self.ensure_one()
|
||||
action = self.env.ref("iot_rule.iot_key_action_act_window").read()[0]
|
||||
action["domain"] = [("lock_id", "=", self.id)]
|
||||
return action
|
||||
|
||||
def get_virtual_keys(self, domain=None):
|
||||
self.ensure_one()
|
||||
if domain is None:
|
||||
domain = []
|
||||
keys = self.rule_ids._get_virtual_keys(domain)
|
||||
return keys.get_iot_rule_values()
|
||||
55
odoo-bringout-oca-iot-iot_rule/iot_rule/models/iot_rule.py
Normal file
55
odoo-bringout-oca-iot-iot_rule/iot_rule/models/iot_rule.py
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
# Copyright 2019 Creu Blanca
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import _, api, fields, models
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
|
||||
class IotRule(models.Model):
|
||||
|
||||
_name = "iot.rule"
|
||||
_description = "IoT Rule (Relation Key-Lock)"
|
||||
|
||||
name = fields.Char()
|
||||
active = fields.Boolean(default=True, required=True)
|
||||
lock_ids = fields.Many2many("iot.lock", string="Locks")
|
||||
parent_ids = fields.Many2many(
|
||||
"iot.rule",
|
||||
relation="iot_rule_implied_rel",
|
||||
column1="rule_id",
|
||||
column2="implied_rule_id",
|
||||
)
|
||||
|
||||
def _check_access(self, key):
|
||||
if key.expiration_date and key.expiration_date < fields.Datetime.now():
|
||||
return False
|
||||
return self._check_access_recursion(key)
|
||||
|
||||
def _check_access_recursion(self, key, limit=0):
|
||||
if not self:
|
||||
return False
|
||||
if limit > 50:
|
||||
return False
|
||||
if self & key.rule_ids:
|
||||
return True
|
||||
return self.mapped("parent_ids")._check_access_recursion(key, limit + 1)
|
||||
|
||||
@api.constrains("parent_ids")
|
||||
def _check_recursion_parent_ids(self):
|
||||
if not self._check_m2m_recursion("parent_ids"):
|
||||
raise ValidationError(_("A recurssion was found"))
|
||||
|
||||
def _get_virtual_keys(self, domain, limit=0):
|
||||
if not self or limit > 50:
|
||||
return self.env["iot.key"]
|
||||
return self.mapped("parent_ids")._get_virtual_keys(
|
||||
domain, limit + 1
|
||||
) | self.env["iot.key"].search(
|
||||
domain
|
||||
+ [
|
||||
("rule_ids", "in", self.ids),
|
||||
"|",
|
||||
("expiration_date", "=", False),
|
||||
("expiration_date", ">=", fields.Datetime.now()),
|
||||
]
|
||||
)
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
# Copyright 2020 Creu Blanca
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import models
|
||||
|
||||
|
||||
class ResPartner(models.Model):
|
||||
_name = "res.partner"
|
||||
_inherit = ["res.partner", "iot.key.mixin"]
|
||||
Loading…
Add table
Add a link
Reference in a new issue