""" Logic for Mech Admin. Provides logic for getting mechadmin related info. """ from contracts import response from contracts.constants import error from contracts.logic import account from contracts.models import vendor_contract def is_mech_admin(account_type, account_id): """Check whether vendor or subaccount is mechadmin. Args: account_type: either vendor or subaccount. account_id: unique identifier of the vendor or subaccount. Returns: response.Response: Success if vendor or subaccount is mechadmin. """ if account_type == 'subaccount' and account_id != '': account_response = account.get_vendor_by_subaccount(account_id) if not account_response: return account_response account_id = account_response.message result = get_mech_admin(account_id) if result.status != 200: return result return response.Response({'mechadmin': True}) def get_mech_admin(vendor_id): """Get vendor contract information if vendor is mechadmin. Args: vendor_id: unique identifier of the vendor. Returns: response.Response: vendor contract information if vendor is mechadmin. """ active_contract = vendor_contract.get_active_contract(vendor_id) if not active_contract: return active_contract result_vendor_contract = vendor_contract.get_digital_mech_admin( active_contract.message['vendor_contract_id'], vendor_id ) if not result_vendor_contract: return response.Response(message=error.ERROR_MESSAGE_NOT_MECHADMIN, status=204) return result_vendor_contract def is_subaccount_mechadmin(subaccount_id, grass_vendor_id): """Check whether subaccount is mechadmin. Args: subaccount_id (int): unique identifier of the subaccount. grass_vendor_id (str): vendor id of grass request. Returns: response.Response: Success if subaccount is mechadmin. """ account_response = account.get_vendor_by_subaccount(subaccount_id) if not account_response: return account_response vendor_id = account_response.message if grass_vendor_id and vendor_id != int(grass_vendor_id): return response.Response(message=error.ERROR_MESSAGE_FORBIDDEN_USER, status=403) result_mech_admin = is_mech_admin('vendor', vendor_id) return result_mech_admin def account_is_mech_admin(account_type, account_id): """Check whether vendor or subaccount is digital and physical mechadmin. Args: account_type: either vendor or subaccount. account_id: unique identifier of the vendor or subaccount. Returns: response.Response: contains dict with physical and digital mech admin bool value. """ if account_type == 'subaccount' and account_id != '': account_response = account.get_vendor_by_subaccount(account_id) if not account_response: return account_response account_id = account_response.message active_contract = vendor_contract.get_active_contract(account_id) if not active_contract: return active_contract is_mech_admin = vendor_contract.get_mech_admin_for_account( active_contract.message['vendor_contract_id'], account_id ) return is_mech_admin