Skip to content

Commit

Permalink
refactor serializer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Faakhir30 committed Jan 19, 2025
1 parent 218e64d commit 7b3f360
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
File renamed without changes.
57 changes: 36 additions & 21 deletions src/plone/restapi/serializer/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,48 @@
from zope.publisher.interfaces import IRequest
from zope.interface import Interface

class SerializeRegistryMixin:
def serialize(self):
batch = HypermediaBatch(self.request, list(self.records.keys()))
results = {
"@id": batch.canonical_url,
"items_total": batch.items_total,
"batching": batch.links if batch.links else {},
"items": [
self.make_item(key) for key in batch
]
}
return results

def make_item(self, key):
record = self.records[key]
schema = getMultiAdapter(
(record.field, record, self.request), IJsonSchemaProvider
)
return {
"name": key,
"value": self.records[key],
"schema": {"properties": schema.get_schema()},
}

@implementer(ISerializeToJson)
@adapter(IRegistry, IRequest, Interface)
class SerializeRegistryToJson:
def __init__(self, registry, request, records=None):
class SerializeRegistryToJsonWithFilters(SerializeRegistryMixin):
def __init__(self, registry, request, records):
self.registry = registry
self.request = request
self.records = records or registry.records

self.records = records
def __call__(self):
batch = HypermediaBatch(self.request, list(self.records.keys()))
return self.serialize()

results = {}
results["@id"] = batch.canonical_url
results["items_total"] = batch.items_total
if batch.links:
results["batching"] = batch.links

def make_item(key):
record = self.records[key]
schema = getMultiAdapter(
(record.field, record, self.request), IJsonSchemaProvider
)
data = {"name": key, "value": self.registry[key]}
__traceback_info__ = (record, record.field, schema)
data["schema"] = {"properties": schema.get_schema()}
return data
@implementer(ISerializeToJson)
@adapter(IRegistry, IRequest)
class SerializeRegistryToJson(SerializeRegistryMixin):
def __init__(self, registry, request):
self.registry = registry
self.request = request
self.records = registry.records

results["items"] = [make_item(key) for key in batch]
return results
def __call__(self):
return self.serialize()

0 comments on commit 7b3f360

Please sign in to comment.