Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adaptor implementations should be generic, they should not directly read request.form #1604

Open
tiberiuichim opened this issue Mar 23, 2023 · 2 comments

Comments

@tiberiuichim
Copy link
Contributor

An adapter should be a reusable piece of infrastructure. Its implementation should be as generic as possible. But here, and in many other places, we have the adaptors directly reading the request form. This makes them difficult to use. It's the equivalent of depending on a single global variable (the request.form) that's used across the whole system. That's not flexible.

metadata = self.request.form.get("summary_serializer_metadata", None)

@tiberiuichim tiberiuichim changed the title Adaptor implementation should be generic, they should not directly read request.form Adaptor implementations should be generic, they should not directly read request.form Mar 23, 2023
@davisagli
Copy link
Member

@tiberiuichim I'm not sure I understand the context here. Let's focus on the specific use case. What are you trying to do with the summary serializer or change about it that is currently difficult?

@tiberiuichim
Copy link
Contributor Author

@davisagli

I have a service (exposed as an expander) where I want to expose some serialized brains. It's "adjacent information", not strictly related to the default content that's serialized.

I want to serialize those brains with their full metadata. I can't do:

serializer = getMultiAdapter((brain, request), ISerializeToJsonSummary)

because there's no way of passing any option to that adaptor. Instead, I have to do:

class FakeRequest:
    def __init__(self, form):
        self.form = form

    def set(self, k, value):
        self.form[k] = value

    def get(self, k, default=None):
        return self.form.get(k, default)


def tojson(brain):
    form = {"metadata_fields": "_all"}
    request = FakeRequest(form)
    serializer = getMultiAdapter((brain, request), ISerializeToJsonSummary)
    return serializer()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants