Skip to content

Commit

Permalink
Merge pull request #159 from elliot-100/refactors
Browse files Browse the repository at this point in the history
Refactors
  • Loading branch information
elliot-100 authored Oct 29, 2024
2 parents 0c55161 + a11db8b commit 4766ca8
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 69 deletions.
42 changes: 42 additions & 0 deletions spond/_event_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Module contains template event data, to be used as a base when updating events."""

from spond import JSONDict

_EVENT_TEMPLATE: JSONDict = {
"heading": None,
"description": None,
"spondType": "EVENT",
"startTimestamp": None,
"endTimestamp": None,
"commentsDisabled": False,
"maxAccepted": 0,
"rsvpDate": None,
"location": {
"id": None,
"feature": None,
"address": None,
"latitude": None,
"longitude": None,
},
"owners": [{"id": None}],
"visibility": "INVITEES",
"participantsHidden": False,
"autoReminderType": "DISABLED",
"autoAccept": False,
"payment": {},
"attachments": [],
"id": None,
"tasks": {
"openTasks": [],
"assignedTasks": [
{
"name": None,
"description": "",
"type": "ASSIGNED",
"id": None,
"adultsOnly": True,
"assignments": {"memberIds": [], "profiles": [], "remove": []},
}
],
},
}
7 changes: 5 additions & 2 deletions spond/club.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, ClassVar

from .base import _SpondBase

Expand All @@ -9,8 +9,11 @@


class SpondClub(_SpondBase):

_API_BASE_URL: ClassVar = "https://api.spond.com/club/v1/"

def __init__(self, username: str, password: str) -> None:
super().__init__(username, password, "https://api.spond.com/club/v1/")
super().__init__(username, password, self._API_BASE_URL)
self.transactions: list[JSONDict] | None = None

@_SpondBase.require_authentication
Expand Down
91 changes: 24 additions & 67 deletions spond/spond.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from typing import TYPE_CHECKING, ClassVar

from ._event_template import _EVENT_TEMPLATE
from .base import _SpondBase

if TYPE_CHECKING:
Expand All @@ -14,13 +15,14 @@

class Spond(_SpondBase):

DT_FORMAT = "%Y-%m-%dT00:00:00.000Z"

_API_BASE_URL: ClassVar = "https://api.spond.com/core/v1/"
_DT_FORMAT: ClassVar = "%Y-%m-%dT00:00:00.000Z"
_EVENT_TEMPLATE: ClassVar = _EVENT_TEMPLATE
_EVENT: ClassVar = "event"
_GROUP: ClassVar = "group"

def __init__(self, username: str, password: str) -> None:
super().__init__(username, password, "https://api.spond.com/core/v1/")
super().__init__(username, password, self._API_BASE_URL)
self._chat_url = None
self._auth = None
self.groups: list[JSONDict] | None = None
Expand Down Expand Up @@ -98,29 +100,24 @@ async def get_person(self, user: str) -> JSONDict:
await self.get_groups()
for group in self.groups:
for member in group["members"]:
if (
member["id"] == user
or ("email" in member and member["email"]) == user
or member["firstName"] + " " + member["lastName"] == user
or ("profile" in member and member["profile"]["id"] == user)
):
if self._match_person(member, user):
return member
if "guardians" in member:
for guardian in member["guardians"]:
if (
guardian["id"] == user
or ("email" in guardian and guardian["email"]) == user
or guardian["firstName"] + " " + guardian["lastName"]
== user
or (
"profile" in guardian
and guardian["profile"]["id"] == user
)
):
if self._match_person(guardian, user):
return guardian
errmsg = f"No person matched with identifier '{user}'."
raise KeyError(errmsg)

@staticmethod
def _match_person(person: JSONDict, match_str: str) -> bool:
return (
person["id"] == match_str
or ("email" in person and person["email"]) == match_str
or person["firstName"] + " " + person["lastName"] == match_str
or ("profile" in person and person["profile"]["id"] == match_str)
)

@_SpondBase.require_authentication
async def get_messages(self, max_chats: int = 100) -> list[JSONDict] | None:
"""
Expand Down Expand Up @@ -213,7 +210,7 @@ async def send_message(

if chat_id is not None:
return self._continue_chat(chat_id, text)
elif group_uid is None or user is None:
if group_uid is None or user is None:
return {
"error": "wrong usage, group_id and user_id needed or continue chat with chat_id"
}
Expand Down Expand Up @@ -293,13 +290,13 @@ async def get_events(
"scheduled": str(include_scheduled),
}
if max_end:
params["maxEndTimestamp"] = max_end.strftime(self.DT_FORMAT)
params["maxEndTimestamp"] = max_end.strftime(self._DT_FORMAT)
if max_start:
params["maxStartTimestamp"] = max_start.strftime(self.DT_FORMAT)
params["maxStartTimestamp"] = max_start.strftime(self._DT_FORMAT)
if min_end:
params["minEndTimestamp"] = min_end.strftime(self.DT_FORMAT)
params["minEndTimestamp"] = min_end.strftime(self._DT_FORMAT)
if min_start:
params["minStartTimestamp"] = min_start.strftime(self.DT_FORMAT)
params["minStartTimestamp"] = min_start.strftime(self._DT_FORMAT)
if group_id:
params["groupId"] = group_id
if subgroup_id:
Expand Down Expand Up @@ -353,54 +350,15 @@ async def update_event(self, uid: str, updates: JSONDict):
event = await self._get_entity(self._EVENT, uid)
url = f"{self.api_url}sponds/{uid}"

base_event: JSONDict = {
"heading": None,
"description": None,
"spondType": "EVENT",
"startTimestamp": None,
"endTimestamp": None,
"commentsDisabled": False,
"maxAccepted": 0,
"rsvpDate": None,
"location": {
"id": None,
"feature": None,
"address": None,
"latitude": None,
"longitude": None,
},
"owners": [{"id": None}],
"visibility": "INVITEES",
"participantsHidden": False,
"autoReminderType": "DISABLED",
"autoAccept": False,
"payment": {},
"attachments": [],
"id": None,
"tasks": {
"openTasks": [],
"assignedTasks": [
{
"name": None,
"description": "",
"type": "ASSIGNED",
"id": None,
"adultsOnly": True,
"assignments": {"memberIds": [], "profiles": [], "remove": []},
}
],
},
}

base_event = self._EVENT_TEMPLATE.copy()
for key in base_event:
if event.get(key) is not None and not updates.get(key):
base_event[key] = event[key]
elif updates.get(key) is not None:
base_event[key] = updates[key]

data = dict(base_event)
async with self.clientsession.post(
url, json=data, headers=self.auth_headers
url, json=base_event, headers=self.auth_headers
) as r:
self.events_update = await r.json()
return self.events
Expand All @@ -421,8 +379,7 @@ async def get_event_attendance_xlsx(self, uid: str) -> bytes:
"""
url = f"{self.api_url}sponds/{uid}/export"
async with self.clientsession.get(url, headers=self.auth_headers) as r:
output_data = await r.read()
return output_data
return await r.read()

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

0 comments on commit 4766ca8

Please sign in to comment.