Re: [pylons-discuss] generating a configured Request?

2025-03-25 Thread Michael Merickel
The request factory doesn't fully do every step of initializing a request
since it's a user-overridable hook. It's not really intended to be used
directly. To have a fully initialize request you later need to do a couple
steps iirc.

1. request.registry = app.registry
2. pyramid.request.apply_request_extensions(request)

This is off the top of my head so I may be forgetting a step, apologies but
hopefully this is helpful.

The "right" way to do this is to use pyramid.scripting.prepare:

with pyramid.scripting.prepare(registry=app.registry) as env:
request = env['request']

On Tue, Mar 25, 2025 at 12:27 PM Jonathan Vanasco 
wrote:

> I can't seem to generate a request properly.
>
> For example, in `main(`:
>
> from pyramid.interfaces import IRequestFactory
> from pyramid.request import Request
>
> app = config.make_wsgi_app()
> request_factory = app.registry.queryUtility(IRequestFactory,
> default=Request)
> request = request_factory.blank("/")
>
> The query for IRequestFactory always returns None,  leaving me with the
> default `Request` (which has none of the configured `add_request_method`
> attributes)
>
> I've also tried this using `get_app(config_uri)` from scripting.
>
> Am I querying the wrong interface, or is this just not a compatible
> use-case?
>
>
> --
> 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 view this discussion visit
> https://groups.google.com/d/msgid/pylons-discuss/c46029eb-e0c4-4f75-82d1-2971f55a49bcn%40googlegroups.com
> 
> .
>


-- 

Michael

-- 
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 view this discussion visit 
https://groups.google.com/d/msgid/pylons-discuss/CAKdhhwGmwe2SDV2asnwWiex9g79zBHsCjznmW-ixTESMvF7TEA%40mail.gmail.com.


Re: [pylons-discuss] generating a configured Request?

2025-03-25 Thread Jonathan Vanasco
> Something like this should work:

Wonderful!  Thank you.

I am hoping to replace all mocked requests in tests and some commandline 
utilities.  

On Tuesday, March 25, 2025 at 6:16:08 PM UTC-4 Theron Luhn wrote:

> Something like this should work:
>
> request = Request.blank(“/“, POST={“foo”: “bar”})
> with pyramid.scripting.prepare(registry=app.registry, request=request):
> ...
>
> — Theron
>
>
>
> On Mar 25, 2025, at 1:49 PM, Jonathan Vanasco  wrote:
>
> Michael- one more question:
>
> Do you know if there is better way to stuff POST/GET vars in this context 
> than the following?
>
> > request.environ["REQUEST_METHOD"] = "POST"
> > wsgi_input = BytesIO(b"")
> > request.body_file_raw = wsgi_input
> > request.environ["webob._parsed_post_vars"] = (options, wsgi_input)
>
> This is leveraging (abusing) the `webob._parsed_post_vars` storage and a 
> check against `.body_file_raw`
>
>
> On Tuesday, March 25, 2025 at 3:37:58 PM UTC-4 Michael Merickel wrote:
>
>> The request factory doesn't fully do every step of initializing a request 
>> since it's a user-overridable hook. It's not really intended to be used 
>> directly. To have a fully initialize request you later need to do a couple 
>> steps iirc.
>>
>> 1. request.registry = app.registry
>> 2. pyramid.request.apply_request_extensions(request)
>>
>> This is off the top of my head so I may be forgetting a step, apologies 
>> but hopefully this is helpful.
>>
>> The "right" way to do this is to use pyramid.scripting.prepare:
>>
>> with pyramid.scripting.prepare(registry=app.registry) as env:
>> request = env['request']
>>
>> On Tue, Mar 25, 2025 at 12:27 PM Jonathan Vanasco  
>> wrote:
>>
>>> I can't seem to generate a request properly.
>>>
>>> For example, in `main(`:
>>>
>>> from pyramid.interfaces import IRequestFactory
>>> from pyramid.request import Request
>>>
>>> app = config.make_wsgi_app()
>>> request_factory = app.registry.queryUtility(IRequestFactory, 
>>> default=Request)
>>> request = request_factory.blank("/")
>>>
>>> The query for IRequestFactory always returns None,  leaving me with the 
>>> default `Request` (which has none of the configured `add_request_method` 
>>> attributes)
>>>
>>> I've also tried this using `get_app(config_uri)` from scripting. 
>>>
>>> Am I querying the wrong interface, or is this just not a compatible 
>>> use-case?
>>>
>>>
>>>
>>> -- 
>>> 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 view this discussion visit 
>>> https://groups.google.com/d/msgid/pylons-discuss/c46029eb-e0c4-4f75-82d1-2971f55a49bcn%40googlegroups.com
>>>  
>>> 
>>> .
>>>
>>
>>
>> -- 
>>
>> Michael
>>
>
> -- 
> 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 view this discussion visit 
> https://groups.google.com/d/msgid/pylons-discuss/ca785f12-2b6c-4f64-9f05-3809058987ecn%40googlegroups.com
>  
> 
> .
>
>
>

-- 
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 view this discussion visit 
https://groups.google.com/d/msgid/pylons-discuss/cedb93d5-ac7a-41ee-b169-053f5ef870c8n%40googlegroups.com.


Re: [pylons-discuss] generating a configured Request?

2025-03-25 Thread Theron Luhn
Something like this should work:

request = Request.blank(“/“, POST={“foo”: “bar”})
with pyramid.scripting.prepare(registry=app.registry, request=request):
...

— Theron



> On Mar 25, 2025, at 1:49 PM, Jonathan Vanasco  wrote:
> 
> Michael- one more question:
> 
> Do you know if there is better way to stuff POST/GET vars in this context 
> than the following?
> 
> > request.environ["REQUEST_METHOD"] = "POST"
> > wsgi_input = BytesIO(b"")
> > request.body_file_raw = wsgi_input
> > request.environ["webob._parsed_post_vars"] = (options, wsgi_input)
> 
> This is leveraging (abusing) the `webob._parsed_post_vars` storage and a 
> check against `.body_file_raw`
> 
> 
> On Tuesday, March 25, 2025 at 3:37:58 PM UTC-4 Michael Merickel wrote:
>> The request factory doesn't fully do every step of initializing a request 
>> since it's a user-overridable hook. It's not really intended to be used 
>> directly. To have a fully initialize request you later need to do a couple 
>> steps iirc.
>> 
>> 1. request.registry = app.registry
>> 2. pyramid.request.apply_request_extensions(request)
>> 
>> This is off the top of my head so I may be forgetting a step, apologies but 
>> hopefully this is helpful.
>> 
>> The "right" way to do this is to use pyramid.scripting.prepare:
>> 
>> with pyramid.scripting.prepare(registry=app.registry) as env:
>> request = env['request']
>> 
>> On Tue, Mar 25, 2025 at 12:27 PM Jonathan Vanasco > 
>> wrote:
>>> I can't seem to generate a request properly.
>>> 
>>> For example, in `main(`:
>>> 
>>> from pyramid.interfaces import IRequestFactory
>>> from pyramid.request import Request
>>> 
>>> app = config.make_wsgi_app()
>>> request_factory = app.registry.queryUtility(IRequestFactory, 
>>> default=Request)
>>> request = request_factory.blank("/")
>>> 
>>> The query for IRequestFactory always returns None,  leaving me with the 
>>> default `Request` (which has none of the configured `add_request_method` 
>>> attributes)
>>> 
>>> I've also tried this using `get_app(config_uri)` from scripting. 
>>> 
>>> Am I querying the wrong interface, or is this just not a compatible 
>>> use-case?
>>> 
>>> 
>>> 
>> 
>>> -- 
>>> 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 view this discussion visit 
>>> https://groups.google.com/d/msgid/pylons-discuss/c46029eb-e0c4-4f75-82d1-2971f55a49bcn%40googlegroups.com
>>>  
>>> .
>> 
>> 
>> 
>> --
>> 
>> Michael
> 
> 
> -- 
> 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 view this discussion visit 
> https://groups.google.com/d/msgid/pylons-discuss/ca785f12-2b6c-4f64-9f05-3809058987ecn%40googlegroups.com
>  
> .

-- 
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 view this discussion visit 
https://groups.google.com/d/msgid/pylons-discuss/8799F2ED-D211-4921-B84C-32A3F783A208%40luhn.com.


Re: [pylons-discuss] generating a configured Request?

2025-03-25 Thread Jonathan Vanasco
Michael- one more question:

Do you know if there is better way to stuff POST/GET vars in this context 
than the following?

> request.environ["REQUEST_METHOD"] = "POST"
> wsgi_input = BytesIO(b"")
> request.body_file_raw = wsgi_input
> request.environ["webob._parsed_post_vars"] = (options, wsgi_input)

This is leveraging (abusing) the `webob._parsed_post_vars` storage and a 
check against `.body_file_raw`


On Tuesday, March 25, 2025 at 3:37:58 PM UTC-4 Michael Merickel wrote:

> The request factory doesn't fully do every step of initializing a request 
> since it's a user-overridable hook. It's not really intended to be used 
> directly. To have a fully initialize request you later need to do a couple 
> steps iirc.
>
> 1. request.registry = app.registry
> 2. pyramid.request.apply_request_extensions(request)
>
> This is off the top of my head so I may be forgetting a step, apologies 
> but hopefully this is helpful.
>
> The "right" way to do this is to use pyramid.scripting.prepare:
>
> with pyramid.scripting.prepare(registry=app.registry) as env:
> request = env['request']
>
> On Tue, Mar 25, 2025 at 12:27 PM Jonathan Vanasco  
> wrote:
>
>> I can't seem to generate a request properly.
>>
>> For example, in `main(`:
>>
>> from pyramid.interfaces import IRequestFactory
>> from pyramid.request import Request
>>
>> app = config.make_wsgi_app()
>> request_factory = app.registry.queryUtility(IRequestFactory, 
>> default=Request)
>> request = request_factory.blank("/")
>>
>> The query for IRequestFactory always returns None,  leaving me with the 
>> default `Request` (which has none of the configured `add_request_method` 
>> attributes)
>>
>> I've also tried this using `get_app(config_uri)` from scripting. 
>>
>> Am I querying the wrong interface, or is this just not a compatible 
>> use-case?
>>
>>
>> -- 
>> 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 view this discussion visit 
>> https://groups.google.com/d/msgid/pylons-discuss/c46029eb-e0c4-4f75-82d1-2971f55a49bcn%40googlegroups.com
>>  
>> 
>> .
>>
>
>
> -- 
>
> Michael
>

-- 
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 view this discussion visit 
https://groups.google.com/d/msgid/pylons-discuss/ca785f12-2b6c-4f64-9f05-3809058987ecn%40googlegroups.com.


Re: [pylons-discuss] generating a configured Request?

2025-03-25 Thread Jonathan Vanasco
> The "right" way to do this is to use pyramid.scripting.prepare:

That's even easier for me to implement.  Thank you so much, Michael!

On Tuesday, March 25, 2025 at 3:37:58 PM UTC-4 Michael Merickel wrote:

> The request factory doesn't fully do every step of initializing a request 
> since it's a user-overridable hook. It's not really intended to be used 
> directly. To have a fully initialize request you later need to do a couple 
> steps iirc.
>
> 1. request.registry = app.registry
> 2. pyramid.request.apply_request_extensions(request)
>
> This is off the top of my head so I may be forgetting a step, apologies 
> but hopefully this is helpful.
>
> The "right" way to do this is to use pyramid.scripting.prepare:
>
> with pyramid.scripting.prepare(registry=app.registry) as env:
> request = env['request']
>
> On Tue, Mar 25, 2025 at 12:27 PM Jonathan Vanasco  
> wrote:
>
>> I can't seem to generate a request properly.
>>
>> For example, in `main(`:
>>
>> from pyramid.interfaces import IRequestFactory
>> from pyramid.request import Request
>>
>> app = config.make_wsgi_app()
>> request_factory = app.registry.queryUtility(IRequestFactory, 
>> default=Request)
>> request = request_factory.blank("/")
>>
>> The query for IRequestFactory always returns None,  leaving me with the 
>> default `Request` (which has none of the configured `add_request_method` 
>> attributes)
>>
>> I've also tried this using `get_app(config_uri)` from scripting. 
>>
>> Am I querying the wrong interface, or is this just not a compatible 
>> use-case?
>>
>>
>> -- 
>> 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 view this discussion visit 
>> https://groups.google.com/d/msgid/pylons-discuss/c46029eb-e0c4-4f75-82d1-2971f55a49bcn%40googlegroups.com
>>  
>> 
>> .
>>
>
>
> -- 
>
> Michael
>

-- 
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 view this discussion visit 
https://groups.google.com/d/msgid/pylons-discuss/2ba2ddbd-e1a4-49bb-b300-ad392048964en%40googlegroups.com.