"""Feature flags on backend.""" from flask import g from flask import request from owsrequest.context import get_request_context_from_headers from pythonfeatures import pythonfeatures from pythonfeatures.constants import split as split_constants from product_digital.constants import features as feature_names from product_digital.models import ows_account as ows_account_model def is_feature_flag_enabled(flag_name, use_account_context=False): """Check if users requested feature flag is enabled. Args: flag_name (str): name of the flag to be checked. use_account_context (boolean): use account context (if present) rather than profile """ context = g.request_context if use_account_context: # if account headers are present it will use account context rather than profile context = get_request_context_from_headers(request.headers) is_enabled = pythonfeatures.get_single_feature( flag_name, context ).message == split_constants.FEATURE_ENABLED g.ows.log.info(f'split {flag_name}: called with context_type {context.context_type} is {is_enabled}') return is_enabled def is_feature_flag_enabled_for_vendor(flag_name, vendor_id): """Check if feature flag is enabled for requested vendor.""" return pythonfeatures.get_single_feature_by_attributes( flag_name, {'vendor_id': vendor_id} ).message == split_constants.FEATURE_ENABLED def is_rejections_from_content_review_enabled(vendor_id, distribution_format_id): """Check if get_rejections_from_content_review FF is enabled.""" vendor = ows_account_model.get_vendor(vendor_id) assigned_reviewer = vendor.get('assigned_reviewer') if vendor else None attributes = { 'vendor_id': str(vendor_id), 'distribution_format_id': str(distribution_format_id), 'company_brand_name': ows_account_model.get_vendor_company_brand(vendor_id), 'owner': vendor.get('owner') if vendor else None, 'assigned_to': vendor.get('assigned_to') if vendor else None, 'assigned_reviewer': str(assigned_reviewer) if assigned_reviewer else None } return pythonfeatures.get_single_feature_by_attributes( feature_names.REJECTIONS_FROM_CONTENTREVIEW, attributes ).message == split_constants.FEATURE_ENABLED