限制一楼活动每次上传最多20张照片并保留历史材料

This commit is contained in:
2026-11-12 14:17:37 +08:00
parent 3404fff5fc
commit ddf3ba32c6
7 changed files with 80 additions and 11 deletions
+5 -5
View File
@@ -20,20 +20,20 @@ def package(entries=None):
class CenterExportTests(ApiTestCase):
async def test_material_with_over_100_photos_can_be_created_edited_and_downloaded(self):
async def test_material_with_20_photos_can_be_created_edited_and_downloaded(self):
owner=await register(self.client,'manyphotos');headers=auth(owner['token'])
image=io.BytesIO();Image.new('RGB',(20,20),'blue').save(image,'PNG')
raw=package([(f'照片/{i}.png',image.getvalue()) for i in range(101)])
raw=package([(f'照片/{i}.png',image.getvalue()) for i in range(20)])
with tempfile.TemporaryDirectory() as directory,patch.object(self.m,'_db_path',Path(directory)/'test.db'):
response=await self.client.post('/api/center/materials',headers=headers,content=raw)
self.assertEqual(response.status_code,200,response.text)
material=response.json();self.assertEqual(len(material['photo_ids']),101)
material=response.json();self.assertEqual(len(material['photo_ids']),20)
response=await self.client.post('/api/activities',headers=headers,json={
'title':'多照片一楼活动','start_at':self.m.now_ms(),'record_type':'center',
'material_id':material['id'],'photo_ids':material['photo_ids'],
})
self.assertEqual(response.status_code,200,response.text)
activity=response.json()['activity'];self.assertEqual(len(activity['photo_ids']),101)
activity=response.json()['activity'];self.assertEqual(len(activity['photo_ids']),20)
edited=await self.client.patch('/api/activities/'+activity['id'],headers=headers,json={
'description':'更新说明','photo_ids':material['photo_ids'],
})
@@ -43,7 +43,7 @@ class CenterExportTests(ApiTestCase):
self.assertEqual(downloaded.status_code,200);self.assertEqual(downloaded.content,raw)
last_photo=await self.client.get('/api/center/photos/'+material['photo_ids'][-1],headers=headers)
self.assertEqual(last_photo.status_code,200)
# 同一天已有超过100张照片,也可以继续提交ZIP材料。
# 单次20张后仍可继续补充其他材料。
second=await self.client.post('/api/center/materials',headers=headers,content=package())
self.assertEqual(second.status_code,200,second.text)
+52
View File
@@ -62,6 +62,58 @@ class DirectCenterPhotoTests(ApiTestCase):
more=await c.post('/api/center/materials',headers=headers,content=package([('补充.md','补充文档')]))
self.assertEqual((await c.post('/api/activities/'+aid+'/materials',headers=headers,json={'material_id':more.json()['id']})).status_code,200)
async def test_twenty_photo_batch_limit_and_historical_photos_are_preserved(self):
owner=await register(self.client,'batchlimit');headers=auth(owner['token']);c=self.client
photos=await self.upload(owner,21)
body={'title':'照片上限','start_at':self.m.now_ms(),'record_type':'center','photo_ids':photos}
rejected=await c.post('/api/activities',headers=headers,json=body)
self.assertEqual(rejected.status_code,422,rejected.text);self.assertIn('20张',rejected.text)
body['photo_ids']=photos[:20]
result=await c.post('/api/activities',headers=headers,json=body)
self.assertEqual(result.status_code,200,result.text);aid=result.json()['activity']['id']
path='/api/activities/'+aid
rejected=await c.post(path+'/materials',headers=headers,json={'photo_ids':photos})
self.assertEqual(rejected.status_code,422,rejected.text)
result=await c.post(path+'/materials',headers=headers,json={'photo_ids':photos[20:]})
self.assertEqual(result.status_code,200,result.text);self.assertEqual(len(result.json()['activity']['photo_ids']),21)
# 历史记录超过20张,旧页面提交原照片、或只改说明都能保存。
for changes in ({'description':'只改说明'},{'photo_ids':photos}):
result=await c.patch(path,headers=headers,json=changes)
self.assertEqual(result.status_code,200,result.text);self.assertEqual(len(result.json()['activity']['photo_ids']),21)
extra=await self.upload(owner,21)
rejected=await c.patch(path,headers=headers,json={'photo_ids':extra})
self.assertEqual(rejected.status_code,422,rejected.text)
result=await c.patch(path,headers=headers,json={'photo_ids':extra[:20]})
self.assertEqual(result.status_code,200,result.text);self.assertEqual(len(result.json()['activity']['photo_ids']),41)
detail=(await c.get(path,headers=headers)).json()['activity']
self.assertEqual(set(detail['photo_ids']),set(photos+extra[:20]))
async def test_zip_and_direct_photos_share_twenty_photo_batch_limit(self):
owner=await register(self.client,'mixedlimit');headers=auth(owner['token']);c=self.client
raw=package([(f'{i}.png',image_bytes()) for i in range(21)])
rejected=await c.post('/api/center/materials',headers=headers,content=raw)
self.assertEqual(rejected.status_code,422,rejected.text);self.assertIn('20张',rejected.text)
material=(await c.post('/api/center/materials',headers=headers,content=package())).json()
photos=await self.upload(owner,20)
body={'title':'混合上限','start_at':self.m.now_ms(),'record_type':'center','material_id':material['id'],'photo_ids':photos}
rejected=await c.post('/api/activities',headers=headers,json=body)
self.assertEqual(rejected.status_code,422,rejected.text)
body['photo_ids']=photos[:19]
result=await c.post('/api/activities',headers=headers,json=body)
self.assertEqual(result.status_code,200,result.text);aid=result.json()['activity']['id'];path='/api/activities/'+aid
self.assertEqual(len(result.json()['activity']['photo_ids']),20)
new_material=(await c.post('/api/center/materials',headers=headers,content=package())).json()
batch={'material_id':new_material['id'],'photo_ids':photos}
rejected=await c.post(path+'/materials',headers=headers,json=batch)
self.assertEqual(rejected.status_code,422,rejected.text)
batch['photo_ids']=await self.upload(owner,20)
rejected=await c.patch(path,headers=headers,json=batch)
self.assertEqual(rejected.status_code,422,rejected.text)
batch['photo_ids']=batch['photo_ids'][:19]
result=await c.post(path+'/materials',headers=headers,json=batch)
self.assertEqual(result.status_code,200,result.text)
self.assertEqual(len(result.json()['activity']['photo_ids']),40)
async def test_daily_allowance_allows_more_than_100_and_keeps_1000_limit(self):
owner=await register(self.client,'photolimit');uid=owner['user']['id']
async with self.sessions() as db: