27 lines
694 B
Python
27 lines
694 B
Python
from abc import ABC, abstractmethod
|
|
from django.db.models import Model
|
|
from rest_framework import exceptions
|
|
|
|
class ObjectValidationABC(ABC):
|
|
@abstractmethod
|
|
def is_valid(self, object_id) -> bool:
|
|
pass
|
|
|
|
|
|
class ObjectValidationById(ObjectValidationABC):
|
|
|
|
def __init__(self, object_model, request):
|
|
self.object_model = object_model
|
|
self.request = request
|
|
|
|
def is_valid(self, object_id) -> bool:
|
|
try:
|
|
result = self.object_model.objects.get(id=object_id)
|
|
return result
|
|
except Exception as exp:
|
|
message = f'Error Validation object: {exp}'
|
|
raise exceptions.ValidationError({'error': message})
|
|
|
|
|
|
|