Enable Dark Mode!
By: Ajmunnisa

How to Upload Documents to Google Drive With a Button Click?


 A business or organization might require to generate multiple documents every day as part of its daily activities. Cloud storage, like Google Drive, helps in keeping documents safe. It is also possible to share the documents by simply sharing the google drive link.

This blog will help you to integrate the functionality of uploading documents like sale order reports, purchase order reports, etc., to google drive.

First of all, you have to configure the google drive credentials in Odoo settings to access google drive storage.

Steps to add functionality to upload documents to google drive are:

Step 1: Import required Libraries for using google drive functions.

from google.auth.transport.requests import Request
from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials
from googleapiclient.http import MediaFileUpload

Step 2: Inherit the ‘google.drive.config’

model and add upload_doc function

class GoogleDrive(models.Model):
   _inherit =
  upload_doc(self, res_id):
  google_drive_client_id =
  google_drive_client_secret =
google_drive_refresh_token =
  = self.get_access_token()
      info_token =
  "token": access_token,
  'refresh_token': google_drive_refresh_token,
  "client_id": google_drive_client_id,
  "client_secret": google_drive_client_secret,  
# check token and get right of google drive
       creds =Credentials.from_authorized_user_info(info_token, SCOPES)
       if not creds or not creds.valid:
         if creds and creds.expired and creds.refresh_token:   
# create sale order pdf
       so = self.env['sale.order'].browse(res_id)
template_id = so._find_mail_template()
       template = self.env['mail.template'].browse(template_id)
       res_ids = so.id
       if isinstance(so.ids, int):
           res_ids = [so.ids]
           values = template.generate_email(res_ids, ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc',  'reply_to', 'attachment_ids', 'mail_server_id'])
       if values and values.get('attachments'):
          attachments = values.get('attachments')
          ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 16))
          pdf_name = "%s.pdf" % ran_str with open(pdf_name, 'wb') as f:
          service = build('drive', 'v3', credentials=creds)
#name : name of file, parents : name of directory to which the documentis to uploaded
          file_metadata = {'name': pdf_name, "parents": ['1V1NM-1d-A-Q3UXdVOGUkHcj6gNGZQMga']}
           media = MediaFileUpload(f.name, )
           file_id = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
           post_data = {
             "role": "reader",
             "type": "anyone"
           headers = {
              'Content-type': 'application/json',
              'Authorization': 'Bearer %s' % access_token
           req2 = requests.post("https://www.googleapis.com/drive/v2/files/%s/permissions" % file_id.get('id'), headers=headers, data=json.dumps(post_data))
#if successfully uploaded the file, the link can be obtained
           if req2.status_code == 200:
               google_drive_pdf_link = "https://drive.google.com/file/d/%s/view" % file_id.get('id')
        return ""

In this function, the ‘sale order’ mail template is fetched to generate a sale order report document of the current sale order.  

Here the file_meta is a dictionary that contains two values, “name and “parents.” ‘Name’ is the name of the document, and add the folder id in google drive as the parent.

how-to-upload-documents-to-google-drive-with-a-button-click-cybrosysStep 3: Add an upload button in the form view and corresponding function.

Here, the function ‘action_upload_to_drive’ calls the upload_doc function defined in ‘google_drive_config’ model, which uploads the document

class SaleOrder(models.Model):
   _inherit = 'sale.order'
   sale_upload = fields.Boolean(default=True)
   def action_upload_to_drive(self):
       return True

If you need any assistance in odoo, we are online, please chat with us.


Leave a comment




Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635



Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.



Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message