diff --git a/extras/csrf_migration_helper.py b/extras/csrf_migration_helper.py old mode 100644 new mode 100755 index 6416193c49e..6aaf6b433da --- a/extras/csrf_migration_helper.py +++ b/extras/csrf_migration_helper.py @@ -41,10 +41,6 @@ # loaders are out of the picture, because there is no way to ask them to # return all templates. # -# - If you put the {% csrf_token %} tag on the same line as the
') _TOKEN_RE = re.compile('\{% csrf_token') def get_template_dirs(): @@ -190,12 +187,22 @@ class Template(object): Get information about any POST forms in the template. Returns [(linenumber, csrf_token added)] """ - matches = [] + forms = {} + form_line = 0 for ln, line in enumerate(self.content.split("\n")): - m = _POST_FORM_RE.search(line) - if m is not None: - matches.append((ln + 1, _TOKEN_RE.search(line) is not None)) - return matches + if not form_line and _POST_FORM_RE.search(line): + # record the form with no CSRF token yet + form_line = ln + 1 + forms[form_line] = False + if form_line and _TOKEN_RE.search(line): + # found the CSRF token + forms[form_line] = True + form_line = 0 + if form_line and _FORM_CLOSE_RE.search(line): + # no token found by form closing tag + form_line = 0 + + return forms.items() def includes_template(self, t): """