Andriy Kornatskyy avatar Andriy Kornatskyy committed 65f653d

Minor refactoring working with cookies.

Comments (0)

Files changed (1)

src/wheezy/web/handlers/base.py

 """
 from uuid import uuid4
 
-from wheezy.core.collections import last_item_adapter
 from wheezy.core.descriptors import attribute
 from wheezy.core.i18n import null_translations
 from wheezy.core.i18n import ref_gettext
             return self.__xsrf_token
         options = self.options
         xsrf_name = options['XSRF_NAME']
-        try:
-            xsrf_token = self.request.cookies[xsrf_name]
-        except KeyError:
+        cookies = self.request.cookies
+        if xsrf_name in cookies:
+            xsrf_token = cookies[xsrf_name]
+        else:
             xsrf_token = shrink_uuid(uuid4())
             self.cookies.append(HTTPCookie(
                 xsrf_name,
     xsrf_token = property(getxsrf_token, None, delxsrf_token)
 
     def validate_xsrf_token(self):
-        options = self.options
-        xsrf_name = options['XSRF_NAME']
-        xsrf_token = last_item_adapter(self.request.form)[xsrf_name]
-        if xsrf_token and xsrf_token == self.xsrf_token and parse_uuid(
-                xsrf_token) != UUID_EMPTY:
-            return True
+        xsrf_name = self.options['XSRF_NAME']
+        form = self.request.form
+        if xsrf_name in form:
+            xsrf_token = form[xsrf_name][-1]
+            return xsrf_token == self.xsrf_token \
+                    and parse_uuid(xsrf_token) != UUID_EMPTY
         else:
             self.delxsrf_token()
             return False
     def getresubmission(self):
         if hasattr(self, '_BaseHandler__resubmission'):
             return self.__resubmission
-        try:
-            counter = self.request.cookies[self.options['RESUBMISSION_NAME']]
+        resubmission_name = self.options['RESUBMISSION_NAME']
+        cookies = self.request.cookies
+        if resubmission_name in cookies:
+            counter = cookies[resubmission_name]
             self.__resubmission = counter
-        except (KeyError, TypeError):
+        else:
             counter = '0'
             self.setresubmission(counter)
         return counter
         if self.request.ajax:
             return True
         name = self.options['RESUBMISSION_NAME']
-        counter = last_item_adapter(self.request.form)[name]
-        if counter and counter == self.resubmission:
-            counter = str(int(counter) + 1)
-            self.setresubmission(counter)
-            return True
-        else:
-            return False
+        form = self.request.form
+        if name in form:
+            counter = form[name][-1]
+            if counter == self.resubmission:
+                try:
+                    counter = str(int(counter) + 1)
+                    self.setresubmission(counter)
+                    return True
+                except ValueError:
+                    self.setresubmission('0')
+        return False
 
     def resubmission_widget(self):
         if self.request.ajax:
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 ProjectModifiedEvent.java.
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.