Metadata-Version: 2.5
Name: trytond_etude_web
Version: 8.0.0.2
Summary: Etude Form Server — server-rendered public and anonymous forms
Author: Clemens Hupka
Maintainer-email: Clemens Hupka <clemens.hupka@backend.at>
License-Expression: GPL-3.0-or-later
License-File: COPYRIGHT
License-File: LICENSE
Keywords: anonymous,form,html,portal,survey,tryton,web
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Environment :: Plugins
Classifier: Framework :: Tryton
Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
Classifier: Topic :: Office/Business
Requires-Python: >=3.10
Requires-Dist: trytond-etude-api<8.1,>=8.0
Requires-Dist: trytond-etude-collect<8.1,>=8.0
Requires-Dist: trytond-etude<8.1,>=8.0
Requires-Dist: trytond<8.1,>=8.0
Provides-Extra: test
Requires-Dist: trytond-etude-i18n<8.1,>=8.0; extra == 'test'
Requires-Dist: trytond-etude-logic<8.1,>=8.0; extra == 'test'
Requires-Dist: trytond-keycloak-auth<8.1,>=8.0; extra == 'test'
Requires-Dist: trytond-web-user<8.1,>=8.0; extra == 'test'
Description-Content-Type: text/x-rst

Etude Web Module
================

The ``etude_web`` module serves the forms of the ``etude`` group as HTML pages to
participants who have no Tryton client: a published form is completable in a
browser by an anonymous participant who holds a form token, and by a portal user
who authenticates through ``keycloak_auth`` or ``web_user``.
It depends on ``etude_api``, which owns the API tokens the pages authenticate
with and the payload they render, and it adds no model to the group: the forms,
the responses and the answers are the ones of ``etude``, ``etude_collect`` and
``etude_api``.

The pages are served by trytond
-------------------------------

No additional web server is required.  The routes are registered on the WSGI
application of trytond under ``/<database_name>/etude/web/``, they are declared
with ``with_pool`` and ``with_transaction`` and they return
``trytond.protocols.wrappers.Response`` objects, exactly like the routes of the
REST API.  A page reads and writes inside the transaction of the request.

The contract of the pages is a render, a submit, a save, an upload and a read
back: ``GET /<token>`` renders the definition a form serves, ``GET
/<token>?response=<id>`` renders it with what a partial response already
answered, ``POST /<token>`` submits the answers, ``POST /<token>/save`` saves
them as a draft the participant resumes, ``POST /<token>/upload`` stores the file
of a question of the kind upload, and ``GET /<token>/media/<id>`` reads a file an
answer holds.

A form which was translated is rendered in the language of the request - the
language of the participant who answers, the languages the browser asks for and
the language the form declares - and the response a page starts captures it.
The localisation of forms is the ``etude_i18n`` companion of the group, which
this module reads through the pool rather than depends on: without it, the
definition is rendered as it was published.

A refused answer leaves nothing behind
--------------------------------------

The answers of a submit are checked against the definition the response pins,
against the rules and the quotas of the form and against the bindings it writes
*before* a response exists, and a submission the form refuses leaves neither a
response nor a record behind: the participant reads the failure on the form,
with what was already answered, and the form can be corrected and sent again.  A
form that is accepted is submitted the way the REST API submits it, in the one
transaction of the request.

The failures are told apart the way a browser reads them: ``401`` when the form
token authenticates nothing, ``403`` when a valid token may not run what the page
asks, ``400`` when the definition of the form refuses the request, and ``404``
when the form, the response, the file or the route is not there.  Every failure
is answered as an HTML page that says what went wrong, never as the JSON of the
API.

A form token answers one form, as one user
------------------------------------------

A page names the form it renders by the secret of an ``etude.api.token`` carried
in its path, exactly as the REST API names it by a bearer token: the token is
scoped to one form, it is granted the operations the page may run on it -
``read`` to render, ``write`` to answer - and it acts as one user, so that the
responses a page collects are attributed to that user and every read and write is
checked against the access rights and the record rules of that user.  A response
is reached through the form it answers and through the participant who answered
it, so that holding a token for a form is not holding every response of it.

A portal participant answers as their own account
-------------------------------------------------

A participant who holds a Keycloak access token or a ``web.user`` session key
answers as their own account: the credential is presented as a bearer token or
in the ``etude_portal`` cookie, the identity is resolved by ``keycloak_auth``
where it is installed and by the ``web.user`` session system otherwise, and the
response is attributed to the ``res.user`` the credential resolves to.
``keycloak_auth`` and ``web_user`` are optional dependencies of the module: a
deployment that installs neither serves anonymous forms.

The form token of the address is always required, whichever credential is
presented, and a portal credential that is presented and refused is answered with
``401`` rather than quietly ignored.

The client is progressively enhanced
------------------------------------

The page is a plain HTML form and is answered without any script.  The script of
the page posts a chosen file to the upload route without leaving the form, and
reads the relevance of the definition again on every change, so that a question
appears or disappears as the answers change.  The server reads the same decision
when it composes the page and when it validates the answers.

See ``doc/index.rst`` for the module documentation and ``ETUDE.md`` in
``modules-cch/`` for the architecture of the whole group.
