Skip to content

Commit

Permalink
feat: download attendance report
Browse files Browse the repository at this point in the history
  • Loading branch information
maiminhp committed May 23, 2024
1 parent 6d894bf commit d73eb9b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ Get all your messages.
Send a message with content `text`.
Either specify an existing `chat_id`, or both `user` and `group_uid` for a new chat.

### get_export()
Get excel report for an event attendance, available via the web client.

## Example scripts

The following scripts are included as examples. Some of the scripts might require additional packages to be installed (csv, ical etc).
Expand Down
12 changes: 12 additions & 0 deletions manual_test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Doesn't yet use `get_person(user)` or any `send_`, `update_` methods."""

import asyncio
import tempfile

from config import club_id, password, username
from spond import club, spond
Expand Down Expand Up @@ -40,6 +41,17 @@ async def main() -> None:
for i, message in enumerate(messages):
print(f"[{i}] {_message_summary(message)}")

# ATTENDANCE EXPORT

print("\nGetting a random attendance report...")
e = events[0]
data = await s.get_export(e["id"])
with tempfile.NamedTemporaryFile(
mode="wb", suffix=".xlsx", delete=False
) as temp_file:
temp_file.write(data)
print(f"Check out {temp_file.name}")

await s.clientsession.close()

# SPOND CLUB
Expand Down
19 changes: 19 additions & 0 deletions spond/spond.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,22 @@ async def update_event(self, uid, updates: dict):
) as r:
self.events_update = await r.json()
return self.events

@SpondBase.require_authentication
async def get_export(self, uid: str) -> bytes:
"""get excel export on attendance for an event.
Available via the web client for each event.
Parameters
----------
uid : str
UID of the event.
Returns:
bytes: XLSX binary data
"""
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

27 changes: 27 additions & 0 deletions tests/test_spond.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Test suite for Spond class."""

from unittest.mock import AsyncMock, patch

import pytest

from spond.base import SpondBase
Expand Down Expand Up @@ -131,3 +133,28 @@ async def test_get_group__blank_id_raises_exception(mock_groups, mock_token):

with pytest.raises(IndexError):
await s.get_group("")


@pytest.mark.asyncio
@patch("aiohttp.ClientSession.get")
async def test_get_export(mock_get, mock_token):
s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.token = mock_token

mock_binary = b"\x68\x65\x6c\x6c\x6f\x77\x6f\x72\x6c\x64" # helloworld
mock_get.return_value.__aenter__.return_value.status = 200
mock_get.return_value.__aenter__.return_value.read = AsyncMock(
return_value=mock_binary
)

data = await s.get_export(uid="ID1")

mock_url = "https://api.spond.com/core/v1/sponds/ID1/export"
mock_get.assert_called_once_with(
mock_url,
headers={
"content-type": "application/json",
"Authorization": f"Bearer {mock_token}",
},
)
assert data == mock_binary

0 comments on commit d73eb9b

Please sign in to comment.