1- # ruff: noqa: D100, D101, D102, D103, D107, S603, PLW1510, I001
1+ # ruff: noqa: D100, D101, D102, D103, D107, S603, PLW1510, PLR0913, I001
22
33from typing import Any , Self
44from enum import StrEnum
@@ -40,6 +40,20 @@ class DocType(StrEnum):
4040 rep_profile_form_template = "564cbea3-44d3-4303-b75a-d9fdda7e5a80"
4141
4242
43+ class DocumentRef :
44+ def __init__ (self , doc_id : str , doc_ver : str ) -> None :
45+ self .doc_id = doc_id
46+ self .doc_ver = doc_ver
47+ self .cid = "0x"
48+
49+ def to_json (self ) -> dict :
50+ return {
51+ "id" : self .doc_id ,
52+ "ver" : self .doc_ver ,
53+ "cid" : self .cid ,
54+ }
55+
56+
4357class SignedDocumentBase :
4458 def __init__ (
4559 self ,
@@ -53,9 +67,12 @@ def __init__(
5367 self .cat_id = cat_id
5468 self .key = key
5569
56- def new_version (self ) -> None :
70+ def doc_ref (self ) -> DocumentRef :
71+ return DocumentRef (self .metadata ["id" ], self .metadata ["ver" ])
72+
73+ def new_doc_version (self ) -> None :
5774 time .sleep (1 )
58- self .metadata ["ver " ] = uuid_v7 ()
75+ self .metadata ["doc_ver " ] = uuid_v7 ()
5976
6077
6178class SignedDocument (SignedDocumentBase ):
@@ -117,15 +134,19 @@ def build_and_sign(
117134
118135def proposal_doc (
119136 content : dict [str , Any ],
120- proposal_form_template_doc : SignedDocumentBase ,
121- param_doc : SignedDocumentBase ,
137+ proposal_form_template_doc : DocumentRef ,
138+ param_ref : DocumentRef ,
122139 rbac_chain : RBACChain ,
140+ doc_id : str | None = None ,
141+ doc_ver : str | None = None ,
123142) -> SignedDocument :
124143 metadata = __create_metadata (
125144 doc_type = DocType .proposal ,
126145 content_type = "application/json" ,
127146 template = proposal_form_template_doc ,
128- parameters = [param_doc ],
147+ parameters = [param_ref ],
148+ doc_id = doc_id ,
149+ doc_ver = doc_ver ,
129150 )
130151
131152 (cat_id , key ) = rbac_chain .cat_id_for_role (RoleID .PROPOSER )
@@ -134,118 +155,171 @@ def proposal_doc(
134155
135156def proposal_form_template_doc (
136157 content : dict [str , Any ],
137- param_doc : SignedDocumentBase ,
158+ param_ref : DocumentRef ,
138159 admin_key : AdminKey ,
160+ doc_id : str | None = None ,
161+ doc_ver : str | None = None ,
139162) -> SignedDocument :
140163 metadata = __create_metadata (
141164 doc_type = DocType .proposal_form_template ,
142165 content_type = "application/schema+json" ,
143- parameters = [param_doc ],
166+ parameters = [param_ref ],
167+ doc_id = doc_id ,
168+ doc_ver = doc_ver ,
169+ )
170+
171+ return SignedDocument (metadata , content , admin_key .cat_id (), admin_key .key )
172+
173+
174+ def proposal_comment_form_template_doc (
175+ content : dict [str , Any ],
176+ param_ref : DocumentRef ,
177+ admin_key : AdminKey ,
178+ doc_id : str | None = None ,
179+ doc_ver : str | None = None ,
180+ ) -> SignedDocument :
181+ metadata = __create_metadata (
182+ doc_type = DocType .proposal_comment_form_template ,
183+ content_type = "application/schema+json" ,
184+ parameters = [param_ref ],
185+ doc_id = doc_id ,
186+ doc_ver = doc_ver ,
144187 )
145188
146189 return SignedDocument (metadata , content , admin_key .cat_id (), admin_key .key )
147190
148191
149192def category_parameters_doc (
150193 content : dict [str , Any ],
151- category_parameters_form_template_doc : SignedDocumentBase ,
152- param_doc : SignedDocumentBase ,
194+ category_parameters_form_template_doc : DocumentRef ,
195+ param_ref : DocumentRef ,
153196 admin_key : AdminKey ,
197+ doc_id : str | None = None ,
198+ doc_ver : str | None = None ,
154199) -> SignedDocumentBase :
155200 metadata = __create_metadata (
156201 doc_type = DocType .category_parameters ,
157202 content_type = "application/json" ,
158203 template = category_parameters_form_template_doc ,
159- parameters = [param_doc ],
204+ parameters = [param_ref ],
205+ doc_id = doc_id ,
206+ doc_ver = doc_ver ,
160207 )
161208 return SignedDocument (metadata , content , admin_key .cat_id (), admin_key .key )
162209
163210
164211def category_parameters_form_template_doc (
165212 content : dict [str , Any ],
166- param_doc : SignedDocumentBase ,
213+ param_ref : DocumentRef ,
167214 admin_key : AdminKey ,
215+ doc_id : str | None = None ,
216+ doc_ver : str | None = None ,
168217) -> SignedDocumentBase :
169218 metadata = __create_metadata (
170219 doc_type = DocType .category_parameters_form_template ,
171220 content_type = "application/schema+json" ,
172- parameters = [param_doc ],
221+ parameters = [param_ref ],
222+ doc_id = doc_id ,
223+ doc_ver = doc_ver ,
173224 )
174225 return SignedDocument (metadata , content , admin_key .cat_id (), admin_key .key )
175226
176227
177228def campaign_parameters_doc (
178229 content : dict [str , Any ],
179- campaign_parameters_form_template_doc : SignedDocumentBase ,
180- param_doc : SignedDocumentBase ,
230+ campaign_parameters_form_template_doc : DocumentRef ,
231+ param_ref : DocumentRef ,
181232 admin_key : AdminKey ,
233+ doc_id : str | None = None ,
234+ doc_ver : str | None = None ,
182235) -> SignedDocumentBase :
183236 metadata = __create_metadata (
184237 doc_type = DocType .campaign_parameters ,
185238 content_type = "application/json" ,
186239 template = campaign_parameters_form_template_doc ,
187- parameters = [param_doc ],
240+ parameters = [param_ref ],
241+ doc_id = doc_id ,
242+ doc_ver = doc_ver ,
188243 )
189244 return SignedDocument (metadata , content , admin_key .cat_id (), admin_key .key )
190245
191246
192247def campaign_parameters_form_template_doc (
193248 content : dict [str , Any ],
194- param_doc : SignedDocumentBase ,
249+ param_ref : DocumentRef ,
195250 admin_key : AdminKey ,
251+ doc_id : str | None = None ,
252+ doc_ver : str | None = None ,
196253) -> SignedDocumentBase :
197254 metadata = __create_metadata (
198255 doc_type = DocType .campaign_parameters_form_template ,
199256 content_type = "application/schema+json" ,
200- parameters = [param_doc ],
257+ parameters = [param_ref ],
258+ doc_id = doc_id ,
259+ doc_ver = doc_ver ,
201260 )
202261 return SignedDocument (metadata , content , admin_key .cat_id (), admin_key .key )
203262
204263
205264def brand_parameters_doc (
206265 content : dict [str , Any ],
207- brand_parameters_form_template_doc : SignedDocumentBase ,
266+ brand_parameters_form_template_ref : DocumentRef ,
208267 admin_key : AdminKey ,
268+ doc_id : str | None = None ,
269+ doc_ver : str | None = None ,
209270) -> SignedDocumentBase :
210271 metadata = __create_metadata (
211272 doc_type = DocType .brand_parameters ,
212273 content_type = "application/json" ,
213- template = brand_parameters_form_template_doc ,
274+ template = brand_parameters_form_template_ref ,
275+ doc_id = doc_id ,
276+ doc_ver = doc_ver ,
214277 )
215278 return SignedDocument (metadata , content , admin_key .cat_id (), admin_key .key )
216279
217280
218- def brand_parameters_form_template_doc (content : dict [str , Any ], admin_key : AdminKey ) -> SignedDocumentBase :
281+ def brand_parameters_form_template_doc (
282+ content : dict [str , Any ],
283+ admin_key : AdminKey ,
284+ doc_id : str | None = None ,
285+ doc_ver : str | None = None ,
286+ ) -> SignedDocumentBase :
219287 metadata = __create_metadata (
220288 doc_type = DocType .brand_parameters_form_template ,
221289 content_type = "application/schema+json" ,
290+ doc_id = doc_id ,
291+ doc_ver = doc_ver ,
222292 )
223293 return SignedDocument (metadata , content , admin_key .cat_id (), admin_key .key )
224294
225295
226296def __create_metadata (
227297 doc_type : DocType ,
228298 content_type : str ,
229- template : SignedDocumentBase | None = None ,
230- parameters : list [SignedDocumentBase ] | None = None ,
299+ doc_id : str | None = None ,
300+ doc_ver : str | None = None ,
301+ template : DocumentRef | None = None ,
302+ parameters : list [DocumentRef ] | None = None ,
231303) -> dict [str , Any ]:
232- doc_id = uuid_v7 ()
304+ if doc_id is None and doc_ver is None :
305+ doc_id = uuid_v7 ()
306+ doc_ver = doc_id
307+ if doc_id is None :
308+ doc_id = uuid_v7 ()
309+ if doc_ver is None :
310+ doc_ver = uuid_v7 ()
233311
234312 metadata : dict [str , Any ] = {
235313 "content-encoding" : "br" ,
236314 "content-type" : content_type ,
237315 "id" : doc_id ,
238- "ver" : doc_id ,
316+ "ver" : doc_ver ,
239317 "type" : doc_type ,
240318 }
241319
242320 if template is not None :
243- metadata ["template" ] = {
244- "id" : template .metadata ["id" ],
245- "ver" : template .metadata ["ver" ],
246- "cid" : "0x" ,
247- }
321+ metadata ["template" ] = template .to_json ()
248322 if parameters is not None :
249- metadata ["parameters" ] = [{ "id" : p . metadata [ "id" ], "ver" : p . metadata [ "ver" ], "cid" : "0x" } for p in parameters ]
323+ metadata ["parameters" ] = [p . to_json () for p in parameters ]
250324
251325 return metadata
0 commit comments