diff options
| author | Harry (Open ERP) <hmo@tinyerp.com> | 2010-01-06 16:49:56 +0530 |
|---|---|---|
| committer | Harry (Open ERP) <hmo@tinyerp.com> | 2010-01-06 16:49:56 +0530 |
| commit | 8d12c72ec288227aebf2a152fe62be54b8c68708 (patch) | |
| tree | 4760cd81efef44dae1b54a3feea88238dd3001de /addons/document_ics/document_ics.py | |
| parent | 3c98c9d24d24dee4d80deb08054ad8dc72b7a8ee (diff) | |
[REVERT] document_ics: revert code from rpa@tinyerp.com-20091228092906-rbdz1uur1d28ae1c
bzr revid: hmo@tinyerp.com-20100106111956-geanww0tfaeceu6k
Diffstat (limited to 'addons/document_ics/document_ics.py')
| -rw-r--r-- | addons/document_ics/document_ics.py | 145 |
1 files changed, 139 insertions, 6 deletions
diff --git a/addons/document_ics/document_ics.py b/addons/document_ics/document_ics.py index 65b48da6b29..69cd4179634 100644 --- a/addons/document_ics/document_ics.py +++ b/addons/document_ics/document_ics.py @@ -80,7 +80,7 @@ class document_directory_content(osv.osv): 'obj_iterate': fields.boolean('Iterate object',help="If set, a separate instance will be created for each record of Object"), 'fname_field': fields.char("Filename field",size=16,help="The field of the object used in the filename. Has to be a unique identifier."), 'ics_domain': fields.char('Domain', size=64), - #'ics_field_ids': fields.one2many('document.directory.ics.fields', 'content_id', 'Fields Mapping') + 'ics_field_ids': fields.one2many('document.directory.ics.fields', 'content_id', 'Fields Mapping') } _defaults = { 'ics_domain': lambda *args: '[]' @@ -127,6 +127,11 @@ 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 = {} @@ -138,18 +143,117 @@ 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=ctx) 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: @@ -158,8 +262,37 @@ 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) - ctx.update({'model': content.object_id.model}) - s = obj_class.export_cal(cr, uid, ids, 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() return s document_directory_content() |
