view tests/suite_12_methods/files/name.py @ 72:e8b3b336e63e draft default tip

Update version.
author David Barts <n5jrn@me.com>
date Mon, 15 Jul 2019 13:17:48 -0700
parents 88adf10be709
children
line wrap: on
line source

# It is *technically* an HTTP violation to use POST for an idempotent
# page like this, but it is a good simple test.

from tincan import Page
from jsdict import JSDict

class Name(Page):
    def handle(self):
        self._error_message = JSDict({"subject": "Error", "style": "color: red;"})
        if "name" not in self.request.forms:
            if self.request.method == "GET":
                self.message = None
            else:
                self.message = self.error("This should not happen!")
        else:
            name = self.request.forms["name"]
            if name.strip() == "":
                self.message = self.error("Please enter your name above.")
            else:
                self.message = JSDict({
                    "subject": "Hello, {0}".format(name.split()[0]),
                    "style": None,
                    "body": "Pleased to meet you!"
                })

    def error(self, message):
        self._error_message.body = message
        return self._error_message