Esempio n. 1
0
def project(*args, **kwargs):
    args = current_app.config['args']
    headers = current_app.config['headers']

    response = Response()
    route_params = request.view_args
    get_params = request.args.to_dict(flat=False)
    if request.method in ['POST', 'PUT', 'DELETE']:
        body = request.json

    message = []
    data = None
    error = None
    try:
        if request.method == 'POST':
            body = request.json

            data = Project(**body)
            data.save()
            data = data.to_mongo()
        elif request.method == 'GET':
            _id = route_params.get('project_id')
            if _id is not None:
                data = Project.objects(id=bson.ObjectId(_id))
                if len(data) == 1:
                    data = data[0].to_mongo()
            else:
                data = [obj.to_mongo() for obj in Project.objects]

        elif request.method == 'PUT':
            body = request.json

            _id = route_params['project_id']
            data = Project.objects(id=bson.ObjectId(_id))
            data = data[0]
            for attr in ['name', 'description', 'img', 'link']:
                if body.get(attr) is not None:
                    setattr(data, attr, body[attr])
            data.save()
            data = data.to_mongo()
        elif request.method == 'DELETE':
            _id = route_params['project_id']
            data = Project.objects(id=bson.ObjectId(_id))
            data = [data.delete()]
        elif request.method == 'OPTIONS':
            pass
        else:
            pass

    except Exception as e:
        error = AugmentedException(e).to_dict()
        LOGGER.error('', exc_info=True)

    response = jsonify(message=message, data=data, error=error)
    response = add_headers(response, headers=headers)
    # LOGGER.warning(vars(response))
    return response
Esempio n. 2
0
def device(*args, **kwargs):
    args = current_app.config['args']
    headers = current_app.config['headers']

    response = Response()
    route_params = request.view_args
    get_params = request.args.to_dict(flat=False)
    if request.method in ['POST', 'PUT', 'DELETE']:
        body = request.json

    message = []
    data = None
    error = None
    try:
        if request.method == 'POST':
            body = request.json

            data = Device(name=body['name'], meta_data=body['meta_data'])
            data.save()
            data = data.to_mongo()
        elif request.method == 'GET':
            data = [obj.to_mongo() for obj in Device.objects]
        elif request.method == 'PUT':
            body = request.json

            _id = route_params['device_id']
            data = Device.objects(id=bson.ObjectId(_id))
            data = data[0]
            for attr in ['name', 'meta_data']:
                if body.get(attr) is not None:
                    setattr(data, attr, body[attr])
            data.save()
            data = data.to_mongo()
        elif request.method == 'DELETE':
            _id = route_params['device_id']
            data = Device.objects(id=bson.ObjectId(_id))
            data.delete()
        elif request.method == 'OPTIONS':
            pass
        else:
            pass

    except Exception as e:
        error = AugmentedException(e).to_dict()
        LOGGER.error('', exc_info=True)

    response = jsonify(message=message, data=data, error=error)
    response = add_headers(response, headers=headers)
    return response
Esempio n. 3
0
def user(*args, **kwargs):
    args = current_app.config['args']
    headers = current_app.config['headers']

    response = Response()
    route_params = request.view_args
    get_params = request.args.to_dict(flat=False)
    if request.method in ['POST', 'PUT', 'DELETE']:
        body = request.json

    message = []
    data = None
    error = None
    try:
        if request.method == 'POST':
            data = User(**body)
            data.save()
            data = data.to_mongo()
        elif request.method == 'GET':
            data = [obj.to_mongo() for obj in User.objects]
        elif request.method == 'PUT':
            _id = route_params['user_id']
            data = User.objects(id=bson.ObjectId(_id))
            for attr in [
                    'first_name', 'last_name', 'email', 'password',
                    'authorization'
            ]:
                if body.get(attr) is not None:
                    setattr(data, attr, body[attr])
            data.save()
            data = data.to_mongo()
        elif request.method == 'DELETE':
            _id = route_params['user_id']
            data = User.objects(id=bson.ObjectId(_id))
            data.delete()
        elif request.method == 'OPTIONS':
            pass
        else:
            pass

    except Exception as e:
        error = AugmentedException(e).to_dict()
        LOGGER.error('', exc_info=True)

    response = jsonify(message=message, data=data, error=error)
    response = add_headers(response, headers=headers)
    LOGGER.warning(vars(response))
    return response
Esempio n. 4
0
def ping(*args, **kwargs):
    args = current_app.config['args']
    headers = current_app.config['headers']

    response = Response()
    route_params = request.view_args
    get_params = request.args.to_dict(flat=False)
    body = request.json

    message = [get_params, route_params, body]
    data = 'pong'
    error = None

    response = jsonify(message=message, data=data, error=error)
    response = add_headers(response, headers=headers)
    LOGGER.warning(vars(response))
    return response