fixed bug:308803
[odoo/odoo.git] / addons / document_ics / document.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
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.
12 #
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.
17 #
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/>.
20 #
21 ##############################################################################
22
23 from osv import osv, fields
24 from osv.orm import except_orm
25 import os
26 import StringIO
27 import base64
28 import datetime
29 import time
30 import random
31
32 ICS_TAGS = {
33     'summary':'normal',
34     'uid':'normal' ,
35     'dtstart':'date' ,
36     'dtend':'date' ,
37     'created':'date' ,
38     'dt-stamp':'date' ,
39     'last-modified':'normal' ,
40     'url':'normal' ,
41     'attendee':'multiple',
42     'location':'normal',
43     'categories': 'normal',
44     'description':'normal'
45 }
46
47 class document_directory_ics_fields(osv.osv):
48     _name = 'document.directory.ics.fields'
49     _columns = {
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')
53     }
54 document_directory_ics_fields()
55
56 class document_directory_content(osv.osv):
57     _inherit = 'document.directory.content'
58     _columns = {
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')
62     }
63     _defaults = {
64         'ics_domain': lambda *args: '[]'
65     }
66     def process_write_ics(self, cr, uid, node, data, context={}):
67         import vobject
68         parsedCal = vobject.readOne(data)
69         fields = {}
70         fobj = self.pool.get('document.directory.content')
71         content = fobj.browse(cr, uid, node.content.id, context)
72
73         idomain = {}
74         for d in eval(content.ics_domain):
75             idomain[d[0]]=d[2]
76         for n in content.ics_field_ids:
77             fields[n.name] = n.field_id.name
78         if 'uid' not in fields:
79             return True
80         for child in parsedCal.getChildren():
81             result = {}
82             uuid = None
83             for event in child.getChildren():
84                 if event.name.lower()=='uid':
85                     uuid = event.value
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')
91             if not uuid:
92                 continue
93
94             fobj = self.pool.get(content.ics_object_id.model)
95             id = fobj.search(cr, uid, [(fields['uid'], '=', uuid.encode('utf8'))], context=context)
96             if id:
97                 fobj.write(cr, uid, id, result, context=context)
98             else:
99                 r = idomain.copy()
100                 r.update(result)
101                 fobj.create(cr, uid, r, context=context)
102
103         return True
104
105     def process_read_ics(self, cr, uid, node, context={}):
106         import vobject
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):
121                         value=value.name
122                     value = value or ''
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')
126                     if len(dt)==10:
127                         dt = dt+' 09:00:00'
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'))
133         s.name = node
134         cr.commit()
135         return s
136 document_directory_content()
137
138 class crm_case(osv.osv):
139     _inherit = 'crm.case'
140     _columns = {
141         'code': fields.char('Calendar Code', size=64)
142     }
143 crm_case()
144