Changeset - 9d4fa3689996
[Not reviewed]
default
0 1 0
Dennis Fink - 11 years ago 2014-01-24 00:18:21
dennis.fink@c3l.lu
whitespace
1 file changed with 1 insertions and 1 deletions:
0 comments (0 inline, 0 general)
ennstatus/api/views.py
Show inline comments
 
from flask import (Blueprint, request, current_app, jsonify, render_template,
 
                   abort)
 

	
 
from ennstatus.api.functions import check_json_format, update_server
 
from ennstatus.status.functions import (single_server, all_servers,
 
                                        all_servers_by_type)
 

	
 
api_page = Blueprint('api', __name__)
 

	
 

	
 
@api_page.route('/update', methods=('POST',))
 
def update():
 

	
 
    current_app.logger.info('Handling update')
 
    if current_app.debug:
 
        accepted_ips = ['127.0.0.1']
 
    else:
 
        accepted_ips = current_app.config.get('ENNSTATUS_ACCEPTED_IPS', [])
 

	
 
    json = request.get_json()
 
    if json is None:
 
        current_app.logger.info('No JSON data supplied!')
 
        return 'No JSON data supplied!\n', 400, {'Content-Type': 'text/plain'}
 

	
 
    try:
 
        check_json_format(json)
 
    except ValueError as e:
 
        current_app.logger.warning(' '.join([str(e), str(json)]))
 
        return str(e), 409, {'Content-Type': 'text/plain'}
 

	
 
    if 'ip' in json:
 
        ip = json['ip']
 
    else:
 
        ip = request.remote_addr
 

	
 
    if request.remote_addr not in accepted_ips:
 
        current_app.logger.warn("Unallowed IP %s tried to update data!"
 
                                % ip)
 
        return 'IP not allowed!\n', 403, {'Content-Type': 'text/plain'}
 

	
 
    current_app.logger.info(str(json))    
 
    current_app.logger.info(str(json))
 
    server = update_server(server=json, ip=ip)
 

	
 
    if server:
 
        current_app.logger.info('Return result')
 
        current_app.logger.info(str(server))
 
        return (jsonify(server), 201,
 
                {'Location': '/api/export/json/single?server_name=%s'
 
                 % server['server_name']})
 

	
 
    else:
 
        current_app.logger.error("Unexpected error: %s" % server,
 
                                 exc_info=True)
 
        return abort(500)
 

	
 

	
 
@api_page.route('/export', defaults={'server_type': 'all',
 
                                     'export_format': 'json'})
 
@api_page.route('/export/<any("json", "xml"):export_format>',
 
                defaults={'server_type': 'all'})
 
@api_page.route(('/export/<any("json", "xml"):export_format>'
 
                 '/<any("all", "exit", "bridge", "relay", "single")'
 
                 ':server_type>'))
 
def export(export_format, server_type):
 

	
 
    current_app.logger.info('Handling export')
 
    if server_type == 'single':
 
        server_name = request.args.get('server_name', None)
 
        if server_name is not None:
 
            server = single_server(server_name)
 
            if server:
 
                if export_format == 'json':
 
                    current_app.logger.info('Returning server as json!')
 
                    return jsonify(server)
 
                else:
 
                    current_app.logger.info('Returning server as xml!')
 
                    return (
 
                        render_template(
 
                            'api/extract/xml/single_server.xml',
 
                            server=server),
 
                        200, {'Content-Type': 'text/xml'})
 
            else:
 
                current_app.logger.warning('Server not found!')
 
                return ('Server not found!\n',
 
                        404, {'Content-Type': 'text/plain'})
 
        else:
 
            current_app.logger.warning('No server_name specified!')
 
            return ('No server_name specified!\n',
 
                    400, {'Content-Type': 'text/plain'})
0 comments (0 inline, 0 general)