diff --git a/backend/nango/cogs/serializer_cog.py b/backend/nango/cogs/serializer_cog.py index 6747ec4..65dd3df 100644 --- a/backend/nango/cogs/serializer_cog.py +++ b/backend/nango/cogs/serializer_cog.py @@ -2,14 +2,69 @@ class SerializerCog(AbstractCog): - """Generate serializer from each django's models. + """Generates a serializer from each django's models. Settings: -------- + - speedy: A faster serializer than rest_framework.serializers.Serializer, but with less features. """ id = "serializer" + def _get_drf_imports(self) -> list[str]: + """Return list of imports from rest_framework module.""" + if self.settings.get("speedy", False): + error_msg: str = "The SerializerCog speedy feature is not implemented yet." + raise NotImplementedError(error_msg) + + return [ + "from rest_framework import serializers" if not self.settings.get("speedy", False) else "...", + ] + + def _get_base_imports(self) -> list[str]: + """Return basics imports.""" + return [ + "from __future__ import annotations", + "from typing import ClassVar", + ] + + def get_serializer_name(self) -> dict[str]: + """Return name of generated serializers. + + Returns: + ------- + ``` + { + "cached_serializer_name": str, + "customizable_serializer_name": str, + } + ``` + """ + return { + "cached_serializer_name": f"{self.model.__name__}NangoSerializer", + "customizable_serializer_name": f"{self.model.__name__}Serializer", + } + + def generate_cached_serializer(self) -> None: + """Generate the serializer file (for the given model) that will be cached in the _nango_cache.""" + content: str = f""" + {'\n'.join(self._get_base_imports())} + {'\n'.join(self._get_drf_imports())} + {self._get_model_import()} + + class {self.get_serializer_name().get('cached_serializer_name')}(serializer.Serializer): + \"\"\"...generated...\"\"\" + + class Meta: + model = {self.model.__name__} + """ + print(content) # noqa: T201 + + def generated_customizable_serializer(self) -> None: + """Generate the serializer file (for the given model) that use the cached serializer and that can be custom by the dev.""" + def run(self) -> None: """Generate a serializer for each django's model.""" super().run() + self.generate_cached_serializer() + self.generated_customizable_serializer() diff --git a/backend/nango/utils/abstract_cog.py b/backend/nango/utils/abstract_cog.py index b2ad228..869c749 100644 --- a/backend/nango/utils/abstract_cog.py +++ b/backend/nango/utils/abstract_cog.py @@ -38,7 +38,7 @@ def is_executable(self) -> bool: """Indicate if the cog can run or not.""" return self.settings is not None - def _generate_model_import(self) -> str: + def _get_model_import(self) -> str: """Return the model import line for file generation.""" regex = r"(?<=')(.*)(?=\.)" match = re.search(regex, str(self.model))