Просмотр исходного кода

Add user_cal config block with its own CalDAV credentials

UserCal now reads url/username/password/calendar_name from config.user_cal
instead of sharing the store's CalDAV connection. Drops DEFAULT_CALENDAR_NAME
constant from user_cal; the default 'House' lives in UserCalConfig.
ZiB 4 месяцев назад
Родитель
Сommit
387706e86c

+ 19 - 0
room_cal/_utils.py

@@ -321,6 +321,23 @@ class CalendarEntry:
     )
 
 
+@dataclass
+class UserCalConfig:
+  url: str = ''
+  username: str = ''
+  password: str = ''
+  calendar_name: str = 'House'
+
+  @classmethod
+  def from_dict(cls, d: dict) -> 'UserCalConfig':
+    return cls(
+      url=d.get('url', ''),
+      username=d.get('username', ''),
+      password=d.get('password', ''),
+      calendar_name=d.get('calendar_name', 'House'),
+    )
+
+
 @dataclass
 class CaldavStoreConfig:
   url: str = ''
@@ -407,6 +424,7 @@ class Config:
   ical_feeds: list[CalendarEntry] = field(default_factory=list)
   api_url: str | None = None
   store: StoreConfig = field(default_factory=StoreConfig)
+  user_cal: UserCalConfig = field(default_factory=UserCalConfig)
   reservation: ReservationConfig = field(default_factory=ReservationConfig)
   chat: ChatConfig = field(default_factory=ChatConfig)
 
@@ -420,6 +438,7 @@ class Config:
       ical_feeds=[CalendarEntry.from_dict(e) for e in d.get('ical_feeds', [])],
       api_url=d.get('api_url'),
       store=StoreConfig.from_dict(store_d),
+      user_cal=UserCalConfig.from_dict(d.get('user_cal', {})),
       reservation=ReservationConfig.from_dict(d.get('reservation', {})),
       chat=ChatConfig.from_dict(d.get('chat', {})),
     )

+ 6 - 5
room_cal/actions/fetch_user_cal.py

@@ -1,8 +1,8 @@
 from datetime import date
 
 from room_cal.closed import Closed, ClosedData
-from room_cal.user_cal import UserCal, DEFAULT_CALENDAR_NAME as USER_CAL_DEFAULT
-from room_cal._utils import FetchResult, safe_json_loads, services as _services, event_dates, drop_stale_events
+from room_cal.user_cal import UserCal
+from room_cal._utils import FetchResult, get_config, safe_json_loads, services as _services, event_dates, drop_stale_events
 
 ACTION_SPEC = ('fetch-user-cal', [])
 
@@ -23,7 +23,8 @@ async def fetch_user_cal(
   user_cal = UserCal()
   closed_cal = Closed()
   all_services = sorted(_services())
-  source = {'domain': _USER_CAL_SOURCE, 'name': 'User =>', 'id': USER_CAL_DEFAULT}
+  calendar_name = get_config().user_cal.calendar_name
+  source = {'domain': _USER_CAL_SOURCE, 'name': 'User =>', 'id': calendar_name}
 
   blocked: set[date] = set()
   for ev in await user_cal.find_closed(start_date, end_date):
@@ -36,10 +37,10 @@ async def fetch_user_cal(
   for e in existing_closed:
     xd = safe_json_loads(e.x_data)
     src = (xd or {}).get('source', {})
-    if src.get('domain') == _USER_CAL_SOURCE and src.get('id') == USER_CAL_DEFAULT:
+    if src.get('domain') == _USER_CAL_SOURCE and src.get('id') == calendar_name:
       existing_covered.add(e.start.date())
 
-  await drop_stale_events(existing_closed, blocked, domain=_USER_CAL_SOURCE, id_=USER_CAL_DEFAULT, cal=closed_cal)
+  await drop_stale_events(existing_closed, blocked, domain=_USER_CAL_SOURCE, id_=calendar_name, cal=closed_cal)
   closed_by_date = {e.start.date(): e for e in existing_closed}
 
   synced_closed: list[date] = []

+ 6 - 0
room_cal/config-all.json

@@ -53,6 +53,12 @@
       "db_path": "store.db"
     }
   },
+  "user_cal": {
+    "url": "",
+    "username": "",
+    "password": "",
+    "calendar_name": "House"
+  },
   "reservation": {
     "default": {
       "wifi_key": "",

+ 6 - 0
room_cal/config.json

@@ -50,6 +50,12 @@
     }
   ],
   "api_url": "http://localhost:8000/",
+  "user_cal": {
+    "url": "",
+    "username": "",
+    "password": "",
+    "calendar_name": "House"
+  },
   "reservation": {
     "default": {
       "wifi_key": "",

+ 5 - 5
room_cal/user_cal/__init__.py

@@ -1,17 +1,17 @@
 import json
 from datetime import date
 from room_cal.store import StoreBacked
-from room_cal.store._caldav import CalDAVBackend, get_calendar
-from room_cal._utils import EventSource, safe_json_loads, services as _services, service_name
+from room_cal.store._caldav import CalDAVBackend, Calendar
+from room_cal._utils import EventSource, get_config, safe_json_loads, services as _services, service_name
 
-DEFAULT_CALENDAR_NAME = 'House'
 _ROOM_CLOSED_TITLE = 'Room Closed'
 
 
 class UserCal(StoreBacked, EventSource):
-  def __init__(self, calendar_name: str = DEFAULT_CALENDAR_NAME, *, backend=None):
+  def __init__(self, *, backend=None):
     if backend is None:
-      backend = CalDAVBackend(get_calendar(calendar_name))
+      cfg = get_config().user_cal
+      backend = CalDAVBackend(Calendar(cfg.calendar_name, url=cfg.url, username=cfg.username, password=cfg.password))
     StoreBacked.__init__(self, backend=backend)
     EventSource.__init__(self, 'user_cal')