# -*- coding: utf-8 -*- ############################################################################### # # Cybrosys Technologies Pvt. Ltd. # # Copyright (C) 2023-TODAY Cybrosys Technologies(). # Author: Aslam A K( odoo@cybrosys.com ) # # You can modify it under the terms of the GNU AFFERO # GENERAL PUBLIC LICENSE (AGPL v3), Version 3. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU AFFERO GENERAL PUBLIC LICENSE (AGPL v3) for more details. # # You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE # (AGPL v3) along with this program. # If not, see . # ############################################################################### import dropbox from odoo import fields, models, _ from odoo.exceptions import ValidationError class ResConfigSettings(models.TransientModel): """ Model to configure the Dropbox api credentials.""" _inherit = 'res.config.settings' dropbox_client = fields.Char(string='Dropbox Client ID', copy=False, config_parameter='dropbox_integration.' 'client_id', help="Dropbox Client ID from dropbox api " "credentials", required=True) dropbox_client_secret = fields.Char(string='Dropbox Client Secret', copy=False, config_parameter='dropbox_integration.' 'client_secret', help="Dropbox Client Secret from " "dropbox api credentials", required=True) dropbox_access_token = fields.Char(string='Dropbox Access Token', help="Dropbox Access Token generated by" " dropbox api credentials") dropbox_refresh_token = fields.Char(string='Dropbox Refresh Token', help="Dropbox Refresh Token by dropbox" " api credentials") dropbox_token_validity = fields.Datetime(string='Dropbox Token Validity', help="Dropbox Token expiry date") dropbox_folder_id = fields.Char(string='Folder ID', config_parameter='dropbox_integration.' 'folder_id', help="Dropbox Folder ID from where the " "files to be imported and uploaded") is_dropbox_integration = fields.Boolean(string='Dropbox Cloud Storage', config_parameter= 'dropbox_integration.' 'is_dropbox_integration', default=False, help="Dropbox Integration " "button to set " "credentials") def action_get_dropbox_auth_code(self): """ Function to open a wizard to set up dropbox Authorization code """ return { 'type': 'ir.actions.act_window', 'name': 'Dropbox Authorization Code', 'res_model': 'authentication.code', 'view_mode': 'form', 'target': 'new', 'context': {'dropbox_auth': True} } def get_dropbox_auth_url(self): """ Return dropbox authorization url. """ dbx_auth = dropbox.oauth.DropboxOAuth2FlowNoRedirect( self.env['ir.config_parameter'].get_param( 'dropbox_integration.client_id'), self.env['ir.config_parameter'].get_param( 'dropbox_integration.client_secret'), token_access_type='offline') return dbx_auth.start() def set_dropbox_refresh_token(self, auth_code): """ Generate and set the dropbox refresh token from authorization code. """ try: client_id = self.env['ir.config_parameter'].get_param( 'dropbox_integration.client_id') client_secret = self.env['ir.config_parameter'].get_param( 'dropbox_integration.client_secret') dbx_auth = dropbox.oauth.DropboxOAuth2FlowNoRedirect( client_id, client_secret, token_access_type='offline') outh_result = dbx_auth.finish(auth_code) self.env['dropbox.dashboard'].create({ 'dropbox_client': client_id, 'dropbox_client_secret': client_secret, 'dropbox_refresh_token': outh_result.refresh_token, 'dropbox_access_token': outh_result.access_token, }) except Exception as error: raise ValidationError( _(f"Failed to Connect with Dropbox ( {error}.)'"))