forked from omaha-consulting/omaha-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uncensor_manage.py
169 lines (140 loc) · 4.99 KB
/
uncensor_manage.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from twisted.web import resource
from db_helper import DbHelper
from config import Config
import urllib
class UncensorManageResource(resource.Resource):
isLeaf = True
pathFromRoot = '/service/admin/uncensor'
def render_GET(self, request):
macdb = DbHelper()
res = macdb.uncensor_fetch_all()
msg = request.args['msg'][0] if 'msg' in request.args else ''
output = """<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>House of Life Update Manager</title>
<!-- CSS: implied media="all" -->
<link rel="stylesheet" href="/css/style.css?v=2">
<link rel="stylesheet" href="/css/uncensor_domains.css">
<!-- Load jQuery -->
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1");
</script>
<script type="text/javascript">
function post_to_url(path, params, method) {
method = method || "post"; // Set method to post by default, if not specified.
// The rest of this code assumes you are not using a library.
// It can be made less wordy if you use one.
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);
for(var key in params) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField);
}
document.body.appendChild(form);
form.submit();
}
</script>
<style type="text/css">
#banner {"""
if msg == "":
output += """
display:none;"""
output += """
}
</style>
</head>
<body>
<div id="container">
<header>
<h1>Uncensor domains</h1>
<p>Copyright © 2011, House of Life Property ltd. All rights reserved.<br />
Copyright © 2011, Crystalnix <[email protected]></p>
</header>
<div id="main" role="main">
<div id="banner">
%s
</div>
<p>
<a href="javascript:void(0)" onclick="javascript:$('#add_form').show(); return false;">Add domain pair</a>
</p>
<form id="add_form" style="display:none" method="post">
<label for="srcDomain">
Original domain:
<input type="text" id="srcDomain" name="srcDomain" />
</label>
<label for="dstDomain">
Domain to redirect to:
<input type="text" id="dstDomain" name="dstDomain" />
</label>
<input type="hidden" name="action" value="add" />
<input type="Submit" value="Submit" />
</form>
<table id="domains">
<thead>
<tr><th>Original domain</th>
<th>Redirect domain</th>
<th>Actions</th>
</tr>
</thead>
<tbody>""" % (msg)
if len(res) == 0:
output += """
<tr><td colspan="3" style="text-align: center">No domains in database</td></tr>"""
ctr = 0
for row in res:
output += """
<tr class="{4}">
<td>{0}</td>
<td>{1}</td>
<td><a href="javascript:if (confirm('Do you really want to delete this record?'))
post_to_url('{2}', {{'id': '{3}', 'action':'delete'}})">Delete</a></td>
</tr>""".format(row['srcDomain'], row['dstDomain'], self.pathFromRoot, str(row['id']),
'even-row' if ctr % 2 == 0 else 'odd-row')
ctr += 1
output += """
</tbody>
</table>
</div>
<footer>
</footer>
</body>
</html>
"""
macdb.cleanup()
return output
def render_POST(self, request):
macdb = DbHelper()
if not 'action' in request.args:
request.setResponseCode(400)
return 'Error 400. Bad request.'
output = ""
if request.args['action'][0] == 'delete':
id_ = int(request.args['id'][0])
rec = macdb.uncensor_fetch_by_id(id_)
request.setResponseCode(301)
if rec != None:
macdb.uncensor_delete(id_)
request.setHeader('Location', self.pathFromRoot + '?msg=' + urllib.quote_plus('Success. Record deleted.'))
else:
request.setHeader('Location', self.pathFromRoot + '?msg=' + urllib.quote_plus('Error. Record not found.'))
elif request.args['action'][0] == 'add':
if (not ('srcDomain' in request.args)) or (not ('dstDomain' in request.args)):
request.setResponseCode(400)
return 'Error 400. Bad request.'
insertInfo = { 'srcDomain': request.args['srcDomain'][0], 'dstDomain': request.args['dstDomain'][0] }
macdb.uncensor_insert(insertInfo)
request.setResponseCode(301)
request.setHeader('Location', self.pathFromRoot + '?msg=' + urllib.quote_plus('Success. Record was added.'))
else:
request.setResponseCode(400)
output = 'Error 400. Bad request.'
macdb.cleanup()
return output