[FIX] Revert
[odoo/odoo.git] / addons / document_ics / document.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution    
5 #    Copyright (C) 2004-2009 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     # TODO: handle the 'duration' property
47 }
48
49 class document_directory_ics_fields(osv.osv):
50     _name = 'document.directory.ics.fields'
51     _columns = {
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')
55     }
56 document_directory_ics_fields()
57
58 class document_directory_content(osv.osv):
59     _inherit = 'document.directory.content'
60     _columns = {
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')
64     }
65     _defaults = {
66         'ics_domain': lambda *args: '[]'
67     }
68     def process_write_ics(self, cr, uid, node, data, context={}):
69         import vobject
70         parsedCal = vobject.readOne(data)
71         fields = {}
72         fobj = self.pool.get('document.directory.content')
73         content = fobj.browse(cr, uid, node.content.id, context)
74
75         idomain = {}
76         for d in eval(content.ics_domain):
77             idomain[d[0]]=d[2]
78         for n in content.ics_field_ids:
79             fields[n.name] = n.field_id.name
80         if 'uid' not in fields:
81             return True
82         for child in parsedCal.getChildren():
83             result = {}
84             uuid = None
85             for event in child.getChildren():
86                 if event.name.lower()=='uid':
87                     uuid = event.value
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')
93             if not uuid:
94                 continue
95
96             fobj = self.pool.get(content.ics_object_id.model)
97             id = fobj.search(cr, uid, [(fields['uid'], '=', uuid.encode('utf8'))], context=context)
98             if id:
99                 fobj.write(cr, uid, id, result, context=context)
100             else:
101                 r = idomain.copy()
102                 r.update(result)
103                 fobj.create(cr, uid, r, context=context)
104
105         return True
106
107     def process_read_ics(self, cr, uid, node, context={}):
108         import vobject
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):
123                         value=value.name
124                     value = value or ''
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')
128                     if len(dt)==10:
129                         dt = dt+' 09:00:00'
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'))
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
145 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
146