validation services added
This commit is contained in:
@@ -1,21 +1,103 @@
|
||||
from typing import Any, Union
|
||||
"""
|
||||
Validation function handlers
|
||||
"""
|
||||
|
||||
from typing import Dict, Any
|
||||
from fastapi import Request
|
||||
|
||||
from Events.base_request_model import TokenDictType, BaseRouteModel
|
||||
from ApiLayers.AllConfigs.Redis.configs import RedisValidationKeysAction, RedisCategoryKeys
|
||||
from Services.Redis.Actions.actions import RedisActions
|
||||
from Events.base_request_model import BaseRouteModel
|
||||
|
||||
from config import ValidationsConfig
|
||||
|
||||
|
||||
class Handlers:
|
||||
"""Class for handling authentication functions"""
|
||||
class ValidateBase:
|
||||
|
||||
@classmethod # Requires no auth context
|
||||
def handle_function(cls, **kwargs):
|
||||
"""Handle function with kwargs"""
|
||||
return
|
||||
redis_key: str = f"{RedisCategoryKeys.METHOD_FUNCTION_CODES}:*:"
|
||||
|
||||
def __init__(self, url: str, reachable_codes: list):
|
||||
self.url = url
|
||||
self.reachable_codes = reachable_codes
|
||||
|
||||
@property
|
||||
def function_codes(self):
|
||||
redis_function_codes= RedisActions.get_json(
|
||||
list_keys=[f"{self.redis_key}{self.url}"]
|
||||
)
|
||||
if redis_function_codes.status:
|
||||
return redis_function_codes.first
|
||||
raise ValueError("Function code not found")
|
||||
|
||||
@property
|
||||
def intersection(self):
|
||||
intersection = list(set(self.function_codes).intersection(set(self.reachable_codes)))
|
||||
if not len(intersection) == 1:
|
||||
raise ValueError("Users reachable function codes does not match or match more than one.")
|
||||
return intersection[0]
|
||||
|
||||
|
||||
class TemplateFunctions(BaseRouteModel):
|
||||
"""Class for handling authentication functions"""
|
||||
class RedisHeaderRetrieve(ValidateBase):
|
||||
|
||||
redis_key: str = RedisValidationKeysAction.dynamic_header_request_key
|
||||
|
||||
@property
|
||||
def header(self):
|
||||
"""
|
||||
Headers: Headers which is merged with response model && language models of event
|
||||
"""
|
||||
redis_header= RedisActions.get_json(list_keys=[f"{self.redis_key}:{self.intersection}"])
|
||||
if redis_header.status:
|
||||
return redis_header.first
|
||||
raise ValueError("Header not found")
|
||||
|
||||
|
||||
class RedisValidationRetrieve(ValidateBase):
|
||||
|
||||
redis_key: str = RedisValidationKeysAction.dynamic_validation_key
|
||||
|
||||
@property
|
||||
def validation(self):
|
||||
"""
|
||||
Validation: Validation of event which is merged with response model && language models of event
|
||||
"""
|
||||
redis_validation = RedisActions.get_json(list_keys=[f"{self.redis_key}:{self.intersection}"])
|
||||
if redis_validation.status:
|
||||
return redis_validation.first
|
||||
raise ValueError("Header not found")
|
||||
|
||||
|
||||
class ValidationsBoth(RedisHeaderRetrieve, RedisValidationRetrieve):
|
||||
|
||||
@property
|
||||
def both(self) -> Dict[str, Any]:
|
||||
"""
|
||||
Headers: Headers which is merged with response model && language models of event
|
||||
Validation: Validation of event which is merged with response model && language models of event
|
||||
"""
|
||||
return {"headers": self.header, "validation": self.validation}
|
||||
|
||||
|
||||
class RetrieveValidation(BaseRouteModel):
|
||||
|
||||
@classmethod
|
||||
def template_example_function(cls):
|
||||
return
|
||||
def retrieve_validation(cls, data: Any):
|
||||
"""
|
||||
Retrieve validation by event function code
|
||||
"""
|
||||
if getattr(data, 'asked_field', "") not in ValidationsConfig.SUPPORTED_VALIDATIONS:
|
||||
raise ValueError(f"Invalid asked field please retry with valid fields {ValidationsConfig.SUPPORTED_VALIDATIONS}")
|
||||
|
||||
reachable_codes = []
|
||||
if cls.context_retriever.token.is_employee:
|
||||
reachable_codes = cls.context_retriever.token.selected_company.reachable_event_codes
|
||||
elif cls.context_retriever.token.is_occupant:
|
||||
reachable_codes = cls.context_retriever.token.selected_occupant.reachable_event_codes
|
||||
|
||||
validate_dict = dict(url=data.url, reachable_code=reachable_codes)
|
||||
if data.asked_field == "all":
|
||||
return ValidationsBoth(**validate_dict).both
|
||||
elif data.asked_field == "headers":
|
||||
return RedisHeaderRetrieve(**validate_dict).header
|
||||
elif data.asked_field == "validation":
|
||||
return RedisValidationRetrieve(**validate_dict).validation
|
||||
|
||||
Reference in New Issue
Block a user