diff --git a/api/ovdashboard_api/dav_calendar.py b/api/ovdashboard_api/dav_calendar.py index 88c302a..8825918 100644 --- a/api/ovdashboard_api/dav_calendar.py +++ b/api/ovdashboard_api/dav_calendar.py @@ -12,6 +12,7 @@ from typing import Iterator from caldav import Calendar from caldav.lib.error import ReportError +from isodate import parse_duration from pydantic import BaseModel, validator from vobject.base import Component @@ -82,14 +83,35 @@ class CalEvent(BaseModel): """ data = {} + keys = ("summary", "description", "dtstart", "dtend", "duration") - for key in cls().dict().keys(): + for key in keys: try: data[key] = event.contents[key][0].value # type: ignore except KeyError: pass + if "dtend" not in data: + data["dtend"] = data["dtstart"] + + if "duration" in data: + try: + dtstart = datetime.fromisoformat(data["dtstart"]) + duration = parse_duration(data["duration"]) + dtend = dtstart + duration + + data["dtend"] = dtend.isoformat() + + except (ValueError, TypeError, AttributeError): + _logger.warn( + "Could not add duration %s to %s", + repr(data["duration"]), + repr(data["dtstart"]), + ) + + data["dtend"] = data["dtstart"] + return cls.parse_obj(data) diff --git a/api/poetry.lock b/api/poetry.lock index 7f35829..a72199f 100644 --- a/api/poetry.lock +++ b/api/poetry.lock @@ -127,6 +127,17 @@ docs = ["jaraco.packaging (>=9)", "rst.linker (>=1.9)", "sphinx"] perf = ["ipython"] testing = ["flufl.flake8", "importlib-resources (>=1.3)", "packaging", "pyfakefs", "pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=1.3)", "pytest-flake8", "pytest-mypy (>=0.9.1)", "pytest-perf (>=0.9.2)"] +[[package]] +name = "isodate" +version = "0.6.1" +description = "An ISO 8601 date/time/duration parser and formatter" +category = "main" +optional = false +python-versions = "*" + +[package.dependencies] +six = "*" + [[package]] name = "lxml" version = "4.9.1" @@ -396,7 +407,7 @@ testing = ["func-timeout", "jaraco.itertools", "pytest (>=6)", "pytest-black (>= [metadata] lock-version = "1.1" python-versions = "^3.9" -content-hash = "b25d37c0bf9187d599709a66e05d21c0df7da2b140373f6cd50b070ae2f073ab" +content-hash = "6fb83bae86629a411b61f4964ac137fdaec272a89d6b02db6938269a8d1eef91" [metadata.files] anyio = [ @@ -443,6 +454,10 @@ importlib-metadata = [ {file = "importlib_metadata-4.12.0-py3-none-any.whl", hash = "sha256:7401a975809ea1fdc658c3aa4f78cc2195a0e019c5cbc4c06122884e9ae80c23"}, {file = "importlib_metadata-4.12.0.tar.gz", hash = "sha256:637245b8bab2b6502fcbc752cc4b7a6f6243bb02b31c5c26156ad103d3d45670"}, ] +isodate = [ + {file = "isodate-0.6.1-py2.py3-none-any.whl", hash = "sha256:0751eece944162659049d35f4f549ed815792b38793f07cf73381c1c87cbed96"}, + {file = "isodate-0.6.1.tar.gz", hash = "sha256:48c5881de7e8b0a0d648cb024c8062dc84e7b840ed81e864c7614fd3c127bde9"}, +] lxml = [ {file = "lxml-4.9.1-cp27-cp27m-macosx_10_15_x86_64.whl", hash = "sha256:98cafc618614d72b02185ac583c6f7796202062c41d2eeecdf07820bad3295ed"}, {file = "lxml-4.9.1-cp27-cp27m-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:c62e8dd9754b7debda0c5ba59d34509c4688f853588d75b53c3791983faa96fc"}, diff --git a/api/pyproject.toml b/api/pyproject.toml index 7d73f01..cc2a6c3 100644 --- a/api/pyproject.toml +++ b/api/pyproject.toml @@ -18,6 +18,7 @@ tomli = "^2.0.1" tomli-w = "^1.0.0" uvicorn = "^0.18.3" webdavclient3 = "3.14.5" +isodate = "^0.6.1" [tool.poetry.dev-dependencies] # pytest = "^5.2"