Skip to content

Commit

Permalink
Merge PR #42 into 13.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed Dec 17, 2021
2 parents 026e49e + 2fc902d commit 33c44ff
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 37 deletions.
10 changes: 8 additions & 2 deletions iot_input_oca/models/iot_device_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class IotDeviceInput(models.Model):
_order = "name"

name = fields.Char(required=True)
device_id = fields.Many2one("iot.device", required=True, readonly=True)
device_id = fields.Many2one(
"iot.device", required=True, readonly=True, auto_join=True
)
call_model_id = fields.Many2one("ir.model")
call_function = fields.Char(required=True)
active = fields.Boolean(default=True)
Expand Down Expand Up @@ -50,7 +52,11 @@ def _call_device(self, *args, **kwargs):
def parse_args(self, serial, passphrase):
if not serial or not passphrase:
raise ValidationError(_("Serial and passphrase are required"))
return [("serial", "=", serial), ("passphrase", "=", passphrase)]
return [
("serial", "=", serial),
("passphrase", "=", passphrase),
("device_id.active", "=", True),
]

@api.model
def get_device(self, serial, passphrase):
Expand Down
8 changes: 8 additions & 0 deletions iot_input_oca/tests/test_iot_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ def test_single_controller(self):
)
self.assertEqual(res.json()["status"], "ok")

def test_single_controller_archived_device(self):
self.device.write({"active": False})
res = self.url_open(
"/iot/%s/action" % self.serial,
data={"passphrase": self.input_passphrase, "value": "123"},
)
self.assertEqual(res.json()["status"], "error")

def test_multi_input_controller_error_passphrase(self):
res = self.url_open(
"/iot/%s/multi_input" % self.device_identification,
Expand Down
14 changes: 13 additions & 1 deletion iot_oca/models/iot_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class IoTDevice(models.Model):
_name = "iot.device"
_description = "IoT Device"
_inherit = "image.mixin"

name = fields.Char(required=True)
system_id = fields.Many2one("iot.system", required=True)
Expand All @@ -17,12 +18,23 @@ class IoTDevice(models.Model):
# Create a multiparameter input in order to manage this
state = fields.Selection([], readonly=True)
model = fields.Char()
ip = fields.Char()
ip = fields.Char(string="IP")
action_count = fields.Integer(compute="_compute_action_count")
group_id = fields.Many2one("iot.device.group")
tag_ids = fields.Many2many("iot.device.tag")
color = fields.Integer()
last_contact_date = fields.Datetime(readonly=True)
icon = fields.Selection(
[
("fa fa-television fa-4x", "television"),
("fa fa-wifi fa-4x", "wifi"),
("fa fa-laptop fa-4x", "laptop"),
("fa fa-desktop fa-4x", "desktop"),
("fa fa-archive fa-4x", "archive"),
("fa fa-mobile fa-6x", "mobile"),
],
"Icon",
)

@api.depends("action_ids")
def _compute_action_count(self):
Expand Down
5 changes: 4 additions & 1 deletion iot_oca/security/iot_security.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
>Module administrator, able to modify configuration data.</field>
<field name="category_id" ref="ir_module_category_iot" />
<field name="implied_ids" eval="[(4, ref('group_iot_user'))]" />
<field name="users" eval="[(4,ref('base.user_root'))]" />
<field
name="users"
eval="[(4,ref('base.user_root')), (4,ref('base.user_admin'))]"
/>
</record>
</odoo>
81 changes: 55 additions & 26 deletions iot_oca/views/iot_device_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@
<form>
<header />
<sheet>
<field name="active" invisible="1" />
<div class="oe_button_box" name="button_box" />
<widget
name="web_ribbon"
text="Archived"
bg_color="bg-danger"
attrs="{'invisible': [('active', '=', True)]}"
/>
<field name="active" invisible="1" />
<div class="oe_button_box" name="button_box" />
<field
name="image_1920"
widget='image'
class="oe_avatar"
options='{"preview_image": "image_128"}'
/>
<div class="oe_title">
<h1>
<field name="name" />
Expand All @@ -44,6 +50,7 @@
options="{'color_field': 'color'}"
/>
<field name="device_identification" />
<field name="icon" />
<field name="passphrase" widget="password" />
<field
name="system_id"
Expand Down Expand Up @@ -95,10 +102,13 @@
<field name="system_id" />
<field name="group_id" />
<field name="color" />
<field name="id" />
<field name="image_128" />
<field name="icon" />
<templates>
<t t-name="kanban-box">
<div
t-attf-class="{{!selection_mode ? 'oe_kanban_color_' + kanban_getcolor(record.color.raw_value) : ''}} oe_kanban_card oe_kanban_global_click oe_semantic_html_override"
t-attf-class="{{!selection_mode ? 'oe_kanban_color_' + kanban_getcolor(record.color.raw_value) : ''}} oe_kanban_global_click"
>
<div class="o_dropdown_kanban dropdown">
<a
Expand Down Expand Up @@ -134,36 +144,55 @@
</div>
</div>
<div class="oe_kanban_content">
<div class="o_kanban_record_top">
<b class="o_kanban_record_title">
<field name="name" />
</b>
<div class="o_kanban_image">
<img
t-att-src="kanban_image('iot.device', 'image_128', record.id.raw_value)"
alt="default_image"
t-if="record.image_128.raw_value"
/>
<i
t-attf-class="{{record.icon.raw_value}}"
t-if="!record.image_128.raw_value and record.icon.raw_value"
/>
<i
t-attf-class="fa fa-question fa-6x"
t-if="!record.image_128.raw_value and !record.icon.raw_value"
/>
</div>
<div class="o_kanban_record_body">
<div t-if="record.group_id.raw_value">
<field name="group_id" />
<div class="o_kanban_details">
<div class="o_kanban_record_top">
<b class="o_kanban_record_title">
<field name="name" />
</b>
</div>
<div t-if="record.ip">IP: <field name="ip" /></div>
<div
t-if="record.last_contact_date"
>Last contact: <field
name="last_contact_date"
/></div>
<field
name="tag_ids"
widget="many2many_tags"
options="{'color_field': 'color'}"
/>
<div class="row">
<div class="o_kanban_record_body">
<div t-if="record.group_id.raw_value">
<field name="group_id" />
</div>
<div t-if="record.ip">IP: <field
name="ip"
/></div>
<div
class="col-xs-6 o_kanban_primary_left"
id="actions"
>
t-if="record.last_contact_date"
>Last contact: <field
name="last_contact_date"
/></div>
<field
name="tag_ids"
widget="many2many_tags"
options="{'color_field': 'color'}"
/>
<div class="row">
<div
class="col-xs-6 o_kanban_primary_left"
id="actions"
>
<!-- TODO: Delete this on 14, actions should go to dropdown menu -->
</div>
</div>
</div>
<div class="o_kanban_record_bottom">
</div>
<div class="o_kanban_record_bottom">
</div>
</div>
</div>
Expand Down
6 changes: 5 additions & 1 deletion iot_output_oca/models/iot_device_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class IoTDevice(models.Model):
_description = "IoT Device"

name = fields.Char(required=True)
device_id = fields.Many2one("iot.device", required=True, readonly=True)
device_id = fields.Many2one(
"iot.device", required=True, readonly=True, auto_join=True
)
system_id = fields.Many2one("iot.system", required=True)
action_ids = fields.One2many("iot.device.output.action", inverse_name="output_id")
state = fields.Selection([], readonly=True)
Expand All @@ -32,6 +34,8 @@ def device_run_action(self):
self.env.context.get("iot_system_action_id")
)
for rec in self:
if not self.device_id.active:
continue
action = self.env["iot.device.output.action"].create(
rec._system_action_vals(system_action)
)
Expand Down
9 changes: 9 additions & 0 deletions iot_output_oca/tests/test_iot.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ def test_action(self):
self.assertEqual(self.output.action_count, 1)
self.assertEqual(self.output.action_ids.status, "failed")

def test_action_archived_device(self):
self.assertEqual(self.output.action_count, 0)
self.device.active = False
with mute_logger("odoo.addons.iot_oca.models.iot_system_action"):
self.output.with_context(
iot_system_action_id=self.action.id
).device_run_action()
self.assertEqual(self.output.action_count, 0)

def test_correct_action(self):
self.assertEqual(self.output.action_count, 0)
with patch(
Expand Down
2 changes: 1 addition & 1 deletion iot_template_oca/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": "Creu Blanca, Odoo Community Association (OCA)",
"license": "AGPL-3",
"installable": True,
"summary": "IoT base module",
"summary": "IoT module for managing templates",
"depends": ["iot_input_oca"],
"website": "https://github.com/OCA/iot",
"data": [
Expand Down
7 changes: 4 additions & 3 deletions iot_template_oca/controller/iot_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ class CallIot(http.Controller):
methods=["POST"],
csrf=False,
)
def configure_iot(self, serial, *args, **kwargs):
def configure_iot(self, serial, template, **kwargs):
request = http.request
template = kwargs.get("template", False)
if not request.env:
return json.dumps(False)
return json.dumps(
request.env["iot.device.configure"].sudo().configure(serial, template)
request.env["iot.device.configure"]
.sudo()
.configure(serial, template, **kwargs)
)
8 changes: 8 additions & 0 deletions iot_template_oca/models/iot_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

class IotTemplate(models.Model):
_name = "iot.template"
_inherit = "image.mixin"
_description = "IoT Template for Device"
_parent_name = "parent_id"
_parent_store = True
Expand All @@ -36,6 +37,9 @@ class IotTemplate(models.Model):
parent_id = fields.Many2one("iot.template", ondelete="restrict")
tag_ids = fields.Many2many("iot.device.tag")
group_id = fields.Many2one("iot.device.group")
icon = fields.Selection(
selection=lambda r: r.env["iot.device"]._fields["icon"].selection
)

def _get_keys(self, serial):
if self.parent_id:
Expand All @@ -52,6 +56,10 @@ def apply_template(self, device, keys):
new_vals["group_id"] = self.group_id.id
if self.tag_ids:
new_vals["tag_ids"] = [(4, tag_id) for tag_id in self.tag_ids.ids]
if self.image_1920 and not device.image_1920:
new_vals["image_1920"] = self.image_1920
if self.icon and not device.icon:
new_vals["icon"] = self.icon
if new_vals:
device.write(new_vals)
for element in self.input_ids:
Expand Down
7 changes: 7 additions & 0 deletions iot_template_oca/views/iot_template_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@
<header />
<sheet>
<div class="oe_button_box" name="button_box" />
<field
name="image_1920"
widget='image'
class="oe_avatar"
options='{"preview_image": "image_128"}'
/>
<div class="oe_title">
<h1>
<field name="name" />
</h1>
</div>
<group>
<field name="group_id" />
<field name="icon" />
<field
name="tag_ids"
widget="many2many_tags"
Expand Down
4 changes: 2 additions & 2 deletions iot_template_oca/wizards/iot_device_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def _compute_url(self):
)

@api.model
def configure(self, serial, template_id):
def configure(self, serial, template_id, ip=False, **kwargs):
config = self.search([("serial", "=", serial)])
if not config:
return {}
config.unlink()
device = self.env["iot.device"].create({"name": serial})
device = self.env["iot.device"].create({"name": serial, "ip": ip})
template = self.env["iot.template"].search([("name", "=", template_id)])
if template:
template.apply_template(device, template._get_keys(serial))
Expand Down

0 comments on commit 33c44ff

Please sign in to comment.