Skip to content

Commit

Permalink
[IMP] base_ical: Allow advanced snippets. Use apikeys with scope.
Browse files Browse the repository at this point in the history
  • Loading branch information
fkantelberg committed Apr 23, 2024
1 parent 1e6b6ce commit e2a132a
Show file tree
Hide file tree
Showing 16 changed files with 565 additions and 201 deletions.
62 changes: 42 additions & 20 deletions base_ical/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
Readonly publishing of calendars
================================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:f98a0b075e11b8bf4075af16bbe8ba8b249d03f4b675874d232c68a897235579
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png
:target: https://odoo-community.org/page/development-status
Expand All @@ -23,10 +20,10 @@ Readonly publishing of calendars
:target: https://translation.odoo-community.org/projects/server-backend-15-0/server-backend-15-0-base_ical
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/server-backend&target_branch=15.0
:target: https://runboat.odoo-community.org/webui/builds.html?repo=OCA/server-backend&target_branch=15.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|
|badge1| |badge2| |badge3| |badge4| |badge5|

This module allows administrators to configure iCalendars based on an arbitrary selection on arbitrary models.

Expand All @@ -50,13 +47,45 @@ Configuration
To configure this module, you need to:

#. Go to Settings/Technical/iCalendars
#. Create a calendar, fill in the model you want to expose and possibly a domain to restrict records. You can use the ``user`` variable to restrict things relative to the user using the calendar
#. A few iCalendar-fields have defaults that should work for any model, you'll have to fill in expressions manually though for the start and end date of the records.
#. Create a iCalendar, fill in the model you want to expose and possibly a domain to restrict records. You can use the ``user`` variable to restrict things relative to the user using the iCalendar
#. A iCalendar is only available to the allowed users. Use the `Allow automatically` to make the iCalendar available to all users
#. See the examples below for a start

Examples
~~~~~~~~

Simple example, for model ``calendar.event``, you'd fill in ``record.allday and record.start_date or record.start`` as `DTSTART` and ``record.allday and record.stop_date or record.stop`` as `DTEND`.

Advanced example, for model ``calendar.event``, you'd use ``calendar = record._get_ics_file()`` in the code.

Advanced example, for model ``hr.leave``, you can use the following code and ``[("employee_id.user_id", "=", user.id)]`` in the `domain` to export the own time offs. This is a bit more complex because of the way Odoo handles the begin and end times of leaves, and you'll want the extra day as most clients interpret the end date as non-inclusive.:

.. code-block:: python
For example, for model ``calendar.event``, you'd fill in ``record.allday and record.start_date or record.start`` as `DTSTART` and ``record.allday and record.stop_date or record.stop`` as `DTEND`.
confirmed = ("validate", "validate1")
if record.request_unit_half or record.request_unit_hours:
event = {
"dtstart": event["dtstart"].date(),
"dtend": event["dtend"].date() + timedelta(days=1),
}
else:
event = {
"dtstart": record.date_from,
"dtend": record.date_to,
}
For model ``hr.leave``, you'd write ``(record.request_unit_half or record.request_unit_hours) and record.date_from or record.date_from.date()`` for `DTSTART` and ``(record.request_unit_half or record.request_unit_hours) and record.date_to or (record.date_to.date() + timedelta(days=1))`` for `DTEND` - this is a bit more complex because of the way Odoo handles the begin and end times of leaves, and you'll want the extra day as most clients interpret the end date as non-inclusive.
#. Existing calendars are available for users in the tab `Calendars` of their profile form, where they can enable them to obtain a link they can paste into whatever client they are going to use
event["summary"] = record.name
event["status"] = "CONFIRMED" if record.state in confirmed else "TENTATIVE"
Advanced example, for model ``mail.activity``, you can use the following code and ``[("user_id", "=", user.id)]`` and `domain` to export all user activities.

.. code-block:: python
todo = {
"summary": record.display_name,
"due": record.date_deadline,
"description": html2plaintext(record.note) if record.note else ""
}
Usage
=====
Expand All @@ -78,7 +107,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 to smash it by providing a detailed and welcomed
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_ical%0Aversion:%2015.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 @@ -95,14 +124,7 @@ Contributors
~~~~~~~~~~~~

* Holger Brunn <mail@hunki-enterprises.com> (https://hunki-enterprises.com)

Other credits
~~~~~~~~~~~~~

The development of this module has been financially supported by:

* Company 1 name
* Company 2 name
* Florian Kantelberg <florian.kantelberg@initos.com> (https://www.initos.com)

Maintainers
~~~~~~~~~~~
Expand Down
1 change: 0 additions & 1 deletion base_ical/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"web",
],
"data": [
"security/base_ical.xml",
"security/ir.model.access.csv",
"views/base_ical.xml",
"views/res_users.xml",
Expand Down
51 changes: 39 additions & 12 deletions base_ical/controllers/main.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,54 @@
# Copyright 2023 Hunki Enterprises BV
# Copyright 2024 initOS GmbH
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0)

import logging

import werkzeug.wrappers

from odoo import http
from odoo.http import request

_logger = logging.getLogger(__name__)


class MyController(http.Controller):
class ICalController(http.Controller):
@http.route(
"/base_ical/<string:token>",
auth="public",
"/base_ical/<int:calendar_id>",
auth="none",
csrf=False,
methods=["GET"],
type="http",
)
def get_ical(self, token):
# TODO: respect if-modified-since headers
token = (
http.request.env["base.ical.token"].sudo().search([("token", "=", token)])
def get_ical(self, calendar_id, access_token=None):
if not access_token:
raise request.not_found()

user_id = request.env["res.users.apikeys"]._check_credentials(
scope=f"odoo.plugin.ical.{calendar_id}",
key=access_token,
)
if not token:
return http.request.not_found()
response = http.request.make_response(
token.ical_id.with_user(token.user_id)._get_ical(),
headers={"content-type": "text/calendar"},
if not user_id:
raise request.not_found()

calendar = (
http.request.env["base.ical"]
.sudo()
.search([("id", "=", calendar_id)])
.with_user(user_id)
)
if not calendar:
return request.not_found()

records = calendar._get_items()
response = werkzeug.wrappers.Response()
if records:
response.last_modified = max(records.mapped("write_date"))

response.make_conditional(request.httprequest)
if response.status_code == 304:
return response

response.mimetype = "text/calendar"
response.data = calendar._get_ical(records)
return response
4 changes: 1 addition & 3 deletions base_ical/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
from . import res_users
from . import base_ical
from . import base_ical_token
from . import base_ical, base_ical_url_description, base_ical_url_show, res_users
Loading

0 comments on commit e2a132a

Please sign in to comment.