Webware / WebKit / Examples / CountVisits.py

from ExamplePage import ExamplePage


class CountVisits(ExamplePage):
    """Counting visits example."""

    def writeContent(self):
        if self.request().hasField('expire'):
            self.session().expiring()
            self.sendRedirectAndEnd('CountVisits')
        self.writeln('<h3>Counting Visits</h3>')
        if self.request().isSessionExpired():
            self.writeln('<p><em>Your session has expired.</em></p>')
        count = self.session().value('count', 0) + 1
        self.session().setValue('count', count)
        self.writeln("""<p>You've been here
<strong style="background-color:yellow">&nbsp;%d&nbsp;</strong> time%s.</p>
<p>This page records your visits using a session object.
Every time you <a href="javascript:location.reload()">reload</a>
or <a href="CountVisits">revisit</a> this page, the counter will increase.
If you close your browser or force the session to
<a href="CountVisits?expire=1">expire</a>, then your session will end
and you will see the counter go back to 1 on your next visit.</p>
<p>Try hitting <a href="javascript:location.reload()">reload</a> now.</p>"""
            % (count, count > 1 and 's' or ''))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.