Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 96 lines (77 sloc) 2.326 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
import web
import simplejson, sudo
urls = (
    '/sudo', 'sudoku',
    '/length', 'length',
)


class pwt(object):
    _inFunc = False
    updated = {}
    page = """
<script src="/static/prototype.js"></script>
<script src="/static/behaviour.js"></script>
<script>
Behaviour.register({'input': function (e) {
e.onmouseup = e.onkeyup = e.onchange = function () { send(e) }
}})
</script>

<form name="main" onsubmit="return false;">%s</form>

<script>
function send(e) {
ajax = new Ajax.Request(document.location, {method:'post', parameters:
Form.serialize(document.forms.main)
});
}

function receive(d) {
$H(d).keys().each(function (key) {
v = d[key];
k = document.forms.main[key];

if (k) k.value = v;
else $(key).innerHTML = v;
})
}
</script>
"""

    def GET(self):
        web.header('Content-Type', 'text/html')
        print self.page % self.form()
    
    def POST(self):
        i = web.input()
        if '_' in i: del i['_']
        #for k, v in i.iteritems(): setattr(self, k, v)
        
        self._inFunc = True
        self.work(**i)
        self._inFunc = False
        
        web.header('Content-Type', 'text/javascript')
        print 'receive('+simplejson.dumps(self.updated)+');'
    
    def __setattr__(self, k, v):
        if self._inFunc and k != '_inFunc':
            self.updated[k] = v
        object.__setattr__(self, k, v)

class sudoku(pwt):
    def form(self):
        import sudo
        out = ''
        n = 0
        for i in range(9):
            for j in range(9):
                out += '<input type="text" size="1" name="%s" />' % (sudo.squares[n])
                n += 1
            out += '<br />'

        return out
    
    def work(self, **kw):
        values = dict((s, sudo.digits) for s in sudo.squares)
        for k, v in kw.iteritems():
            if v:
                sudo.assign(values, k, v)

        for k, v in values.iteritems():
            if len(v) == 1:
                setattr(self, k, v)

        return values

class length(pwt):
    def form(self):
        return '<p id="output">&nbsp;</p><input type="range" name="n" value="0" />'
    
    def work(self):
        self.output = ('a' * web.intget(self.n, 0) or '&nbsp;')

if __name__ == "__main__":
    web.run(urls, globals(), web.reloader)
Something went wrong with that request. Please try again.