Commits

Andriy Kornatskyy committed 9af5160

Refactoring due to eliminated cache_factory concept.

  • Participants
  • Parent commits d4e79b0

Comments (0)

Files changed (4)

File i18n/captcha.po

 "Content-Type: text/plain; charset=UTF-8\n"
 "Content-Transfer-Encoding: 8bit\n"
 
-#: src/wheezy/captcha/http.py:91 src/wheezy/captcha/http.py:95
+#: src/wheezy/captcha/http.py:86 src/wheezy/captcha/http.py:91
+#: src/wheezy/captcha/http.py:95
 msgid "The challenge code is not available."
 msgstr "The challenge code is not available."
 
-#: src/wheezy/captcha/http.py:95 src/wheezy/captcha/http.py:99
+#: src/wheezy/captcha/http.py:90 src/wheezy/captcha/http.py:95
+#: src/wheezy/captcha/http.py:99
 msgid "The turing number is not available."
 msgstr "The turing number is not available."
 
-#: src/wheezy/captcha/http.py:101 src/wheezy/captcha/http.py:105
+#: src/wheezy/captcha/http.py:96 src/wheezy/captcha/http.py:101
+#: src/wheezy/captcha/http.py:105
 msgid "The challenge code is invalid."
 msgstr "The challenge code is invalid."
 
-#: src/wheezy/captcha/http.py:106 src/wheezy/captcha/http.py:110
+#: src/wheezy/captcha/http.py:101 src/wheezy/captcha/http.py:106
+#: src/wheezy/captcha/http.py:110
 msgid "The turing number is invalid."
 msgstr "The turing number is invalid."
 
-#: src/wheezy/captcha/http.py:116 src/wheezy/captcha/http.py:120
+#: src/wheezy/captcha/http.py:108 src/wheezy/captcha/http.py:116
+#: src/wheezy/captcha/http.py:120
 #, python-format
 msgid "The code you typed has expired after %d seconds."
 msgstr "The code you typed has expired after %d seconds."
 
-#: src/wheezy/captcha/http.py:125 src/wheezy/captcha/http.py:129
+#: src/wheezy/captcha/http.py:115 src/wheezy/captcha/http.py:125
+#: src/wheezy/captcha/http.py:129
 #, python-format
 msgid "The code was typed too quickly. Wait at least %d seconds."
 msgstr "The code was typed too quickly. Wait at least %d seconds."
 
-#: src/wheezy/captcha/http.py:130 src/wheezy/captcha/http.py:134
+#: src/wheezy/captcha/http.py:120 src/wheezy/captcha/http.py:130
+#: src/wheezy/captcha/http.py:134
 msgid "The code you typed has no match."
 msgstr "The code you typed has no match."
 

File i18n/en/LC_MESSAGES/captcha.po

 "Content-Type: text/plain; charset=UTF-8\n"
 "Content-Transfer-Encoding: 8bit\n"
 
-#: src/wheezy/captcha/http.py:91 src/wheezy/captcha/http.py:95
+#: src/wheezy/captcha/http.py:86 src/wheezy/captcha/http.py:91
+#: src/wheezy/captcha/http.py:95
 msgid "The challenge code is not available."
 msgstr "The challenge code is not available."
 
-#: src/wheezy/captcha/http.py:95 src/wheezy/captcha/http.py:99
+#: src/wheezy/captcha/http.py:90 src/wheezy/captcha/http.py:95
+#: src/wheezy/captcha/http.py:99
 msgid "The turing number is not available."
 msgstr "The turing number is not available."
 
-#: src/wheezy/captcha/http.py:101 src/wheezy/captcha/http.py:105
+#: src/wheezy/captcha/http.py:96 src/wheezy/captcha/http.py:101
+#: src/wheezy/captcha/http.py:105
 msgid "The challenge code is invalid."
 msgstr "The challenge code is invalid."
 
-#: src/wheezy/captcha/http.py:106 src/wheezy/captcha/http.py:110
+#: src/wheezy/captcha/http.py:101 src/wheezy/captcha/http.py:106
+#: src/wheezy/captcha/http.py:110
 msgid "The turing number is invalid."
 msgstr "The turing number is invalid."
 
-#: src/wheezy/captcha/http.py:116 src/wheezy/captcha/http.py:120
+#: src/wheezy/captcha/http.py:108 src/wheezy/captcha/http.py:116
+#: src/wheezy/captcha/http.py:120
 #, python-format
 msgid "The code you typed has expired after %d seconds."
 msgstr "The code you typed has expired after %d seconds."
 
-#: src/wheezy/captcha/http.py:125 src/wheezy/captcha/http.py:129
+#: src/wheezy/captcha/http.py:115 src/wheezy/captcha/http.py:125
+#: src/wheezy/captcha/http.py:129
 #, python-format
 msgid "The code was typed too quickly. Wait at least %d seconds."
 msgstr "The code was typed too quickly. Wait at least %d seconds."
 
-#: src/wheezy/captcha/http.py:130 src/wheezy/captcha/http.py:134
+#: src/wheezy/captcha/http.py:120 src/wheezy/captcha/http.py:130
+#: src/wheezy/captcha/http.py:134
 msgid "The code you typed has no match."
 msgstr "The code you typed has no match."
 

File src/wheezy/captcha/http.py

 class CaptchaContext(object):
 
     def __init__(self, image,
-                 cache_factory, prefix='captcha:', namespace=None,
+                 cache, prefix='captcha:', namespace=None,
                  timeout=5 * 60, profile=None,
                  chars='ABCDEFGHJKLMNPQRSTUVWXYZ23456789',
                  max_chars=4, wait_timeout=2,
                  challenge_key='c', turing_key='turing_number',
                  enabled=True):
         self.image = image
-        self.cache_factory = cache_factory
+        self.cache = cache
         self.prefix = prefix
         self.namespace = namespace
         self.timeout = timeout
             challenge_code = last_item_adapter(
                 request.query)[self.challenge_key]
             turing_number = ''.join(random.sample(self.chars, self.max_chars))
-            context = self.cache_factory()
-            try:
-                cache = context.__enter__()
-                if not cache.set(self.prefix + challenge_code,
-                                 (int(time()), turing_number),
-                                 self.timeout, self.namespace):
-                    return bad_request()
-            finally:
-                context.__exit__(None, None, None)
+            if not self.cache.set(self.prefix + challenge_code,
+                                  (int(time()), turing_number),
+                                  self.timeout, self.namespace):
+                return bad_request()
             response = HTTPResponse(content_type)
             self.image(turing_number).save(
                 FileAdapter(response), format, **options)
             return False
 
         key = self.prefix + challenge_code
-        context = self.cache_factory()
-        try:
-            cache = context.__enter__()
-            data = cache.get(key, self.namespace)
-            if not data:
-                self.append_error(errors, gettext(
-                    'The code you typed has expired after %d seconds.')
-                    % self.timeout)
-                return False
-            cache.delete(key, 0, self.namespace)
-        finally:
-            context.__exit__(None, None, None)
+        data = self.cache.get(key, self.namespace)
+        if not data:
+            self.append_error(errors, gettext(
+                'The code you typed has expired after %d seconds.')
+                % self.timeout)
+            return False
+        self.cache.delete(key, 0, self.namespace)
         issued, turing_number = data
         if issued + self.wait_timeout > int(time()):
             self.append_error(errors, gettext(

File src/wheezy/captcha/mixin.py

 
     def captcha_widget(self, path):
         ctx = self.captcha_context
-        return ('<img id="captcha" src="' + path + '?'
-                + ctx.challenge_key + '=' + self.challenge_code
-                + '" title="'
-                + self._('If you cannot read, click to generate a new one.')
-                + '" /><input type="hidden" name="'
-                + ctx.challenge_key
-                + '" value="'
-                + self.challenge_code
-                + '" />')
+        return ('<img id="captcha" src="%s?%s=%s" title="%s" />'
+                '<input type="hidden" name="%s" value="%s" />' %
+                (path, ctx.challenge_key, self.challenge_code,
+                 self._('If you cannot read, click to generate a new one.'),
+                 ctx.challenge_key, self.challenge_code))