You could also use a metaclass to generate the class with class
variables baked into it, but that is complicated to do and it may take
a few tries to get it right.

On Wed, Jan 2, 2019 at 8:48 AM Arndt Droullier <[email protected]> wrote:
>
>
> I had the same 'problem' with an older pyramid version. To pass additional 
> configuration values to view code I came to a similar solution like the one 
> described by Mike.
>
> For class based views I used dynamic type generation to store specific values 
> as class attributes which is then used as view code. Its easy to serialize 
> and load the values
> e.g from a json file.
>
> def view_class(values, cls):
>     newcls = type("_extended_"+cls.__name__, (cls,), {})
>     newcls.__values__ = values
>     return newcls
>
> def MyView:
>     # view code
>     # ....
>
> ViewCls = view_class(dict(title="Title"), MyView)
> config.add_view(view=ViewCls, ...)
>
> Arndt.
>
>> I had an idea about this. When you want to associate data with a
>> callable you can use a partial function. For instance:
>>
>> # views.py
>> def my_view(title, request):
>>     ....
>>
>> # __init__.py
>> import functools
>> title = "My Title"
>> view1 = functools.partial(my_view, title)
>> config.add_view(view1, ...)
>>
>> Or you could write a wrapper for view_config that does this.
>>
>> I've never needed additional view data like this because my titles are
>> in my templates or in my view code. (I have a Mako site template with
>> a 'page_title' function which I override in the individual templates.)
>> But if you can't specify the title in either of those places or in the
>> context, then I'd use a partial function like this.
>>
>> Outside Pyramid I'd use a callable class:
>>
>> class MyClass:
>>     def __init__(self, title):
>>         self.title = title
>>     def __call__(self, request):
>>         # Use self.title.
>>
>> But that wouldn't work as a view class because Pyramid makes certain
>> assumptions about view classes that are incompatible with this. You
>> might be able to configure an instance of it:
>>
>> config.add_view(MyClass("My Title"), ...)
>>
>> But I haven't tried it and I don't know whether Pyramid would treat it
>> like a function.
>
>
> --
> You received this message because you are subscribed to the Google Groups 
> "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/pylons-discuss/CAJYR-0P%2BnSjccnX8D5fMFhW0XgiQZ9O%3DkO1BoODynram7%2BAWow%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.



-- 
Mike Orr <[email protected]>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/CAH9f%3DuozZas_iFdCp63-O1AmRrdop-bpUvXnaM0cTocPRTe7yw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to