1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2008 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'
47 class document_directory_ics_fields(osv.osv):
48 _name = 'document.directory.ics.fields'
50 'field_id': fields.many2one('ir.model.fields', 'Open ERP Field', required=True),
51 'name': fields.selection(map(lambda x: (x,x), ICS_TAGS.keys()),'ICS Value', required=True),
52 'content_id': fields.many2one('document.directory.content', 'Content', required=True, ondelete='cascade')
54 document_directory_ics_fields()
56 class document_directory_content(osv.osv):
57 _inherit = 'document.directory.content'
59 'ics_object_id': fields.many2one('ir.model', 'Object'),
60 'ics_domain': fields.char('Domain', size=64),
61 'ics_field_ids': fields.one2many('document.directory.ics.fields', 'content_id', 'Fields Mapping')
64 'ics_domain': lambda *args: '[]'
66 def process_write_ics(self, cr, uid, node, data, context={}):
68 parsedCal = vobject.readOne(data)
70 fobj = self.pool.get('document.directory.content')
71 content = fobj.browse(cr, uid, node.content.id, context)
74 for d in eval(content.ics_domain):
76 for n in content.ics_field_ids:
77 fields[n.name] = n.field_id.name
78 if 'uid' not in fields:
80 for child in parsedCal.getChildren():
83 for event in child.getChildren():
84 if event.name.lower()=='uid':
86 if event.name.lower() in fields:
87 if ICS_TAGS[event.name.lower()]=='normal':
88 result[fields[event.name.lower()]] = event.value.encode('utf8')
89 elif ICS_TAGS[event.name.lower()]=='date':
90 result[fields[event.name.lower()]] = event.value.strftime('%Y-%m-%d %H:%M:%S')
94 fobj = self.pool.get(content.ics_object_id.model)
95 id = fobj.search(cr, uid, [(fields['uid'], '=', uuid.encode('utf8'))], context=context)
97 fobj.write(cr, uid, id, result, context=context)
101 fobj.create(cr, uid, r, context=context)
105 def process_read_ics(self, cr, uid, node, context={}):
107 obj_class = self.pool.get(node.content.ics_object_id.model)
108 # Can be improved to use context and active_id !
109 domain = eval(node.content.ics_domain)
110 ids = obj_class.search(cr, uid, domain, context)
111 cal = vobject.iCalendar()
112 for obj in obj_class.browse(cr, uid, ids, context):
113 event = cal.add('vevent')
114 for field in node.content.ics_field_ids:
115 value = getattr(obj, field.field_id.name)
116 if (not value) and field.name=='uid':
117 value = 'OpenERP-'+str(random.randint(1999999999, 9999999999))
118 obj_class.write(cr, uid, [obj.id], {field.field_id.name: value})
119 if ICS_TAGS[field.name]=='normal':
120 if type(value)==type(obj):
123 event.add(field.name).value = value and value.decode('utf8') or ''
124 elif ICS_TAGS[field.name]=='date':
125 dt = value or time.strftime('%Y-%m-%d %H:%M:%S')
128 value = datetime.datetime.strptime(dt, '%Y-%m-%d %H:%M:%S')
129 if field.name=='dtend':
130 value += datetime.timedelta(hours=3)
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)