Merge client and server in a single bottle app

This commit is contained in:
boyska 2013-11-26 12:04:43 +01:00
parent beba17e871
commit 36eae35528
17 changed files with 71 additions and 55 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
*.swp
*.pyc

View file

@ -5,19 +5,19 @@
<title> TechREC </title> <title> TechREC </title>
<link rel="stylesheet" type="text/css" href="css/style.css"> <link rel="stylesheet" type="text/css" href="/static/css/style.css">
<script src="js/jquery-1.9.1.min.js"></script> <script src="/static/js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="js/reclibrary.js"></script> <script type="text/javascript" src="/static/js/reclibrary.js"></script>
<script> <script>
/* /*
TODO: TODO:
prendere lo stato !!! prendere lo stato !!!
*/ */
$(document).ready(function(){ $(document).ready(function(){
$("#searchform").submit( $("#searchform").submit(
function (event) { function (event) {
event.preventDefault(); event.preventDefault();
dataString = $(this).serialize(); dataString = $(this).serialize();
@ -28,29 +28,29 @@ $(document).ready(function(){
$.each(data, function(key, val) { $.each(data, function(key, val) {
console.log("Extract " + key ); console.log("Extract " + key );
var divstring = "<div class=\"searchresult\" id=\""+ rs_trxarea(key) +"\"> </div>"; var divstring = "<div class=\"searchresult\" id=\""+ rs_trxarea(key) +"\"> </div>";
$("#searchresult").append( divstring ); $("#searchresult").append( divstring );
// var str = newformstr( key ); // new form // var str = newformstr( key ); // new form
var str = ""; var str = "";
str += "<div class=\"namevalues\">"+val.name+" - <a href=\"\">Scarica</a> <a href=\"\" id=\"delete-"+val.id+"\">Cancella</a></div>"; str += "<div class=\"namevalues\">"+val.name+" - <a href=\"\">Scarica</a> <a href=\"\" id=\"delete-"+val.id+"\">Cancella</a></div>";
str += "<div class=\"namevalues\">RECID: "+val.recid+" ID: "+ val.id + " Active " + val.active + "</div>"; str += "<div class=\"namevalues\">RECID: "+val.recid+" ID: "+ val.id + " Active " + val.active + "</div>";
str += "<div class=\"timevalues\">["+val.starttime+" >>> "+val.endtime+"]</div>" str += "<div class=\"timevalues\">["+val.starttime+" >>> "+val.endtime+"]</div>"
$("#"+rs_trxarea(key)).html(str); $("#"+rs_trxarea(key)).html(str);
$("#delete-"+val.id).click(function(evt) { $("#delete-"+val.id).click(function(evt) {
evt.preventDefault(); evt.preventDefault();
recDelete(val.recid, rs_trxarea(key) ); recDelete(val.recid, rs_trxarea(key) );
} }
); // End of delete link handler ); // End of delete link handler
}); });
}); });
}); });
$("#searchform").submit(); $("#searchform").submit();
}); });
</script> </script>
@ -60,12 +60,12 @@ $(document).ready(function(){
<body> <body>
<div id="pagecontainer"> <div id="pagecontainer">
<h1> techr*c </h1> <h1> techr*c </h1>
<h2> <a href="index.html"> Nuova registrazione</a> </h2> <h2> <a href="index.html"> Nuova registrazione</a> </h2>
<div id="buttonscontainer"> <div id="buttonscontainer">
<form action="#" method="POST" id="searchform"> <form action="#" method="POST" id="searchform">
<input type="text" id="recid" name="recid" placeholder="Codice Registrazione" /> <br /> <input type="text" id="recid" name="recid" placeholder="Codice Registrazione" /> <br />
<input type="text" id="name" name="name" placeholder="name" /> <br /> <input type="text" id="name" name="name" placeholder="name" /> <br />
@ -75,11 +75,11 @@ $(document).ready(function(){
</form> </form>
</div> </div>
<div id="searchresult"> <div id="searchresult">
</div> </div>
</div> </div>
<div id="footer"> <div id="footer">

4
server/requirements.txt Normal file
View file

@ -0,0 +1,4 @@
PyYAML==3.10
SQLAlchemy==0.8.3
bottle==0.11.6
wsgiref==0.1.2

View file

@ -1,9 +1,10 @@
# from bottle import hook, response, route, run, static_file, request # from bottle import hook, response, route, run, static_file, request
from bottle import Bottle, hook, template, response, request,static_file import datetime
import json import logging
import socket
from techrec import * from bottle import Bottle, hook, response, request,static_file
from techrec import Rec, RecDB
class RecServer: class RecServer:
def __init__(self,host="127.0.0.1", port=8000): def __init__(self,host="127.0.0.1", port=8000):
@ -16,7 +17,7 @@ class RecServer:
self.db = RecDB() self.db = RecDB()
def start(self): def start(self):
self._app.run(host=self._host, port=self._port) self._app.run(host=self._host, port=self._port, debug=True)
@hook('after_request') @hook('after_request')
def enable_cors(self): def enable_cors(self):
@ -31,20 +32,29 @@ class RecServer:
self._app.route('/help', callback=self.help) self._app.route('/help', callback=self.help)
self._app.route('/help/', callback=self.help) self._app.route('/help/', callback=self.help)
self._app.route('/create', method="POST", callback=self.create) self._app.route('/create', method="POST", callback=self.create)
# self._app.post('/create', callback=self.create) # self._app.post('/create', callback=self.create)
self._app.route('/update', method="POST", callback=self.update) self._app.route('/update', method="POST", callback=self.update)
self._app.route('/search', method="POST", callback=self.search)
self._app.route('/search', method="POST", callback=self.search)
self._app.route('/delete', method="POST", callback=self.delete) self._app.route('/delete', method="POST", callback=self.delete)
self._app.route('/static/<filepath:path>',
callback= lambda filepath: static_file(filepath, root='static/'))
self._app.route('/js/<f>',
callback= lambda f: static_file(f, root='static/js/'))
self._app.route('/css/<f>',
callback= lambda f: static_file(f, root='static/css/'))
self._app.route('/img/<f>',
callback= lambda f: static_file(f, root='static/img/'))
self._app.route('/', callback=lambda: static_file('index.html',
root='pages/'))
self._app.route('/tempo', callback=lambda: static_file('tempo.html',
root='pages/'))
def extsearch( self, args ): def extsearch( self, args ):
print "ARG", args print "ARG", args
return self.rec_err("EXT") return self.rec_err("EXT")
""" """
CREATE HANDLER CREATE HANDLER
""" """
@ -63,12 +73,12 @@ class RecServer:
if req["endtime-"+req["recid"]] != "": if req["endtime-"+req["recid"]] != "":
endtime = datetime.datetime.strptime( req["endtime-"+req["recid"]] , "%Y/%m/%d %H:%M:%S") endtime = datetime.datetime.strptime( req["endtime-"+req["recid"]] , "%Y/%m/%d %H:%M:%S")
print "Name %s RECID %s Starttime %s EndTime %s" %(req["name-"+req["recid"]],req["recid"], starttime,endtime ) print "Name %s RECID %s Starttime %s EndTime %s" %(req["name-"+req["recid"]],req["recid"], starttime,endtime )
ret = self.db.add( Rec(name=req["name-"+req["recid"]], ret = self.db.add( Rec(name=req["name-"+req["recid"]],
recid=req["recid"], recid=req["recid"],
starttime=starttime, starttime=starttime,
endtime=endtime ) endtime=endtime )
) )
return { "msg": "Nuova registrazione aggiunta", } return { "msg": "Nuova registrazione aggiunta", }
@ -77,7 +87,7 @@ class RecServer:
# @route('/active') # @route('/active')
def getactive(self): def getactive(self):
print "GetActive" print "GetActive"
""" """
DELETE HANDLER DELETE HANDLER
""" """
@ -88,7 +98,7 @@ class RecServer:
logging.info("Server: request delete %s " % ( req ) ) logging.info("Server: request delete %s " % ( req ) )
if not req.has_key( "recid" ): if not req.has_key( "recid" ):
return self.rec_err("No valid ID") return self.rec_err("No valid ID")
if self.db.delete( req["recid"] ): if self.db.delete( req["recid"] ):
return self.rec_msg("DELETE OK") return self.rec_msg("DELETE OK")
else: else:
@ -106,12 +116,12 @@ class RecServer:
ret["starttime"] = req ["starttime-"+req["recid"]] ret["starttime"] = req ["starttime-"+req["recid"]]
ret["endtime"] = req["endtime-"+req["recid"]] ret["endtime"] = req["endtime-"+req["recid"]]
ret["name"] = req["name-"+req["recid"]] ret["name"] = req["name-"+req["recid"]]
if self.db.update( req["recid"], ret ): if self.db.update( req["recid"], ret ):
return self.rec_msg("Aggiornamento completato!"); return self.rec_msg("Aggiornamento completato!");
else: else:
return self.rec_err("Errore Aggiornamento"); return self.rec_err("Errore Aggiornamento");
""" """
JSON' RESPONDER JSON' RESPONDER
""" """
@ -128,27 +138,27 @@ class RecServer:
req = dict( request.POST.allitems() ) req = dict( request.POST.allitems() )
print "Search request: %s" % (req) print "Search request: %s" % (req)
name = "%s" % req["name"] name = "%s" % req["name"]
if req["name"] == "": name = None if req["name"] == "": name = None
starttime = req["starttime"] starttime = req["starttime"]
if req["starttime"] == "": starttime = None if req["starttime"] == "": starttime = None
endtime = req["endtime"] endtime = req["endtime"]
if req["endtime"] == "": endtime = None if req["endtime"] == "": endtime = None
recid = req["recid"] recid = req["recid"]
if req["recid"]== "": recid = None if req["recid"]== "": recid = None
active = True active = True
values = self.db._search(recid=recid,name=name, starttime=starttime, endtime=endtime,active=active) values = self.db._search(recid=recid,name=name, starttime=starttime, endtime=endtime,active=active)
print "Returned Values %s" % values print "Returned Values %s" % values
ret = {} ret = {}
for rec in values: for rec in values:
recid = "rec-" + str(rec.id) recid = "rec-" + str(rec.id)
ret [recid] = {} ret [recid] = {}
ret [recid]["name"] = rec.name ret [recid]["name"] = rec.name
ret [recid]["id"] = rec.id ret [recid]["id"] = rec.id
@ -156,15 +166,15 @@ class RecServer:
ret [recid]["starttime"] = rec.starttime.strftime("%Y-%m-%d-%H-%H-%s") ret [recid]["starttime"] = rec.starttime.strftime("%Y-%m-%d-%H-%H-%s")
if rec.endtime != None: if rec.endtime != None:
ret [recid]["endtime"] = rec.endtime.strftime("%Y-%m-%d-%H-%H-%s") ret [recid]["endtime"] = rec.endtime.strftime("%Y-%m-%d-%H-%H-%s")
ret [recid]["active"] = rec.active ret [recid]["active"] = rec.active
logging.info("Return: %s" % ret); logging.info("Return: %s" % ret);
return ret return ret
# @route('/favicon.ico') # @route('/favicon.ico')
def favicon(self): def favicon(self):
return static_file('icon.ico', root="./img/", mimetype="image/ico") return static_file('icon.ico', root="/static/img/", mimetype="image/ico")
# @route('/help') # @route('/help')
def help(self): def help(self):
@ -184,5 +194,5 @@ class RecServer:
TESTs TESTs
""" """
if __name__ == "__main__": if __name__ == "__main__":
c = RecServer(host="0.0.0.0") c = RecServer(host="localhost")
c.start() c.start()

View file

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View file

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View file

Before

Width:  |  Height:  |  Size: 673 B

After

Width:  |  Height:  |  Size: 673 B

View file

Before

Width:  |  Height:  |  Size: 2.8 KiB

After

Width:  |  Height:  |  Size: 2.8 KiB

View file

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB