Skip to content

Commit 8a5a898

Browse files
committed
Unify from_json methods
1 parent 9103cdd commit 8a5a898

File tree

1 file changed

+58
-235
lines changed

1 file changed

+58
-235
lines changed

iib/web/models.py

Lines changed: 58 additions & 235 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,19 +1012,6 @@ def _from_json(
10121012
db.session.add(batch)
10131013
request_kwargs['batch'] = batch
10141014

1015-
@staticmethod
1016-
def from_json_replacement(
1017-
request_kwargs: RequestPayload,
1018-
batch: Optional[Batch] = None,
1019-
):
1020-
# current_user.is_authenticated is only ever False when auth is disabled
1021-
if current_user.is_authenticated:
1022-
request_kwargs['user'] = current_user
1023-
1024-
# Add the request to a new batch
1025-
batch = batch or Batch()
1026-
db.session.add(batch)
1027-
request_kwargs['batch'] = batch
10281015

10291016
def get_common_index_image_json(self) -> CommonIndexImageResponseBase:
10301017
"""
@@ -1201,44 +1188,6 @@ def from_json( # type: ignore[override] # noqa: F821
12011188
request.add_state('in_progress', 'The request was initiated')
12021189
return request
12031190

1204-
def from_json_replacement(
1205-
cls,
1206-
payload: AddPydanticModel,
1207-
batch: Optional[Batch] = None,
1208-
):
1209-
"""
1210-
Handle JSON requests for the builds/add API endpoint.
1211-
1212-
:param AddPydanticModel payload: the Pydantic model representing the request.
1213-
:param Batch batch: the batch to specify with the request.
1214-
"""
1215-
request_kwargs = payload.get_json_for_request()
1216-
1217-
request_kwargs["bundles"] = [
1218-
Image.get_or_create(pull_specification=item) for item in payload.bundles
1219-
]
1220-
request_kwargs["deprecation_list"] = [
1221-
Image.get_or_create(pull_specification=item) for item in payload.deprecation_list
1222-
]
1223-
request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)
1224-
request_kwargs['from_index'] = Image.get_or_create(pull_specification=payload.from_index)
1225-
1226-
# current_user.is_authenticated is only ever False when auth is disabled
1227-
if current_user.is_authenticated:
1228-
request_kwargs['user'] = current_user
1229-
1230-
# Add the request to a new batch
1231-
batch = batch or Batch()
1232-
db.session.add(batch)
1233-
request_kwargs['batch'] = batch
1234-
1235-
request = cls(**request_kwargs)
1236-
1237-
for bt in payload.build_tags:
1238-
request.add_build_tag(bt)
1239-
1240-
request.add_state('in_progress', 'The request was initiated')
1241-
return request
12421191

12431192
def to_json(self, verbose: Optional[bool] = True) -> AddRequestResponse:
12441193
"""
@@ -1339,38 +1288,6 @@ def from_json( # type: ignore[override] # noqa: F821
13391288

13401289
return request
13411290

1342-
def from_json_replacement(
1343-
cls,
1344-
payload: RmPydanticModel,
1345-
batch: Optional[Batch] = None,
1346-
):
1347-
"""
1348-
Handle JSON requests for the builds/rm API endpoint.
1349-
1350-
:param RmPydanticModel payload: the Pydantic model representing the request.
1351-
:param Batch batch: the batch to specify with the request.
1352-
"""
1353-
request_kwargs = payload.get_json_for_request()
1354-
1355-
request_kwargs['operators'] = [Operator.get_or_create(name=item) for item in payload.operators]
1356-
request_kwargs['from_index'] = Image.get_or_create(pull_specification=payload.from_index)
1357-
request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)
1358-
1359-
if current_user.is_authenticated:
1360-
request_kwargs['user'] = current_user
1361-
1362-
# Add the request to a new batch
1363-
batch = batch or Batch()
1364-
db.session.add(batch)
1365-
request_kwargs['batch'] = batch
1366-
1367-
request = cls(**request_kwargs)
1368-
request.add_state('in_progress', 'The request was initiated')
1369-
1370-
for bt in payload.build_tags:
1371-
request.add_build_tag(bt)
1372-
1373-
return request
13741291

13751292
def to_json(self, verbose: Optional[bool] = True) -> AddRmRequestResponseBase:
13761293
"""
@@ -1518,35 +1435,6 @@ def from_json( # type: ignore[override] # noqa: F821
15181435
request.add_state('in_progress', 'The request was initiated')
15191436
return request
15201437

1521-
def from_json_replacement(
1522-
cls,
1523-
payload: RegenerateBundlePydanticModel,
1524-
batch: Optional[Batch] = None,
1525-
):
1526-
"""
1527-
Handle JSON requests for the builds/egenerate-bundle API endpoint.
1528-
1529-
:param RegenerateBundlePydanticModel payload: the Pydantic model representing the request.
1530-
:param Batch batch: the batch to specify with the request.
1531-
"""
1532-
request_kwargs = payload.get_json_for_request()
1533-
1534-
request_kwargs['from_bundle_image'] = Image.get_or_create(
1535-
pull_specification=payload.from_bundle_image
1536-
)
1537-
1538-
# current_user.is_authenticated is only ever False when auth is disabled
1539-
if current_user.is_authenticated:
1540-
request_kwargs['user'] = current_user
1541-
1542-
# Add the request to a new batch
1543-
batch = batch or Batch()
1544-
db.session.add(batch)
1545-
request_kwargs['batch'] = batch
1546-
1547-
request = cls(**request_kwargs)
1548-
request.add_state('in_progress', 'The request was initiated')
1549-
return request
15501438

15511439
def to_json(self, verbose: Optional[bool] = True) -> RegenerateBundleRequestResponse:
15521440
"""
@@ -1750,44 +1638,6 @@ def from_json( # type: ignore[override] # noqa: F821
17501638
request.add_state('in_progress', 'The request was initiated')
17511639
return request
17521640

1753-
def from_json_replacement(
1754-
cls,
1755-
payload: MergeIndexImagePydanticModel,
1756-
batch: Optional[Batch] = None,
1757-
):
1758-
"""
1759-
Handle JSON requests for the builds/merge-index-image API endpoint.
1760-
1761-
:param MergeIndexImagePydanticModel payload: the Pydantic model representing the request.
1762-
:param Batch batch: the batch to specify with the request.
1763-
"""
1764-
request_kwargs = payload.get_json_for_request()
1765-
1766-
request_kwargs['deprecation_list'] = [
1767-
Image.get_or_create(pull_specification=item) for item in payload.deprecation_list
1768-
]
1769-
request_kwargs['source_from_index'] = Image.get_or_create(
1770-
pull_specification=payload.source_from_index
1771-
)
1772-
request_kwargs['target_index'] = Image.get_or_create(pull_specification=payload.target_index)
1773-
request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)
1774-
1775-
# current_user.is_authenticated is only ever False when auth is disabled
1776-
if current_user.is_authenticated:
1777-
request_kwargs['user'] = current_user
1778-
1779-
# Add the request to a new batch
1780-
batch = batch or Batch()
1781-
db.session.add(batch)
1782-
request_kwargs['batch'] = batch
1783-
1784-
request = cls(**request_kwargs)
1785-
1786-
for bt in payload.build_tags:
1787-
request.add_build_tag(bt)
1788-
1789-
request.add_state('in_progress', 'The request was initiated')
1790-
return request
17911641

17921642
def to_json(self, verbose: Optional[bool] = True) -> MergeIndexImageRequestResponse:
17931643
"""
@@ -2061,35 +1911,6 @@ def from_json( # type: ignore[override] # noqa: F821
20611911

20621912
return request
20631913

2064-
def from_json_replacement(
2065-
cls,
2066-
payload: CreateEmptyIndexPydanticModel,
2067-
batch: Optional[Batch] = None,
2068-
):
2069-
"""
2070-
Handle JSON requests for the builds/create-empty-index API endpoint.
2071-
2072-
:param CreateEmptyIndexPydanticModel payload: the Pydantic model representing the request.
2073-
:param Batch batch: the batch to specify with the request.
2074-
"""
2075-
request_kwargs = payload.get_json_for_request()
2076-
2077-
request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)
2078-
request_kwargs['from_index'] = Image.get_or_create(pull_specification=payload.from_index)
2079-
2080-
# current_user.is_authenticated is only ever False when auth is disabled
2081-
if current_user.is_authenticated:
2082-
request_kwargs['user'] = current_user
2083-
2084-
# Add the request to a new batch
2085-
batch = batch or Batch()
2086-
db.session.add(batch)
2087-
request_kwargs['batch'] = batch
2088-
2089-
request = cls(**request_kwargs)
2090-
request.add_state('in_progress', 'The request was initiated')
2091-
2092-
return request
20931914

20941915
def to_json(self, verbose: Optional[bool] = True) -> CreateEmptyIndexRequestResponse:
20951916
"""
@@ -2215,34 +2036,6 @@ def from_json( # type: ignore[override] # noqa: F821
22152036
request.add_state('in_progress', 'The request was initiated')
22162037
return request
22172038

2218-
def from_json_replacement(
2219-
cls,
2220-
payload: RecursiveRelatedBundlesPydanticModel,
2221-
batch: Optional[Batch] = None,
2222-
):
2223-
"""
2224-
Handle JSON requests for the builds/recursive-related-bundles API endpoint.
2225-
2226-
:param RecursiveRelatedBundlesPydanticModel payload: the Pydantic model representing the request.
2227-
:param Batch batch: the batch to specify with the request.
2228-
"""
2229-
2230-
request_kwargs = payload.get_json_for_request()
2231-
2232-
request_kwargs['parent_bundle_image'] = Image.get_or_create(pull_specification=payload.parent_bundle_image)
2233-
2234-
# current_user.is_authenticated is only ever False when auth is disabled
2235-
if current_user.is_authenticated:
2236-
request_kwargs['user'] = current_user
2237-
2238-
# Add the request to a new batch
2239-
batch = batch or Batch()
2240-
db.session.add(batch)
2241-
request_kwargs['batch'] = batch
2242-
2243-
request = cls(**request_kwargs)
2244-
request.add_state('in_progress', 'The request was initiated')
2245-
return request
22462039

22472040
def to_json(self, verbose: Optional[bool] = True) -> RecursiveRelatedBundlesRequestResponse:
22482041
"""
@@ -2352,34 +2145,6 @@ def from_json( # type: ignore[override] # noqa: F821
23522145
request.add_state('in_progress', 'The request was initiated')
23532146
return request
23542147

2355-
def from_json_replacement(
2356-
cls,
2357-
payload: FbcOperationsPydanticModel,
2358-
):
2359-
"""
2360-
Handle JSON requests for the builds/fbc-operations API endpoint.
2361-
2362-
:param FbcOperationsPydanticModel payload: the Pydantic model representing the request.
2363-
:param Batch batch: the batch to specify with the request.
2364-
"""
2365-
request_kwargs = payload.get_json_for_request()
2366-
2367-
request_kwargs['fbc_fragment'] = Image.get_or_create(pull_specification=payload.fbc_fragment)
2368-
request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)
2369-
request_kwargs['from_index'] = Image.get_or_create(pull_specification=payload.from_index)
2370-
2371-
# current_user.is_authenticated is only ever False when auth is disabled
2372-
if current_user.is_authenticated:
2373-
request_kwargs['user'] = current_user
2374-
2375-
request = cls(**request_kwargs)
2376-
2377-
for bt in payload.build_tags:
2378-
request.add_build_tag(bt)
2379-
2380-
request.add_state('in_progress', 'The request was initiated')
2381-
return request
2382-
23832148

23842149
def to_json(self, verbose: Optional[bool] = True) -> FbcOperationRequestResponse:
23852150
"""
@@ -2416,3 +2181,61 @@ def get_mutable_keys(self) -> Set[str]:
24162181
rv.update(self.get_index_image_mutable_keys())
24172182
rv.add('fbc_fragment_resolved')
24182183
return rv
2184+
2185+
2186+
def from_json_replacement_even_shorter(
2187+
payload: UnionPydanticRequestType,
2188+
batch: Optional[Batch] = None,
2189+
batch_allowed: Optional[bool] = False,
2190+
build_tags_allowed: Optional[bool] = False,
2191+
):
2192+
"""
2193+
Handle JSON requests for the builds/* API endpoint.
2194+
2195+
:param UnionPydanticRequestType payload: the Pydantic model representing the request.
2196+
:param Batch batch: the batch to specify with the request.
2197+
"""
2198+
request_kwargs = payload.get_json_for_request()
2199+
2200+
keys_to_check = payload.get_keys_to_check_in_db()
2201+
for key in keys_to_check:
2202+
if key in [
2203+
'binary_image',
2204+
'fbc_fragment',
2205+
'from_index',
2206+
'from_bundle_image',
2207+
'source_from_index',
2208+
'target_index',
2209+
'parent_bundle_image',
2210+
]:
2211+
request_kwargs[key] = Image.get_or_create(pull_specification=request_kwargs[key])
2212+
2213+
elif key in ["bundles", "deprecation_list"]:
2214+
images = request_kwargs.get(key, [])
2215+
request_kwargs[key] = [
2216+
Image.get_or_create(pull_specification=image) for image in images
2217+
]
2218+
2219+
elif key == ["operators"]:
2220+
request_kwargs['operators'] = [Operator.get_or_create(name=item) for item in request_kwargs["operators"]]
2221+
else:
2222+
raise ValidationError(f"Unexpected key: {key} during from_json() method.")
2223+
2224+
# current_user.is_authenticated is only ever False when auth is disabled
2225+
if current_user.is_authenticated:
2226+
request_kwargs['user'] = current_user
2227+
2228+
# Add the request to a new batch
2229+
if batch_allowed:
2230+
batch = batch or Batch()
2231+
db.session.add(batch)
2232+
request_kwargs['batch'] = batch
2233+
2234+
request = Request(**request_kwargs)
2235+
2236+
if build_tags_allowed:
2237+
for bt in payload.build_tags:
2238+
request.add_build_tag(bt)
2239+
2240+
request.add_state('in_progress', 'The request was initiated')
2241+
return request

0 commit comments

Comments
 (0)