Skip to content

Commit

Permalink
frontend: refactor InputSchema and ParamsSchema implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
FrostyX committed Aug 3, 2024
1 parent 96cc4cf commit 8d5c46c
Showing 1 changed file with 21 additions and 41 deletions.
62 changes: 21 additions & 41 deletions frontend/coprs_frontend/coprs/views/apiv3_ns/schema/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,35 +116,17 @@ class InputSchema(Schema):
@property
def required_attrs(self) -> list:
"""
Specify required attributes in model in these methods if needed.
Specify this property in subclass with required attributes. If no attribute
is required, then don;t specify this property in subclass.
In case every attribute is required in input schema, you have two options:
A):
Specify every attribute in required_attrs property.
B):
Define `__all_required` attribute in input schema and set it to `True`.
A list of required attributes in the schema
"""
return []

def _do_require_attrs(self):
change_this_args_as_required = self.required_attrs
if getattr(self, f"_{self.__class__.__name__}__all_required", False):
change_this_args_as_required = [
getattr(self, field.name) for field in self.public_fields()
]

for field in change_this_args_as_required:
field.required = True

def input_model(self):
"""
Returns an input model (input to @ns.expect()) with properly set required
parameters.
"""
self._do_require_attrs()
for field in self.required_attrs:
field.required = True
return api.model(self.__class__.__name__, self.schema())


Expand All @@ -164,23 +146,13 @@ def params_schema(self) -> dict:
documentation taken from api_foo_ns.route. Documentation is a dictionary
in specific structure to match Swagger UI schema.
"""
super()._do_require_attrs()
schema = {}
for field in self.public_fields():
attr = getattr(self, field.name)
schema[field.name] = attr.schema()

keys_to_delete = []
for key, val in schema[field.name].items():
if val is None:
keys_to_delete.append(key)

for key in keys_to_delete:
schema[field.name].pop(key)

if attr.required:
schema[field.name] |= {"required": True}
return {x.name: self._field_schema(x) for x in self.public_fields()}

def _field_schema(self, field):
attr = getattr(self, field.name)
schema = {k: v for k, v in attr.schema().items() if v is not None}
if attr in self.required_attrs:
schema |= {"required": True}
return schema


Expand Down Expand Up @@ -244,7 +216,9 @@ class ProjectChrootGet(ParamsSchema):
projectname: String = fields.projectname
chrootname: String = mock_chroot

__all_required: bool = True
@property
def required_attrs(self) -> list:
return [self.ownername, self.projectname, self.chrootname]


@dataclass
Expand Down Expand Up @@ -566,14 +540,18 @@ class FullnameSchema(ParamsSchema):
ownername: String = fields.ownername
projectname: String = fields.projectname

__all_required: bool = True
@property
def required_attrs(self) -> list:
return [self.ownername, self.projectname]


@dataclass
class CanBuildParams(FullnameSchema):
who: String = String(example="user123")

__all_required: bool = True
@property
def required_attrs(self) -> list:
return [self.who]


@dataclass
Expand Down Expand Up @@ -607,7 +585,9 @@ class BuildChrootParams(ParamsSchema):
build_id: Integer = id_field
chrootname: String = mock_chroot

__all_required: bool = True
@property
def required_attrs(self) -> list:
return [self.build_id, self.chrootname]


@dataclass
Expand Down

0 comments on commit 8d5c46c

Please sign in to comment.