|
|
|
|
|
|
|
from odoo import models, fields, api
|
|
from odoo.exceptions import UserError, ValidationError,Warning
|
|
from psycopg2 import sql, DatabaseError
|
|
|
|
from werkzeug import utils
|
|
|
|
|
|
import os.path
|
|
from google.oauth2 import service_account
|
|
from googleapiclient.discovery import build
|
|
from datetime import datetime, timedelta
|
|
import json
|
|
|
|
# IF YOU MODIFY THE SCOPE DELETE THE TOKEN.TXT FILE
|
|
SCOPES = ['https://www.googleapis.com/auth/calendar']
|
|
SERVICE_ACCOUNT_FILE = '/usr/lib/python3/dist-packages/odoo/kalachakra_module/kalachakra/models/creds.json' # You should make it an environment variable
|
|
SUBJECT = 'kalachakra-srv@kalachakra-351613.iam.gserviceaccount.com'
|
|
|
|
|
|
class EventTagCategory(models.Model):
|
|
_inherit = "event.tag.category"
|
|
_description = 'Event tag category'
|
|
|
|
calendar_id=fields.Char('google calendar id')
|
|
|
|
class EventEvent(models.Model):
|
|
_inherit = "event.event"
|
|
_description = 'Event'
|
|
|
|
calendar_id=fields.Char('calendar id')
|
|
calendar_event_id=fields.Char('event id')
|
|
free_participation=fields.Boolean('Free participation')
|
|
participation_product_id=fields.Many2one('product.product',string='participation product')
|
|
participation_standard_price=fields.Monetary('Standard Price',currency_field='currency_id')
|
|
participation_member_price=fields.Monetary('Member price',currency_field='currency_id')
|
|
participation_super_member_price=fields.Monetary('Super member price',currency_field='currency_id')
|
|
subscription_product_id=fields.Many2one('product.product',string='subscription product')
|
|
subscription_standard_price=fields.Monetary('Standard Price',currency_field='currency_id')
|
|
subscription_member_price=fields.Monetary('Member price',currency_field='currency_id')
|
|
subscription_super_member_price=fields.Monetary('Super member price',currency_field='currency_id')
|
|
|
|
recurring_event=fields.Boolean('Recurring event')
|
|
recurring_event_newsletter_id=fields.Many2one('mailing.list',string='Recurring event Newsletter')
|
|
online_event=fields.Boolean('Online event')
|
|
online_link=fields.Char('link')
|
|
online_id=fields.Char('id')
|
|
online_password=fields.Char('password')
|
|
|
|
@api.model
|
|
def _default_currency(self):
|
|
company = self.env['res.company']._company_default_get(
|
|
'event.event')
|
|
return company.currency_id
|
|
|
|
currency_id = fields.Many2one(
|
|
'res.currency',
|
|
string='Currency',
|
|
required=True,
|
|
track_visibility='onchange',
|
|
ondelete='restrict',
|
|
default=_default_currency
|
|
)
|
|
|
|
|
|
def add_event_to_google_agenda(self):
|
|
|
|
|
|
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
|
|
delegated_credentials = credentials.with_subject(SUBJECT)
|
|
service = build('calendar', 'v3', credentials=delegated_credentials)
|
|
|
|
d = datetime.utcnow().date()
|
|
tomorrow = datetime(d.year, d.month, d.day, 10)+timedelta(days=1)
|
|
start = tomorrow.isoformat()
|
|
end = (tomorrow + timedelta(hours=1)).isoformat()
|
|
|
|
start=self.date_begin.isoformat()
|
|
end=self.date_end.isoformat()
|
|
diff_date=self.date_end-self.date_begin
|
|
if diff_date.days>0:
|
|
date_param=self.date_begin.strftime('%Y-%m-%d')+'-'+self.date_end.strftime('%Y-%m-%d')+'-'+str(self.id)
|
|
else:
|
|
date_param=self.date_begin.strftime('%Y-%m-%d')+'-'+str(self.id)
|
|
|
|
name_param=self.name.replace(' ','-')
|
|
name_param=name_param.replace('\'','-')
|
|
description='<a href="'+self.env['ir.config_parameter'].get_param('web.base.url')+'/event/'+name_param+'-'+date_param+'/register">Click here to register</a>'
|
|
#str(self.date_begin.year())+'-'+str(self.date_begin.month())+'-'+self.date_begin.day()
|
|
|
|
body={"summary": self.name,
|
|
"description": description,
|
|
"start": {"dateTime": start, "timeZone": 'Europe/Paris'},
|
|
"end": {"dateTime": end, "timeZone": 'Europe/Paris'},
|
|
}
|
|
|
|
#recherche de l'id calendar lié à l'étiquette de l'événément
|
|
calendar_id=self.tag_ids.category_id.calendar_id
|
|
if calendar_id:
|
|
event = service.events().insert(calendarId=calendar_id, body=body).execute()
|
|
|
|
self.calendar_event_id=event['id']
|
|
self.calendar_id=calendar_id
|
|
else:
|
|
raise Warning('no calendar id, please check configuration tags')
|
|
|
|
def remove_event_to_google_agenda(self):
|
|
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
|
|
delegated_credentials = credentials.with_subject(SUBJECT)
|
|
service = build('calendar', 'v3', credentials=delegated_credentials)
|
|
|
|
|
|
event = service.events().get(calendarId=self.calendar_id, eventId=self.calendar_event_id).execute()
|
|
#raise Warning(json.dumps(event, indent = 4) )
|
|
if event['status'] != "cancelled" :
|
|
service.events().delete(calendarId=self.calendar_id, eventId=self.calendar_event_id).execute()
|
|
|
|
self.calendar_event_id=False
|
|
self.calendar_id=False
|
|
|
|
|
|
|