feat: export center activities with photos to Word

This commit is contained in:
2026-12-02 15:32:35 +08:00
parent 626a683876
commit 3489b5f8fa
6 changed files with 92 additions and 13 deletions
+37 -4
View File
@@ -6,6 +6,7 @@ import tempfile
from pathlib import Path
from unittest.mock import patch
from PIL import Image
from docx import Document
from sqlalchemy import insert
from test_api import ApiTestCase, auth, register
from server.models import center_items, center_loans, center_checks
@@ -88,10 +89,42 @@ class CenterExportTests(ApiTestCase):
self.assertEqual(guest_csv['guest_college'],'艺术学院')
self.assertNotIn('secret@example.test',archive.read('loans.csv').decode('utf-8-sig'))
self.assertNotIn('13900000777',archive.read('loans.csv').decode('utf-8-sig'))
recorded=(await self.client.get('/api/center/data?dataset=loans&loan_status=recorded&q=独立登记',headers=headers)).json()
self.assertEqual(len(recorded['data']['loans']),1)
self.assertEqual(recorded['data']['loans'][0]['item_name'],'=独立登记物品')
self.assertEqual((await self.client.get('/api/center/schema',headers=headers)).json()['schema_version'],4)
async def test_word_export_combines_visible_activities_and_photos(self):
owner=await register(self.client,'wordexport');headers=auth(owner['token'])
image=io.BytesIO();Image.new('RGB',(40,30),'green').save(image,'PNG')
directory=Path(self.m._db_path).parent
uploads=directory/'uploads';uploads.mkdir(parents=True,exist_ok=True)
photo_id='a'*32
(uploads/(photo_id+'.jpg')).write_bytes(image.getvalue())
stamp=self.m.now_ms()
async with self.m.SessionLocal() as db:
for activity_id,title in (('word-one','Word导出活动'),('word-two','无照片活动')):
await db.execute(insert(self.m.activities).values(
id=activity_id,title=title,start_at=stamp,location='',description='不应写入Word',
visibility='public',capacity=0,status='active',organizer_id=owner['user']['id'],
created_at=stamp,updated_at=stamp,
))
await db.execute(insert(self.m.center_activity_meta).values(
activity_id='word-one',photo_ids=json.dumps([photo_id]),material_id=None,
))
await db.execute(insert(self.m.center_activity_meta).values(
activity_id='word-two',photo_ids=json.dumps(['not-a-photo-id']),material_id=None,
))
await db.commit()
response=await self.client.get('/api/center/export?dataset=activities&format=word_activities',headers=headers)
self.assertEqual(response.status_code,200,response.text)
mime='application/vnd.openxmlformats-officedocument.wordprocessingml.document'
self.assertEqual(response.headers['content-type'],mime)
self.assertTrue(response.headers['content-disposition'].endswith('.docx"'))
document=Document(io.BytesIO(response.content))
text='\n'.join(paragraph.text for paragraph in document.paragraphs)
self.assertIn('Word导出活动',text)
self.assertIn('无照片活动',text)
self.assertNotIn('不应写入Word',text)
self.assertEqual(len(document.inline_shapes),1)
response=await self.client.get('/api/center/export?dataset=items&format=word_activities',headers=headers)
self.assertEqual(response.status_code,422)
async def test_pagination_and_export_are_not_silently_truncated(self):
user=await register(self.client,'exportpagination');headers=auth(user['token'])