diff --git a/iot_ras_oca/README.rst b/iot_ras_oca/README.rst new file mode 100644 index 000000000..e27455951 --- /dev/null +++ b/iot_ras_oca/README.rst @@ -0,0 +1,73 @@ +======== +IoT Base +======== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fiot-lightgray.png?logo=github + :target: https://github.com/OCA/iot/tree/11.0/iot + :alt: OCA/iot +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/iot-11-0/iot-11-0-iot + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/269/11.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This addon is a base module used for all iot modules. + +**Table of contents** + +.. contents:: + :local: + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Creu Blanca + +Contributors +~~~~~~~~~~~~ + +* Enric Tobella + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/iot `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/iot_ras_oca/__init__.py b/iot_ras_oca/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/iot_ras_oca/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/iot_ras_oca/__manifest__.py b/iot_ras_oca/__manifest__.py new file mode 100644 index 000000000..b41c253b4 --- /dev/null +++ b/iot_ras_oca/__manifest__.py @@ -0,0 +1,14 @@ +# Copyright (C) 2018 Creu Blanca +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +{ + "name": "IoT Templates", + "version": "16.0.1.0.0", + "category": "IoT", + "author": "CreuBlanca", + "license": "AGPL-3", + "installable": True, + "summary": "IoT base module", + "depends": ["iot_template_oca", "hr_attendance_rfid"], + "data": ["data/ras_template.xml"], + "website": "https://github.com/tegin/cb-addons", +} diff --git a/iot_ras_oca/data/ras_template.xml b/iot_ras_oca/data/ras_template.xml new file mode 100644 index 000000000..94df528a0 --- /dev/null +++ b/iot_ras_oca/data/ras_template.xml @@ -0,0 +1,14 @@ + + + + eficent.ras + + + + rfid_read + + iot_ras_default_action + {} + + + diff --git a/iot_ras_oca/models/__init__.py b/iot_ras_oca/models/__init__.py new file mode 100644 index 000000000..8a6d1c15f --- /dev/null +++ b/iot_ras_oca/models/__init__.py @@ -0,0 +1,2 @@ +from . import hr_employee +from . import iot_device_input diff --git a/iot_ras_oca/models/hr_employee.py b/iot_ras_oca/models/hr_employee.py new file mode 100644 index 000000000..0a62a9906 --- /dev/null +++ b/iot_ras_oca/models/hr_employee.py @@ -0,0 +1,25 @@ +# Copyright 2017 Comunitea Servicios Tecnológicos S.L. +# Copyright 2018 Eficent Business and IT Consulting Services, S.L. +# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html + +import logging + +from odoo import _, api, models + +_logger = logging.getLogger(__name__) + + +class HrEmployee(models.Model): + _inherit = "hr.employee" + + @api.model + def register_attendance(self, card_code): + res = super().register_attendance(card_code) + if "action" in res: + if res["action"] == "check_in": + res["action_msg"] = _("Checked in %s") % res["employee_name"] + elif res["action"] == "check_out": + res["action_msg"] = _("Checked out %s") % res["employee_name"] + elif res["action"] == "FALSE": + res["action_msg"] = _("Contact your admin") + return res diff --git a/iot_ras_oca/models/iot_device_input.py b/iot_ras_oca/models/iot_device_input.py new file mode 100644 index 000000000..31c341156 --- /dev/null +++ b/iot_ras_oca/models/iot_device_input.py @@ -0,0 +1,9 @@ +from odoo import api, models + + +class IotDeviceInput(models.Model): + _inherit = "iot.device.input" + + @api.model + def iot_ras_default_action(self, message): + return {"action_msg": message, "action": "check_in"} diff --git a/iot_ras_oca/tests/__init__.py b/iot_ras_oca/tests/__init__.py new file mode 100644 index 000000000..5e2aa21fe --- /dev/null +++ b/iot_ras_oca/tests/__init__.py @@ -0,0 +1 @@ +from . import test_ras diff --git a/iot_ras_oca/tests/test_ras.py b/iot_ras_oca/tests/test_ras.py new file mode 100644 index 000000000..9531fa553 --- /dev/null +++ b/iot_ras_oca/tests/test_ras.py @@ -0,0 +1,84 @@ +from odoo.tests.common import HttpCase + + +class TestRas(HttpCase): + def setUp(self): + super().setUp() + self.card_code = "e313441e" + self.employee = self.env["hr.employee"].create( + {"name": "Anita", "rfid_card_code": self.card_code} + ) + self.template = self.env.ref("iot_ras_oca.ras_template") + + def _get_wizard(self): + wizard = self.env["iot.device.configure"].create({}) + self.assertTrue(wizard.serial) + self.assertTrue(wizard.url) + return wizard + + def test_generation(self): + wizard = self._get_wizard() + device_config = self.url_open( + wizard.url, data={"template": self.template.name} + ).json() + device = self.env["iot.device"].search([("name", "=", device_config["name"])]) + self.assertTrue(device) + ras_input = device.input_ids + self.assertTrue(ras_input) + self.assertTrue(ras_input.serial) + self.assertTrue(ras_input.passphrase) + res = self.url_open( + "/iot/%s/action" % ras_input.serial, + data={"passphrase": ras_input.passphrase, "value": "123"}, + ).json() + # This should work properly because it has not been assigned to + # Checking attendance + self.assertEqual(res["status"], "ok") + + ras_input.write( + { + "call_model_id": self.env.ref("hr.model_hr_employee").id, + "call_function": "register_attendance", + } + ) # We are assigning it to the proper function and model + + res = self.url_open( + "/iot/%s/action" % ras_input.serial, + data={"passphrase": ras_input.passphrase, "value": "123"}, + ).json() + # Now it must fail as the value is not correct + self.assertEqual(res["status"], "ok") + self.assertIn("error_message", res) + self.assertRegex(res["error_message"], ".*No employee found.*") + self.assertFalse( + self.env["hr.attendance"].search([("employee_id", "=", self.employee.id)]) + ) + res = self.url_open( + "/iot/%s/action" % ras_input.serial, + data={"passphrase": ras_input.passphrase, "value": self.card_code}, + ).json() + # The employee has checked in + self.assertEqual(res["status"], "ok") + self.assertEqual(res["action"], "check_in") + self.assertEqual(res["error_message"], "") + attendance = self.env["hr.attendance"].search( + [("employee_id", "=", self.employee.id)] + ) + self.assertTrue(attendance) + self.assertEqual(1, len(attendance)) + self.assertFalse(attendance.check_out) + res = self.url_open( + "/iot/%s/action" % ras_input.serial, + data={"passphrase": ras_input.passphrase, "value": self.card_code}, + ).json() + # The employee has checked out + self.assertEqual(res["status"], "ok") + self.assertEqual(res["action"], "check_out") + self.assertEqual(res["error_message"], "") + attendance = self.env["hr.attendance"].search( + [("employee_id", "=", self.employee.id)] + ) + attendance.invalidate_recordset() + self.assertTrue(attendance) + self.assertEqual(1, len(attendance)) + self.assertTrue(attendance.check_out) diff --git a/setup/iot_ras_oca/odoo/addons/iot_ras_oca b/setup/iot_ras_oca/odoo/addons/iot_ras_oca new file mode 120000 index 000000000..f230c6fdf --- /dev/null +++ b/setup/iot_ras_oca/odoo/addons/iot_ras_oca @@ -0,0 +1 @@ +../../../../iot_ras_oca \ No newline at end of file diff --git a/setup/iot_ras_oca/setup.py b/setup/iot_ras_oca/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/iot_ras_oca/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)