Skip to content

Commit e3c26b6

Browse files
author
Casey Boettcher
committed
ajax call from main form working
1 parent ed78927 commit e3c26b6

File tree

4 files changed

+39
-42
lines changed

4 files changed

+39
-42
lines changed

web/server.cfg

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11

22
[/]
3-
tools.sessions.on: True
43
tools.staticdir.on = True
5-
tools.staticdir.root = os.path.abspath('.')
6-
tools.staticdir.dir = os.path.join(os.path.abspath('.'), 'web')
4+
tools.staticdir.root = os.path.abspath(os.getcwd())
5+
tools.staticdir.index = "xfipchk.html"
6+
tools.staticdir.dir = os.getcwd()
7+
8+
[/process_form]
9+
tools.response_headers.on = True
10+
tools.response_headers.headers = [('Content-Type', 'application/json')]

web/webui.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,38 @@
33
import cherrypy
44
import xfipchk
55

6+
67
class XforceForm(object):
8+
79
def __init__(self, address='127.0.0.1', port=8000):
8-
cherrypy.config.update({'tools.staticdir.debug': True})
9-
# cherrypy.config.update('web/server.cfg')
1010
# parms override anything in config
1111
cherrypy.config.update({'server.socket_port': port,
1212
'server.socket_host': address})
1313

14-
1514
@cherrypy.expose
1615
def index(self):
1716
return "/"
1817

1918
@cherrypy.expose()
19+
@cherrypy.tools.json_out()
2020
def process_form(self, api_key, api_password, ip_addresses):
21-
if isinstance(ip_addresses, list):
22-
xfipchk.r
23-
xfipchk.call_xforce_api(ip_addresses, api_key, api_password)
21+
if xfipchk.validate_api_creds(api_key.strip()) and xfipchk.validate_api_creds(api_password.strip()):
22+
form_ips = []
23+
if isinstance(ip_addresses, str):
24+
form_ips.append(ip_addresses)
25+
elif isinstance(ip_addresses, list):
26+
form_ips.extend(ip_addresses)
27+
good_ips = []
28+
for i in form_ips:
29+
if xfipchk.validate_ip(i):
30+
good_ips.append(i)
31+
return xfipchk.call_xforce_api(good_ips, api_key, api_password)
32+
else:
33+
return cherrypy.HTTPError("400: Bad Request", 400)
2434

2535
@cherrypy.expose()
2636
def stop_demo(self, stop_demo):
2737
cherrypy.engine.stop()
2838

29-
3039
if __name__ == '__main__':
31-
cherrypy.quickstart(XforceForm())
40+
xfipchk.start_server()

web/xfipchk.html

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,16 @@
1414

1515
if (conf)
1616
{
17-
$.post("/stop_demo", function(data){ alert("Data Loaded: " + data); });
17+
$.post( "/stop_demo" )
1818
}
19-
/*$.post( "/stop_demo", function() {
20-
21-
})
22-
.success(data, status, ajaxRequestObj) function() {
19+
}
2320

24-
.done(function() {
25-
alert( "second success" );
26-
})
27-
.fail(function() {
28-
alert( "error" );
29-
})
30-
.always(function() {
31-
alert( "finished" );
32-
});
33-
}*/
21+
function check_ips()
22+
{
23+
var req = $.ajax({'url': '/process_form'});
24+
req.done(function (data){ $( ".result" ).html( data ); }, "json");
3425
}
26+
3527
</script>
3628
<link rel="stylesheet" type="text/css" href="xfipchk.css"/>
3729
</head>
@@ -42,7 +34,7 @@
4234
<p><span class="instructions">Instructions:</span></p>
4335
<ol>
4436
<li>Paste your IBM X-Force API key and password into the corresponding fields.</li>
45-
<li>Enter IP addresses in the box labeled accordingly. They must be delimited by commas or newlines.</li>
37+
<li>Enter IP addresses in the box labeled accordingly, each IP address on its own line.</li>
4638
<li>Press the Submit button to call the API.</li>
4739
<li>Review the results.</li>
4840
<li>Press the Stop Demo button to stop the demo.</li>
@@ -61,7 +53,7 @@
6153
<textarea class="ip_addresses" name="ip_addresses" id="ip_addresses"></textarea>
6254
</div>
6355
<div id="std_buttons">
64-
<button id="submit" type="submit" form="the_form" value="submit">Submit</button> <button id="reset" type="reset" form="the_form" value="reset">Reset</button>
56+
<button id="submit" form="the_form" onclick="check_ips()">Submit</button> <button id="reset" type="reset" form="the_form" value="reset">Reset</button>
6557
</div>
6658
</form>
6759
<form id="stop_form">

xfipchk.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -190,22 +190,14 @@ def print_json_file(results, file):
190190

191191

192192
def start_server(address='127.0.0.1', port=8000):
193-
os.chdir('./web')
194-
print("This is getcwd 1: {}".format(os.path.abspath(os.getcwd())))
195-
print("This is getcwd 2: {}".format(os.getcwd()))
196-
config = {'/':
197-
{
198-
'tools.staticdir.on': True,
199-
'tools.staticdir.root': os.path.abspath(os.getcwd()),
200-
'tools.staticdir.index': "xfipchk.html",
201-
'tools.staticdir.dir': os.getcwd()
202-
}
203-
}
193+
if not os.path.abspath(os.getcwd()).endswith("python_challenge/web"):
194+
os.chdir('./web')
204195
webapp = web.webui.XforceForm(address, port)
205196
d = cherrypy.process.plugins.Daemonizer(cherrypy.engine)
206197
d.subscribe()
207-
cherrypy.config.update(config)
208-
cherrypy.tree.mount(webapp, config=config)
198+
#cherrypy.config.update('server.cfg')
199+
cherrypy.tree.mount(webapp, config='./server.cfg')
200+
#cherrypy.tree.mount(webapp)
209201
cherrypy.engine.start()
210202
pidfile = tempfile.TemporaryFile(prefix='xfipchk', suffix='.pid')
211203
PIDFile(cherrypy.engine, pidfile).subscribe()
@@ -231,7 +223,7 @@ def main():
231223
try:
232224
pidfile = start_server(args.address, args.port)
233225
except (ConnectionError, KeyboardInterrupt) as err:
234-
print("Server failed to start: {}".format(err.strerror))
226+
print("Server failed to start: {}".format(err))
235227

236228
# assume cli if user passed in api key file
237229
elif hasattr(args, 'authN'):

0 commit comments

Comments
 (0)