"""Product ownership validation module.""" from functools import wraps from oto import response from oto.adaptors.flask import flaskify from assets.constants import error from assets.constants import field_const from assets.models import ows_product def check_products_ownership(request): """Check ownership of a product. Args: request (werkzeug.local.LocalProxy): request object from the handler. Returns: callable: the wrapped function. """ def wrap(function): @wraps(function) def wrapped_f(product_id, *args, **kwargs): if (field_const.GRASS_ACCOUNT_ID not in request.headers or field_const.GRASS_ACCOUNT_TYPE not in request.headers): return flaskify( response.create_error_response( code=error.ERROR_CODE_HEADER_VALIDATION, message='{0} and {1} are required values'.format( field_const.GRASS_ACCOUNT_ID, field_const.GRASS_ACCOUNT_TYPE))) account_type = request.headers.get( field_const.GRASS_ACCOUNT_TYPE) account_id = request.headers.get( field_const.GRASS_ACCOUNT_ID) ownership_response = ows_product.check_ownership( account_type, account_id, product_id) if not ownership_response: return flaskify(ownership_response) return function(product_id, *args, **kwargs) return wrapped_f return wrap