1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
23 from osv import osv, fields
24 from osv.orm import except_orm
39 'last-modified':'normal' ,
41 'attendee':'multiple',
43 'categories': 'normal',
44 'description':'normal',
46 # TODO: handle the 'duration' property
49 class document_directory_ics_fields(osv.osv):
50 _name = 'document.directory.ics.fields'
52 'field_id': fields.many2one('ir.model.fields', 'Open ERP Field', required=True),
53 'name': fields.selection(map(lambda x: (x,x), ICS_TAGS.keys()),'ICS Value', required=True),
54 'content_id': fields.many2one('document.directory.content', 'Content', required=True, ondelete='cascade')
56 document_directory_ics_fields()
58 class document_directory_content(osv.osv):
59 _inherit = 'document.directory.content'
61 'ics_object_id': fields.many2one('ir.model', 'Object'),
62 'ics_domain': fields.char('Domain', size=64),
63 'ics_field_ids': fields.one2many('document.directory.ics.fields', 'content_id', 'Fields Mapping')
66 'ics_domain': lambda *args: '[]'
68 def process_write_ics(self, cr, uid, node, data, context={}):
70 parsedCal = vobject.readOne(data)
72 fobj = self.pool.get('document.directory.content')
73 content = fobj.browse(cr, uid, node.content.id, context)
76 for d in eval(content.ics_domain):
78 for n in content.ics_field_ids:
79 fields[n.name] = n.field_id.name
80 if 'uid' not in fields:
82 for child in parsedCal.getChildren():
85 for event in child.getChildren():
86 if event.name.lower()=='uid':
88 if event.name.lower() in fields:
89 if ICS_TAGS[event.name.lower()]=='normal':
90 result[fields[event.name.lower()]] = event.value.encode('utf8')
91 elif ICS_TAGS[event.name.lower()]=='date':
92 result[fields[event.name.lower()]] = event.value.strftime('%Y-%m-%d %H:%M:%S')
96 fobj = self.pool.get(content.ics_object_id.model)
97 id = fobj.search(cr, uid, [(fields['uid'], '=', uuid.encode('utf8'))], context=context)
99 fobj.write(cr, uid, id, result, context=context)
103 fobj.create(cr, uid, r, context=context)
107 def process_read_ics(self, cr, uid, node, context={}):
109 obj_class = self.pool.get(node.content.ics_object_id.model)
110 # Can be improved to use context and active_id !
111 domain = eval(node.content.ics_domain)
112 ids = obj_class.search(cr, uid, domain, context)
113 cal = vobject.iCalendar()
114 for obj in obj_class.browse(cr, uid, ids, context):
115 event = cal.add('vevent')
116 for field in node.content.ics_field_ids:
117 value = getattr(obj, field.field_id.name)
118 if (not value) and field.name=='uid':
119 value = 'OpenERP-'+str(random.randint(1999999999, 9999999999))
120 obj_class.write(cr, uid, [obj.id], {field.field_id.name: value})
121 if ICS_TAGS[field.name]=='normal':
122 if type(value)==type(obj):
125 event.add(field.name).value = value or ''
126 elif ICS_TAGS[field.name]=='date':
127 dt = value or time.strftime('%Y-%m-%d %H:%M:%S')
130 value = datetime.datetime.strptime(dt, '%Y-%m-%d %H:%M:%S')
131 event.add(field.name).value = value
132 s= StringIO.StringIO(cal.serialize().encode('utf8'))
136 document_directory_content()
138 class crm_case(osv.osv):
139 _inherit = 'crm.case'
141 'code': fields.char('Calendar Code', size=64)
145 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: