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