forked from theopolis/RTFn-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rlite.py
285 lines (247 loc) · 11 KB
/
rlite.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import os, json, sys
from rlite import rLite
from rlite.db import DB
from rlite.auth import AuthController, valid_user
from rlite.etherpad import EtherpadLiteClient
try:
import cherrypy
except ImportError:
print "Cannot import 'cherrypy', did you install CherryPy3 > 3.1?"
sys.exit(1)
#Todo: add check for cherrypy.tools
class ViewController(object):
_cp_config = {
'auth.require': [valid_user()]
}
def __init__(self, caller=None):
self.caller = caller
self.header = self.caller.header + """<body onload="javascript: pad_list(); "><div class="topbar">
<div class="main">
<a href="/view">Home</a>
<a id="create-challenge" style="margin-left: 10px;">Create New</a>
|
<a href="/auth/logout">Logout</a>
</div>
<div id="actions" class="actions">
<a id="upload-file">Upload</a>
|
<a id="show-files">Show Files</a>
</div>
<div class="clear"></div>
<div id="create-modal">
<form method="post" action="/view/create">
<div>In the form of ^[\w]+$ please.</div>
<input type="hidden" name="from_page" value="/view" />
<div id="login-type">
<div>Challenge Name:</div>
</div>
<div id="login-box">
<div><input type="text" name="name" /></div>
</div>
<div id="login-button"><input type="submit" value="Create" /></div>
</form>
</div>
<div id="upload-modal">
<form method="post" enctype="multipart/form-data" action="/view/upload">
<div>Select file to upload:</div>
<input type="hidden" name="from_page" value="/view" />
<div>Upload location: <span id="upload-location"> </span></div>
<input type="hidden" id="upload-location-data" name="location" value="" />
<input type="file" name="file" value="" />
<div id="login-button"><input type="submit" value="Upload" /></div>
</form>
</div>
<div id="files-modal">
<iframe id="files-frame" src="" frameborder=""></iframe>
</div>
</div>"""
self.footer = "</body>" + self.caller.footer
@cherrypy.expose
def index(self, name=None, from_page="/view"):
"""Set etherpad cookie if none found"""
cookies = cherrypy.request.cookie
if "sessionID" not in cookies.keys():
cherrypy.response.cookie["sessionID"] = cherrypy.request.login["sessionID"]
cherrypy.response.cookie["sessionID"]["path"] = "/"
if name is not None:
self.create(name)
# Find all competition pads
pads_response = self.caller.elite.listPads(cherrypy.request.login["comp"][1])
page = ""
# on error, say so
if pads_response["data"] is None:
return self.header + """<div id="Login-warning">
Problem listing challenges :(
</div>""" + self.footer
# otherwise, display each challenge
for pad_id in pads_response["data"]["padIDs"]:
page += self.print_pad(pad_id)
page += """
<div id="delete-modal">
<form method="post" action="/view/delete">
<div>Are you sure you want to delete: </div>
<div id="delete-pad-name"> </div>
<input type="hidden" name="from_page" value="/view" />
<input type="hidden" id="delete-pad-input" name="name" value="" />
<div id="login-button"><input type="submit" value="Delete" /></div>
</form>
</div>
"""
return self.header + page + self.footer
@cherrypy.expose
def files(self, id=None):
if id is None:
return "No files found (no id)..."
dir_name = id
if not id == "main":
pads_response = self.caller.elite.listPads(cherrypy.request.login["comp"][1])
pad_name = ""
for pad in pads_response["data"]["padIDs"]:
if id == pad:
pad_name = pad.split("$")[1]
if pad_name is "":
return "No files found (bad id)..."
dir_name = pad_name
current_dir = os.path.dirname(os.path.abspath(__file__))
upload_path = os.path.abspath(os.path.join(current_dir, "static/uploads", dir_name))
if not os.path.exists(upload_path):
return "No files found (no dir)..."
page = ""
for file in os.listdir(upload_path):
page += """<div><a href="/static/uploads/%(dir)s/%(file)s" target="_blank">%(file)s</a></div>""" % {"dir": dir_name, "file": file}
return page
@cherrypy.expose
def upload(self, location=None, file=None, from_page="/view"):
"""Uploaded a file to ./static, expecting current_dir var set in main"""
if file is None or file.file is None:
return self.index()
print "uploading:", location, file.filename
"""If we are trying to save to a specific pad, check to make sure that pad exists"""
dir_name = location
if not location == "main":
print "uploading:", "searching for pad"
pads_response = self.caller.elite.listPads(cherrypy.request.login["comp"][1])
pad_name = ""
for pad in pads_response["data"]["padIDs"]:
if location == pad:
pad_name = pad.split("$")[1]
if pad_name is "":
return self.index()
"""Pad name was found, set the real location"""
print "location:", pad_name
dir_name = pad_name
current_dir = os.path.dirname(os.path.abspath(__file__))
upload_path = os.path.abspath(os.path.join(current_dir, "static/uploads"))
if not os.path.exists(upload_path):
os.mkdir(upload_path)
loc_path = os.path.abspath(os.path.join(upload_path, dir_name))
if not os.path.exists(loc_path):
os.mkdir(loc_path)
fp = os.open(os.path.join(loc_path, file.filename), os.O_CREAT | os.O_WRONLY)
while True:
data = file.file.read(4096)
if not data:
os.close(fp)
break
os.write(fp, data)
raise cherrypy.HTTPRedirect('/view' if location =="main" else '/view/pad/?id=%s' % location)
@cherrypy.expose
def create(self, name, from_page="/view"):
import re
if name is not None and re.match("^[\w]+$", name) is not None:
self.caller.elite.createGroupPad(cherrypy.request.login["comp"][1], name, "Created by %s" % cherrypy.request.login["user"][0])
raise cherrypy.HTTPRedirect('/view')
pass
@cherrypy.expose
def pad(self, id):
"""id - pad id with groupID"""
page = """
<iframe src="%(url)s/p/%(pad_id)s" frameborder="0" style="width: 100%%; height:100%%"></iframe>
""" % {"url": self.caller.rtfn.get("etherpad-url"), "pad_id": id}
return self.header % {"comp": cherrypy.request.login["comp"][0]} + page + self.footer
def print_pad(self, pad_id):
pad_display = """
<div class="box">
<a href="/view/pad?id=%(pad_id)s">%(pad_name)s</a>
<div class="delete" onclick="javascript:delete_pad({'%(pad_name)s':'%(pad_id)s'}); "> </div>
<div class="solve"> </div>
<div class="move"> </div>
</div>
""" % {
"pad_id": pad_id,
"url": self.caller.rtfn.get("etherpad-url"),
"pad_name": pad_id.split("$")[1]
}
return pad_display
pass
@cherrypy.expose
def delete(self, name=None, from_page="/view"):
"""Removes pad name"""
if name is not None and cherrypy.request.login["admin"]:
self.caller.elite.deletePad(name)
raise cherrypy.HTTPRedirect('/view')
@cherrypy.expose
def admin(self):
"""Show create comeptition page"""
if not cherrypy.request.login["admin"]:
return self.header + """<div id="Login-warning">
You are not an admin :(
</div>""" + self.footer
"""Todo: add admin panel, autheticate via static value in config, not via DB."""
class RootController(object):
def __init__(self, db, rtfn, elite):
self.db = db
self.elite = elite
self.rtfn = rtfn
self.header = '''
<link type='text/css' href='/static/css/basic.css' rel='stylesheet' media='screen' />
'''
self.footer = '''
<script type='text/javascript' src='/static/js/jquery.js'></script>
<script type='text/javascript' src='/static/js/jquery.simplemodal.js'></script>
<script type='text/javascript' src='/static/js/basic.js'></script>
'''
self._cp_config = {'tools.sessions.on': True, 'tools.auth.on': True}
self.auth = AuthController(self)
self.view = ViewController(self)
@cherrypy.expose
def index(self):
return self.header + """<div id="login-warning">
RTFn-lite (<a href="/view">Go!</a>)
</div>""" + self.footer
def main(args):
# Read settings used by etherpad client
rtfn = rLite()
# Start etherpad lite client
apiKey = rtfn.api_key()
baseUrl = rtfn.get("etherpad-url")
print "[rlite//Debug]: Starting with api key: %s, elite hosted at: %s" % (apiKey, baseUrl)
elite = EtherpadLiteClient(apiKey, baseUrl)
# rLite's DB management hooks etherpad lite API calls
db = DB(elite)
if args.add is not None and args.key is not None:
print "Adding competition {0} with key {1}.".format(args.add, args.key)
db.create_competition(args.add, args.key, add_elite=False)
sys.exit(0)
# Start web interface
current_dir = os.path.dirname(os.path.abspath(__file__))
config = {"/static": {
'tools.staticdir.on': True,
'tools.staticdir.dir': current_dir + "/static"
}}
cherrypy.config.update('rtfn.ini')
# Web interface uses DB (authentication), and elite (pad lists/info)
cherrypy.quickstart(RootController(db, rtfn, elite), "/", config=config)
if __name__ == '__main__':
try:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--add', default=None, help='Add a new competition then quit, must specify a --key for the competition.')
parser.add_argument('--key', default=None, help='Used with -add, to set the competition key.')
args = parser.parse_args()
except ImportError:
class ArgumentParser(object):
add = None
key = None
args = ArgumentParser()
main(args)