From 55a14c38742c20c42e2a19835f4cf2e53ef94d9f Mon Sep 17 00:00:00 2001 From: sabr Date: Thu, 23 Nov 2023 11:17:14 +0530 Subject: [PATCH] [FIX] hr_attendance: generated unique kiosk key for every company This issue occurs when a customer creates multiple companies. Then, after installing the attendance module, after selecting the kiosk mode, an error is generated. This is because, before installing the attendance module, if the customer created a company, a single kiosk key is generated. This key is different for each company, but it is the same. step to reproduce- - First created two company. - Install the `Attendances` & Open it. - Click on the Kiosk Mode from Menu. - Error will be generated. Error-ValueError Expected singleton: res.company(2, 1, 4, 3) after this commit,Each attendance kiosk key generated during the company's creation is unique. sentry-4617967282 closes odoo/odoo#145382 Signed-off-by: Sofie Gvaladze (sgv) --- addons/hr_attendance/models/res_company.py | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/addons/hr_attendance/models/res_company.py b/addons/hr_attendance/models/res_company.py index 6e0290010c4..74cdb652995 100644 --- a/addons/hr_attendance/models/res_company.py +++ b/addons/hr_attendance/models/res_company.py @@ -10,6 +10,9 @@ from werkzeug.urls import url_join class ResCompany(models.Model): _inherit = 'res.company' + def _default_company_token(self): + return str(uuid.uuid4()) + hr_attendance_overtime = fields.Boolean(string="Count Extra Hours") overtime_start_date = fields.Date(string="Extra Hours Starting Date") overtime_company_threshold = fields.Integer(string="Tolerance Time In Favor Of Company", default=0) @@ -36,6 +39,29 @@ class ResCompany(models.Model): for company in self: company.attendance_kiosk_url = url_join(company.get_base_url(), '/hr_attendance/%s' % company.attendance_kiosk_key) + # --------------------------------------------------------- + # ORM Overrides + # --------------------------------------------------------- + def _init_column(self, column_name): + """ Initialize the value of the given column for existing rows. + Overridden here because we need to generate different access tokens + and by default _init_column calls the default method once and applies + it for every record. + """ + if column_name != 'attendance_kiosk_key': + super(ResCompany, self)._init_column(column_name) + else: + self.env.cr.execute("SELECT id FROM %s WHERE attendance_kiosk_key IS NULL" % self._table) + attendance_ids = self.env.cr.dictfetchall() + values_args = [(attendance_id['id'], self._default_company_token()) for attendance_id in attendance_ids] + query = """ + UPDATE {table} + SET attendance_kiosk_key = vals.token + FROM (VALUES %s) AS vals(id, token) + WHERE {table}.id = vals.id + """.format(table=self._table) + self.env.cr.execute_values(query, values_args) + def write(self, vals): search_domain = False # Overtime to generate delete_domain = False # Overtime to delete