1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import osv, fields
23 from osv.orm import except_orm
33 from tools.translate import _
34 from document.nodes import node_content
36 from tools.safe_eval import safe_eval
45 'last-modified': 'normal' ,
47 'attendee': 'multiple',
49 'categories': 'normal',
50 'description': 'normal',
52 # TODO: handle the 'duration' property
56 ('field', 'Use the field'),
57 ('const', 'Expression as constant'),
58 ('hours', 'Interval in hours'),
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'
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)
75 'fn': lambda *a: 'field',
78 document_directory_ics_fields()
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+)$')
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')
96 'ics_domain': lambda *args: '[]'
99 def _file_get(self, cr, node, nodename, content, context=None):
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
108 if not content.obj_iterate:
109 return super(document_directory_content, self)._file_get(cr, node, nodename, content)
111 if not content.object_id:
113 mod = self.pool.get(content.object_id.model)
114 uid = node.context.uid
115 fname_fld = content.fname_field or 'id'
118 where += eval(node.domain)
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):
125 if not nodename.endswith(suffix):
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)
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'] }
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)
146 def process_write(self, cr, uid, node, data, context=None):
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
156 if node.extension != '.ics':
157 return super(document_directory_content, self).process_write(cr, uid, node, data, context)
159 parsedCal = vobject.readOne(data)
163 content = self.browse(cr, uid, node.cnt_id, context)
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):
173 for n in content.ics_field_ids:
174 fields[n.name] = n.field_id.name and str(n.field_id.name)
176 fexprs[n.name] = n.expr
178 if 'uid' not in fields:
181 for child in parsedCal.getChildren():
185 for event in child.getChildren():
186 enl = event.name.lower()
189 if not enl in fields:
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')
197 elif fields[enl] and funcs[enl] == 'hours':
198 ntag = fexprs[enl] or 'dtstart'
199 ts_start = child.getChildValue(ntag, default=False)
201 raise Exception("Cannot parse hours (for %s) without %s" % (enl, ntag))
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)
208 # put other functions here..
210 # print "Unhandled tag in ICS:", enl
218 cmodel = content.object_id.model
221 wexpr = [(fields['uid'], '=', uuid.encode('utf8'))]
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.
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)
234 # TODO: perhaps guess the model from the iCal, is it safe?
237 wexpr = [ ( 'id', '=', wematch.group(2) ) ]
239 fobj = self.pool.get(content.object_id.model)
244 id = fobj.search(cr, uid, wexpr, context=context)
246 if isinstance(id, list):
248 raise Exception("Multiple matches found for ICS")
250 fobj.write(cr, uid, id, result, context=context)
254 fobj.create(cr, uid, r, context=context)
258 def process_read(self, cr, uid, node, context=None):
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
267 def ics_datetime(idate, short=False):
269 return datetime.date.fromtimestamp(time.mktime(time.strptime(idate, '%Y-%m-%d')))
271 return datetime.datetime.strptime(idate, '%Y-%m-%d %H:%M:%S')
273 if node.extension != '.ics':
274 return super(document_directory_content, self).process_read(cr, uid, node, context)
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)
285 if content.ics_domain:
286 domain = safe_eval(content.ics_domain,ctx)
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)
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):
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')
319 value = ics_datetime(value, True)
321 value = ics_datetime(value)
322 event.add(field.name).value = value
325 document_directory_content()
327 class crm_meeting(osv.osv):
328 _inherit = 'crm.meeting'
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"),
336 'code': lambda obj, cr, uid, context: obj.pool.get('ir.sequence').get(cr, uid, 'crm.meeting'),
339 def copy(self, cr, uid, id, default=None, context=None):
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
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)
356 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: