Generate Google calendar event from odoo

blog-banner

Generate Google calendar event from odoo

Introduction

We can generate google calendar event programmatically from odoo. The advantage is that you don’t have to synchronize your odoo calendar with google calendar manually and you can see and manage meetings from both Google and Odoo calendars.

This patch code will help you to generate all types of events from odoo.

Following packages are required to install for generating google calendar:

  • pip install httplib2
  • pip install google-api-python-client
  • pip install --upgrade oauth2client

Using the Google Calendar API

You can take reference from the following link to generate OAuth2 credentials:
https://developers.google.com/adwords/api/sunset

Use above generated app credentials in ‘GoogleCredentials’ method.

Following are parameters for an event:

  • summary: string, Title of the event
  • start.dateTime, end.dateTime: datetime, The time, as a combined date-time value
  • start.timezone, end.timezone: string, The time zone in which the time is specified
  • attendees: list, The attendees of the event
  • reminders.useDefault: boolean, Whether the default reminders of the calendar refer to the event
  • reminder.overrides: the list, If the event doesn't use the default reminders, this lists the reminders specific to the event, or, if not set, indicates that no reminders are set for this event

To create an event, call the events.insert() method providing at least these parameters:

  • calendarId: Either be the email address or ‘primary’ which will use the primary calendar of the logged in user.
  • event: The event to create with all the necessary details such as start and end.
  • sendNotifications: To ask the Calendar API for sending email notifications to the guests.

Note: If an event is timed then it is better to use start.dateTime and end.dateTime fields. If an event is all-day then it is better to use start.date and end.date fields.

Use the insert() method of the events() service to add the event.

To confirm that the calendar event was created successfully. We can do that by checking the return value, it should be an Event object with all the details we passed.

Now, if you want to check the event was created, one of the fields that's created is a link to the calendar event. You can just use

  • event['htmlLink']

CONCLUSION

The following examples demonstrate creating an event and setting its metadata:

    import httplib2
    from apiclient import discovery
    from oauth2client import client
    
    credentials = client.GoogleCredentials(access_token,
                cal_client_id,
                cal_client_secret,
                refresh_token,
                update_token_validity,
                "https://accounts.google.com/o/oauth2/token",
                "Google Calendar API Python Quickstart")
    
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('calendar', 'v3', http=http)
    event = {}
    event['summary'] =  record.name
    event['start'] =  {
    'dateTime': update_start_date,
    'timeZone': 'Asia/Kolkata',
    }
    event['end'] =  {
    'dateTime': update_end_date,
    'timeZone': 'Asia/Kolkata',
    }
    event['attendees'] =   [
    {'email': '[email protected]'},
    ]
    event['reminders'] =  {
    'useDefault': False,
    'overrides': [
     {'method': 'email', 'minutes': 1},
     {'method': 'popup', 'minutes': 10},
     ],
    }
    event = service.events().insert(calendarId='primary', body=event,
    sendNotifications=True).execute()
    print('''*** %r event added:
    Start: %s
    End:   %s''' % (event['summary'].encode('utf-8'), event['start']['dateTime'], 
    event['end']['dateTime']))
    
  • Here is the sample of python code which will help you to generate and send google calendar.
    def send_calendar_invite(self):
    
        ir_attachment = self.env['ir.attachment']
        Mail = self.env['mail.mail']
        
        file_data = "BEGIN:VCALENDAR\n"
        file_data += "PRODID:-//Google Inc//Google Calendar 70.9054//EN\n"
        file_data += "VERSION:2.0\n"
        file_data += "CALSCALE:GREGORIAN\n"
        file_data += "METHOD:REQUEST\n"
        file_data += "BEGIN:VEVENT\n"
        file_data += "SUMMARY:Peg Legge On Leave\n"
        file_data += "DTSTART;TZID=Asia/Kolkata:’2019-01-24’\n"
        file_data += "DTEND;TZID=Asia/Kolkata:’2019-01-24’\n"
        file_data += "TRANSP:TRANSPARENT\n"
        file_data += "END:VEVENT\n"
        file_data += "END:VCALENDAR\n"
      
        attachment_ids = []
        ir_attachment_vals = {}
        mail_values = {}
    
        ir_attachment_vals['type'] = 'binary'
        ir_attachment_vals['mimetype'] = 'text/calendar'
        ir_attachment_vals['name'] = 'invite-calendar'
        ir_attachment_vals['datas_fname'] = 'invite_cal.ics'
        ir_attachment_vals['datas'] = base64.encodestring(file_data)
    
        attachment_id = ir_attachment.create(ir_attachment_vals)
        attachment_ids.append(attachment_id)
    
        mail_values['email_from'] = “[email protected]”
        mail_values['email_to'] = “[email protected]”
        mail_values['subject'] = “Invitation: Peg Legge On Leave @ Thu Jan 24, 2019 
        9:30am - 7:30pm”
        mail_values['message_type'] = 'email'
        mail_values['auto_delete'] = True
        mail_values['model'] = 'st.google.calendar'
        mail_values['attachment_ids'] = [(6, 0, attachment_ids)]
        today = datetime.datetime.now()
    
        if today.date() <= end_date.date() and record.late_coming != True:
             Mail.create(mail_values)
    
        Mail.process_email_queue()
Contact us

For Your Business Requirements

Text to Identify Refresh CAPTCHA
Background Image Close Button

2 - 4 October 2024

Hall: 10, Booth: #B8 Brussels, Belgium