[ADD] sync_google_calendar: Added a module to import events from google calendar.

bzr revid: uco@tinyerp.com-20110307134215-f79hz1jgi3cre2cr
This commit is contained in:
Ujjvala Collins (OpenERP) 2011-03-07 19:12:15 +05:30
parent d00bdceae2
commit 5edc07449d
6 changed files with 292 additions and 3 deletions

View File

@ -36,10 +36,12 @@ class google_login(osv.osv_memory):
def google_login(self,cr,uid,user,password,type='',context=None):
gd_client=False
if type=='group':
gd_client=gdata.contacts.client.ContactsClient(source='OpenERP')
if type=='contact' :
if type == 'group':
gd_client = gdata.contacts.client.ContactsClient(source='OpenERP')
if type == 'contact' :
gd_client = gdata.contacts.service.ContactsService()
if type == 'calendar':
gd_client = gdata.calendar.service.CalendarService()
try:
gd_client.ClientLogin(user, password,gd_client.source)
except Exception, e:

View File

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# 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 for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import wizard
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# 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 for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
{
'name': 'Google Calendar',
'version': '1.0',
'category': 'Generic Modules/Others',
'description': """The module adds google calendar events to meetings.""",
'author': 'OpenERP SA',
'website': 'http://www.openerp.com',
'depends': ['base','google_base_account', 'crm'],
'init_xml': [],
'update_xml': ['wizard/wizard_import_calendar_events_view.xml'],
'demo_xml': [],
'installable': True,
'active': False,
'certificate': '',
}
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# 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 for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import wizard_import_calendar_events
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,155 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# 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 for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import fields,osv
from tools.translate import _
import tools
import time
import datetime
import dateutil
from dateutil.tz import *
from dateutil.parser import *
try:
import gdata
import gdata.calendar.service
import gdata.calendar
except ImportError:
raise osv.except_osv(_('Google Contacts Import Error!'), _('Please install gdata-python-client from http://code.google.com/p/gdata-python-client/downloads/list'))
def _get_tinydates(self, stime, etime):
stime = dateutil.parser.parse(stime)
etime = dateutil.parser.parse(etime)
try:
au_dt = au_tz.normalize(stime.astimezone(au_tz))
timestring = datetime.datetime(*au_dt.timetuple()[:6]).strftime('%Y-%m-%d %H:%M:%S')
au_dt = au_tz.normalize(etime.astimezone(au_tz))
timestring_end = datetime.datetime(*au_dt.timetuple()[:6]).strftime('%Y-%m-%d %H:%M:%S')
except:
timestring = datetime.datetime(*stime.timetuple()[:6]).strftime('%Y-%m-%d %H:%M:%S')
timestring_end = datetime.datetime(*etime.timetuple()[:6]).strftime('%Y-%m-%d %H:%M:%S')
return (timestring, timestring_end)
class google_login(osv.osv_memory):
_inherit = 'google.login'
_name = 'google.login'
def _get_next_action(self, cr, uid, context=None):
data_obj = self.pool.get('ir.model.data')
data_id = data_obj._get_id(cr, uid, 'sync_google_calendar', 'view_synchronize_google_calendar_import_form')
view_id = False
if data_id:
view_id = data_obj.browse(cr, uid, data_id, context=context).res_id
value = {
'name': _('Import Events'),
'view_type': 'form',
'view_mode': 'form,tree',
'res_model': 'synchronize.google.calendar',
'view_id': False,
'context': context,
'views': [(view_id, 'form')],
'type': 'ir.actions.act_window',
'target': 'new',
}
return value
google_login()
class synchronize_google_calendar_events(osv.osv_memory):
_name = 'synchronize.google.calendar'
def _get_calendars(self, cr, uid, context=None):
user_obj = self.pool.get('res.users').browse(cr, uid, uid)
google = self.pool.get('google.login')
res = []
gd_client = google.google_login(cr, uid, user_obj.gmail_user, user_obj.gmail_password, type='calendar')
calendars = gd_client.GetAllCalendarsFeed()
for cal in calendars.entry:
res.append((cal.id.text, cal.title.text))
res.append(('all','All Calendars'))
return res
_columns = {
'calendar_name': fields.selection(_get_calendars, "Calendar Name", size=32),
}
_defaults = {
'calendar_name': 'all',
}
def import_calendar_events(self, cr, uid, ids, context=None):
obj = self.browse(cr, uid, ids, context=context)[0]
if not ids:
return { 'type': 'ir.actions.act_window_close' }
user_obj = self.pool.get('res.users').browse(cr, uid, uid)
meeting_obj = self.pool.get('crm.meeting')
model_obj = self.pool.get('ir.model.data')
gmail_user = user_obj.gmail_user
gamil_pwd = user_obj.gmail_password
google = self.pool.get('google.login')
gd_client = google.google_login(cr, uid, gmail_user, gamil_pwd, type='calendar')
if not gmail_user or not gamil_pwd:
raise osv.except_osv(_('Error'), _("Please specify the user and password !"))
# TODO: Get events from selected calendar only.
event_feed = gd_client.GetCalendarEventFeed()
meeting_ids = []
for feed in event_feed.entry:
google_id = feed.id.text
model_data = {
'name': google_id,
'model': 'crm.meeting',
'module': 'sync_google_calendar',
}
vals = {
'name': feed.title.text,
'description': feed.summary
}
timestring, timestring_end = _get_tinydates(self, feed.when[0].start_time, feed.when[0].end_time)
vals.update({'date': timestring, 'date_deadline': timestring_end})
data_ids = model_obj.search(cr, uid, [('model','=','crm.meeting'), ('name','=',google_id)])
if data_ids:
meeting_ids.append(model_obj.browse(cr, uid, data_ids[0], context=context).res_id)
else:
res_id = meeting_obj.create(cr, uid, vals)
meeting_ids.append(res_id)
model_data.update({'res_id': res_id})
model_obj.create(cr, uid, model_data, context=context)
return {
'name': _('Meetings'),
'domain': "[('id','in', ["+','.join(map(str,meeting_ids))+"])]",
'view_type': 'form',
'view_mode': 'tree,form',
'res_model': 'crm.meeting',
'context': context,
'views': [(False, 'tree'),(False, 'form')],
'type': 'ir.actions.act_window',
}
synchronize_google_calendar_events()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,45 @@
<?xml version="1.0"?>
<openerp>
<data>
<record model="ir.ui.view" id="view_synchronize_google_calendar_import_form">
<field name="name">synchronize.google.calendar.form</field>
<field name="model">synchronize.google.calendar</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Import Google Calendar Events">
<group colspan="4" col="4">
<field name="calendar_name" />
</group>
<separator string="" colspan="4"/>
<group colspan="4" col="4">
<group colspan="2" col="2"/>
<group colspan="2" col="2">
<button special="cancel" string="_Cancel" icon="gtk-cancel"/>
<button name="import_calendar_events" string="_Import Calendar" type="object" icon="terp-personal+"/>
</group>
</group>
</form>
</field>
</record>
<!--
Login Action
-->
<record model="ir.actions.act_window" id="act_google_login_form">
<field name="name">Import google calendar events</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">google.login</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
<field name="view_id" ref="google_base_account.view_google_login_form" />
</record>
<menuitem id="menu_sync_contact"
parent="crm.menu_meeting_sale"
action="act_google_login_form"
sequence="20" />
</data>
</openerp>