Skip to content

Commit 4766ca8

Browse files
authored
Merge pull request #159 from elliot-100/refactors
Refactors
2 parents 0c55161 + a11db8b commit 4766ca8

File tree

3 files changed

+71
-69
lines changed

3 files changed

+71
-69
lines changed

spond/_event_template.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Module contains template event data, to be used as a base when updating events."""
2+
3+
from spond import JSONDict
4+
5+
_EVENT_TEMPLATE: JSONDict = {
6+
"heading": None,
7+
"description": None,
8+
"spondType": "EVENT",
9+
"startTimestamp": None,
10+
"endTimestamp": None,
11+
"commentsDisabled": False,
12+
"maxAccepted": 0,
13+
"rsvpDate": None,
14+
"location": {
15+
"id": None,
16+
"feature": None,
17+
"address": None,
18+
"latitude": None,
19+
"longitude": None,
20+
},
21+
"owners": [{"id": None}],
22+
"visibility": "INVITEES",
23+
"participantsHidden": False,
24+
"autoReminderType": "DISABLED",
25+
"autoAccept": False,
26+
"payment": {},
27+
"attachments": [],
28+
"id": None,
29+
"tasks": {
30+
"openTasks": [],
31+
"assignedTasks": [
32+
{
33+
"name": None,
34+
"description": "",
35+
"type": "ASSIGNED",
36+
"id": None,
37+
"adultsOnly": True,
38+
"assignments": {"memberIds": [], "profiles": [], "remove": []},
39+
}
40+
],
41+
},
42+
}

spond/club.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING
3+
from typing import TYPE_CHECKING, ClassVar
44

55
from .base import _SpondBase
66

@@ -9,8 +9,11 @@
99

1010

1111
class SpondClub(_SpondBase):
12+
13+
_API_BASE_URL: ClassVar = "https://api.spond.com/club/v1/"
14+
1215
def __init__(self, username: str, password: str) -> None:
13-
super().__init__(username, password, "https://api.spond.com/club/v1/")
16+
super().__init__(username, password, self._API_BASE_URL)
1417
self.transactions: list[JSONDict] | None = None
1518

1619
@_SpondBase.require_authentication

spond/spond.py

+24-67
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from typing import TYPE_CHECKING, ClassVar
66

7+
from ._event_template import _EVENT_TEMPLATE
78
from .base import _SpondBase
89

910
if TYPE_CHECKING:
@@ -14,13 +15,14 @@
1415

1516
class Spond(_SpondBase):
1617

17-
DT_FORMAT = "%Y-%m-%dT00:00:00.000Z"
18-
18+
_API_BASE_URL: ClassVar = "https://api.spond.com/core/v1/"
19+
_DT_FORMAT: ClassVar = "%Y-%m-%dT00:00:00.000Z"
20+
_EVENT_TEMPLATE: ClassVar = _EVENT_TEMPLATE
1921
_EVENT: ClassVar = "event"
2022
_GROUP: ClassVar = "group"
2123

2224
def __init__(self, username: str, password: str) -> None:
23-
super().__init__(username, password, "https://api.spond.com/core/v1/")
25+
super().__init__(username, password, self._API_BASE_URL)
2426
self._chat_url = None
2527
self._auth = None
2628
self.groups: list[JSONDict] | None = None
@@ -98,29 +100,24 @@ async def get_person(self, user: str) -> JSONDict:
98100
await self.get_groups()
99101
for group in self.groups:
100102
for member in group["members"]:
101-
if (
102-
member["id"] == user
103-
or ("email" in member and member["email"]) == user
104-
or member["firstName"] + " " + member["lastName"] == user
105-
or ("profile" in member and member["profile"]["id"] == user)
106-
):
103+
if self._match_person(member, user):
107104
return member
108105
if "guardians" in member:
109106
for guardian in member["guardians"]:
110-
if (
111-
guardian["id"] == user
112-
or ("email" in guardian and guardian["email"]) == user
113-
or guardian["firstName"] + " " + guardian["lastName"]
114-
== user
115-
or (
116-
"profile" in guardian
117-
and guardian["profile"]["id"] == user
118-
)
119-
):
107+
if self._match_person(guardian, user):
120108
return guardian
121109
errmsg = f"No person matched with identifier '{user}'."
122110
raise KeyError(errmsg)
123111

112+
@staticmethod
113+
def _match_person(person: JSONDict, match_str: str) -> bool:
114+
return (
115+
person["id"] == match_str
116+
or ("email" in person and person["email"]) == match_str
117+
or person["firstName"] + " " + person["lastName"] == match_str
118+
or ("profile" in person and person["profile"]["id"] == match_str)
119+
)
120+
124121
@_SpondBase.require_authentication
125122
async def get_messages(self, max_chats: int = 100) -> list[JSONDict] | None:
126123
"""
@@ -213,7 +210,7 @@ async def send_message(
213210

214211
if chat_id is not None:
215212
return self._continue_chat(chat_id, text)
216-
elif group_uid is None or user is None:
213+
if group_uid is None or user is None:
217214
return {
218215
"error": "wrong usage, group_id and user_id needed or continue chat with chat_id"
219216
}
@@ -293,13 +290,13 @@ async def get_events(
293290
"scheduled": str(include_scheduled),
294291
}
295292
if max_end:
296-
params["maxEndTimestamp"] = max_end.strftime(self.DT_FORMAT)
293+
params["maxEndTimestamp"] = max_end.strftime(self._DT_FORMAT)
297294
if max_start:
298-
params["maxStartTimestamp"] = max_start.strftime(self.DT_FORMAT)
295+
params["maxStartTimestamp"] = max_start.strftime(self._DT_FORMAT)
299296
if min_end:
300-
params["minEndTimestamp"] = min_end.strftime(self.DT_FORMAT)
297+
params["minEndTimestamp"] = min_end.strftime(self._DT_FORMAT)
301298
if min_start:
302-
params["minStartTimestamp"] = min_start.strftime(self.DT_FORMAT)
299+
params["minStartTimestamp"] = min_start.strftime(self._DT_FORMAT)
303300
if group_id:
304301
params["groupId"] = group_id
305302
if subgroup_id:
@@ -353,54 +350,15 @@ async def update_event(self, uid: str, updates: JSONDict):
353350
event = await self._get_entity(self._EVENT, uid)
354351
url = f"{self.api_url}sponds/{uid}"
355352

356-
base_event: JSONDict = {
357-
"heading": None,
358-
"description": None,
359-
"spondType": "EVENT",
360-
"startTimestamp": None,
361-
"endTimestamp": None,
362-
"commentsDisabled": False,
363-
"maxAccepted": 0,
364-
"rsvpDate": None,
365-
"location": {
366-
"id": None,
367-
"feature": None,
368-
"address": None,
369-
"latitude": None,
370-
"longitude": None,
371-
},
372-
"owners": [{"id": None}],
373-
"visibility": "INVITEES",
374-
"participantsHidden": False,
375-
"autoReminderType": "DISABLED",
376-
"autoAccept": False,
377-
"payment": {},
378-
"attachments": [],
379-
"id": None,
380-
"tasks": {
381-
"openTasks": [],
382-
"assignedTasks": [
383-
{
384-
"name": None,
385-
"description": "",
386-
"type": "ASSIGNED",
387-
"id": None,
388-
"adultsOnly": True,
389-
"assignments": {"memberIds": [], "profiles": [], "remove": []},
390-
}
391-
],
392-
},
393-
}
394-
353+
base_event = self._EVENT_TEMPLATE.copy()
395354
for key in base_event:
396355
if event.get(key) is not None and not updates.get(key):
397356
base_event[key] = event[key]
398357
elif updates.get(key) is not None:
399358
base_event[key] = updates[key]
400359

401-
data = dict(base_event)
402360
async with self.clientsession.post(
403-
url, json=data, headers=self.auth_headers
361+
url, json=base_event, headers=self.auth_headers
404362
) as r:
405363
self.events_update = await r.json()
406364
return self.events
@@ -421,8 +379,7 @@ async def get_event_attendance_xlsx(self, uid: str) -> bytes:
421379
"""
422380
url = f"{self.api_url}sponds/{uid}/export"
423381
async with self.clientsession.get(url, headers=self.auth_headers) as r:
424-
output_data = await r.read()
425-
return output_data
382+
return await r.read()
426383

427384
@_SpondBase.require_authentication
428385
async def change_response(self, uid: str, user: str, payload: JSONDict) -> JSONDict:

0 commit comments

Comments
 (0)