Skip to content

Commit

Permalink
[MIG] base_dav: Migration to 12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarlr committed Apr 19, 2022
1 parent b595122 commit 5b4443b
Show file tree
Hide file tree
Showing 18 changed files with 254 additions and 93 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ sudo: false
cache: pip

python:
- "3.5"
- "3.6"

addons:
postgresql: "9.6"
Expand Down
11 changes: 6 additions & 5 deletions base_dav/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ Caldav and Carddav support
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--backend-lightgray.png?logo=github
:target: https://github.com/OCA/server-backend/tree/11.0/base_dav
:target: https://github.com/OCA/server-backend/tree/12.0/base_dav
:alt: OCA/server-backend
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-backend-11-0/server-backend-11-0-base_dav
:target: https://translation.odoo-community.org/projects/server-backend-12-0/server-backend-12-0-base_dav
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/253/11.0
:target: https://runbot.odoo-community.org/runbot/253/12.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|
Expand Down Expand Up @@ -60,7 +60,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-backend/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/server-backend/issues/new?body=module:%20base_dav%0Aversion:%2011.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/server-backend/issues/new?body=module:%20base_dav%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Expand All @@ -78,6 +78,7 @@ Contributors

* Holger Brunn <[email protected]>
* Florian Kantelberg <[email protected]>
* César López Ramírez <[email protected]>

Other credits
~~~~~~~~~~~~~
Expand All @@ -98,6 +99,6 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/server-backend <https://github.com/OCA/server-backend/tree/11.0/base_dav>`_ project on GitHub.
This module is part of the `OCA/server-backend <https://github.com/OCA/server-backend/tree/12.0/base_dav>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 1 addition & 1 deletion base_dav/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
{
"name": "Caldav and Carddav support",
"version": "11.0.1.0.0",
"version": "12.0.1.0.0",
"author": "initOS GmbH,Therp BV,Odoo Community Association (OCA)",
"license": "AGPL-3",
"category": "Extra Tools",
Expand Down
9 changes: 5 additions & 4 deletions base_dav/controllers/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Copyright 2018 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

import logging
from configparser import RawConfigParser as ConfigParser
import logging

import werkzeug
from odoo import http
Expand Down Expand Up @@ -30,10 +30,10 @@ def handle_well_known_request(self):
)
def handle_dav_request(self, davpath=None):
config = ConfigParser()
for section, values in radicale.config.INITIAL_CONFIG.items():
for section, values in radicale.config.DEFAULT_CONFIG_SCHEMA.items():
config.add_section(section)
for key, data in values.items():
config.set(section, key, data["value"])
config.set(section, key, data["value"] if type(data) == dict else data)
config.set('auth', 'type', 'odoo.addons.base_dav.radicale.auth')
config.set(
'storage', 'type', 'odoo.addons.base_dav.radicale.collection'
Expand All @@ -42,8 +42,9 @@ def handle_dav_request(self, davpath=None):
'rights', 'type', 'odoo.addons.base_dav.radicale.rights'
)
config.set('web', 'type', 'none')
request.httprequest.environ['wsgi.errors'] = logging.getLogger('radicale')
application = radicale.Application(
config, logging.getLogger('radicale'),
config
)

response = None
Expand Down
10 changes: 5 additions & 5 deletions base_dav/demo/dav_collection.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@
</record>
<record id="field_mapping_addressbook_n" model="dav.collection.field_mapping">
<field name="name">N</field>
<field name="field_id" ref="base.field_res_partner_name" />
<field name="field_id" ref="base.field_res_partner__name" />
<field name="collection_id" ref="collection_addressbook" />
</record>
<record id="field_mapping_addressbook_fn" model="dav.collection.field_mapping">
<field name="name">FN</field>
<field name="field_id" ref="base.field_res_partner_display_name" />
<field name="field_id" ref="base.field_res_partner__display_name" />
<field name="collection_id" ref="collection_addressbook" />
</record>
<record id="field_mapping_addressbook_photo" model="dav.collection.field_mapping">
<field name="name">photo</field>
<field name="field_id" ref="base.field_res_partner_image" />
<field name="field_id" ref="base.field_res_partner__image" />
<field name="collection_id" ref="collection_addressbook" />
</record>
<record id="field_mapping_addressbook_email" model="dav.collection.field_mapping">
<field name="name">email</field>
<field name="field_id" ref="base.field_res_partner_email" />
<field name="field_id" ref="base.field_res_partner__email" />
<field name="collection_id" ref="collection_addressbook" />
</record>
<record id="field_mapping_addressbook_tel" model="dav.collection.field_mapping">
<field name="name">tel</field>
<field name="field_id" ref="base.field_res_partner_phone" />
<field name="field_id" ref="base.field_res_partner__phone" />
<field name="collection_id" ref="collection_addressbook" />
</record>
</odoo>
28 changes: 15 additions & 13 deletions base_dav/models/dav_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

import os
import time
from operator import itemgetter
from urllib.parse import quote_plus

from odoo import api, fields, models, tools
import logging

import vobject

# pylint: disable=missing-import-error
from ..controllers.main import PREFIX
from ..radicale.collection import Collection, FileItem, Item

logger = logging.getLogger(__name__)


class DavCollection(models.Model):
_name = 'dav.collection'
Expand Down Expand Up @@ -88,6 +90,10 @@ def _eval_context(self):
'user': self.env.user,
}

@api.model
def get_logger(self):
return logger

@api.multi
def _eval_domain(self):
self.ensure_one()
Expand Down Expand Up @@ -155,16 +161,12 @@ def to_vobject(self, record):
if 'uid' not in vobj.contents:
vobj.add('uid').value = '%s,%s' % (record._name, record.id)
if 'rev' not in vobj.contents and 'write_date' in record._fields:
vobj.add('rev').value = record.write_date.\
replace(':', '').replace(' ', 'T').replace('.', '') + 'Z'
vobj.add('rev').value = record.write_date.strftime('%Y-%m-%dT%H%M%SZ')
return result

@api.model
def _odoo_to_http_datetime(self, value):
return time.strftime(
'%a, %d %b %Y %H:%M:%S GMT',
time.strptime(value, '%Y-%m-%d %H:%M:%S'),
)
return value.strftime('%a, %d %b %Y %H:%M:%S GMT')

@api.model
def _split_path(self, path):
Expand Down Expand Up @@ -212,7 +214,8 @@ def dav_list(self, collection, path_components):
uuid = record[self.field_uuid.name]
else:
uuid = str(record.id)
result.append('/' + '/'.join(path_components + [uuid]))
href = '/' + '/'.join(path_components + [uuid])
result.append(self.dav_get(collection, href))
return result

@api.multi
Expand All @@ -234,7 +237,6 @@ def dav_upload(self, collection, href, item):
if self.dav_type == 'files':
# TODO: Handle upload of attachments
return None

data = self.from_vobject(item)
record = self.get_record(components)

Expand All @@ -249,8 +251,8 @@ def dav_upload(self, collection, href, item):
record.write(data)

return Item(
collection,
item=self.to_vobject(record),
collection=collection,
vobject_item=self.to_vobject(record),
href=href,
last_modified=self._odoo_to_http_datetime(record.write_date),
)
Expand Down Expand Up @@ -294,8 +296,8 @@ def dav_get(self, collection, href):
return None

return Item(
collection,
item=self.to_vobject(record),
collection=collection,
vobject_item=self.to_vobject(record),
href=href,
last_modified=self._odoo_to_http_datetime(record.write_date),
)
5 changes: 3 additions & 2 deletions base_dav/models/dav_collection_field_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import datetime

from odoo import api, fields, models, tools
from odoo.tools.safe_eval import safe_eval

import dateutil
import vobject
Expand Down Expand Up @@ -67,7 +68,7 @@ def _from_vobject_code(self, child):
'tz': tz,
'vobject': vobject,
}
tools.safe_eval(self.import_code, context, mode="exec", nocopy=True)
safe_eval(self.import_code, context, mode="exec", nocopy=True)
return context.get('result', {})

@api.multi
Expand Down Expand Up @@ -137,7 +138,7 @@ def _to_vobject_code(self, record):
'tz': tz,
'vobject': vobject,
}
tools.safe_eval(self.export_code, context, mode="exec", nocopy=True)
safe_eval(self.export_code, context, mode="exec", nocopy=True)
return context.get('result', None)

@api.multi
Expand Down
5 changes: 3 additions & 2 deletions base_dav/radicale/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@


class Auth(BaseAuth):
def is_authenticated2(self, login, user, password):
def login(self, user, password):
env = request.env
uid = env['res.users']._login(env.cr.dbname, user, password)
login = request.env['res.users'].browse(uid).login
if uid:
request._env = env(user=uid)
return bool(uid)
return login
58 changes: 38 additions & 20 deletions base_dav/radicale/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import base64
import os
import time
from contextlib import contextmanager

from odoo.http import request

try:
from radicale.storage import BaseCollection, Item, get_etag
from radicale.storage import BaseCollection, BaseStorage
from radicale.item import Item, get_etag
from radicale import types
except ImportError:
BaseCollection = None
Item = None
Expand Down Expand Up @@ -37,22 +38,20 @@ def etag(self):
return get_etag(self.item.datas.decode('ascii'))


class Collection(BaseCollection):
@classmethod
def static_init(cls):
pass
class Storage(BaseStorage):

@classmethod
def _split_path(cls, path):
return list(filter(
None, os.path.normpath(path or '').strip('/').split('/')
))
@types.contextmanager
def acquire_lock(cls, mode, user=None):
"""We have a database for that"""
yield

@classmethod
def discover(cls, path, depth=None):
depth = int(depth or "0")
components = cls._split_path(path)
collection = cls(path)
collection = Collection(path)
collection.logger = collection.collection.get_logger()
if len(components) > 2:
# TODO: this probably better should happen in some dav.collection
# function
Expand All @@ -71,10 +70,23 @@ def discover(cls, path, depth=None):
yield collection.get(href)

@classmethod
@contextmanager
def acquire_lock(cls, mode, user=None):
"""We have a database for that"""
yield
def _split_path(cls, path):
return list(filter(
None, os.path.normpath(path or '').strip('/').split('/')
))

@classmethod
def create_collection(cls, href, collection=None, props=None):
return Collection(href)


class Collection(BaseCollection):

@classmethod
def _split_path(cls, path):
return list(filter(
None, os.path.normpath(path or '').strip('/').split('/')
))

@property
def env(self):
Expand All @@ -84,9 +96,13 @@ def env(self):
def last_modified(self):
return self._odoo_to_http_datetime(self.collection.create_date)

@property
def path(self):
return '/'.join(self.path_components) or '/'

def __init__(self, path):
self.path_components = self._split_path(path)
self.path = '/'.join(self.path_components) or '/'
self._path = '/'.join(self.path_components) or '/'
self.collection = self.env['dav.collection']
if len(self.path_components) >= 2 and str(
self.path_components[1]
Expand All @@ -98,7 +114,7 @@ def __init__(self, path):
def _odoo_to_http_datetime(self, value):
return time.strftime(
'%a, %d %b %Y %H:%M:%S GMT',
time.strptime(value, '%Y-%m-%d %H:%M:%S'),
value.timetuple(),
)

def get_meta(self, key=None):
Expand All @@ -117,16 +133,18 @@ def get_meta(self, key=None):
elif key == 'ICAL:calendar-color':
# TODO: set in dav.collection
return '#48c9f4'
elif key == 'C:calendar-description':
return self.collection.name
self.logger.warning('unsupported metadata %s', key)

def get(self, href):
return self.collection.dav_get(self, href)
def get_multi(self, hrefs):
return [self.collection.dav_get(self, href) for href in hrefs]

def upload(self, href, vobject_item):
return self.collection.dav_upload(self, href, vobject_item)

def delete(self, href):
return self.collection.dav_delete(self, self._split_path(href))

def list(self):
def get_all(self):
return self.collection.dav_list(self, self.path_components)
Loading

0 comments on commit 5b4443b

Please sign in to comment.