[IMP]: document_ics: Improvement in process_read, process_write functions for using caldav import/export in synchronization of calendar data with OpenERP and external calendar utility
bzr revid: rpa@tinyerp.com-20091228092906-rbdz1uur1d28ae1c
This commit is contained in:
@@ -127,11 +127,6 @@ class document_directory_content(osv.osv):
|
||||
def process_write(self, cr, uid, node, data, context=None):
|
||||
if node.extension != '.ics':
|
||||
return super(document_directory_content, self).process_write(cr, uid, node, data, context)
|
||||
import vobject
|
||||
parsedCal = vobject.readOne(data)
|
||||
fields = {}
|
||||
funcs = {}
|
||||
fexprs = {}
|
||||
content = self.browse(cr, uid, node.cnt_id, context)
|
||||
|
||||
idomain = {}
|
||||
@@ -143,117 +138,18 @@ class document_directory_content(osv.osv):
|
||||
for d in safe_eval(content.ics_domain,ctx):
|
||||
# TODO: operator?
|
||||
idomain[d[0]]=d[2]
|
||||
for n in content.ics_field_ids:
|
||||
fields[n.name] = n.field_id.name and str(n.field_id.name)
|
||||
funcs[n.name] = n.fn
|
||||
fexprs[n.name] = n.expr
|
||||
|
||||
if 'uid' not in fields:
|
||||
print "uid not in ", fields
|
||||
# FIXME: should pass
|
||||
return True
|
||||
for child in parsedCal.getChildren():
|
||||
result = {}
|
||||
uuid = None
|
||||
|
||||
for event in child.getChildren():
|
||||
enl = event.name.lower()
|
||||
if enl =='uid':
|
||||
uuid = event.value
|
||||
if not enl in fields:
|
||||
# print "skip", enl
|
||||
continue
|
||||
if fields[enl] and funcs[enl] == 'field':
|
||||
if ICS_TAGS[enl]=='normal':
|
||||
result[fields[enl]] = event.value.encode('utf8')
|
||||
elif ICS_TAGS[enl]=='date':
|
||||
result[fields[enl]] = event.value.strftime('%Y-%m-%d %H:%M:%S')
|
||||
|
||||
# print "Field ",enl, result[fields[enl]]
|
||||
elif fields[enl] and funcs[enl] == 'hours':
|
||||
ntag = fexprs[enl] or 'dtstart'
|
||||
ts_start = child.getChildValue(ntag, default=False)
|
||||
if not ts_start:
|
||||
raise Exception("Cannot parse hours (for %s) without %s" % (enl, ntag))
|
||||
ts_end = event.value
|
||||
assert isinstance(ts_start, datetime.datetime)
|
||||
assert isinstance(ts_end, datetime.datetime)
|
||||
td = ts_end - ts_start
|
||||
result[fields[enl]] = td.days * 24.0 + ( td.seconds / 3600.0)
|
||||
|
||||
# put other functions here..
|
||||
else:
|
||||
# print "Unhandled tag in ICS:", enl
|
||||
pass
|
||||
# end for
|
||||
|
||||
if not uuid:
|
||||
print "Skipping cal", child
|
||||
# FIXME: should pass
|
||||
continue
|
||||
|
||||
cmodel = content.object_id.model
|
||||
wexpr = False
|
||||
if fields['uid']:
|
||||
wexpr = [(fields['uid'], '=', uuid.encode('utf8'))]
|
||||
else:
|
||||
# Parse back the uid from 'OpenERP-%s_%s@%s'
|
||||
wematch = self.__rege.match(uuid.encode('utf8'))
|
||||
# TODO: perhaps also add the domain to wexpr, restrict.
|
||||
if not wematch:
|
||||
raise Exception("Cannot locate UID in %s" % uuid)
|
||||
if wematch.group(3) != cr.dbname:
|
||||
raise Exception("Object is not for our db!")
|
||||
if content.object_id:
|
||||
if wematch.group(1) != cmodel:
|
||||
raise Exception("ICS must be at the wrong folder, this one is for %s" % cmodel)
|
||||
else:
|
||||
# TODO: perhaps guess the model from the iCal, is it safe?
|
||||
pass
|
||||
|
||||
wexpr = [ ( 'id', '=', wematch.group(2) ) ]
|
||||
|
||||
# print "Looking at ", cmodel, " for ", wexpr
|
||||
# print "domain=", idomain
|
||||
|
||||
fobj = self.pool.get(content.object_id.model)
|
||||
|
||||
if not wexpr:
|
||||
id = False
|
||||
else:
|
||||
id = fobj.search(cr, uid, wexpr, context=context)
|
||||
|
||||
if isinstance(id, list):
|
||||
if len(id) > 1:
|
||||
raise Exception("Multiple matches found for ICS")
|
||||
if id:
|
||||
fobj.write(cr, uid, id, result, context=context)
|
||||
else:
|
||||
r = idomain.copy()
|
||||
r.update(result)
|
||||
fobj.create(cr, uid, r, context=context)
|
||||
|
||||
fobj = self.pool.get(content.object_id.model)
|
||||
fobj.import_cal(cr, uid, base64.encodestring(data), context=context)
|
||||
return True
|
||||
|
||||
def process_read(self, cr, uid, node, context=None):
|
||||
def ics_datetime(idate, short=False):
|
||||
if short:
|
||||
return datetime.date.fromtimestamp(time.mktime(time.strptime(idate, '%Y-%m-%d')))
|
||||
else:
|
||||
return datetime.datetime.strptime(idate, '%Y-%m-%d %H:%M:%S')
|
||||
|
||||
if node.extension != '.ics':
|
||||
return super(document_directory_content, self).process_read(cr, uid, node, context)
|
||||
|
||||
import vobject
|
||||
ctx = (context or {})
|
||||
ctx.update(node.context.context.copy())
|
||||
ctx.update(node.dctx)
|
||||
content = self.browse(cr, uid, node.cnt_id, ctx)
|
||||
if not content.object_id:
|
||||
return super(document_directory_content, self).process_read(cr, uid, node, context)
|
||||
obj_class = self.pool.get(content.object_id.model)
|
||||
|
||||
#
|
||||
if content.ics_domain:
|
||||
domain = safe_eval(content.ics_domain,ctx)
|
||||
else:
|
||||
@@ -262,37 +158,8 @@ class document_directory_content(osv.osv):
|
||||
domain.append(('id','=',node.act_id))
|
||||
# print "process read clause:",domain
|
||||
ids = obj_class.search(cr, uid, domain, context=ctx)
|
||||
cal = vobject.iCalendar()
|
||||
for obj in obj_class.browse(cr, uid, ids):
|
||||
event = cal.add('vevent')
|
||||
# Fix dtstamp et last-modified with create and write date on the object line
|
||||
perm = obj_class.perm_read(cr, uid, [obj.id], context)
|
||||
event.add('created').value = ics_datetime(time.strftime('%Y-%m-%d %H:%M:%S'))
|
||||
event.add('dtstamp').value = ics_datetime(perm[0]['create_date'][:19])
|
||||
if perm[0]['write_date']:
|
||||
event.add('last-modified').value = ics_datetime(perm[0]['write_date'][:19])
|
||||
for field in content.ics_field_ids:
|
||||
if field.field_id.name:
|
||||
value = getattr(obj, field.field_id.name)
|
||||
else: value = None
|
||||
if (not value) and field.name=='uid':
|
||||
value = 'OpenERP-%s_%s@%s' % (content.object_id.model, str(obj.id), cr.dbname,)
|
||||
# Why? obj_class.write(cr, uid, [obj.id], {field.field_id.name: value})
|
||||
if ICS_TAGS[field.name]=='normal':
|
||||
if type(value)==type(obj):
|
||||
value=value.name
|
||||
event.add(field.name).value = tools.ustr(value) or ''
|
||||
elif ICS_TAGS[field.name]=='date' and value:
|
||||
if field.name == 'dtstart':
|
||||
date_start = start_date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(value , "%Y-%m-%d %H:%M:%S")))
|
||||
if field.name == 'dtend' and ( isinstance(value, float) or field.fn == 'hours'):
|
||||
value = (start_date + datetime.timedelta(hours=value)).strftime('%Y-%m-%d %H:%M:%S')
|
||||
if len(value)==10:
|
||||
value = ics_datetime(value, True)
|
||||
else:
|
||||
value = ics_datetime(value)
|
||||
event.add(field.name).value = value
|
||||
s = cal.serialize()
|
||||
context.update({'model': content.object_id.model})
|
||||
s = obj_class.export_cal(cr, uid, ids, context=context)
|
||||
return s
|
||||
document_directory_content()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user