Branko Vukelic avatar Branko Vukelic committed b5c9c58

Added hash property

Comments (0)

Files changed (2)


         u.query = dict(foo=1, bar=2)
         u.fragment = 'baz'
         self.assertEqual(str(u), '/foo/bar?foo=1&bar=2#baz')
+    def test_use_with_URL_as_param_value(self):
+        u = UrlHelper('/foo/bar/')
+        u.query = {'redir': '/foo/bar/baz?test=1'}
+        self.assertEqual(u.get_full_path(),
+                         '/foo/bar/?redir=%2Ffoo%2Fbar%2Fbaz%3Ftest%3D1')
+    def test_hashing_basically_works(self):
+        u = UrlHelper('/foo/bar/')
+        self.assertEqual(u.hash, '1c184f3891344400380281315d9e7388')
+    def test_hashling_with_query_string(self):
+        u = UrlHelper('/foo/bar')
+        u.query = dict(foo=1)
+        self.assertEqual(u.hash, '06f0a42bdd474f053fb1343165a31d42')


 import urllib
 import urlparse
+import hashlib
 from django.http.request import QueryDict
 from django.utils.encoding import iri_to_uri
 class UrlHelper(object):
+    _hash = None
     def __init__(self, full_path):
         # parse the path
         r = urlparse.urlparse(full_path)
         return self.query_dict
     def update_query_data(self, **kwargs):
+        self._hash = None
         for key in kwargs:
             val = kwargs[key]
             if hasattr(val, '__iter__'):
         return urllib.quote_plus(self.get_full_path(**kwargs), safe='/')
+    def hash(self):
+        if self._hash:
+            return self._hash
+        md5 = hashlib.md5()
+        md5.update(self.get_full_path())
+        self._hash = md5.hexdigest()
+        return self._hash
+    @property
     def query(self):
         return self.get_query_data()
     def query(self, value):
+        self._hash = None
         if type(value) is dict:
             self.query_dict = QueryDict('', mutable=True)
     def query_string(self, value):
+        self._hash = None
         self.query_dict = QueryDict(value, mutable=True)
     def __str__(self):
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
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.