From 17fa3bba0c7fbe208c4ee37600d510daf3aa01f3 Mon Sep 17 00:00:00 2001 From: Fabien BOURGEOIS Date: Thu, 10 May 2018 11:38:28 +0200 Subject: [PATCH] [IMP][WIP]Odoo storage plugin : CARDDav (RO) is enabled and works with several clients --- radicale_odoo_storage/__init__.py | 94 +++++++++++++++++-------------- 1 file changed, 52 insertions(+), 42 deletions(-) diff --git a/radicale_odoo_storage/__init__.py b/radicale_odoo_storage/__init__.py index d617adf..fddfefb 100644 --- a/radicale_odoo_storage/__init__.py +++ b/radicale_odoo_storage/__init__.py @@ -96,7 +96,6 @@ class Collection(BaseCollection): def odoo_init(self): """ Init Odoo collections if not found """ - # TODO: disallow collection deletion ? user_ids = self.odoo.env['res.users'].search([]) users = self.odoo.execute('res.users', 'read', user_ids, ['login', 'email']) for user in users: @@ -149,10 +148,14 @@ class Collection(BaseCollection): (path, depth, cls.user, attributes)) yield cls(path) if len(attributes) == 1: # Got all if root is needed - contact_path = '%s/odoo-contact' % path - calendar_path = '%s/odoo-calendar' % path + contact_path = '%sodoo-contact' % path + calendar_path = '%sodoo-calendar' % path yield cls(contact_path) yield cls(calendar_path) + elif len(attributes) == 2: # Then we need children + collection = cls(path) + for item in collection.list(): + yield collection.get(item) def get_meta(self, key=None): """Get metadata value for collection """ @@ -171,48 +174,71 @@ class Collection(BaseCollection): """ Gets all contacts available from one Odoo login """ cls.logger.info('Get contacts for Odoo user %s' % login) partner_ids = cls.odoo.env['res.partner'].search([]) - cls.logger.debug(partner_ids) return ['res.partner:%s' % pid for pid in partner_ids] + def sync(self, old_token=None): + """ Debug purpose """ + token, ilist = super(Collection, self).sync(old_token) + self.logger.debug('Sync token : %s' % token) + self.logger.debug('Sync list : %s' % ilist) + return token, ilist + def list(self): """List collection items.""" # TODO : get all ICS from Odoo... - self.logger.warning('List collection %s' % self.path) - self.logger.warning('Collection tag %s' % self.tag) + self.logger.debug('List collection %s' % self.path) + self.logger.debug('Collection tag %s' % self.tag) if self.tag: if self.tag == 'VADDRESSBOOK': for oid in self.__class__.get_contacts_from_odoo(self.owner): - yield oid - # for item in self.collection.list(): - # yield item.uid + self.content_suffix + yield oid + self.content_suffix def _get_with_metadata(self, href): """Fetch a single item from Odoo database""" model, database_id = href.split(':') - fields = ['name', 'write_date', 'comment'] - data = self.odoo.execute(model, 'read', [int(database_id)], fields)[0] - self.logger.warning(data) + database_id = int(database_id[:-len(self.content_suffix)]) + # fields = ['name', 'write_date', 'comment', 'street', 'street2', 'zip', + # 'city', 'state_id' 'country_id'] + # data = self.odoo.execute(model, 'read', [database_id], fields)[0] + data = self.odoo.env[model].browse([database_id]) if model == 'res.partner': # last_modified = strftime("%a, %d %b %Y %H:%M:%S GMT", # strptime(data.get('write_date'), '%Y-%m-%d %H:%M:%S')) - last_modified = strftime("%Y-%m-%dT%H:%M:%SZ", - strptime(data.get('write_date'), '%Y-%m-%d %H:%M:%S')) - self.logger.warning(last_modified) + last_modified = str(data.write_date) vobject_item = vobject.vCard() vobject_item.add('n') - vobject_item.n.value = vobject.vcard.Name(family=data.get('name')) vobject_item.add('fn') - vobject_item.fn.value = data.get('name') - vobject_item.add('uid').value = database_id + vobject_item.fn.value = data.name + vobject_item.add('adr') + state_name = data.state_id.name if data.state_id else '' + country_name = data.country_id.name if data.country_id else '' + vobject_item.adr.value = vobject.vcard.Address( + street=' '.join([data.street or '', data.street2 or '']), + code=data.zip or '', city=data.city or '', + region=state_name, country=country_name) + if data.image: + vobject_item.add('photo;encoding=b;type=jpeg').value = data.image.replace('\n', '') + if data.phone: + vobject_item.add('tel').value = data.phone + if data.email: + vobject_item.add('email').value = data.email + if data.website: + vobject_item.add('url').value = data.website + if data.function: + vobject_item.add('role').value = data.function + if data.comment: + vobject_item.add('note').value = data.comment + if data.category_id: + categs = [categ.name for categ in data.category_id] + vobject_item.add('categories').value = categs + if data.parent_id: + vobject_item.add('org').value = [data.parent_id.name] + vobject_item.add('uid').value = href vobject_item.add('rev').value = last_modified - self.logger.warning(vobject_item.name) - self.logger.warning([c for c in vobject_item.components()]) tag, start, end = xmlutils.find_tag_and_time_range(vobject_item) - self.logger.warning('Tag, start, end : %s %s %s' % (tag, start, end)) text = vobject_item.serialize() etag = get_etag(text) uid = get_uid_from_object(vobject_item) - self.logger.warning('Text, ETAG, UID : %s %s %s' % (text, etag, uid)) return Item( self, href=href, last_modified=last_modified, etag=etag, text=text, item=vobject_item, uid=href, @@ -224,17 +250,10 @@ class Collection(BaseCollection): def get(self, href, verify_href=True): item, metadata = self._get_with_metadata(href) - self.logger.warning(item) - self.logger.warning(item.serialize()) - self.logger.warning(item.name) - self.logger.warning(item.last_modified) - self.logger.warning([c for c in item.components()]) - self.logger.warning(metadata) return item def delete(self, href=None): """ Can not delete collection but item, yes """ - self.logger.warning(href) if href is None: # Delete the collection self.logger.error('Attempt to delete collection %s' % self.path) @@ -243,21 +262,12 @@ class Collection(BaseCollection): # Delete an item raise NotImplementedError - # def serialize(self): - # """ Get the whole collection unicode """ - # # TODO: CALENDAR - # self.logger.warning('From serialize : %s' % self.tag) - # if self.tag == "VADDRESSBOOK": - # self.logger.warning('HERE') - # self.logger.warning([item.serialize() for item in self.get_all()]) - # return ''.join((item.serialize() for item in self.get_all())) - # return '' - @property def last_modified(self): """ Return last modified """ last = self.odoo.env[self.odoo_model].search([], limit=1, order='write_date desc') last_fields = self.odoo.execute(self.odoo_model, 'read', last, ['write_date'])[0] - self.logger.info(last_fields) - return strftime("%a, %d %b %Y %H:%M:%S GMT", - strptime(last_fields.get('write_date'), '%Y-%m-%d %H:%M:%S')) + self.logger.debug(last_fields) + # return strftime("%a, %d %b %Y %H:%M:%S GMT", + # strptime(last_fields.get('write_date'), '%Y-%m-%d %H:%M:%S')) + return str(last_fields['write_date'])