I forgot the last line. The end of 'my_tween_factory' needs to 'return my_tween'.
On Thu, Jan 7, 2021 at 7:05 PM Mike Orr <sluggos...@gmail.com> wrote: > I have a request-logging tween in only a page of code. It's > straightforward to write from the documentation. > > https://pyramid.readthedocs.io/en/latest/narr/hooks.html#registering-tweens > > I adapted the code for your use case (untested). Your module would have > something like this: > > # Module 'myapp.lib.mytween' > > def includeme(config): > # Calculate the dotted name of the factory function. > # E.g., "myapp.lib.mytween.my_tween_factory". > factory_name = __name__ + ".my_tween_factory" > config.add_tween(factory_name) > # You can wrap app this in an 'if' to conditionally enable it; e.g., > # ``if > pyramid.settings.asbool(config.registry.settings.get("myoption", True))`` > > def my_tween_factory(handler, registry): > # Return a tween callable. > # 'handler' is the next tween or the WSGI application. > # Deployment settings are in 'registry.settings'. > def my_tween(handler, request): > if is_maintenance_mode: # Could limit it to certain > request.path's. > # Return an error response, bypassing the application. > return > pyramid.response.HTTPServiceUnavailable(maintenance_message) > else: > # Call next tween/application and return its response > unchanged. > return handler(request) > > Then list your module in 'pyramid.includes' in the config file. E.g., > "myapp.lib.mytween". > > That 'if is_maintenance_mode' condition could check whether a specified > file exists. The file path could be in a config setting. > > On Thu, Jan 7, 2021 at 3:32 PM C J <work.cedricjos...@gmail.com> wrote: > >> >> That's really interesting Thierry. Can you please show me how to do? >> I have tried to use tweens. I tried many things starting with >> *pyramid.tweens = pyramid_maintenance.tween_maintenance *and modifying >> the *__init__.py* file, however, I do not understand what they are and >> how to use them despite hours spent reading the Pyramid documentation and >> different articles. >> Being working on a website that is already in production I also wonder >> how I would be able to implement your solution based on an external CMS. >> >> >> [image: Mailtrack] >> <https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_campaign=signaturevirality5&> >> Sender >> notified by >> Mailtrack >> <https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_campaign=signaturevirality5&> >> 07/01/21 >> à 21:59:31 >> >> On Thu, Jan 7, 2021 at 9:22 PM Thierry Florac <tflo...@gmail.com> wrote: >> >>> I've built a custom Pyramid tween to handle this and redirect requests >>> while application is up! >>> It can handle redirects (based on regular expressions) before or after >>> the request is handled by Pyramid application, to be able to set the site >>> in "maintenance mode", or to handle custom redirects in case of NotFound >>> exceptions... >>> All configuration is done through our Pyramid CMS. >>> >>> Best regards, >>> Thierry >>> -- >>> https://www.ulthar.net -- http://pyams.readthedocs.io >>> >>> >>> Le jeu. 7 janv. 2021 à 18:43, 'Jonathan Vanasco' via pylons-discuss < >>> pylons-discuss@googlegroups.com> a écrit : >>> >>>> I typically handle this on nginx which sites in front of Pyramid. if >>>> you wanted to do everything in python, you could probably use WSGI >>>> middleware to route to a separate maintenance application or html file. >>>> >>>> On Thursday, January 7, 2021 at 10:09:34 AM UTC-5 C J wrote: >>>> >>>>> Hi everybody, >>>>> >>>>> I am looking for an easy way to temporarily redirect all the users of >>>>> my pyramid website to a maintenance vue without having to comment/delete, >>>>> etc my routes. >>>>> I would like to make it easy to re-activate the others routes when the >>>>> maintenance is done. >>>>> I found this : >>>>> https://pypi.org/project/pyramid_maintenance/ >>>>> but I always get : >>>>> >>>>> in renderer >>>>> raise ValueError('No such renderer factory %s' % str(self.type)) >>>>> ValueError: No such renderer factory .jinja2" >>>>> >>>>> with my browser displaying : >>>>> "Internal Server Error The server encountered an unexpected internal >>>>> server error (generated by waitress)" >>>>> >>>>> I am new to Pyramid so please give me the necessary details step by >>>>> step. >>>>> >>>>> Best regards. >>>>> Cedric J. >>>>> >>>> -- >>>> 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 pylons-discuss+unsubscr...@googlegroups.com. >>>> To view this discussion on the web visit >>>> https://groups.google.com/d/msgid/pylons-discuss/98a779a5-5bcc-4971-a271-a202cc49f732n%40googlegroups.com >>>> <https://groups.google.com/d/msgid/pylons-discuss/98a779a5-5bcc-4971-a271-a202cc49f732n%40googlegroups.com?utm_medium=email&utm_source=footer> >>>> . >>>> >>> -- >>> You received this message because you are subscribed to a topic in the >>> Google Groups "pylons-discuss" group. >>> To unsubscribe from this topic, visit >>> https://groups.google.com/d/topic/pylons-discuss/jKTnofibd00/unsubscribe >>> . >>> To unsubscribe from this group and all its topics, send an email to >>> pylons-discuss+unsubscr...@googlegroups.com. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/pylons-discuss/CAPX_VWBO5j0im6r0RRKEf%3D%3DzXyx5y_Qp%3DUJ5bntEGQtdiDejKQ%40mail.gmail.com >>> <https://groups.google.com/d/msgid/pylons-discuss/CAPX_VWBO5j0im6r0RRKEf%3D%3DzXyx5y_Qp%3DUJ5bntEGQtdiDejKQ%40mail.gmail.com?utm_medium=email&utm_source=footer> >>> . >>> >> -- >> 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 pylons-discuss+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/pylons-discuss/CAJgq6FJC11OfFNbfhhPwfMKRJsJfpd2UpWUDs5SAm5hKaMWpzA%40mail.gmail.com >> <https://groups.google.com/d/msgid/pylons-discuss/CAJgq6FJC11OfFNbfhhPwfMKRJsJfpd2UpWUDs5SAm5hKaMWpzA%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> > > > -- > Mike Orr <sluggos...@gmail.com> > -- Mike Orr <sluggos...@gmail.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 pylons-discuss+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/CAH9f%3DuorGVTX7vCBAZTwvnOhKCos4q1coiZQnzna0zP_v4aeCA%40mail.gmail.com.