Commit 5348ec11 authored by David Mendez's avatar David Mendez

Start to add blueprint to submit subset generation

parent 6df42283
"""
Module that provides logging functions for any other module and adapts to any environment where it is running.
"""
import logging
from app.config import RUN_CONFIG
DEBUG = 'DEBUG'
INFO = 'INFO'
WARNING = 'WARNING'
ERROR = 'ERROR'
CRITICAL = 'CRITICAL'
def debug(msg):
"""
Logs a debug message
"""
log_msg(DEBUG, msg)
def info(msg):
"""
Logs an info message
"""
log_msg(INFO, msg)
def warning(msg):
"""
Logs a warning message
"""
log_msg(WARNING, msg)
def error(msg):
"""
Logs an error message
"""
log_msg(ERROR, msg)
def critical(msg):
"""
Logs a critical message
"""
log_msg(CRITICAL, msg)
def log_msg(level, msg):
"""
Prints a message to the console
"""
logger = RUN_CONFIG.get('logger')
if logger is None:
print(level, ':', msg)
else:
gunicorn_logger = logging.getLogger(logger)
if level == DEBUG:
gunicorn_logger.debug(msg)
elif level == INFO:
gunicorn_logger.info(msg)
elif level == WARNING:
gunicorn_logger.warning(msg)
elif level == ERROR:
gunicorn_logger.error(msg)
elif level == CRITICAL:
gunicorn_logger.critical(msg)
"""
Marshmallow schemas for validating the subset generator blueprind
"""
from marshmallow import Schema, fields
class SubmitSubsetGenerationRequest(Schema):
"""
Class with the schema for submitting the generation of a subset
"""
origin_index = fields.String(required=True)
items_ids = fields.Nested(required=True)
"""
The blueprint used for submitting the generation of a subset
"""
from flask import Blueprint, jsonify, abort, request
from app.blueprints.subset_generator_blueprint.controllers import marshmallow_schemas
from app.request_validation.decorators import validate_form_with, validate_url_params_with
from app import app_logging
SUBSET_GENERATOR_BLUEPRINT = Blueprint('es_subsets', __name__)
@SUBSET_GENERATOR_BLUEPRINT.route('/submit_subset_creation_from_ids', methods=['POST'])
@validate_form_with(marshmallow_schemas.SubmitSubsetGenerationRequest)
def submit_subset_creation_from_ids():
"""
:return: the json response with the id of the subset creation process
"""
form_data = request.form
origin_index = form_data.get('origin_index')
items_ids = form_data.get('items_ids')
app_logging.debug(f'origin_index: {origin_index}')
app_logging.debug(f'items_ids: {items_ids}')
# try:
return jsonify({'msg': 'hola'})
# except es_proxy_service.ESProxyServiceError as error:
#
# abort(500, msg=f'Internal server error: {str(error)}')
\ No newline at end of file
"""
Module that handles decorators used in request validation
"""
from functools import wraps
from flask import request, abort
def validate_form_with(validation_schema):
"""
validates the form params with the marshmallow schema given as parameter
:param validation_schema: schema to use for validation
"""
def wrap(func):
@wraps(func)
def wrapped_func(*args, **kwargs):
validation_errors = validation_schema().validate(request.form)
if validation_errors:
abort(400, str(validation_errors))
return func(*args, **kwargs)
return wrapped_func
return wrap
def validate_url_params_with(validation_schema):
"""
validates the url params with the marshmallow schema given as parameter
:param validation_schema: schema to use for validation
"""
def wrap(func):
@wraps(func)
def wrapped_func(*args, **kwargs):
validation_errors = validation_schema().validate(kwargs)
if validation_errors:
abort(400, str(validation_errors))
return func(*args, **kwargs)
return wrapped_func
return wrap
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment