diff --git a/consumer/mq_consumer.py b/consumer/mq_consumer.py index ddad2015..ea37d576 100644 --- a/consumer/mq_consumer.py +++ b/consumer/mq_consumer.py @@ -1,11 +1,15 @@ import json import logging +from pprint import pprint import pika from django.conf import settings -from consumer.serializers.importer import ArticleImportSerializer, BlogImportSerializer -from consumer.serializers.importer.report import ReportImportSerializer +from consumer.serializers import ( + ArticleImportSerializer, + BlogImportSerializer, + ReportImportSerializer, +) class MqConsumer: @@ -23,13 +27,15 @@ def _message_callback( # Decode the message message = json.loads(body) + pprint(message) + match message["type"]: case "article": - self._save_article(message["data"]) + self._save_data(ArticleImportSerializer, message["data"]) case "blog": - self._save_blog(message["data"]) + self._save_data(BlogImportSerializer, message["data"]) case "report": - self._save_report(message["data"]) + self._save_data(ReportImportSerializer, message["data"]) case _: channel.basic_nack( delivery_tag=method_frame.delivery_tag, requeue=False @@ -62,15 +68,6 @@ def _save_data( f"Saved {serializer_class.__name__} with title: {serializer.validated_data['title']}" ) - def _save_article(self, data: dict[str, str | dict[str, str | int]]) -> None: - self._save_data(ArticleImportSerializer, data) - - def _save_blog(self, data: dict[str, str | dict[str, str | int]]) -> None: - self._save_data(BlogImportSerializer, data) - - def _save_report(self, data: dict[str, str | dict[str, str | int]]) -> None: - self._save_data(ReportImportSerializer, data) - def consume(self) -> None: self.connection = pika.BlockingConnection( parameters=pika.ConnectionParameters( diff --git a/consumer/serializers/__init__.py b/consumer/serializers/__init__.py new file mode 100644 index 00000000..87de2e24 --- /dev/null +++ b/consumer/serializers/__init__.py @@ -0,0 +1,5 @@ +from consumer.serializers.article import ArticleImportSerializer +from consumer.serializers.blog import BlogImportSerializer +from consumer.serializers.report import ReportImportSerializer + +__all__ = ["ArticleImportSerializer", "BlogImportSerializer", "ReportImportSerializer"] diff --git a/consumer/serializers/importer/article.py b/consumer/serializers/article.py similarity index 100% rename from consumer/serializers/importer/article.py rename to consumer/serializers/article.py diff --git a/consumer/serializers/importer/blog.py b/consumer/serializers/blog.py similarity index 100% rename from consumer/serializers/importer/blog.py rename to consumer/serializers/blog.py diff --git a/consumer/serializers/importer/__init__.py b/consumer/serializers/importer/__init__.py deleted file mode 100644 index 69b85e98..00000000 --- a/consumer/serializers/importer/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -from consumer.serializers.importer.article import ArticleImportSerializer -from consumer.serializers.importer.blog import BlogImportSerializer - -__all__ = ["ArticleImportSerializer", "BlogImportSerializer"] diff --git a/consumer/serializers/importer/report.py b/consumer/serializers/report.py similarity index 100% rename from consumer/serializers/importer/report.py rename to consumer/serializers/report.py