-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added CalendarAPI with the help of caldav
Signed-off-by: Alexander Piskun <[email protected]>
- Loading branch information
Showing
11 changed files
with
120 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
.. py:currentmodule:: nc_py_api.calendar | ||
Calendar API | ||
============ | ||
|
||
.. note:: To make this API work you should install **nc_py_api** with **calendar** extra dependency. | ||
|
||
.. code-block:: python | ||
principal = nc.cal.principal() | ||
calendars = principal.calendars() # get list of calendars | ||
``nc.cal`` is usual ``caldav.DAVClient`` object with the same API. | ||
|
||
Documentation for ``caldav`` can be found here: `CalDAV <"https://caldav.readthedocs.io/en/latest">`_ | ||
|
||
.. class:: _CalendarAPI | ||
|
||
Class that encapsulates ``caldav.DAVClient``. Avalaible as **cal** in the Nextcloud class. | ||
|
||
.. note:: You should not call ``close`` or ``request`` methods of CalendarAPI, they will be removed somewhere | ||
in the future when ``caldav.DAVClient`` will be rewritten(API compatability will remains). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,5 @@ Reference | |
Exceptions | ||
Talk | ||
TalkBot | ||
Calendar | ||
Session |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
"""Version of nc_py_api.""" | ||
|
||
__version__ = "0.3.0" | ||
__version__ = "0.3.1.dev0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
"""Nextcloud Calendar DAV API.""" | ||
|
||
from ._session import NcSessionBasic | ||
|
||
try: | ||
from caldav.davclient import DAVClient, DAVResponse | ||
|
||
class _CalendarAPI(DAVClient): | ||
"""Class that encapsulates ``caldav.DAVClient`` to work with the Nextcloud calendar.""" | ||
|
||
def __init__(self, session: NcSessionBasic): | ||
self._session = session | ||
super().__init__(session.cfg.dav_endpoint) | ||
|
||
@property | ||
def available(self) -> bool: | ||
"""Returns True if ``caldav`` package is avalaible, False otherwise.""" | ||
return True | ||
|
||
def request(self, url, method="GET", body="", headers={}): # noqa pylint: disable=dangerous-default-value | ||
if isinstance(body, str): | ||
body = body.encode("UTF-8") | ||
if body: | ||
body = body.replace(b"\n", b"\r\n").replace(b"\r\r\n", b"\r\n") | ||
r = self._session.dav(method, url, data=body, headers=headers) | ||
return DAVResponse(r) | ||
|
||
except ImportError: | ||
|
||
class _CalendarAPI: # type: ignore | ||
"""A stub class in case **caldav** is missing.""" | ||
|
||
def __init__(self, session: NcSessionBasic): | ||
self._session = session | ||
|
||
@property | ||
def available(self) -> bool: | ||
"""Returns True if ``caldav`` package is avalaible, False otherwise.""" | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import datetime | ||
|
||
import pytest | ||
|
||
|
||
def test_create_delete(nc_client): | ||
if nc_client.cal.available is False: | ||
pytest.skip("``caldav`` package is not installed") | ||
|
||
principal = nc_client.cal.principal() | ||
calendars = principal.calendars() | ||
assert calendars | ||
calendar = calendars[0] | ||
all_events_before = calendar.events() | ||
event = calendar.save_event( | ||
dtstart=datetime.datetime.now(), | ||
dtend=datetime.datetime.now() + datetime.timedelta(hours=1), | ||
summary="NcPyApi + CalDAV test", | ||
) | ||
all_events_after = calendar.events() | ||
assert len(all_events_after) == len(all_events_before) + 1 | ||
event.delete() | ||
assert len(calendar.events()) == len(all_events_before) |