-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates__add_csrf_to_forms.py
67 lines (50 loc) · 1.97 KB
/
templates__add_csrf_to_forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
# Copyright 2011 Machinalis: http://www.machinalis.com/
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
__author__ = "Matias Herranz (matiasherranz@gmail.com)"
__version__ = "0.1"
import os
LINE_TO_ADD_TO_FORMS = '{% csrf_token %}\n'
# Base directory path from where to walk template directories.
# Set the paths according to your project.
CURRENT_PATH = os.path.realpath(".")
BASE_PATH = CURRENT_PATH + '/trunk/'
print BASE_PATH
def csrf_form_adder():
for root, dirs, files in os.walk(BASE_PATH):
for name in files:
if ('.svn' not in root) and name.endswith('.html'):
print '==> root: ', root
print 'root: ', root
print 'name: ', name
make_replacements(root, name)
def make_replacements(root, name):
filepath = os.path.join(root, name)
f = open(filepath, 'r')
lines = f.readlines()
f.close()
new_lines = []
for line in lines:
new_lines.append(line)
if ('<form' in line) and (not '</' in line) and (not '"form' in line):
# exclude form closing exclude form as a css classname
new_lines.append(LINE_TO_ADD_TO_FORMS)
print 'Added csrf token ok.\n'
new_content = ''.join(new_lines)
f = open(filepath, 'w')
f.write(new_content)
f.close()
if __name__ == '__main__':
csrf_form_adder()