import sys
from quixote import get_response, redirect
from quixote.directory import Directory
from quixote.errors import TraversalError

def fact(n):
    f = 1L
    while n > 1:
        f *= n
        n -= 1
    return f

class IntegerUI(Directory):

    _q_exports = ["", "factorial", "prev", "next"]

    def __init__(self, component):
        try:
            self.n = int(component)
        except ValueError, exc:
            raise TraversalError(str(exc))

    def factorial(self):
        if self.n > 10000:
            sys.stderr.write("warning: possible denial-of-service attack "
                             "(request for factorial(%d))\n" % self.n)
        get_response().set_content_type("text/plain")
        return "%d! = %d\n" % (self.n, fact(self.n))

    def _q_index(self):
        return """\
        <html>
        <head><title>The Number %d</title></head>
        <body>
        You have selected the integer %d.<p>

        You can compute its <a href="factorial">factorial</a> (%d!)<p>

        Or, you can visit the web page for the
        <a href="../%d/">previous</a> or
        <a href="../%d/">next</a> integer.<p>

        Or, you can use redirects to visit the
        <a href="prev">previous</a> or
        <a href="next">next</a> integer.  This makes
        it a bit easier to generate this HTML code, but
        it's less efficient -- your browser has to go through
        two request/response cycles.  And someone still
        has to generate the URLs for the previous/next
        pages -- only now it's done in the <code>prev()</code>
        and <code>next()</code> methods for this integer.<p>

        </body>
        </html>
        """ % (self.n, self.n, self.n, self.n-1, self.n+1)

    def prev(self):
        return redirect("../%d/" % (self.n-1))

    def next(self):
        return redirect("../%d/" % (self.n+1))
