Permalink
Browse files

Switch to use sha-1 hash of url as id, start on web.py templates for …

…_tools, add javascript sha1 implementation
  • Loading branch information...
1 parent 7cab616 commit a0580895e5c68201a3cca72f4abb1e8cb6272181 @harryf committed Jun 6, 2008
Showing with 550 additions and 37 deletions.
  1. +15 −13 dammit/webtests.py
  2. +466 −0 static/sha1.js
  3. +8 −0 templates/addurl.html
  4. +10 −0 templates/base.html
  5. +3 −0 templates/checkurl.html
  6. +5 −0 templates/tools.html
  7. +43 −24 urldammit.py
View
@@ -5,15 +5,24 @@
class WebTests(unittest.TestCase):
def setUp(self):
- self.http = httplib2.Http()
+ self._init_http()
self.url = 'http://localhost:8080'
self.headers = {'Content-type': 'application/x-www-form-urlencoded'}
self.body = {
'uri': 'http://foobar.com/1.html',
'status': '200',
}
-
+ def _init_http(self):
+ self.http = httplib2.Http()
+
+ def _post(self):
+ return self.http.request(
+ self.url, 'POST',
+ headers=self.headers,
+ body=urllib.urlencode(self.body)
+ )
+
def testHome(self):
response, content = self.http.request(self.url, 'GET')
self.assert_( response['status'] == '200' )
@@ -24,18 +33,9 @@ def test404(self):
response, content = self.http.request("%s/test1234" % self.url, 'GET')
self.assert_(response['status'] == '404')
- def _post(self):
- return self.http.request(
- self.url, 'POST',
- headers=self.headers,
- body=urllib.urlencode(self.body)
- )
def test303(self):
- """
- test the direct response to a POST, without following
- the redirect
- """
+ # test the direct response to a POST, without following the redirect
self.http.follow_redirects = False
response, content = self._post()
self.assert_( response['status'] == '303' )
@@ -62,12 +62,14 @@ def testMoreTags(self):
self.assert_( response['status'] == '200' )
self.assert_( self.body['uri'] in content )
self.assert_( '["abc", "xyz"]' in content )
-
+
def testDELETE(self):
self.body['uri'] = 'http://foobar.com/deleteme.html'
response, content = self._post()
self.assert_( response['status'] == '200' )
self.assert_( self.body['uri'] in content )
+
+ self._init_http()
response, content = self.http.request(
response['content-location'], 'DELETE'
)
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,8 @@
+<h1>Submit a URL</h1>
+<form action="/" method="POST">
+<label for="uri">uri:</label><input name="uri" type="text"><br>
+<label for="status">status:</label><input name="status" type="text"><br>
+<label for="tags">tags:</label><input name="tags" type="text"><br>
+<label for="pairs">pairs:</label><input name="pairs" type="text"><br>
+<input type="submit" value="submit">
+</form>
View
@@ -0,0 +1,10 @@
+$def with (page, title=None)
+<html><head>
+ <title>urldammit\
+$if title: : $title\
+</title>
+</head><body>
+
+$:page
+
+</body></html>
View
@@ -0,0 +1,3 @@
+<h1>Check a URL</h1>
+
+<p>in progress...</p>
View
@@ -0,0 +1,5 @@
+<h1>Tools</h1>
+<ul>
+<li><a href="/_tools/addurl">Add a URL</a></li>
+<li><a href="/_tools/checkurl">Check a URL</a></li>
+</ul>
View
@@ -9,11 +9,15 @@
from couchdb import Database
from dammit.http import status
from dammit.request import unpack_tags, unpack_pairs, pack_response
+import view
+from view import render
urls = (
'/', 'urldammit',
'/_tools', 'tools',
- '/(.*)', 'urldammit',
+ '/_tools/addurl', 'tools_addurl',
+ '/_tools/checkurl', 'tools_checkurl',
+ '/([0-9a-f]{40})', 'urldammit',
)
db = Database('http://localhost:5984/urldammit')
@@ -37,7 +41,7 @@ def GET(self, uri = None):
if not uri:
print "where's my url dammit?"
return
-
+
r = self._locate(uri)
if not r: return
@@ -84,45 +88,55 @@ def POST(self):
r = register_uri(db, uri = uri, status = int(status),\
tags = tags, pairs = pairs )
- if r: known[id] = r
- web.http.seeother("%s/%s" % ( web.ctx.home, urllib2.quote(i.uri)) )
+
+ id = uri_to_id(i.uri)
+ if r:
+ known[id] = r
+ web.http.seeother(
+ "%s/%s" % ( web.ctx.home, id)
+ )
self._render(r)
- def DELETE(self):
- i = web.input()
- uri = self._reqd(i, 'uri')
- if not uri: return
+ def DELETE(self, uri):
+ """
+ TODO: take the uri as a param
+ currently very broken...
+ """
+ logging.debug("got a delete request")
- delete(db, id)
+ try:
+ delete(db, uri)
+ except Exception, e:
+ logging.error(e)
+
web.ctx.status = status[204]
def _locate(self, uri):
"""
See what we know about this uri...
+ uri is in fact a SHA-1 hash of the uri
"""
- id = uri_to_id(uri)
-
def ithas(key, cache):
if key in cache:
return cache[key]
return None
- u = ithas(id, unknown)
+ u = ithas(uri, unknown)
if u:
web.notfound()
return None
- r = ithas(id, known)
+ r = ithas(uri, known)
if not r:
- r = URI.load(db, id)
+ r = URI.load(db, uri)
if not r:
- unknown[id] = True
+ unknown[uri] = True
web.notfound()
return None
- known[id] = r
+ known[uri] = r
return r
@@ -138,7 +152,10 @@ def _redirect(self, r):
if 300 <= r.status < 400:
if r.status in status:
web.ctx.status = status[r.status]
- web.header('Location', r.location)
+ web.header(
+ 'Location',
+ "%s/%s" % ( web.ctx.home, uri_to_id(r.location) )
+ )
return True
return False
@@ -176,13 +193,15 @@ def _render(self, r):
class tools:
def GET(self):
- print '<form action="/" method="POST">'
- print '<label for="uri">uri:</label><input name="uri" type="text"><br>'
- print '<label for="status">status:</label><input name="status" type="text"><br>'
- print '<label for="tags">tags:</label><input name="tags" type="text"><br>'
- print '<label for="pairs">pairs:</label><input name="pairs" type="text"><br>'
- print '<input type="submit" value="submit">'
- print '</form>'
+ print render.base(view.tools())
+
+class tools_addurl:
+ def GET(self):
+ print render.base(view.addurl())
+
+class tools_checkurl:
+ def GET(self):
+ print render.base(view.checkurl())
if __name__ == '__main__':
import logging, sys

0 comments on commit a058089

Please sign in to comment.