def test_decorators(self): def foo(): return bottle.request.method bottle.get('/')(foo) bottle.post('/')(foo) bottle.put('/')(foo) bottle.delete('/')(foo) for verb in 'GET POST PUT DELETE'.split(): self.assertBody(verb, '/', method=verb)
def routeapp(obj): for kw in dir(obj): attr = getattr(obj, kw) if hasattr(attr, 'error'): bottle.error(attr.error)(attr) if hasattr(attr, 'delete'): bottle.delete(attr.delete)(attr) if hasattr(attr, 'put'): bottle.put(attr.put)(attr) if hasattr(attr, 'post'): bottle.post(attr.post)(attr) if hasattr(attr, 'route'): bottle.route(attr.route)(attr)
def test_decorators(self): app = bottle.Bottle() app.route('/g')('foo') bottle.route('/g')('foo') app.route('/g2', method='GET')('foo') bottle.get('/g2')('foo') app.route('/p', method='POST')('foo') bottle.post('/p')('foo') app.route('/p2', method='PUT')('foo') bottle.put('/p2')('foo') app.route('/d', method='DELETE')('foo') bottle.delete('/d')('foo') self.assertEqual(app.routes, bottle.app().routes)
def routeapp(server): # print dir(server) for kw in dir(server): # print "kw: ", kw attr = getattr(server, kw) if hasattr(attr, 'get'): print "- attr: ", attr print "- attr.get: ", attr.get # traceback.print_exc() bottle.get(attr.get)(attr) if hasattr(attr, 'delete'): print "- attr: ", attr print "- attr.delete: ", attr.delete # traceback.print_exc() bottle.delete(attr.delete)(attr)
def run(self): # UI Functions route('/')(self.frontend) # route('/', method='OPTIONS')(self.options_handler) # route('/<path:path>', method='OPTIONS')(self.options_handler) # hook('after_request')(self.enable_cors) # API functions # v1 route('/api/v1/')(self.index) route('/api/v1/status')(self.status) route('/api/v1/stations')(self.stations) route('/api/v1/streams')(self.streams) route('/api/v1/<station>/streams')(self.streams) route('/api/v1/<station>')(self.station) post('/api/v1/<station>')(self.set) route('/api/v1/<station>/<stream>')(self.stream) post('/api/v1/<station>/<stream>')(self.set) route('/api/v1/play')(self.play) route('/api/v1/pause')(self.pause) route('/api/v1/stop')(self.stop) # API functions # v1.1 route('/api/v1.1/stations')(self.stations) route('/api/v1.1/stations/<station>')(self.station) route('/api/v1.1/stations/<station>/streams')(self.streams) route('/api/v1.1/stations/<station>/streams/<stream>')(self.stream) route('/api/v1.1/streams')(self.streams) route('/api/v1.1/streams/<station>')(self.streams) route('/api/v1.1/streams/<station>/<stream>')(self.stream) route('/api/v1.1/player')(self.status) post('/api/v1.1/player')(self.play) post('/api/v1.1/player/<station>/<stream>')(self.play) post('/api/v1.1/volume/<value>')(self.volume) put('/api/v1.1/player')(self.pause) delete('/api/v1.1/player')(self.stop) try: run(host=self.host, port=self.port, debug=BOTTLE_DEBUG, quiet=not BOTTLE_DEBUG) except (OSError, OverflowError) as exc_info: print("SERVER ERROR: %s." % exc_info) print("Check network settings (host, port) in config") sys.exit(1)
def run(self): # UI Functions route('/')(self.frontend) # route('/', method='OPTIONS')(self.options_handler) # route('/<path:path>', method='OPTIONS')(self.options_handler) # hook('after_request')(self.enable_cors) # API functions # v1 route('/api/v1/')(self.index) route('/api/v1/status')(self.status) route('/api/v1/stations')(self.stations) route('/api/v1/streams')(self.streams) route('/api/v1/<station>/streams')(self.streams) route('/api/v1/<station>')(self.station) post('/api/v1/<station>')(self.set) route('/api/v1/<station>/<stream>')(self.stream) post('/api/v1/<station>/<stream>')(self.set) route('/api/v1/play')(self.play) route('/api/v1/pause')(self.pause) route('/api/v1/stop')(self.stop) # API functions # v1.1 route('/api/v1.1/stations')(self.stations) route('/api/v1.1/stations/<station>')(self.station) route('/api/v1.1/stations/<station>/streams')(self.streams) route('/api/v1.1/stations/<station>/streams/<stream>')(self.stream) route('/api/v1.1/streams')(self.streams) route('/api/v1.1/streams/<station>')(self.streams) route('/api/v1.1/streams/<station>/<stream>')(self.stream) route('/api/v1.1/player')(self.status) post('/api/v1.1/player')(self.play) post('/api/v1.1/player/<station>/<stream>')(self.play) put('/api/v1.1/player')(self.pause) delete('/api/v1.1/player')(self.stop) run(host=self.host, port=self.port, debug=BOTTLE_DEBUG, quiet=not BOTTLE_DEBUG)
def postREMOVE(): name = bottle.request.forms.get('name') if (name is None): formulaireStudentAdd = """ <form action="/student/remove" method='post'> <input type='text' name='name' placeholder='Name'/> <input type='submit' value='Validez !'/> </form> """ return formulaireStudentAdd else: return delete(name), goBack
def getDelete(name): return delete(name)
def create_rest_api(base_collection, url_base, restricted=False, override_={}, post_={}): def list_collection(**context): collection = base_collection.fetch(**context) bottle.response.context.update({'collection': collection}) return collection def update_collection(**context): pass def add_to_collection(**context): new_item = bottle.request.json new_item['user'] = context['user'] item = base_collection.new(**new_item) bottle.response.context.update({'item': item}) return item def delete_collection(**context): pass def show_item(id, **context): item = base_collection.get(_id=id) bottle.response.context.update({'item': item}) return item def update_item(id, **context): updated_item = bottle.request.json updated_item['_id'] = id item = base_collection.update(**updated_item) bottle.response.context.update({'item': item}) return item def add_to_item(id, **context): pass def delete_item(id, **context): base_collection.remove(_id=id) return list_collection(**context) route_map = { # Collections '%s' % url_base: list_collection, '>%s' % url_base: update_collection, '#%s' % url_base: add_to_collection, 'X%s' % url_base: delete_collection, # Entities '%s/:id' % url_base: show_item, '>%s/:id' % url_base: update_item, '#%s/:id' % url_base: add_to_item, 'X%s/:id' % url_base: delete_item, } template_map = { # Collections list_collection: 'list', update_collection: 'list', add_to_collection: 'show', delete_collection: 'list', # Entities show_item: 'show', update_item: 'show', add_to_item: 'show', delete_item: 'list', } # Create the full callback for a route and action def make_templated_callback(callback): template_base = 'generic/' def wrapper(**context): #print context context['user'] = bottle.response.context.user.id cb=callback if override_.has_key(callback.__name__): cb = override_[callback.__name__] result = cb(**context) if post_.has_key(callback.__name__): result = post_[cb.__name__](**result) return render(template_base + template_map[callback], **result) if restricted: wrapper = require_auth(wrapper) return wrapper # Make a JSON version too def make_json_callback(callback): def wrapper(**context): context['user'] = bottle.response.context.user.id cb=callback if override_.has_key(callback.__name__): cb = override_[callback.__name__] #print "CONTEXT: %s" % context result = cb(**context) if post_.has_key(callback.__name__): result = post_[cb.__name__](**result) #print "RESULT: %s" % result return pymongo_to_json(result) if restricted: wrapper = require_auth(wrapper) return wrapper # Generate the routes for route in route_map.items(): path, callback = route if path.startswith('#'): bottle.post(path[1:] + '.json')(make_json_callback(callback)) bottle.post(path[1:])(make_templated_callback(callback)) elif path.startswith('X'): bottle.delete(path[1:] + '.json')(make_json_callback(callback)) bottle.delete(path[1:])(make_templated_callback(callback)) elif path.startswith('>'): bottle.put(path[1:] + '.json')(make_json_callback(callback)) bottle.put(path[1:])(make_templated_callback(callback)) else: bottle.get(path + '.json')(make_json_callback(callback)) bottle.get(path)(make_templated_callback(callback))
'X/'+_node_id_: [authenticate, delete_node], } # Define routes from the route map def make_route_callback(callback): if type(callback) == list: callback = partial(*callback) def wrapper(**context): return callback(**get_session(**start_context(**context))) return wrapper for route in route_map.items(): path, callback = route if path.startswith('#'): bottle.post(path[1:])(make_route_callback(callback)) elif path.startswith('X'): bottle.delete(path[1:])(make_route_callback(callback)) elif path.startswith('>'): bottle.put(path[1:])(make_route_callback(callback)) else: bottle.get(path)(make_route_callback(callback)) # Static fallback @bottle.route('/<filepath:path>') def static_file(filepath): return bottle.static_file(filepath, root='./static/') # Run the server bottle.debug(config.debug) bottle.run(server=config.server, host='0.0.0.0', port=config.port, reloader=config.debug)