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 15, 2024
1 parent 87e7b88 commit 97fdee1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
12 changes: 12 additions & 0 deletions manual_test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import asyncio

from config import password, username, club_id
import tempfile
from spond import spond, club

DUMMY_ID = "DUMMY_ID"
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 @@ -364,3 +364,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 dats
"""
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

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

import pytest


from unittest.mock import AsyncMock, patch
from spond.spond import Spond
from spond.base import SpondBase

Expand Down Expand Up @@ -132,3 +131,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 97fdee1

Please sign in to comment.