2013-11-26 12:04:43 +01:00
|
|
|
import datetime
|
|
|
|
import logging
|
2013-09-15 16:14:27 +02:00
|
|
|
|
2013-11-26 14:45:31 +01:00
|
|
|
from bottle import Bottle, request, static_file, redirect
|
2013-11-26 12:04:43 +01:00
|
|
|
|
|
|
|
from techrec import Rec, RecDB
|
2013-09-15 16:14:27 +02:00
|
|
|
|
|
|
|
class RecServer:
|
|
|
|
def __init__(self,host="127.0.0.1", port=8000):
|
|
|
|
self._host = host
|
|
|
|
self._port = port
|
|
|
|
|
|
|
|
self._app = Bottle()
|
|
|
|
self._route()
|
|
|
|
|
|
|
|
self.db = RecDB()
|
|
|
|
|
|
|
|
def start(self):
|
2013-11-26 12:04:43 +01:00
|
|
|
self._app.run(host=self._host, port=self._port, debug=True)
|
2013-09-15 16:14:27 +02:00
|
|
|
|
|
|
|
def _route(self):
|
2013-11-26 14:45:31 +01:00
|
|
|
### This is the API part of the app
|
|
|
|
# TODO: move to namespace /api/
|
|
|
|
# TODO: create a "sub-application"
|
2013-09-15 16:14:27 +02:00
|
|
|
self._app.route('/help', callback=self.help)
|
|
|
|
self._app.route('/help/', callback=self.help)
|
|
|
|
|
2013-11-26 12:04:43 +01:00
|
|
|
self._app.route('/create', method="POST", callback=self.create)
|
|
|
|
# self._app.post('/create', callback=self.create)
|
2013-09-15 16:14:27 +02:00
|
|
|
|
2013-11-26 12:04:43 +01:00
|
|
|
self._app.route('/update', method="POST", callback=self.update)
|
2013-11-26 14:45:31 +01:00
|
|
|
self._app.route('/search', method=["GET", "POST"], callback=self.search)
|
2013-11-24 23:43:56 +01:00
|
|
|
self._app.route('/delete', method="POST", callback=self.delete)
|
2013-11-26 14:45:31 +01:00
|
|
|
|
|
|
|
## Static part of the site
|
2013-11-26 12:04:43 +01:00
|
|
|
self._app.route('/static/<filepath:path>',
|
|
|
|
callback= lambda filepath: static_file(filepath, root='static/'))
|
2013-11-26 14:04:33 +01:00
|
|
|
self._app.route('/', callback=lambda: redirect('/new.html'))
|
|
|
|
self._app.route('/new.html', callback=lambda: static_file('new.html',
|
2013-11-26 12:04:43 +01:00
|
|
|
root='pages/'))
|
2013-11-26 14:04:33 +01:00
|
|
|
self._app.route('/tempo.html', callback=lambda: static_file('tempo.html',
|
2013-11-26 12:04:43 +01:00
|
|
|
root='pages/'))
|
2013-11-24 23:43:56 +01:00
|
|
|
|
2013-09-15 16:14:27 +02:00
|
|
|
def extsearch( self, args ):
|
|
|
|
print "ARG", args
|
|
|
|
return self.rec_err("EXT")
|
2013-11-26 12:04:43 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
"""
|
|
|
|
CREATE HANDLER
|
|
|
|
"""
|
2013-09-15 16:14:27 +02:00
|
|
|
# @route('/create', method=['OPTIONS','POST'])
|
|
|
|
def create(self):
|
2013-11-19 23:16:03 +01:00
|
|
|
req = dict( request.POST.allitems() )
|
2013-09-15 16:14:27 +02:00
|
|
|
ret = {}
|
2013-11-24 23:43:56 +01:00
|
|
|
print "Server:: Create request %s " % req
|
|
|
|
|
|
|
|
starttime = ""
|
2013-11-19 23:16:03 +01:00
|
|
|
if req["starttime-"+req["recid"]] != "":
|
|
|
|
starttime = datetime.datetime.strptime( req["starttime-"+req["recid"]] , "%Y/%m/%d %H:%M:%S")
|
2013-09-15 16:14:27 +02:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
endtime = datetime.datetime.now()
|
2013-11-19 23:16:03 +01:00
|
|
|
if req["endtime-"+req["recid"]] != "":
|
|
|
|
endtime = datetime.datetime.strptime( req["endtime-"+req["recid"]] , "%Y/%m/%d %H:%M:%S")
|
2013-11-24 23:43:56 +01:00
|
|
|
|
2013-11-26 12:04:43 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
print "Name %s RECID %s Starttime %s EndTime %s" %(req["name-"+req["recid"]],req["recid"], starttime,endtime )
|
2013-11-26 12:04:43 +01:00
|
|
|
ret = self.db.add( Rec(name=req["name-"+req["recid"]],
|
|
|
|
recid=req["recid"],
|
2013-11-24 23:43:56 +01:00
|
|
|
starttime=starttime,
|
2013-11-26 12:04:43 +01:00
|
|
|
endtime=endtime )
|
2013-11-24 23:43:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
return { "msg": "Nuova registrazione aggiunta", }
|
|
|
|
return self.rec_msg("Nuova registrazione creata! (id:" + ret.id *")")
|
2013-11-19 23:16:03 +01:00
|
|
|
|
2013-09-15 16:14:27 +02:00
|
|
|
# @route('/active')
|
|
|
|
def getactive(self):
|
|
|
|
print "GetActive"
|
2013-11-26 12:04:43 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
"""
|
|
|
|
DELETE HANDLER
|
|
|
|
"""
|
2013-11-19 23:16:03 +01:00
|
|
|
# @route('/delete/<recid>') # @route('/delete/<recid>/')
|
2013-09-15 16:14:27 +02:00
|
|
|
def delete( self, recid = None ):
|
2013-11-24 23:43:56 +01:00
|
|
|
req = dict( request.POST.allitems() )
|
2013-11-25 00:29:24 +01:00
|
|
|
logging.info("Server: request delete %s " % ( req ) )
|
|
|
|
if not req.has_key( "recid" ):
|
2013-11-24 23:43:56 +01:00
|
|
|
return self.rec_err("No valid ID")
|
2013-11-26 12:04:43 +01:00
|
|
|
|
2013-11-25 00:29:24 +01:00
|
|
|
if self.db.delete( req["recid"] ):
|
2013-11-24 23:43:56 +01:00
|
|
|
return self.rec_msg("DELETE OK")
|
|
|
|
else:
|
|
|
|
return self.rec_err("DELETE error: %s" % (self.db.get_err()))
|
|
|
|
|
|
|
|
"""
|
|
|
|
UPDATE HANDLER
|
|
|
|
"""
|
|
|
|
# @route('/delete/<recid>') # @route('/delete/<recid>/')
|
|
|
|
def update( self ):
|
|
|
|
req = dict( request.POST.allitems() )
|
|
|
|
|
|
|
|
ret={}
|
|
|
|
ret["starttime"] = req ["starttime-"+req["recid"]]
|
|
|
|
ret["endtime"] = req["endtime-"+req["recid"]]
|
|
|
|
ret["name"] = req["name-"+req["recid"]]
|
2013-11-26 12:04:43 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
if self.db.update( req["recid"], ret ):
|
|
|
|
return self.rec_msg("Aggiornamento completato!");
|
|
|
|
else:
|
|
|
|
return self.rec_err("Errore Aggiornamento");
|
2013-11-26 12:04:43 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
"""
|
|
|
|
JSON' RESPONDER
|
|
|
|
"""
|
2013-11-26 14:45:31 +01:00
|
|
|
def rec_msg(self, msg): return {"message": msg, "status": True}
|
|
|
|
def rec_err(self, msg): return {"error": msg, "status": False}
|
2013-09-15 16:14:27 +02:00
|
|
|
|
|
|
|
|
2013-11-19 23:16:03 +01:00
|
|
|
"""
|
|
|
|
@route('/search') # @route('/search/') # @route('/search/<key>/<value>')
|
|
|
|
"""
|
2013-09-15 16:14:27 +02:00
|
|
|
def search( self, args=None):
|
2013-11-26 14:45:31 +01:00
|
|
|
if request.method == 'GET':
|
|
|
|
req = dict( request.GET.allitems() )
|
|
|
|
else:
|
|
|
|
req = dict( request.POST.allitems() )
|
2013-11-24 23:43:56 +01:00
|
|
|
print "Search request: %s" % (req)
|
2013-11-26 12:04:43 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
name = "%s" % req["name"]
|
2013-11-19 23:16:03 +01:00
|
|
|
if req["name"] == "": name = None
|
2013-11-26 12:04:43 +01:00
|
|
|
|
2013-11-19 23:16:03 +01:00
|
|
|
starttime = req["starttime"]
|
2013-11-24 23:43:56 +01:00
|
|
|
if req["starttime"] == "": starttime = None
|
2013-11-26 12:04:43 +01:00
|
|
|
|
2013-11-19 23:16:03 +01:00
|
|
|
endtime = req["endtime"]
|
|
|
|
if req["endtime"] == "": endtime = None
|
2013-11-26 12:04:43 +01:00
|
|
|
|
|
|
|
recid = req["recid"]
|
2013-11-24 23:43:56 +01:00
|
|
|
if req["recid"]== "": recid = None
|
2013-11-26 12:04:43 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
active = True
|
2013-11-26 12:04:43 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
values = self.db._search(recid=recid,name=name, starttime=starttime, endtime=endtime,active=active)
|
|
|
|
print "Returned Values %s" % values
|
2013-11-19 23:16:03 +01:00
|
|
|
ret = {}
|
|
|
|
for rec in values:
|
2013-11-26 12:04:43 +01:00
|
|
|
recid = "rec-" + str(rec.id)
|
|
|
|
|
2013-11-19 23:16:03 +01:00
|
|
|
ret [recid] = {}
|
|
|
|
ret [recid]["name"] = rec.name
|
2013-11-24 23:43:56 +01:00
|
|
|
ret [recid]["id"] = rec.id
|
|
|
|
ret [recid]["recid"] = rec.recid
|
2013-11-19 23:16:03 +01:00
|
|
|
ret [recid]["starttime"] = rec.starttime.strftime("%Y-%m-%d-%H-%H-%s")
|
|
|
|
if rec.endtime != None:
|
|
|
|
ret [recid]["endtime"] = rec.endtime.strftime("%Y-%m-%d-%H-%H-%s")
|
2013-11-26 12:04:43 +01:00
|
|
|
|
2013-11-24 23:43:56 +01:00
|
|
|
ret [recid]["active"] = rec.active
|
|
|
|
|
2013-11-19 23:16:03 +01:00
|
|
|
logging.info("Return: %s" % ret);
|
|
|
|
return ret
|
2013-11-26 12:04:43 +01:00
|
|
|
|
2013-09-15 16:14:27 +02:00
|
|
|
# @route('/help')
|
|
|
|
def help(self):
|
|
|
|
return " <h1>help</h1><hr/>\
|
|
|
|
<h2>/get, /get/, /get/<recid> </h2>\
|
|
|
|
<h3>Get Info about rec identified by RECID </h3>\
|
|
|
|
\
|
|
|
|
<h2>/search, /search/, /search/<key>/<value></h2>\
|
|
|
|
<h3>Search rec that match key/value (or get all)</h3>\
|
|
|
|
\
|
|
|
|
<h2>/delete/<recid> </h2>\
|
|
|
|
<h3>Delete rec identified by RECID </h3>\
|
|
|
|
<h2>/update </h2>\
|
|
|
|
<h3>Not implemented.</h3>"
|
|
|
|
|
|
|
|
"""
|
|
|
|
TESTs
|
|
|
|
"""
|
|
|
|
if __name__ == "__main__":
|
2013-11-26 12:04:43 +01:00
|
|
|
c = RecServer(host="localhost")
|
2013-09-15 16:14:27 +02:00
|
|
|
c.start()
|