Launchpad automatic translations update.
[odoo/odoo.git] / addons / document_ics / document_ics.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from osv import osv, fields
23 from osv.orm import except_orm
24 import os
25 import StringIO
26 import base64
27 import datetime
28 import time
29 import random
30 import tools
31 import re
32
33 from tools.translate import _
34 from document.nodes import node_content
35
36 from tools.safe_eval import safe_eval
37
38 ICS_TAGS = {
39     'summary': 'normal',
40     'uid': 'normal' ,
41     'dtstart': 'date' ,
42     'dtend': 'date' ,
43     'created': 'date' ,
44     'dtstamp': 'date' ,
45     'last-modified': 'normal' ,
46     'url': 'normal',
47     'attendee': 'multiple',
48     'location': 'normal',
49     'categories': 'normal',
50     'description': 'normal',
51
52     # TODO: handle the 'duration' property
53 }
54
55 ICS_FUNCTIONS = [
56     ('field', 'Use the field'),
57     ('const', 'Expression as constant'),
58     ('hours', 'Interval in hours'),
59     ]
60
61 class document_directory_ics_fields(osv.osv):
62     """ Document Directory ICS Fields """
63     _name = 'document.directory.ics.fields'
64     _description = 'Document Directory ICS Fields'
65     _columns = {
66         'field_id': fields.many2one('ir.model.fields', 'OpenERP Field'),
67         'name': fields.selection(map(lambda x: (x, x), ICS_TAGS.keys()), 'ICS Value', required=True),
68         'content_id': fields.many2one('document.directory.content', 'Content',\
69                              required=True, ondelete='cascade'),
70         'expr': fields.char("Expression", size=64),
71         'fn': fields.selection(ICS_FUNCTIONS, 'Function', help="Alternate method \
72                 of calculating the value", required=True)
73     }
74     _defaults = {
75         'fn': lambda *a: 'field',
76     }
77
78 document_directory_ics_fields()
79
80 class document_directory_content(osv.osv):
81     """ Document Directory Content """
82     _inherit = 'document.directory.content'
83     _description = 'Document Directory Content'
84     __rege = re.compile(r'OpenERP-([\w|\.]+)_([0-9]+)@(\w+)$')
85
86     _columns = {
87         'object_id': fields.many2one('ir.model', 'Object', oldname= 'ics_object_id'),
88         'obj_iterate': fields.boolean('Iterate object', help="If set, a separate \
89                         instance will be created for each record of Object"),
90         'fname_field': fields.char("Filename field", size=16, help="The field of the \
91                         object used in the filename. Has to be a unique identifier."),
92         'ics_domain': fields.char('Domain', size=64),
93         'ics_field_ids': fields.one2many('document.directory.ics.fields', 'content_id', 'Fields Mapping')
94     }
95     _defaults = {
96         'ics_domain': lambda *args: '[]'
97     }
98
99     def _file_get(self, cr, node, nodename, content, context=None):
100         """  Get the file
101             @param self: The object pointer
102             @param cr: the current row, from the database cursor,
103             @param node: pass the node
104             @param nodename: pass the nodename
105             @param context: A standard dictionary for contextual values
106         """
107
108         if not content.obj_iterate:
109             return super(document_directory_content, self)._file_get(cr, node, nodename, content)
110         else:
111             if not content.object_id:
112                 return False
113             mod = self.pool.get(content.object_id.model)
114             uid = node.context.uid
115             fname_fld = content.fname_field or 'id'
116             where = []
117             if node.domain:
118                 where += eval(node.domain)
119             if nodename:
120                 # Reverse-parse the nodename to deduce the clause:
121                 prefix = (content.prefix or '')
122                 suffix = (content.suffix or '') + (content.extension or '')
123                 if not nodename.startswith(prefix):
124                     return False
125                 if not nodename.endswith(suffix):
126                     return False
127                 tval = nodename[len(prefix):0 - len(suffix)]
128                 where.append((fname_fld,'=',tval))
129             # print "ics iterate clause:", where
130             resids = mod.search(cr, uid, where, context=context)
131             if not resids:
132                 return False
133
134             res2 = []
135             for ro in mod.read(cr,uid,resids,['id', fname_fld]):
136                 tname = (content.prefix or '') + str(ro[fname_fld])
137                 tname += (content.suffix or '') + (content.extension or '')
138                 dctx2 = { 'active_id': ro['id'] }
139                 if fname_fld:
140                     dctx2['active_'+fname_fld] = ro[fname_fld]
141                 n = node_content(tname, node, node.context, content, dctx=dctx2, act_id = ro['id'])
142                 n.fill_fields(cr, dctx2)
143                 res2.append(n)
144             return res2
145
146     def process_write(self, cr, uid, node, data, context=None):
147         """
148             @param self: The object pointer
149             @param cr: the current row, from the database cursor,
150             @param uid: the current user’s ID for security checks,
151             @param node: pass the node
152             @param data: pass the data
153             @param context: A standard dictionary for contextual values
154         """
155
156         if node.extension != '.ics':
157                 return super(document_directory_content, self).process_write(cr, uid, node, data, context)
158         import vobject
159         parsedCal = vobject.readOne(data)
160         fields = {}
161         funcs = {}
162         fexprs = {}
163         content = self.browse(cr, uid, node.cnt_id, context)
164
165         idomain = {}
166         ctx = (context or {})
167         ctx.update(node.context.context.copy())
168         ctx.update(node.dctx)
169         if content.ics_domain:
170             for d in safe_eval(content.ics_domain,ctx):
171                 # TODO: operator?
172                 idomain[d[0]]=d[2]
173         for n in content.ics_field_ids:
174             fields[n.name] = n.field_id.name and str(n.field_id.name)
175             funcs[n.name] = n.fn
176             fexprs[n.name] = n.expr
177
178         if 'uid' not in fields:
179             # FIXME: should pass
180             return True
181         for child in parsedCal.getChildren():
182             result = {}
183             uuid = None
184
185             for event in child.getChildren():
186                 enl = event.name.lower()
187                 if enl =='uid':
188                     uuid = event.value
189                 if not enl in fields:
190                         continue
191                 if fields[enl] and funcs[enl] == 'field':
192                     if ICS_TAGS[enl]=='normal':
193                         result[fields[enl]] = event.value.encode('utf8')
194                     elif ICS_TAGS[enl]=='date':
195                         result[fields[enl]] = event.value.strftime('%Y-%m-%d %H:%M:%S')
196
197                 elif fields[enl] and funcs[enl] == 'hours':
198                     ntag = fexprs[enl] or 'dtstart'
199                     ts_start = child.getChildValue(ntag, default=False)
200                     if not ts_start:
201                         raise Exception("Cannot parse hours (for %s) without %s" % (enl, ntag))
202                     ts_end = event.value
203                     assert isinstance(ts_start, datetime.datetime)
204                     assert isinstance(ts_end, datetime.datetime)
205                     td = ts_end - ts_start
206                     result[fields[enl]] = td.days * 24.0 + ( td.seconds / 3600.0)
207
208                 # put other functions here..
209                 else:
210                     # print "Unhandled tag in ICS:", enl
211                     pass
212             # end for
213
214             if not uuid:
215                 # FIXME: should pass
216                 continue
217
218             cmodel = content.object_id.model
219             wexpr = False
220             if fields['uid']:
221                 wexpr = [(fields['uid'], '=', uuid.encode('utf8'))]
222             else:
223                 # Parse back the uid from 'OpenERP-%s_%s@%s'
224                 wematch = self.__rege.match(uuid.encode('utf8'))
225                 # TODO: perhaps also add the domain to wexpr, restrict.
226                 if not wematch:
227                     raise Exception("Cannot locate UID in %s" % uuid)
228                 if wematch.group(3) != cr.dbname:
229                     raise Exception("Object is not for our db!")
230                 if content.object_id:
231                     if wematch.group(1) != cmodel:
232                         raise Exception("ICS must be at the wrong folder, this one is for %s" % cmodel)
233                 else:
234                     # TODO: perhaps guess the model from the iCal, is it safe?
235                     pass
236
237                 wexpr = [ ( 'id', '=', wematch.group(2) ) ]
238
239             fobj = self.pool.get(content.object_id.model)
240
241             if not wexpr:
242                 id = False
243             else:
244                 id = fobj.search(cr, uid, wexpr, context=context)
245
246             if isinstance(id, list):
247                 if len(id) > 1:
248                     raise Exception("Multiple matches found for ICS")
249             if id:
250                 fobj.write(cr, uid, id, result, context=context)
251             else:
252                 r = idomain.copy()
253                 r.update(result)
254                 fobj.create(cr, uid, r, context=context)
255
256         return True
257
258     def process_read(self, cr, uid, node, context=None):
259         """
260             @param self: The object pointer
261             @param cr: the current row, from the database cursor,
262             @param uid: the current user’s ID for security checks,
263             @param node: pass the node
264             @param context: A standard dictionary for contextual values
265         """
266
267         def ics_datetime(idate, short=False):
268             if short:
269                 return datetime.date.fromtimestamp(time.mktime(time.strptime(idate, '%Y-%m-%d')))
270             else:
271                 return datetime.datetime.strptime(idate, '%Y-%m-%d %H:%M:%S')
272
273         if node.extension != '.ics':
274             return super(document_directory_content, self).process_read(cr, uid, node, context)
275
276         import vobject
277         ctx = (context or {})
278         ctx.update(node.context.context.copy())
279         ctx.update(node.dctx)
280         content = self.browse(cr, uid, node.cnt_id, ctx)
281         if not content.object_id:
282             return super(document_directory_content, self).process_read(cr, uid, node, context)
283         obj_class = self.pool.get(content.object_id.model)
284
285         if content.ics_domain:
286             domain = safe_eval(content.ics_domain,ctx)
287         else:
288             domain = []
289         if node.act_id:
290             domain.append(('id','=',node.act_id))
291         # print "process read clause:",domain
292         ids = obj_class.search(cr, uid, domain, context=ctx)
293         cal = vobject.iCalendar()
294         for obj in obj_class.browse(cr, uid, ids):
295             event = cal.add('vevent')
296             # Fix dtstamp et last-modified with create and write date on the object line
297             perm = obj_class.perm_read(cr, uid, [obj.id], context)
298             event.add('created').value = ics_datetime(time.strftime('%Y-%m-%d %H:%M:%S'))
299             event.add('dtstamp').value = ics_datetime(perm[0]['create_date'][:19])
300             if perm[0]['write_date']:
301                 event.add('last-modified').value = ics_datetime(perm[0]['write_date'][:19])
302             for field in content.ics_field_ids:
303                 if field.field_id.name:
304                     value = getattr(obj, field.field_id.name)
305                 else: value = None
306                 if (not value) and field.name=='uid':
307                     value = 'OpenERP-%s_%s@%s' % (content.object_id.model, str(obj.id), cr.dbname,)
308                     # Why? obj_class.write(cr, uid, [obj.id], {field.field_id.name: value})
309                 if ICS_TAGS[field.name]=='normal':
310                     if type(value)==type(obj):
311                         value=value.name
312                     event.add(field.name).value = tools.ustr(value) or ''
313                 elif ICS_TAGS[field.name]=='date' and value:
314                     if field.name == 'dtstart':
315                         date_start = start_date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(value , "%Y-%m-%d %H:%M:%S")))
316                     if field.name == 'dtend' and ( isinstance(value, float) or field.fn == 'hours'):
317                         value = (start_date + datetime.timedelta(hours=value)).strftime('%Y-%m-%d %H:%M:%S')
318                     if len(value)==10:
319                         value = ics_datetime(value, True)
320                     else:
321                         value = ics_datetime(value)
322                     event.add(field.name).value = value
323         s = cal.serialize()
324         return s
325 document_directory_content()
326
327 class crm_meeting(osv.osv):
328     _inherit = 'crm.meeting'
329     _columns = {
330         'code': fields.char('Calendar Code', size=64),
331         'date_deadline': fields.datetime('Deadline', help="Deadline Date is automatically\
332                          computed from Start Date + Duration"),
333     }
334
335     _defaults = {
336         'code': lambda obj, cr, uid, context: obj.pool.get('ir.sequence').get(cr, uid, 'crm.meeting'),
337     }
338
339     def copy(self, cr, uid, id, default=None, context=None):
340         """
341             code field must be unique in ICS file
342             @param self: The object pointer
343             @param cr: the current row, from the database cursor,
344             @param uid: the current user’s ID for security checks,
345             @param id: crm case's ID
346             @param context: A standard dictionary for contextual values
347         """
348
349         if not default: default = {}
350         if not context: context = {}
351         default.update({'code': self.pool.get('ir.sequence').get(cr, uid, 'crm.meeting'), 'id': False})
352         return super(crm_meeting, self).copy(cr, uid, id, default, context)
353
354 crm_meeting()
355
356 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
357