Skip to content

Commit

Permalink
Unify from_json methods
Browse files Browse the repository at this point in the history
  • Loading branch information
xDaile committed Dec 11, 2023
1 parent 9103cdd commit 8a5a898
Showing 1 changed file with 58 additions and 235 deletions.
293 changes: 58 additions & 235 deletions iib/web/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,19 +1012,6 @@ def _from_json(
db.session.add(batch)
request_kwargs['batch'] = batch

@staticmethod
def from_json_replacement(
request_kwargs: RequestPayload,
batch: Optional[Batch] = None,
):
# current_user.is_authenticated is only ever False when auth is disabled
if current_user.is_authenticated:
request_kwargs['user'] = current_user

# Add the request to a new batch
batch = batch or Batch()
db.session.add(batch)
request_kwargs['batch'] = batch

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

def from_json_replacement(
cls,
payload: AddPydanticModel,
batch: Optional[Batch] = None,
):
"""
Handle JSON requests for the builds/add API endpoint.
:param AddPydanticModel payload: the Pydantic model representing the request.
:param Batch batch: the batch to specify with the request.
"""
request_kwargs = payload.get_json_for_request()

request_kwargs["bundles"] = [
Image.get_or_create(pull_specification=item) for item in payload.bundles
]
request_kwargs["deprecation_list"] = [
Image.get_or_create(pull_specification=item) for item in payload.deprecation_list
]
request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)
request_kwargs['from_index'] = Image.get_or_create(pull_specification=payload.from_index)

# current_user.is_authenticated is only ever False when auth is disabled
if current_user.is_authenticated:
request_kwargs['user'] = current_user

# Add the request to a new batch
batch = batch or Batch()
db.session.add(batch)
request_kwargs['batch'] = batch

request = cls(**request_kwargs)

for bt in payload.build_tags:
request.add_build_tag(bt)

request.add_state('in_progress', 'The request was initiated')
return request

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

return request

def from_json_replacement(
cls,
payload: RmPydanticModel,
batch: Optional[Batch] = None,
):
"""
Handle JSON requests for the builds/rm API endpoint.
:param RmPydanticModel payload: the Pydantic model representing the request.
:param Batch batch: the batch to specify with the request.
"""
request_kwargs = payload.get_json_for_request()

request_kwargs['operators'] = [Operator.get_or_create(name=item) for item in payload.operators]
request_kwargs['from_index'] = Image.get_or_create(pull_specification=payload.from_index)
request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)

if current_user.is_authenticated:
request_kwargs['user'] = current_user

# Add the request to a new batch
batch = batch or Batch()
db.session.add(batch)
request_kwargs['batch'] = batch

request = cls(**request_kwargs)
request.add_state('in_progress', 'The request was initiated')

for bt in payload.build_tags:
request.add_build_tag(bt)

return request

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

def from_json_replacement(
cls,
payload: RegenerateBundlePydanticModel,
batch: Optional[Batch] = None,
):
"""
Handle JSON requests for the builds/egenerate-bundle API endpoint.
:param RegenerateBundlePydanticModel payload: the Pydantic model representing the request.
:param Batch batch: the batch to specify with the request.
"""
request_kwargs = payload.get_json_for_request()

request_kwargs['from_bundle_image'] = Image.get_or_create(
pull_specification=payload.from_bundle_image
)

# current_user.is_authenticated is only ever False when auth is disabled
if current_user.is_authenticated:
request_kwargs['user'] = current_user

# Add the request to a new batch
batch = batch or Batch()
db.session.add(batch)
request_kwargs['batch'] = batch

request = cls(**request_kwargs)
request.add_state('in_progress', 'The request was initiated')
return request

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

def from_json_replacement(
cls,
payload: MergeIndexImagePydanticModel,
batch: Optional[Batch] = None,
):
"""
Handle JSON requests for the builds/merge-index-image API endpoint.
:param MergeIndexImagePydanticModel payload: the Pydantic model representing the request.
:param Batch batch: the batch to specify with the request.
"""
request_kwargs = payload.get_json_for_request()

request_kwargs['deprecation_list'] = [
Image.get_or_create(pull_specification=item) for item in payload.deprecation_list
]
request_kwargs['source_from_index'] = Image.get_or_create(
pull_specification=payload.source_from_index
)
request_kwargs['target_index'] = Image.get_or_create(pull_specification=payload.target_index)
request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)

# current_user.is_authenticated is only ever False when auth is disabled
if current_user.is_authenticated:
request_kwargs['user'] = current_user

# Add the request to a new batch
batch = batch or Batch()
db.session.add(batch)
request_kwargs['batch'] = batch

request = cls(**request_kwargs)

for bt in payload.build_tags:
request.add_build_tag(bt)

request.add_state('in_progress', 'The request was initiated')
return request

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

return request

def from_json_replacement(
cls,
payload: CreateEmptyIndexPydanticModel,
batch: Optional[Batch] = None,
):
"""
Handle JSON requests for the builds/create-empty-index API endpoint.
:param CreateEmptyIndexPydanticModel payload: the Pydantic model representing the request.
:param Batch batch: the batch to specify with the request.
"""
request_kwargs = payload.get_json_for_request()

request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)
request_kwargs['from_index'] = Image.get_or_create(pull_specification=payload.from_index)

# current_user.is_authenticated is only ever False when auth is disabled
if current_user.is_authenticated:
request_kwargs['user'] = current_user

# Add the request to a new batch
batch = batch or Batch()
db.session.add(batch)
request_kwargs['batch'] = batch

request = cls(**request_kwargs)
request.add_state('in_progress', 'The request was initiated')

return request

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

def from_json_replacement(
cls,
payload: RecursiveRelatedBundlesPydanticModel,
batch: Optional[Batch] = None,
):
"""
Handle JSON requests for the builds/recursive-related-bundles API endpoint.
:param RecursiveRelatedBundlesPydanticModel payload: the Pydantic model representing the request.
:param Batch batch: the batch to specify with the request.
"""

request_kwargs = payload.get_json_for_request()

request_kwargs['parent_bundle_image'] = Image.get_or_create(pull_specification=payload.parent_bundle_image)

# current_user.is_authenticated is only ever False when auth is disabled
if current_user.is_authenticated:
request_kwargs['user'] = current_user

# Add the request to a new batch
batch = batch or Batch()
db.session.add(batch)
request_kwargs['batch'] = batch

request = cls(**request_kwargs)
request.add_state('in_progress', 'The request was initiated')
return request

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

def from_json_replacement(
cls,
payload: FbcOperationsPydanticModel,
):
"""
Handle JSON requests for the builds/fbc-operations API endpoint.
:param FbcOperationsPydanticModel payload: the Pydantic model representing the request.
:param Batch batch: the batch to specify with the request.
"""
request_kwargs = payload.get_json_for_request()

request_kwargs['fbc_fragment'] = Image.get_or_create(pull_specification=payload.fbc_fragment)
request_kwargs['binary_image'] = Image.get_or_create(pull_specification=payload.binary_image)
request_kwargs['from_index'] = Image.get_or_create(pull_specification=payload.from_index)

# current_user.is_authenticated is only ever False when auth is disabled
if current_user.is_authenticated:
request_kwargs['user'] = current_user

request = cls(**request_kwargs)

for bt in payload.build_tags:
request.add_build_tag(bt)

request.add_state('in_progress', 'The request was initiated')
return request


def to_json(self, verbose: Optional[bool] = True) -> FbcOperationRequestResponse:
"""
Expand Down Expand Up @@ -2416,3 +2181,61 @@ def get_mutable_keys(self) -> Set[str]:
rv.update(self.get_index_image_mutable_keys())
rv.add('fbc_fragment_resolved')
return rv


def from_json_replacement_even_shorter(
payload: UnionPydanticRequestType,
batch: Optional[Batch] = None,
batch_allowed: Optional[bool] = False,
build_tags_allowed: Optional[bool] = False,
):
"""
Handle JSON requests for the builds/* API endpoint.
:param UnionPydanticRequestType payload: the Pydantic model representing the request.
:param Batch batch: the batch to specify with the request.
"""
request_kwargs = payload.get_json_for_request()

keys_to_check = payload.get_keys_to_check_in_db()
for key in keys_to_check:
if key in [
'binary_image',
'fbc_fragment',
'from_index',
'from_bundle_image',
'source_from_index',
'target_index',
'parent_bundle_image',
]:
request_kwargs[key] = Image.get_or_create(pull_specification=request_kwargs[key])

elif key in ["bundles", "deprecation_list"]:
images = request_kwargs.get(key, [])
request_kwargs[key] = [
Image.get_or_create(pull_specification=image) for image in images
]

elif key == ["operators"]:
request_kwargs['operators'] = [Operator.get_or_create(name=item) for item in request_kwargs["operators"]]
else:
raise ValidationError(f"Unexpected key: {key} during from_json() method.")

# current_user.is_authenticated is only ever False when auth is disabled
if current_user.is_authenticated:
request_kwargs['user'] = current_user

# Add the request to a new batch
if batch_allowed:
batch = batch or Batch()
db.session.add(batch)
request_kwargs['batch'] = batch

request = Request(**request_kwargs)

if build_tags_allowed:
for bt in payload.build_tags:
request.add_build_tag(bt)

request.add_state('in_progress', 'The request was initiated')
return request

0 comments on commit 8a5a898

Please sign in to comment.