toyz.web package — Toyz 0.0.0 documentation

# toyz.web package

## Submodules

## toyz.web.app module

Runs the webapp for Toyz

class `toyz.web.app.` `AuthHandler`

  Subclass for all secure handlers.

  `get_current_user`()

    Load the name of the current user

class `toyz.web.app.` `AuthLoginHandler`(application, request, **kwargs)

  Bases: `toyz.web.app.AuthHandler`, `tornado.web.RequestHandler`

  `get`()

    Send the login.html page to the client

  `initialize`()

    When the handler is initialized, set the path to the template that needs to be rendered

  `post`()

    Load the user_id and password passed to the server from the client and check authentication

  `set_current_user`(user_id)

    Send a secure cookie to the client to keep the user logged into the system

    Parameters
      user_id (string ): User id of the current user

class `toyz.web.app.` `AuthLogoutHandler`(application, request, **kwargs)

  Bases: `tornado.web.RequestHandler`

  `get`()

    Clear the users cookie and log them out

class `toyz.web.app.` `AuthMainHandler`(application, request, **kwargs)

  Bases: `toyz.web.app.MainHandler`

  `toyz.web.app.MainHandler` extensions when using secure cookies.

  `get`(*args, **kwargs)

class `toyz.web.app.` `AuthStaticFileHandler`(application, request, **kwargs)

  Bases: `toyz.web.app.AuthHandler`, `tornado.web.StaticFileHandler`

  Handles static files and checks for a secure user cookie and that the user has permission to view the file.

  `get`(*args, **kwargs)
  `validate_absolute_path`(root, full_path)

    Check that the user has permission to view the file

class `toyz.web.app.` `AuthToyzStaticFileHandler`(application, request, **kwargs)

  Bases: `toyz.web.app.AuthHandler`, `toyz.web.app.ToyzStaticFileHandler`

  Secure handler for all toyz added to the application. This handles security, such as making sure the user has a registered cookie and that the user has acces to the requested files.

  `get`(*args, **kwargs)

class `toyz.web.app.` `AuthToyzTemplateHandler`(application, request, **kwargs)

  Bases: `toyz.web.app.AuthHandler`, `toyz.web.app.ToyzTemplateHandler`

  `get`(*args, **kwargs)

class `toyz.web.app.` `MainHandler`(application, request, **kwargs)

  Bases: `toyz.web.app.ToyzHandler`

  Main Handler when user connects to localhost:8888/ (or whatever port is used by the application).

  `get`(*args, **kwargs)

    Render the main web page

  `get_template_path`()

    Get the path for the main webpage

  `initialize`(template_name, template_path)

    Initialize handler

class `toyz.web.app.` `ToyzHandler`(application, request, **kwargs)

  Bases: `tornado.web.RequestHandler`

  Not yet implemented

  `get_current_user`()

    Load the name of the current user

  `get_toyz_path`(path)

    Given a toyz path, return the root path for the toy.

  `get_user_id`()

class `toyz.web.app.` `ToyzStaticFileHandler`(application, request, **kwargs)

  Bases: `toyz.web.app.ToyzHandler`, `tornado.web.StaticFileHandler`

  Handler for all toyz added to the application.

  `get`(*args, **kwargs)

    Called when the application recieves a get command from the client.

class `toyz.web.app.` `ToyzTemplateHandler`(application, request, **kwargs)

  Bases: `toyz.web.app.ToyzHandler`, `tornado.web.RequestHandler`

  - Not yet implemented*
  `get`(*args, **kwargs)
  `get_template_path`()
  `initialize`(**options)

    Initialize handler

class `toyz.web.app.` `ToyzWebApp`

  Bases: `tornado.web.Application`

  Web application that runs on the server. Along with setting up the Tornado web application, it also processes jobs sent to the server from clients.

  `close_session`(session)

    Close a websocket session and delete any temporary files or directories created during the session. To help ensure all temp files are deleted (in case of a server or client error), if the user doesn’t have any open session his/her temp directory is also deleted.

  `find_open_port`(port)

    Begin at `port` and search for an open port on the server

  `new_session`(user_id, websocket)

    Open a new websocket session for a given user

    Parameters
      user_id ( `toyz.utils.core.ToyzUser` ): User id websocket (`toyz.web.app.WebSocketHandler` ): new websocket opened
  `process_job`(msg)

    Currently all jobs are run directly from the web app. Later a job server might be implemented that will maintain a queue of long duration jobs, so this function will be used to tag the batch jobs and send them to the job application.

    Parameters
      - msg (dict): Message sent from web client (see `toyz.utils.core.run_job()` for the format of the msg)
  `respond`(response)

    Once a job has completed, including jobs run by an external job application, send the response to the user. Also, update any objects that need to be updated.

    Parameters
      - response (dict ): Message to send to the user. See `toyz.utils.core.run_job()` for the format of the msg.
  `run_job`(*args, **kwargs)

    Run a job by opening a new process (possible on a new cpu) and passing the job and parameters to the process. This function is actually a generator of a Future() object, so the exception tornado.gen.Return is raised to return the result.

    In the future this function will be modified to be threaded for certain types of web applications, allowing variables to be passed more easily and stored in work environments when that type of usage is prefereable.

    Parameters
      - msg (dict): Message sent from web client (see `toyz.utils.core.run_job()` for the format of the msg)
    Returns
      - result (future containing a dict ): Message to send back to the user. If there is no response, then `result={}`
  `update`(attr)

    Certain properties of the application may be changed by an external job, for example a user may change his/her password, a new user may be created, a setting may be changed, etc. When this happens the job notifies the application that something has changed and this function is called to reload the property.

    Parameters
      - attr (string ): Name of attribute that needs to be updated. So far only toyz_settings is supported

class `toyz.web.app.` `WebSocketHandler`(application, request, **kwargs)

  Bases: `tornado.websocket.WebSocketHandler`

  Websocket that handles jobs sent to the server from clients

  `on_close`()

    Called when the websocket is closed. This function calls the applications `-toyz.web.app.ToyzWebApp.close_session)()` function.

  `on_message`(message)

    Called when the websocket recieves a message from the client. The user and session information is then extracted and processed before running the job initiated by the client. Only `modules` and `toyz` that the user has permission to view are accepted, all others return a `toyz.utils.errors.ToyzJobError` .

    Parameters
      - message (JSON unicode string ): see( `toyz.utils.core.run_job()` for the format of the msg)
  `open`(*args)

    Called when a new websocket is opened

    Parameters
      *args: Currently no arguments are passed to this function

`toyz.web.app.` `init_web_app`()

  Run the web application on the server

## toyz.web.tasks module

While each toy may contain a large number of functions, only the functions located in the `tasks.py` file will be callable from the job queue.

`toyz.web.tasks.` `add_new_user`(toyz_settings, tid, params)

  Add a new user to the toyz application.

  Parameters
    - toyz_settings ( `toyz.utils.core.ToyzSettings`): Settings for the toyz application
    - tid (string ): Task ID of the client user running the task
    - params (dict ): Any parameters sent by the client (see params below)
  Params
    - user_id (string ): Id of user to add
  Response
    - id: ‘notification’
    - func: ‘add_new_user’
    - msg: ‘User/Group added correctly’

`toyz.web.tasks.` `change_pwd`(toyz_settings, tid, params)

  Change a users password.

  Parameters
    - toyz_settings ( `toyz.utils.core.ToyzSettings`): Settings for the toyz application
    - tid (string ): Task ID of the client user running the task
    - params (dict ): Any parameters sent by the client (see params below)
  Params
    - current_pwd (string ): Users current password. Must match the password on file or an exception is raised
    - new_pwd (string ): New password
    - confirm_pwd (string ): Confirmation of the the new password. If new_pwd and confirm_pwd do not match, an exception is raised
  Response
    - id: ‘notification’
    - func: ‘change_pwd’
    - msg: ‘Password changed successfully’

`toyz.web.tasks.` `create_dir`(app, id, params)

  Creates a new path on the server (if it does not already exist).

  Parameters
    - toyz_settings ( `toyz.utils.core.ToyzSettings`): Settings for the toyz application
    - tid (string ): Task ID of the client user running the task
    - params (dict ): Any parameters sent by the client (see params below)
  Params
    - path (string ): path to create on the server
  Response
    id: ‘create_folder’, status (string ): ‘success’, path (string ): path created on the server

`toyz.web.tasks.` `load_directory`(toyz_settings, tid, params)

  Used by the file browser to load the folders and files in a given path.

  Parameters
    - toyz_settings ( `toyz.utils.core.ToyzSettings`): Settings for the toyz application
    - tid (string ): Task ID of the client user running the task
    - params (dict ): Any parameters sent by the client (see params below)
  Params
    - path (string ): Path to search
  Response
    - id: ‘directory’
    - path (string ): path passed to the function
    - shortcuts (dict ): Dictionary of `shortcut_name: shortcut_path` ‘s for the user
    - folders (list of strings): folders contained in the path
    - files (list of strings): files contained in the path
    - parent (string ): parent directory of current path

`toyz.web.tasks.` `load_user_info`(toyz_settings, tid, params)

  Load info for a given user from the database

  Parameters
    - toyz_settings ( `toyz.utils.core.ToyzSettings`): Settings for the toyz application
    - tid (string ): Task ID of the client user running the task
    - params (dict ): Any parameters sent by the client (see params below)
  Params
    - user_id or group_id (string ): User or group to load parameters for
    - user_attr (list ): List of user attributes to load
  Response
    - id: ‘user_info’
    - Each attribute requested by the client is also returned as a key in the response

`toyz.web.tasks.` `load_user_settings`(toyz_settings, tid, params)

  Load settings for a given user

  Parameters
    - toyz_settings ( `toyz.utils.core.ToyzSettings`): Settings for the toyz application
    - tid (string ): Task ID of the client user running the task
    - params (dict ): Any parameters sent by the client (None for this function)
  Response for all users
    - id: ‘user_settings’
    - shortcuts (dict ): Dictionary of `shortcut_name: shortcut_path` ‘s for the user
    - workspaces (dict ): Dictionary of `workspace_name: workspace_settings` for the user
  Additional response keys for users in the modify_toyz group
    - modules (list ): List of toyz modules the user can run
    - toyz (dict ): Dictionary of `toy_name: path_to_toy` ‘s that the user can run
  Additional reponse keys for admins
    - config (dict ): Configuration settings for the application
    - db (dict ): Database settings
    - web (dict): Web settings
    - security (dict ): Security settings
    - users (list ): list of all users in the database
    - groups (list ): list of all groups in the database
    - user_settings (dict ): Settings for a specified user (initially the admin)
    - group_settings (dict ): Settings for a specified group (initially the admin group)

`toyz.web.tasks.` `save_user_info`(toyz_settings, tid, params)

  Save a users info. If any admin settings are being changed, ensures that the user is in the admin group.

  Parameters
    - toyz_settings ( `toyz.utils.core.ToyzSettings`): Settings for the toyz application
    - tid (string ): Task ID of the client user running the task
    - params (dict ): Any parameters sent by the client (see params below)

  Params can be any settings the user has permission to set on the server.

  Response
    - id: ‘notification’
    - func: ‘save_user_info’
    - msg: ‘Settings saved for <user_id>’

## Module contents