bugfixes
[odoo/odoo.git] / addons / document_ics / document.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
5 #
6 # $Id$
7 #
8 # WARNING: This program as such is intended to be used by professional
9 # programmers who take the whole responsability of assessing all potential
10 # consequences resulting from its eventual inadequacies and bugs
11 # End users who are looking for a ready-to-use solution with commercial
12 # garantees and support are strongly adviced to contract a Free Software
13 # Service Company
14 #
15 # This program is Free Software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28 #
29 ##############################################################################
30
31 from osv import osv, fields
32 from osv.orm import except_orm
33 import os
34 import StringIO
35 import base64
36 import datetime
37 import time
38
39 ICS_TAGS = {
40     'summary':'normal',
41     'uid':'normal' ,
42     'dtstart':'date' ,
43     'dtend':'date' ,
44     'created':'date' ,
45     'dt-stamp':'date' ,
46     'last-modified':'normal' ,
47     'url':'normal' ,
48     'attendee':'multiple',
49     'location':'normal',
50     'categories': 'normal',
51     'description':'normal'
52 }
53
54 class document_directory_ics_fields(osv.osv):
55     _name = 'document.directory.ics.fields'
56     _columns = {
57         'field_id': fields.many2one('ir.model.fields', 'Open ERP Field', required=True),
58         'name': fields.selection(map(lambda x: (x,x), ICS_TAGS.keys()),'ICS Value', required=True),
59         'content_id': fields.many2one('document.directory.content', 'Content', required=True, ondelete='cascade')
60     }
61 document_directory_ics_fields()
62
63 class document_directory_content(osv.osv):
64     _inherit = 'document.directory.content'
65     _columns = {
66         'ics_object_id': fields.many2one('ir.model', 'Object'),
67         'ics_domain': fields.char('Domain', size=64),
68         'ics_field_ids': fields.one2many('document.directory.ics.fields', 'content_id', 'Fields Mapping')
69     }
70     _defaults = {
71         'ics_domain': lambda *args: '[]'
72     }
73     def process_read_ics(self, cr, uid, node, context={}):
74         print 'READ ICS'
75         import vobject
76         obj_class = self.pool.get(node.content.ics_object_id.model)
77         # Can be improved to use context and active_id !
78         domain = eval(node.content.ics_domain)
79         ids = obj_class.search(cr, uid, domain, context)
80         cal = vobject.iCalendar()
81         for obj in obj_class.browse(cr, uid, ids, context):
82             event = cal.add('vevent')
83             for field in node.content.ics_field_ids:
84                 if ICS_TAGS[field.name]=='normal':
85                     value = getattr(obj, field.field_id.name) or ''
86                     event.add(field.name).value = value
87                 elif ICS_TAGS[field.name]=='date':
88                     dt = getattr(obj, field.field_id.name) or time.strftime('%Y-%m-%d %H:%M:%S')
89                     if len(dt)==10:
90                         if field.name=='dtend':
91                             dt = dt+' 10:00:00'
92                         else:
93                             dt = dt+' 09:00:00'
94                     value = datetime.datetime.strptime(dt, '%Y-%m-%d %H:%M:%S')
95                     event.add(field.name).value = value
96         s= StringIO.StringIO(cal.serialize())
97         s.name = node
98         return s
99 document_directory_content()
100