Controller and ControllerFactory¶
-
class
pycommunicate.server.bases.controller.
ControllerFactory
¶ This class is used to define how a
Controller
should be created. Its methods are all chainable, or in other words, all return the instance. The constructor has no arguments-
add_view
(view)¶ Adds a
View
to the controller factory’s view list.Warning
Adding the same view twice can cause issues
Parameters: view (pycommunicate.server.bases.views.View) – The view class to add
-
set_default_view
(view)¶ Sets the default view for the controller factory. The default view is the one that is shown initially.
Warning
The client will crash with a 500 server error if this is not set.
Parameters: view (pycommunicate.server.bases.views.View) – The view class to set as default.
-
with_before_connect
(before_connect)¶ Sets the before_connect function. This is called as soon as a request comes in for the page, and should be used to do something before a page loads.
before_connect takes one argument, an instance of
CallCTX
. This CallCTX contains one function,abort
, which when called will interrupt the request and send back the error code passed to it.Parameters: before_connect (function) – The before_connect function. See above for signature New in version 0.0.7.
-
-
class
pycommunicate.server.bases.controller.
Controller
¶ The Controller class handles one url, or route. It contains multiple
View
and manages switching between them.Warning
Do not try and create
Controller
instances on your own. UseControllerFactory
for that instead.-
route_data
¶ This contains the values of the variable parts in the route. See
add_controller()
for more information on variable route parts.
-
d
¶ This is the data object, a simple dictionary that use can use to store data across multiple views. It is reset every request, for full sessions across requests use
user.session
instead.
-
user
¶ An instance of
User
of which this controller is currently servicing. Use itssession
attribute for proper sessions.
-
special_return_handler
¶ If this is not None, then whatever this function returns will be sent directly to flask as the response. Use with caution.
New in version 0.0.7.
-
controller
¶ An instance of
Templater
.
-
change_view
(new_view_index)¶ Change the active view to the index provided. View indices start at 0 and increase in the order you added them in the controller factory.
Note
This will only do anything if the page has the pycommunicate JS libraries loaded. This function will not work from within a
render()
function.Parameters: new_view_index (int) – The new view index to switch to.
-
redirect
(location)¶ If called from a child view’s
render()
function, this will change the special_return_handler to a function that returns a redirect to the location. Otherwise, it signals the page to redirect elsewhere.Note
This will only do anything outside of
render()
if the page has the pycommunicate JS libraries loaded.Parameters: location (str) – The url to redirect to.
-