ctfy.sdk.admin_resources.competition_attachments
client.admin.competition_attachments — the files an organiser hangs
on a competition: its rules, handbook or notice (#1808).
1"""``client.admin.competition_attachments`` — the files an organiser hangs 2on a competition: its rules, handbook or notice (#1808).""" 3 4from __future__ import annotations 5 6from typing import Any 7 8from ctfy.core.localized import LocalizedText 9from ctfy.sdk._helpers import _raise_for_status 10from ctfy.sdk.base import BaseHttpClient 11from ctfy.server.models import ( 12 AdminCompetitionAttachmentsResponse, 13 CompetitionAttachmentInfo, 14) 15 16 17class AdminCompetitionAttachmentsResource: 18 """Upload, list, edit and delete one competition's files. For its 19 organisers (global admins and the event's own competition admins).""" 20 21 def __init__(self, http: BaseHttpClient) -> None: 22 self._http = http 23 24 def list(self, competition_id: str) -> AdminCompetitionAttachmentsResponse: 25 """The files in display order, plus what the next upload may be: 26 ``uploads_enabled``, ``remaining`` (``None`` = no cap), ``limit`` 27 and ``max_bytes``.""" 28 resp = self._http.request("GET", f"/admin/competitions/{competition_id}/attachments") 29 _raise_for_status(resp) 30 return AdminCompetitionAttachmentsResponse.model_validate(resp.json()) 31 32 def upload( 33 self, 34 competition_id: str, 35 filename: str, 36 content: bytes, 37 *, 38 locale: str = "", 39 topic_id: str = "", 40 ) -> CompetitionAttachmentInfo: 41 """Attach one file. ``locale`` ``""`` shows it to every reader; a 42 tag (``en`` / ``zh-CN``) only to readers of that language. 43 ``topic_id`` files it under one of the competition's scheduled 44 topics, released with it (``422 attachment_topic_not_found`` for an 45 id that is not one of this competition's). 46 47 The type is decided from the bytes: PDF, Word / Excel / PowerPoint 48 (OOXML, not macro-enabled), PNG / JPEG / GIF / WebP, or UTF-8 text. 49 Anything else is ``415 competition_attachment_type_refused``; over 50 the size setting is ``413 competition_attachment_too_large``; over 51 the count is ``409 competition_attachment_limit``; no object 52 storage is ``503 storage_unavailable``. 53 """ 54 data: dict[str, Any] = {"locale": locale} if locale else {} 55 if topic_id: 56 data["topic_id"] = topic_id 57 resp = self._http.request( 58 "POST", 59 f"/admin/competitions/{competition_id}/attachments", 60 files={"file": (filename, content, "application/octet-stream")}, 61 data=data, 62 ) 63 _raise_for_status(resp) 64 return CompetitionAttachmentInfo.model_validate(resp.json()) 65 66 def update( 67 self, 68 competition_id: str, 69 attachment_id: str, 70 *, 71 title: LocalizedText | None = None, 72 locale: str | None = None, 73 position: int | None = None, 74 ) -> CompetitionAttachmentInfo: 75 """Edit one file's title, language or position. Partial: only the 76 arguments you pass are sent, so the rest are left alone.""" 77 body: dict[str, Any] = {} 78 if title is not None: 79 body["title"] = title 80 if locale is not None: 81 body["locale"] = locale 82 if position is not None: 83 body["position"] = position 84 resp = self._http.request( 85 "PATCH", 86 f"/admin/competitions/{competition_id}/attachments/{attachment_id}", 87 json=body, 88 ) 89 _raise_for_status(resp) 90 return CompetitionAttachmentInfo.model_validate(resp.json()) 91 92 def delete( 93 self, competition_id: str, attachment_id: str 94 ) -> AdminCompetitionAttachmentsResponse: 95 """Remove one file, bytes and all; returns the list as it now stands.""" 96 resp = self._http.request( 97 "DELETE", f"/admin/competitions/{competition_id}/attachments/{attachment_id}" 98 ) 99 _raise_for_status(resp) 100 return AdminCompetitionAttachmentsResponse.model_validate(resp.json())
18class AdminCompetitionAttachmentsResource: 19 """Upload, list, edit and delete one competition's files. For its 20 organisers (global admins and the event's own competition admins).""" 21 22 def __init__(self, http: BaseHttpClient) -> None: 23 self._http = http 24 25 def list(self, competition_id: str) -> AdminCompetitionAttachmentsResponse: 26 """The files in display order, plus what the next upload may be: 27 ``uploads_enabled``, ``remaining`` (``None`` = no cap), ``limit`` 28 and ``max_bytes``.""" 29 resp = self._http.request("GET", f"/admin/competitions/{competition_id}/attachments") 30 _raise_for_status(resp) 31 return AdminCompetitionAttachmentsResponse.model_validate(resp.json()) 32 33 def upload( 34 self, 35 competition_id: str, 36 filename: str, 37 content: bytes, 38 *, 39 locale: str = "", 40 topic_id: str = "", 41 ) -> CompetitionAttachmentInfo: 42 """Attach one file. ``locale`` ``""`` shows it to every reader; a 43 tag (``en`` / ``zh-CN``) only to readers of that language. 44 ``topic_id`` files it under one of the competition's scheduled 45 topics, released with it (``422 attachment_topic_not_found`` for an 46 id that is not one of this competition's). 47 48 The type is decided from the bytes: PDF, Word / Excel / PowerPoint 49 (OOXML, not macro-enabled), PNG / JPEG / GIF / WebP, or UTF-8 text. 50 Anything else is ``415 competition_attachment_type_refused``; over 51 the size setting is ``413 competition_attachment_too_large``; over 52 the count is ``409 competition_attachment_limit``; no object 53 storage is ``503 storage_unavailable``. 54 """ 55 data: dict[str, Any] = {"locale": locale} if locale else {} 56 if topic_id: 57 data["topic_id"] = topic_id 58 resp = self._http.request( 59 "POST", 60 f"/admin/competitions/{competition_id}/attachments", 61 files={"file": (filename, content, "application/octet-stream")}, 62 data=data, 63 ) 64 _raise_for_status(resp) 65 return CompetitionAttachmentInfo.model_validate(resp.json()) 66 67 def update( 68 self, 69 competition_id: str, 70 attachment_id: str, 71 *, 72 title: LocalizedText | None = None, 73 locale: str | None = None, 74 position: int | None = None, 75 ) -> CompetitionAttachmentInfo: 76 """Edit one file's title, language or position. Partial: only the 77 arguments you pass are sent, so the rest are left alone.""" 78 body: dict[str, Any] = {} 79 if title is not None: 80 body["title"] = title 81 if locale is not None: 82 body["locale"] = locale 83 if position is not None: 84 body["position"] = position 85 resp = self._http.request( 86 "PATCH", 87 f"/admin/competitions/{competition_id}/attachments/{attachment_id}", 88 json=body, 89 ) 90 _raise_for_status(resp) 91 return CompetitionAttachmentInfo.model_validate(resp.json()) 92 93 def delete( 94 self, competition_id: str, attachment_id: str 95 ) -> AdminCompetitionAttachmentsResponse: 96 """Remove one file, bytes and all; returns the list as it now stands.""" 97 resp = self._http.request( 98 "DELETE", f"/admin/competitions/{competition_id}/attachments/{attachment_id}" 99 ) 100 _raise_for_status(resp) 101 return AdminCompetitionAttachmentsResponse.model_validate(resp.json())
Upload, list, edit and delete one competition's files. For its organisers (global admins and the event's own competition admins).
25 def list(self, competition_id: str) -> AdminCompetitionAttachmentsResponse: 26 """The files in display order, plus what the next upload may be: 27 ``uploads_enabled``, ``remaining`` (``None`` = no cap), ``limit`` 28 and ``max_bytes``.""" 29 resp = self._http.request("GET", f"/admin/competitions/{competition_id}/attachments") 30 _raise_for_status(resp) 31 return AdminCompetitionAttachmentsResponse.model_validate(resp.json())
The files in display order, plus what the next upload may be:
uploads_enabled, remaining (None = no cap), limit
and max_bytes.
33 def upload( 34 self, 35 competition_id: str, 36 filename: str, 37 content: bytes, 38 *, 39 locale: str = "", 40 topic_id: str = "", 41 ) -> CompetitionAttachmentInfo: 42 """Attach one file. ``locale`` ``""`` shows it to every reader; a 43 tag (``en`` / ``zh-CN``) only to readers of that language. 44 ``topic_id`` files it under one of the competition's scheduled 45 topics, released with it (``422 attachment_topic_not_found`` for an 46 id that is not one of this competition's). 47 48 The type is decided from the bytes: PDF, Word / Excel / PowerPoint 49 (OOXML, not macro-enabled), PNG / JPEG / GIF / WebP, or UTF-8 text. 50 Anything else is ``415 competition_attachment_type_refused``; over 51 the size setting is ``413 competition_attachment_too_large``; over 52 the count is ``409 competition_attachment_limit``; no object 53 storage is ``503 storage_unavailable``. 54 """ 55 data: dict[str, Any] = {"locale": locale} if locale else {} 56 if topic_id: 57 data["topic_id"] = topic_id 58 resp = self._http.request( 59 "POST", 60 f"/admin/competitions/{competition_id}/attachments", 61 files={"file": (filename, content, "application/octet-stream")}, 62 data=data, 63 ) 64 _raise_for_status(resp) 65 return CompetitionAttachmentInfo.model_validate(resp.json())
Attach one file. locale "" shows it to every reader; a
tag (en / zh-CN) only to readers of that language.
topic_id files it under one of the competition's scheduled
topics, released with it (422 attachment_topic_not_found for an
id that is not one of this competition's).
The type is decided from the bytes: PDF, Word / Excel / PowerPoint
(OOXML, not macro-enabled), PNG / JPEG / GIF / WebP, or UTF-8 text.
Anything else is 415 competition_attachment_type_refused; over
the size setting is 413 competition_attachment_too_large; over
the count is 409 competition_attachment_limit; no object
storage is 503 storage_unavailable.
67 def update( 68 self, 69 competition_id: str, 70 attachment_id: str, 71 *, 72 title: LocalizedText | None = None, 73 locale: str | None = None, 74 position: int | None = None, 75 ) -> CompetitionAttachmentInfo: 76 """Edit one file's title, language or position. Partial: only the 77 arguments you pass are sent, so the rest are left alone.""" 78 body: dict[str, Any] = {} 79 if title is not None: 80 body["title"] = title 81 if locale is not None: 82 body["locale"] = locale 83 if position is not None: 84 body["position"] = position 85 resp = self._http.request( 86 "PATCH", 87 f"/admin/competitions/{competition_id}/attachments/{attachment_id}", 88 json=body, 89 ) 90 _raise_for_status(resp) 91 return CompetitionAttachmentInfo.model_validate(resp.json())
Edit one file's title, language or position. Partial: only the arguments you pass are sent, so the rest are left alone.
93 def delete( 94 self, competition_id: str, attachment_id: str 95 ) -> AdminCompetitionAttachmentsResponse: 96 """Remove one file, bytes and all; returns the list as it now stands.""" 97 resp = self._http.request( 98 "DELETE", f"/admin/competitions/{competition_id}/attachments/{attachment_id}" 99 ) 100 _raise_for_status(resp) 101 return AdminCompetitionAttachmentsResponse.model_validate(resp.json())
Remove one file, bytes and all; returns the list as it now stands.