diff --git a/odoo_slack_connector/README.rst b/odoo_slack_connector/README.rst index 0a162aa97..ff96b7119 100644 --- a/odoo_slack_connector/README.rst +++ b/odoo_slack_connector/README.rst @@ -3,7 +3,7 @@ :alt: License: LGPL-3 Slack Odoo Connector -========================= +==================== Allows to integrate with slack. Configuration @@ -25,12 +25,12 @@ Developer: Viswanth K v15 @ cybrosys, odoo@cybrosys.com Viswanth K v16 @ cybrosys, odoo@cybrosys.com Contacts -------- +-------- * Mail Contact : odoo@cybrosys.com * Website : https://cybrosys.com Bug Tracker -------- +----------- Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. Maintainer diff --git a/odoo_slack_connector/__manifest__.py b/odoo_slack_connector/__manifest__.py index dac180412..96ef4d26f 100644 --- a/odoo_slack_connector/__manifest__.py +++ b/odoo_slack_connector/__manifest__.py @@ -22,7 +22,7 @@ { 'name': "Slack Odoo Connector", - 'version': '16.0.1.0.0', + 'version': '16.0.1.0.2', 'summary': "Integrate slack with Odoo", 'description': "This module will help you to connect with slack to manage slack Conversations", 'category': 'Extra Tools', diff --git a/odoo_slack_connector/data/scheduled_action.xml b/odoo_slack_connector/data/scheduled_action.xml index 0b66e5df4..b20a0a38a 100644 --- a/odoo_slack_connector/data/scheduled_action.xml +++ b/odoo_slack_connector/data/scheduled_action.xml @@ -1,16 +1,15 @@ - + Auto Synchronization of slack and odoo code model.synchronization_slack() - 1 minutes -1 - \ No newline at end of file + diff --git a/odoo_slack_connector/doc/RELEASE_NOTES.md b/odoo_slack_connector/doc/RELEASE_NOTES.md index 1acd9b27e..313875251 100644 --- a/odoo_slack_connector/doc/RELEASE_NOTES.md +++ b/odoo_slack_connector/doc/RELEASE_NOTES.md @@ -8,4 +8,9 @@ #### 11.06.2023 #### Version 16.0.1.0.1 ##### FIX -- Bug Fix: Fixed the issue where slack members are not loading to odoo \ No newline at end of file +- Bug Fix: Fixed the issue where slack members are not loading to odoo + +#### 26.10.2023 +#### Version 16.0.1.0.2 +##### FIX +- Bug Fix: Resolved the issue causing a server error during the synchronization of Slack with Odoo. \ No newline at end of file diff --git a/odoo_slack_connector/models/mail_channel.py b/odoo_slack_connector/models/mail_channel.py index eae8d66fd..1d2cc7acb 100644 --- a/odoo_slack_connector/models/mail_channel.py +++ b/odoo_slack_connector/models/mail_channel.py @@ -31,9 +31,9 @@ class MailChannel(models.Model): _inherit = 'mail.channel' _description = 'Slack Channel' - is_slack = fields.Boolean("Slack", default=False) + is_slack = fields.Boolean(string="Slack", default=False) channel = fields.Char("ID") - msg_date = fields.Datetime() + msg_date = fields.Datetime(string="Message Date") def sync_members(self): """To load members into channels""" @@ -52,18 +52,22 @@ class MailChannel(models.Model): response = session.get(url, headers=headers, data=payload) channel_members = response.json() slack_user_list = [] - for slack_user_id in channel_members['members']: contact = self.env['res.partner'].search([('slack_user_id', '=', slack_user_id)]) - slack_user_list.append(contact.id) - + if contact: + slack_user_list.append(contact.id) + else: + contact = self.env['res.partner'].create({ + 'name': 'Slack User', + 'slack_user_id': slack_user_id + }) + slack_user_list.append(contact.id) current_channel_user_list = channel.channel_member_ids.mapped('partner_id.id') sample_list = [ (0, 0, {'partner_id': contact_id}) for contact_id in slack_user_list if contact_id not in current_channel_user_list ] - channel.channel_member_ids = sample_list except RequestException as e: diff --git a/odoo_slack_connector/models/mail_message.py b/odoo_slack_connector/models/mail_message.py index b73db1e4e..03eefafd0 100644 --- a/odoo_slack_connector/models/mail_message.py +++ b/odoo_slack_connector/models/mail_message.py @@ -19,7 +19,7 @@ # If not, see . # ############################################################################# -from odoo import fields, models, api +from odoo import api, fields, models import requests, re, json from datetime import datetime from dateutil import tz @@ -35,7 +35,7 @@ class MailMessage(models.Model): _description = 'Slack Message' is_slack = fields.Boolean(string="Slack", default=False) - channel = fields.Many2one('mail.channel') + channel = fields.Many2one('mail.channel', string="Channel") @api.model def create(self, vals): @@ -57,9 +57,12 @@ class MailMessage(models.Model): } new_text = remove_html(rec.body) url = "https://slack.com/api/chat.postMessage?channel=" + rec.record_name + "&" + "text=" + new_text - requests.request("POST", url, headers=headers, data=payload) + requests.request("POST", url, headers=headers, + data=payload) rec.is_slack = True - converted_time = datetime.strptime(channel_date,'%Y-%m-%d %H:%M:%S').astimezone(to_zone) + converted_time = datetime.strptime(channel_date, + '%Y-%m-%d %H:%M:%S').astimezone( + to_zone) channel.msg_date = converted_time.strftime('%Y-%m-%d %H:%M:%S') return res @@ -74,41 +77,45 @@ class MailMessage(models.Model): payload = {} headers = {'Authorization': 'Bearer ' + company_record.token} url = "https://slack.com/api/conversations.history?channel=" + channel_id.channel - channel_response = requests.request("GET", url, headers=headers, + channel_response = requests.request("GET", url, + headers=headers, data=payload) channels_response = channel_response.__dict__['_content'] dict_channels = channels_response.decode("UTF-8") channels_history = json.loads(dict_channels) - channels_history['messages'].reverse() - if not channel_id.msg_date: - converted_time = datetime.strptime(channel_date, - '%Y-%m-%d %H:%M:%S').astimezone( - to_zone) - channel_id.msg_date = converted_time.strftime( - '%Y-%m-%d %H:%M:%S') - else: - for i in channels_history['messages']: - if 'user' in i: - users = self.env['res.users'].search( - [('slack_user_id', '=', i['user'])]) - dt_object = ( - datetime.fromtimestamp(float(i['ts'])).strftime( - '%Y-%m-%d %H:%M:%S')) - date_time_obj = datetime.strptime(dt_object, - '%Y-%m-%d %H:%M:%S') - converted_time = date_time_obj.astimezone(to_zone) - if datetime.strptime(converted_time.strftime( - '%Y-%m-%d %H:%M:%S'), - '%Y-%m-%d %H:%M:%S') > channel_id.msg_date: - self.with_user(users.id).create({ - 'body': i['text'], - 'record_name': channel_id.name, - 'model': 'mail.channel', - 'is_slack': True, - 'res_id': channel_id.id, - }) - converted_time = datetime.strptime(channel_date, - '%Y-%m-%d %H:%M:%S').astimezone( - to_zone) - channel_id.msg_date = converted_time.strftime( - '%Y-%m-%d %H:%M:%S') + if channels_history['ok']: + channels_history['messages'].reverse() + if not channel_id.msg_date: + converted_time = datetime.strptime(channel_date, + '%Y-%m-%d %H:%M:%S').astimezone( + to_zone) + channel_id.msg_date = converted_time.strftime( + '%Y-%m-%d %H:%M:%S') + else: + for i in channels_history['messages']: + if 'user' in i: + users = self.env['res.users'].search( + [('slack_user_id', '=', i['user'])]) + dt_object = ( + datetime.fromtimestamp( + float(i['ts'])).strftime( + '%Y-%m-%d %H:%M:%S')) + date_time_obj = datetime.strptime(dt_object, + '%Y-%m-%d %H:%M:%S') + converted_time = date_time_obj.astimezone( + to_zone) + if datetime.strptime(converted_time.strftime( + '%Y-%m-%d %H:%M:%S'), + '%Y-%m-%d %H:%M:%S') > channel_id.msg_date: + self.with_user(users.id).create({ + 'body': i['text'], + 'record_name': channel_id.name, + 'model': 'mail.channel', + 'is_slack': True, + 'res_id': channel_id.id, + }) + converted_time = datetime.strptime(channel_date, + '%Y-%m-%d %H:%M:%S').astimezone( + to_zone) + channel_id.msg_date = converted_time.strftime( + '%Y-%m-%d %H:%M:%S') diff --git a/odoo_slack_connector/models/res_company.py b/odoo_slack_connector/models/res_company.py index 27af6f097..00afadb80 100644 --- a/odoo_slack_connector/models/res_company.py +++ b/odoo_slack_connector/models/res_company.py @@ -20,9 +20,13 @@ # ############################################################################# import json +import logging import requests +from requests import RequestException from odoo import fields, models +logger = logging.getLogger(__name__) + class ResCompany(models.Model): _inherit = 'res.company' @@ -48,7 +52,6 @@ class ResCompany(models.Model): headers = { 'Authorization': 'Bearer ' + self.token } - users_response = requests.request("GET", url1, headers=headers, data=payload) users_response = users_response.__dict__['_content'] @@ -61,7 +64,6 @@ class ResCompany(models.Model): channels_response = channel_response.__dict__['_content'] dict_channels = channels_response.decode("UTF-8") channels = json.loads(dict_channels) - search_channel = self.env['mail.channel'].search([]) for i in search_channel: channel_list.append(i.channel) @@ -74,7 +76,8 @@ class ResCompany(models.Model): 'is_slack': True, }, ]) search_channel.sync_members() - self.env['res.users'].search([]).sync_users() + users = self.env['res.users'].search([]) + users.sync_users() users_list = [] slack_id_list = [] channels_list = [] @@ -88,7 +91,6 @@ class ResCompany(models.Model): {'name': channel['name']} )) self.slack_channel_ids = channels_list - record_user_list = [] for rec in record.slack_users_ids: record_user_list.append(rec.user) @@ -105,7 +107,6 @@ class ResCompany(models.Model): 'email': email}, )) self.slack_users_ids = users_list - for partner_id in self.env['res.partner'].search([]): slack_id_list.append(partner_id.slack_user_id) if members is not False: diff --git a/odoo_slack_connector/models/res_users.py b/odoo_slack_connector/models/res_users.py index 3861754ab..5602208e0 100644 --- a/odoo_slack_connector/models/res_users.py +++ b/odoo_slack_connector/models/res_users.py @@ -23,6 +23,7 @@ import requests import logging from odoo import fields, models +from odoo.exceptions import UserError class ResPartner(models.Model): @@ -35,16 +36,13 @@ class ResPartner(models.Model): def sync_users(self): """To load slack users""" slack_internal_user_list = [user_id.slack_user_id for user_id in self] - url = "https://slack.com/api/users.list" company_record = self.env.user.company_id payload = {} headers = { 'Authorization': 'Bearer ' + company_record.token } - logger = logging.getLogger(__name__) - try: response = requests.get(url, headers=headers, data=payload) response.raise_for_status() @@ -69,7 +67,17 @@ class ResPartner(models.Model): vals_list.append(vals) try: if vals_list: - self.create(vals_list) + for val in vals_list: + existing_user = self.search([ + ('login', '=', val['login'])], limit=1) + if existing_user: + if existing_user.is_slack_internal_users: + existing_user.write( + {'slack_user_id': val['slack_user_id']}) + else: + raise UserError("User with email '%s' already exists as a Slack user." % val['login']) + else: + self.create(val) except Exception as e: # Handle creation exception logger.error(f"Error creating Slack users: {e}") diff --git a/odoo_slack_connector/security/ir.model.access.csv b/odoo_slack_connector/security/ir.model.access.csv index 38f65d222..80d4929ce 100644 --- a/odoo_slack_connector/security/ir.model.access.csv +++ b/odoo_slack_connector/security/ir.model.access.csv @@ -1,7 +1,3 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink access_res_company_slack_users,access.res.company.slack.users,model_res_company_slack_users,base.group_user,1,1,1,1 access_res_company_slack_channels,access.res.company.slack.channels,model_res_company_slack_channels,base.group_user,1,1,1,1 - - - - diff --git a/odoo_slack_connector/views/mail_channel.xml b/odoo_slack_connector/views/mail_channel.xml index db5debe3c..bd9ebc016 100644 --- a/odoo_slack_connector/views/mail_channel.xml +++ b/odoo_slack_connector/views/mail_channel.xml @@ -13,4 +13,4 @@ - \ No newline at end of file + diff --git a/odoo_slack_connector/views/mail_message.xml b/odoo_slack_connector/views/mail_message.xml index 08b14332d..e1ed700d5 100644 --- a/odoo_slack_connector/views/mail_message.xml +++ b/odoo_slack_connector/views/mail_message.xml @@ -12,4 +12,4 @@ - \ No newline at end of file + diff --git a/odoo_slack_connector/views/res_company.xml b/odoo_slack_connector/views/res_company.xml index 0c59e234f..d1e87b339 100644 --- a/odoo_slack_connector/views/res_company.xml +++ b/odoo_slack_connector/views/res_company.xml @@ -1,48 +1,46 @@ - - - res.company.for.inherit - res.company - - - - - + + res.company.inherit.odoo.slack.connector + res.company + + + + + + - - - - -