def getCurrentSession(sessionClass=None, apiInstance=None): apiInstance = retrieveApiInstance(apiInstance=apiInstance) rawJwt = getJwtBody(apiInstance=apiInstance) identity = getIdentity(rawJwt=rawJwt, apiInstance=apiInstance) context = getContext(rawJwt=rawJwt, apiInstance=apiInstance) data = getData(rawJwt=rawJwt, apiInstance=apiInstance) if ObjectHelper.isNone(sessionClass): return { JwtConstant.KW_IDENTITY: identity, JwtConstant.KW_CONTEXT: context, JwtConstant.KW_DATA: data } else: currentSession = sessionClass() currentSession._contextInfo = { JwtConstant.KW_IDENTITY: identity, JwtConstant.KW_CONTEXT: context } for attributeName in data: if ReflectionHelper.hasAttributeOrMethod(currentSession, attributeName): ReflectionHelper.setAttributeOrMethod(currentSession, attributeName, data.get(attributeName)) return currentSession
def getCurrentUser(userClass=None, apiInstance=None): currentUsert = get_current_user() if ObjectHelper.isNotNone(currentUsert): return currentUsert else: rawJwt = getJwtBody() identity = getIdentity(rawJwt=rawJwt) context = getContext(rawJwt=rawJwt) data = getData(rawJwt=rawJwt) if ObjectHelper.isNone(userClass): return { JwtConstant.KW_IDENTITY: identity, JwtConstant.KW_CONTEXT: context, JwtConstant.KW_DATA: data } else: currentUsert = userClass() currentUsert._contextInfo = { JwtConstant.KW_IDENTITY: identity, JwtConstant.KW_CONTEXT: context } for attributeName in data: if ReflectionHelper.hasAttributeOrMethod( currentUsert, attributeName): ReflectionHelper.setAttributeOrMethod( currentUsert, attributeName, data.get(attributeName)) return currentUsert
def addResourceAttibutes(apiInstance): ReflectionHelper.setAttributeOrMethod(apiInstance, FlaskManager.KW_RESOURCE, FlaskResource()) for resourceName in FlaskManager.KW_RESOURCE_LIST: ReflectionHelper.setAttributeOrMethod( apiInstance.resource, f'{resourceName[0].lower()}{resourceName[1:]}', FlaskResource())
def setResource(apiInstance, resourceInstance, resourceName=None): resourceName = getResourceFinalName(resourceInstance, resourceName=resourceName) ReflectionHelper.setAttributeOrMethod(apiInstance, resourceName, resourceInstance)
def updateEnumItem(enumItem, key, value): enumItem.__enumItemMap__[key] = value enumItem.__enumItemEqMap__[key] = str(value) ReflectionHelper.setAttributeOrMethod( enumItem, key, ENUM_ITEM_CLASS_DICTIONARY.get(type(value))(value))
def __init__(self, *args, **kwargs): originalClassAttributeValueList = ReflectionHelper.getAttributeNameList( OuterEnum) if instanceLog: log.prettyPython(OuterEnum, 'originalClassAttributeValueList', originalClassAttributeValueList) OuterEnum.__init__(self, *args, **kwargs) self.__associateReturnsTo__ = associateReturnsTo attributeDataList = ReflectionHelper.getAttributeDataList(self) if instanceLog: log.prettyPython(OuterEnum, 'attributeDataList', attributeDataList) for attribute, enumValue in attributeDataList: if enumValue not in ENUM_STATIC_METHOD_NAME_LIST and enumValue not in originalClassAttributeValueList: __raiseBadImplementation__(enumValue, enum=self) nativeClassDataList = [] attributeAttributeDataList = ReflectionHelper.getAttributeDataList( attribute) if instanceLog: log.prettyPython(OuterEnum, 'attributeAttributeDataList', attributeAttributeDataList) # print(f'type({attribute}): {type(attribute)}') association = ReflectionHelper.getAttributeOrMethod( attribute, self.__associateReturnsTo__, muteLogs=True) if type(association) is type(None): # print('type(association) is type(None)') ReflectionHelper.setAttributeOrMethod( attribute, ENUM_VALUE_AS_STRING_KEY, ENUM_ITEM_CLASS_DICTIONARY.get( type(enumValue))(enumValue)) nativeClassDataList = ReflectionHelper.getAttributeDataList( type(enumValue)()) # print(f'type({enumValue}): {type(enumValue)}, type({type(enumValue)}()): {type(type(enumValue)())}') else: # print('not type(association) is type(None)') ReflectionHelper.setAttributeOrMethod( attribute, ENUM_VALUE_AS_STRING_KEY, ENUM_ITEM_CLASS_DICTIONARY.get( type(association))(association)) nativeClassDataList = ReflectionHelper.getAttributeDataList( type(association)()) # print(f'type({association}): {type(association)}, type({type(association)}()): {type(type(association)())}') # print() attribute.enumValue.enumName = enumValue ReflectionHelper.setAttributeOrMethod( attribute, ENUM_NAME_AS_STRING_KEY, enumValue) if isinstance(attribute, EnumItem): # print(attribute, attribute.enumName, attribute) updateEnumItem( attribute, attribute.enumName, attribute) ###- so that __eq__ does not crash if not c.TYPE_FUNCTION == ReflectionHelper.getClassName( attribute): attribute.enumValue.__enumItemMap__ = attribute.__enumItemMap__ attribute.enumValue.__enumItemEqMap__ = attribute.__enumItemEqMap__ for dataInstance, dataName in attributeAttributeDataList: nativeData = False for _, name in nativeClassDataList: if dataName == name: nativeData = True break if not nativeData: ReflectionHelper.setAttributeOrMethod( attribute.enumValue, dataName, dataInstance) ReflectionHelper.setAttributeOrMethod( attribute.enumValue, ENUM_VALUE_AS_STRING_KEY, ReflectionHelper.getAttributeOrMethod( attribute, self.__associateReturnsTo__, muteLogs=True)) ReflectionHelper.setAttributeOrMethod( attribute.enumValue, ENUM_NAME_AS_STRING_KEY, enumValue) ReflectionHelper.setAttributeOrMethod( attribute.enumValue, ENUM_AS_STRING_KEY, self) if ObjectHelper.isEmpty(OuterEnum.__enumEqList__): for attribute, value in attributeDataList: # print(f'type({value}): {type(value)}') # print(f'type({attribute}): {type(attribute)}') valueAsString = str(attribute.enumValue) if valueAsString not in ENUM_STATIC_METHOD_NAME_LIST: if valueAsString not in self.__enumMap__: self.__enumMap__[ valueAsString] = attribute.enumValue ###- ReflectionHelper.getAttributeOrMethod(attribute, valueAsString, muteLogs=True) ###- attribute else: __raiseBadImplementation__(valueAsString, enum=self) if value not in self.__enumMap__: self.__enumMap__[value] = attribute elif not self.__enumMap__[value] == attribute: __raiseBadImplementation__(valueAsString, enum=self) self.__enumEqList__.append( f'{value}({self.__associateReturnsTo__}:{valueAsString})' )