[FIX] Corrected language code for Slovenian and Danish locales and renamed correspond...
[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 import tools
32 from tools.translate import _
33
34 ICS_TAGS = {
35     'summary':'normal',
36     'uid':'normal' ,
37     'dtstart':'date' ,
38     'dtend':'date' ,
39     'created':'date' ,
40     'dtstamp':'date' ,
41     'last-modified':'normal' ,
42     'url':'normal' ,
43     'attendee':'multiple',
44     'location':'normal',
45     'categories': 'normal',
46     'description':'normal',
47
48     # TODO: handle the 'duration' property
49 }
50
51 class document_directory_ics_fields(osv.osv):
52     _name = 'document.directory.ics.fields'
53     _columns = {
54         'field_id': fields.many2one('ir.model.fields', 'Open ERP Field', required=True),
55         'name': fields.selection(map(lambda x: (x, x), ICS_TAGS.keys()), 'ICS Value', required=True),
56         'content_id': fields.many2one('document.directory.content', 'Content', required=True, ondelete='cascade')
57     }
58 document_directory_ics_fields()
59
60 class document_directory_content(osv.osv):
61     _inherit = 'document.directory.content'
62     _columns = {
63         'ics_object_id': fields.many2one('ir.model', 'Object'),
64         'ics_domain': fields.char('Domain', size=64),
65         'ics_field_ids': fields.one2many('document.directory.ics.fields', 'content_id', 'Fields Mapping')
66     }
67     _defaults = {
68         'ics_domain': lambda *args: '[]'
69     }
70     def process_write_ics(self, cr, uid, node, data, context={}):
71         import vobject
72         parsedCal = vobject.readOne(data)
73         fields = {}
74         fobj = self.pool.get('document.directory.content')
75         content = fobj.browse(cr, uid, node.content.id, context)
76
77         idomain = {}
78         for d in eval(content.ics_domain):
79             idomain[d[0]]=d[2]
80         for n in content.ics_field_ids:
81             fields[n.name] = n.field_id.name
82         if 'uid' not in fields:
83             return True
84         for child in parsedCal.getChildren():
85             result = {}
86             uuid = None
87             for event in child.getChildren():
88                 if event.name.lower()=='uid':
89                     uuid = event.value
90                 if event.name.lower() in fields:
91                     if ICS_TAGS[event.name.lower()]=='normal':
92                         result[fields[event.name.lower()]] = event.value.encode('utf8')
93                     elif ICS_TAGS[event.name.lower()]=='date':
94                         result[fields[event.name.lower()]] = event.value.strftime('%Y-%m-%d %H:%M:%S')
95             if not uuid:
96                 continue
97
98             fobj = self.pool.get(content.ics_object_id.model)
99             id = fobj.search(cr, uid, [(fields['uid'], '=', uuid.encode('utf8'))], context=context)
100             if id:
101                 fobj.write(cr, uid, id, result, context=context)
102             else:
103                 r = idomain.copy()
104                 r.update(result)
105                 fobj.create(cr, uid, r, context=context)
106
107         return True
108
109     def process_read_ics(self, cr, uid, node, context={}):
110         def ics_datetime(idate, short=False):
111             if short:
112                 return datetime.date.fromtimestamp(time.mktime(time.strptime(idate, '%Y-%m-%d')))
113             else:
114                 return datetime.datetime.strptime(idate, '%Y-%m-%d %H:%M:%S')
115
116         import vobject
117         obj_class = self.pool.get(node.content.ics_object_id.model)
118         # Can be improved to use context and active_id !
119         domain = eval(node.content.ics_domain)
120         ids = obj_class.search(cr, uid, domain, context)
121         cal = vobject.iCalendar()
122         for obj in obj_class.browse(cr, uid, ids, context):
123             event = cal.add('vevent')
124             # Fix dtstamp et last-modified with create and write date on the object line
125             perm = obj_class.perm_read(cr, uid, [obj.id], context)
126             event.add('created').value = ics_datetime(time.strftime('%Y-%m-%d %H:%M:%S'))
127             event.add('dtstamp').value = ics_datetime(perm[0]['create_date'][:19])
128             if perm[0]['write_date']:
129                 event.add('last-modified').value = ics_datetime(perm[0]['write_date'][:19])
130             for field in node.content.ics_field_ids:
131                 value = getattr(obj, field.field_id.name)
132                 value = value and tools.ustr(value)
133                 if (not value) and field.name=='uid':
134                     value = 'OpenERP-%s_%s@%s' % (node.content.ics_object_id.model, str(obj.id), cr.dbname,)
135                     obj_class.write(cr, uid, [obj.id], {field.field_id.name: value})
136                 if ICS_TAGS[field.name]=='normal':
137                     if type(value)==type(obj):
138                         value=value.name
139                     value = value or ''
140                     event.add(field.name).value = value or ''
141                 elif ICS_TAGS[field.name]=='date' and value:
142                     if field.name == 'dtstart':
143                         date_start = start_date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(value , "%Y-%m-%d %H:%M:%S")))
144                     if field.name == 'dtend' and isinstance(value, float):
145                         value = (start_date + datetime.timedelta(hours=value)).strftime('%Y-%m-%d %H:%M:%S')
146                     if len(value)==10:
147                         value = ics_datetime(value, True)
148                     else:
149                         value = ics_datetime(value)
150                     event.add(field.name).value = value
151         s= StringIO.StringIO(cal.serialize())
152         s.name = node
153         cr.commit()
154         return s
155 document_directory_content()
156
157 class crm_case(osv.osv):
158     _inherit = 'crm.case'
159     _columns = {
160         'code': fields.char('Calendar Code', size=64),
161         'date_deadline': fields.datetime('Deadline', help="Deadline Date is automatically computed from Start Date + Duration"),
162     }
163
164     _defaults = {
165         'code': lambda obj, cr, uid, context: obj.pool.get('ir.sequence').get(cr, uid, 'crm.case'),
166     }
167
168     def copy(self, cr, uid, id, default=None, context=None):
169         """
170         code field must be unique in ICS file
171         """
172         if not default: default = {}
173         if not context: context = {}
174         default.update({'code': self.pool.get('ir.sequence').get(cr, uid, 'crm.case'), 'id': False})
175         return super(crm_case, self).copy(cr, uid, id, default, context)
176
177     def on_change_duration(self, cr, uid, id, date, duration):
178         if not date:
179             return {}
180         start_date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(date, "%Y-%m-%d %H:%M:%S")))
181         if duration >= 0 :
182             end = start_date + datetime.timedelta(hours=duration)
183         if duration < 0:
184             raise osv.except_osv(_('Warning !'),
185                     _('You can not set negative Duration.'))
186
187         res = {'value' : {'date_deadline' : end.strftime('%Y-%m-%d %H:%M:%S')}}
188         return res
189
190 crm_case()
191
192 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
193