You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.2 KiB
35 lines
1.2 KiB
import re
|
|
import importlib
|
|
from django.apps import apps
|
|
|
|
def is_valid_email(email):
|
|
email_regex = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
|
|
return re.match(email_regex, email) is not None
|
|
|
|
def build_serializer_class(source):
|
|
|
|
# Remove the 's' character at the end if present
|
|
if source.endswith('s') and not source.endswith('ss'):
|
|
source = source[:-1]
|
|
|
|
# Capitalize words separated by a dash
|
|
words = source.split('-')
|
|
capitalized_words = [word[0].upper() + word[1:] for word in words]
|
|
transformed_string = ''.join(capitalized_words)
|
|
|
|
# Add 'Serializer' at the end
|
|
transformed_string += 'Serializer'
|
|
|
|
module = importlib.import_module('api.serializers')
|
|
return getattr(module, transformed_string)
|
|
|
|
def get_data(app_label, model_name, model_id):
|
|
model = apps.get_model(app_label=app_label, model_name=model_name)
|
|
return model.objects.get(id=model_id)
|
|
|
|
def get_serialized_data(app_label, model_name, model_id):
|
|
model = apps.get_model(app_label=app_label, model_name=model_name)
|
|
instance = model.objects.get(id=model_id)
|
|
serializer_class = build_serializer_class(model_name)
|
|
serializer = serializer_class(instance)
|
|
return serializer.data
|
|
|