[docs]defget_basic_session(request,**kwargs):""" Create/get a "basic" Beaker session object. """kwargs['use_cookies']=FalsereturnBeakerSession(request,**kwargs)
[docs]defget_progress_session(request,key,**kwargs):""" Create/get a Beaker session object, to be used for progress. """kwargs['id']=f'{request.session.id}.progress.{key}'returnget_basic_session(request,**kwargs)
[docs]classSessionProgress(ProgressBase):""" Progress indicator which uses Beaker session storage to track current status. This is a subclass of :class:`wuttjamaican:wuttjamaican.progress.ProgressBase`. A view callable can create one of these, and then pass it into :meth:`~wuttjamaican.app.AppHandler.progress_loop()` or similar. As the loop updates progress along the way, this indicator will update the Beaker session to match. Separately then, the client side can send requests for the :func:`~wuttaweb.views.progress.progress()` view, to fetch current status out of the Beaker session. :param request: Current :term:`request` object. :param key: Unique key for this progress indicator. Used to distinguish progress indicators in the Beaker session. Note that in addition to :meth:`~wuttjamaican:wuttjamaican.progress.ProgressBase.update()` and :meth:`~wuttjamaican:wuttjamaican.progress.ProgressBase.finish()` this progres class has some extra attributes and methods: .. attribute:: success_msg Optional message to display to the user (via session flash) when the operation completes successfully. .. attribute:: success_url URL to which user should be redirected, once the operation completes. .. attribute:: error_url URL to which user should be redirected, if the operation encounters an error. If not specified, will fall back to :attr:`success_url`. """def__init__(self,request,key,success_msg=None,success_url=None,error_url=None):self.key=keyself.success_msg=success_msgself.success_url=success_urlself.error_url=error_urlorself.success_urlself.session=get_progress_session(request,key)self.clear()def__call__(self,message,maximum):self.clear()self.session['message']=messageself.session['maximum']=maximumself.session['maximum_display']=f'{maximum:,d}'self.session['value']=0self.session.save()returnselfdefclear(self):""" """self.session.clear()self.session['complete']=Falseself.session['error']=Falseself.session.save()defupdate(self,value):""" """self.session.load()self.session['value']=valueself.session.save()
[docs]defhandle_error(self,error,error_url=None):""" This should be called by the view code, within a try/catch block upon error. The session storage will be updated to reflect details of the error. Next time client requests the progress status it will learn of the error and redirect the user. :param error: :class:`python:Exception` instance. :param error_url: Optional redirect URL; if not specified :attr:`error_url` is used. """self.session.load()self.session['error']=Trueself.session['error_msg']=str(error)self.session['error_url']=error_urlorself.error_urlself.session.save()
[docs]defhandle_success(self,success_msg=None,success_url=None):""" This should be called by the view code, when the long-running operation completes. The session storage will be updated to reflect the completed status. Next time client requests the progress status it will discover it has completed, and redirect the user. :param success_msg: Optional message to display to the user (via session flash) when the operation completes successfully. If not specified :attr:`success_msg` (or nothing) is used :param success_url: Optional redirect URL; if not specified :attr:`success_url` is used. """self.session.load()self.session['complete']=Trueself.session['success_msg']=success_msgorself.success_msgself.session['success_url']=success_urlorself.success_urlself.session.save()