├── .vscode └── settings.json ├── EmailService ├── EmailService │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-311.pyc │ │ ├── settings.cpython-311.pyc │ │ └── urls.cpython-311.pyc │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── mailer │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-311.pyc │ │ ├── admin.cpython-311.pyc │ │ ├── apps.cpython-311.pyc │ │ ├── models.cpython-311.pyc │ │ └── queue_listener.cpython-311.pyc │ ├── admin.py │ ├── apps.py │ ├── management │ │ └── commands │ │ │ ├── __pycache__ │ │ │ └── launch_queue_listener.cpython-311.pyc │ │ │ └── launch_queue_listener.py │ ├── migrations │ │ └── __init__.py │ ├── models.py │ ├── queue_listener.py │ ├── tests.py │ └── views.py └── manage.py ├── LogginService ├── LogginService │ ├── __init__.py │ ├── __init__.pyc │ ├── __pycache__ │ │ ├── __init__.cpython-311.pyc │ │ ├── settings.cpython-311.pyc │ │ └── urls.cpython-311.pyc │ ├── settings.py │ ├── settings.pyc │ ├── urls.py │ └── wsgi.py ├── logger │ ├── __init__.py │ ├── __init__.pyc │ ├── __pycache__ │ │ ├── __init__.cpython-311.pyc │ │ ├── admin.cpython-311.pyc │ │ ├── apps.cpython-311.pyc │ │ ├── listener_user_created.cpython-311.pyc │ │ ├── models.cpython-311.pyc │ │ └── queue_listener.cpython-311.pyc │ ├── admin.py │ ├── admin.pyc │ ├── apps.py │ ├── management │ │ └── commands │ │ │ ├── __pycache__ │ │ │ ├── launch_queue_listener.cpython-311.pyc │ │ │ └── launch_user_created_listener.cpython-311.pyc │ │ │ └── launch_queue_listener.py │ ├── migrations │ │ └── __init__.py │ ├── models.py │ ├── models.pyc │ ├── queue_listener.py │ ├── tests.py │ └── views.py └── manage.py ├── README.md └── UserService ├── UserService ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-311.pyc │ ├── settings.cpython-311.pyc │ ├── urls.cpython-311.pyc │ └── wsgi.cpython-311.pyc ├── settings.py ├── urls.py └── wsgi.py ├── manage.py ├── requirements.txt └── userauth ├── __init__.py ├── __pycache__ ├── __init__.cpython-311.pyc ├── admin.cpython-311.pyc ├── apps.cpython-311.pyc ├── models.cpython-311.pyc ├── producer_user_created.cpython-311.pyc ├── serializers.cpython-311.pyc ├── urls.cpython-311.pyc └── views.cpython-311.pyc ├── admin.py ├── apps.py ├── migrations ├── __init__.py └── __pycache__ │ └── __init__.cpython-311.pyc ├── models.py ├── producer_user_created.py ├── serializers.py ├── tests.py ├── urls.py └── views.py /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "[python]": { 3 | "editor.defaultFormatter": "ms-python.autopep8" 4 | }, 5 | "python.formatting.provider": "none" 6 | } -------------------------------------------------------------------------------- /EmailService/EmailService/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/EmailService/EmailService/__init__.py -------------------------------------------------------------------------------- /EmailService/EmailService/__pycache__/__init__.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/EmailService/EmailService/__pycache__/__init__.cpython-311.pyc -------------------------------------------------------------------------------- /EmailService/EmailService/__pycache__/settings.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/EmailService/EmailService/__pycache__/settings.cpython-311.pyc -------------------------------------------------------------------------------- /EmailService/EmailService/__pycache__/urls.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/EmailService/EmailService/__pycache__/urls.cpython-311.pyc -------------------------------------------------------------------------------- /EmailService/EmailService/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for EmailService project. 3 | 4 | Generated by 'django-admin startproject' using Django 1.11.29. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.11/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/1.11/ref/settings/ 11 | """ 12 | 13 | import os 14 | 15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'd)x*in7@5jzonjuy=1-n#aa$mhg@bi^pv0v2y%=@w2bv(xpzgd' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | "mailer" 41 | ] 42 | 43 | MIDDLEWARE = [ 44 | 'django.middleware.security.SecurityMiddleware', 45 | 'django.contrib.sessions.middleware.SessionMiddleware', 46 | 'django.middleware.common.CommonMiddleware', 47 | 'django.middleware.csrf.CsrfViewMiddleware', 48 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 49 | 'django.contrib.messages.middleware.MessageMiddleware', 50 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 51 | ] 52 | 53 | ROOT_URLCONF = 'EmailService.urls' 54 | 55 | TEMPLATES = [ 56 | { 57 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 58 | 'DIRS': [], 59 | 'APP_DIRS': True, 60 | 'OPTIONS': { 61 | 'context_processors': [ 62 | 'django.template.context_processors.debug', 63 | 'django.template.context_processors.request', 64 | 'django.contrib.auth.context_processors.auth', 65 | 'django.contrib.messages.context_processors.messages', 66 | ], 67 | }, 68 | }, 69 | ] 70 | 71 | WSGI_APPLICATION = 'EmailService.wsgi.application' 72 | 73 | 74 | # Database 75 | # https://docs.djangoproject.com/en/1.11/ref/settings/#databases 76 | 77 | DATABASES = { 78 | 'default': { 79 | 'ENGINE': 'django.db.backends.sqlite3', 80 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 81 | } 82 | } 83 | 84 | 85 | # Password validation 86 | # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators 87 | 88 | AUTH_PASSWORD_VALIDATORS = [ 89 | { 90 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 91 | }, 92 | { 93 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 94 | }, 95 | { 96 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 97 | }, 98 | { 99 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 100 | }, 101 | ] 102 | 103 | 104 | # Internationalization 105 | # https://docs.djangoproject.com/en/1.11/topics/i18n/ 106 | 107 | LANGUAGE_CODE = 'en-us' 108 | 109 | TIME_ZONE = 'UTC' 110 | 111 | USE_I18N = True 112 | 113 | USE_L10N = True 114 | 115 | USE_TZ = True 116 | 117 | 118 | # Static files (CSS, JavaScript, Images) 119 | # https://docs.djangoproject.com/en/1.11/howto/static-files/ 120 | 121 | STATIC_URL = '/static/' 122 | -------------------------------------------------------------------------------- /EmailService/EmailService/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import include, re_path 2 | 3 | 4 | urlpatterns = [ 5 | ] -------------------------------------------------------------------------------- /EmailService/EmailService/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for EmailService project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "EmailService.settings") 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /EmailService/mailer/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/EmailService/mailer/__init__.py -------------------------------------------------------------------------------- /EmailService/mailer/__pycache__/__init__.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/EmailService/mailer/__pycache__/__init__.cpython-311.pyc -------------------------------------------------------------------------------- /EmailService/mailer/__pycache__/admin.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/EmailService/mailer/__pycache__/admin.cpython-311.pyc -------------------------------------------------------------------------------- /EmailService/mailer/__pycache__/apps.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/EmailService/mailer/__pycache__/apps.cpython-311.pyc -------------------------------------------------------------------------------- /EmailService/mailer/__pycache__/models.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/EmailService/mailer/__pycache__/models.cpython-311.pyc -------------------------------------------------------------------------------- /EmailService/mailer/__pycache__/queue_listener.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/EmailService/mailer/__pycache__/queue_listener.cpython-311.pyc -------------------------------------------------------------------------------- /EmailService/mailer/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /EmailService/mailer/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class MailerConfig(AppConfig): 5 | default_auto_field = "django.db.models.BigAutoField" 6 | name = "mailer" 7 | -------------------------------------------------------------------------------- /EmailService/mailer/management/commands/__pycache__/launch_queue_listener.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/EmailService/mailer/management/commands/__pycache__/launch_queue_listener.cpython-311.pyc -------------------------------------------------------------------------------- /EmailService/mailer/management/commands/launch_queue_listener.py: -------------------------------------------------------------------------------- 1 | from django.core.management.base import BaseCommand 2 | from mailer.queue_listener import UserCreatedListener 3 | class Command(BaseCommand): 4 | help = 'Launches Listener for user_created message : RaabitMQ' 5 | def handle(self, *args, **options): 6 | td = UserCreatedListener() 7 | td.start() 8 | self.stdout.write("Started Consumer Thread") -------------------------------------------------------------------------------- /EmailService/mailer/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/EmailService/mailer/migrations/__init__.py -------------------------------------------------------------------------------- /EmailService/mailer/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | -------------------------------------------------------------------------------- /EmailService/mailer/queue_listener.py: -------------------------------------------------------------------------------- 1 | import json 2 | import pika 3 | import threading 4 | ROUTING_KEY = 'user.created.key' 5 | EXCHANGE = 'user_exchange' 6 | THREADS = 5 7 | 8 | class UserCreatedListener(threading.Thread): 9 | def __init__(self): 10 | threading.Thread.__init__(self) 11 | #BlockingConnection abstracts its I/O loop from the application while 12 | # adhering to asynchronous RPC nature of the AMQP protocol, 13 | connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) 14 | #Channel multiplexes a TCP connection 15 | self.channel = connection.channel() 16 | #Creates an exchange if it does not already exist (fanout or direct or topic) 17 | self.channel.exchange_declare(exchange=EXCHANGE, exchange_type='direct') 18 | #queue_declare() Creates or checks a queue 19 | #queue='' will cause RabbitMQ to create a unique queue 20 | #exclusive=True : Only allow access by the current connection 21 | result = self.channel.queue_declare(queue='', exclusive=True) 22 | queue_name = result.method.queue 23 | #Bind the queue to the specified exchange 24 | self.channel.queue_bind(queue=queue_name, exchange=EXCHANGE, routing_key=ROUTING_KEY) 25 | #Specify quality of service. 26 | #prefetch_count specifies a prefetch window size 27 | self.channel.basic_qos(prefetch_count=THREADS*10) 28 | # When ever a new message arrives on queue 29 | # callback method will be self.callback 30 | self.channel.basic_consume(queue=queue_name, on_message_callback=self.callback) 31 | 32 | # channel: Channel used for communication 33 | # method : Message delivery details (if curious, print it to see the result) 34 | # property: user-defined properties on the message 35 | # body : contains message 36 | def callback(self, channel, method, properties, body): 37 | if(properties.content_type=="user_created_method"): 38 | message = json.loads(body) 39 | # Do What ever with message, In real world we should be 40 | # sending an Email asynchronously to user 41 | print(message) 42 | #send acknowledgement back (Good practice) 43 | channel.basic_ack(delivery_tag=method.delivery_tag) 44 | 45 | def run(self): 46 | print ('Inside EmailService : Created Listener ') 47 | self.channel.start_consuming() 48 | 49 | -------------------------------------------------------------------------------- /EmailService/mailer/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /EmailService/mailer/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | 3 | # Create your views here. 4 | -------------------------------------------------------------------------------- /EmailService/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import sys 4 | 5 | if __name__ == "__main__": 6 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "EmailService.settings") 7 | try: 8 | from django.core.management import execute_from_command_line 9 | except ImportError: 10 | # The above import may fail for some other reason. Ensure that the 11 | # issue is really that Django is missing to avoid masking other 12 | # exceptions on Python 2. 13 | try: 14 | import django 15 | except ImportError: 16 | raise ImportError( 17 | "Couldn't import Django. Are you sure it's installed and " 18 | "available on your PYTHONPATH environment variable? Did you " 19 | "forget to activate a virtual environment?" 20 | ) 21 | raise 22 | execute_from_command_line(sys.argv) 23 | -------------------------------------------------------------------------------- /LogginService/LogginService/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/LogginService/LogginService/__init__.py -------------------------------------------------------------------------------- /LogginService/LogginService/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/LogginService/LogginService/__init__.pyc -------------------------------------------------------------------------------- /LogginService/LogginService/__pycache__/__init__.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/LogginService/LogginService/__pycache__/__init__.cpython-311.pyc -------------------------------------------------------------------------------- /LogginService/LogginService/__pycache__/settings.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/LogginService/LogginService/__pycache__/settings.cpython-311.pyc -------------------------------------------------------------------------------- /LogginService/LogginService/__pycache__/urls.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/LogginService/LogginService/__pycache__/urls.cpython-311.pyc -------------------------------------------------------------------------------- /LogginService/LogginService/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for LogginService project. 3 | 4 | Generated by 'django-admin startproject' using Django 1.11.29. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.11/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/1.11/ref/settings/ 11 | """ 12 | 13 | import os 14 | 15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = '-=9plp5y((b*#g@mjldv)bvmteqx+=p)#x$1adstt%ieeb*+t1' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'logger' 41 | ] 42 | 43 | MIDDLEWARE = [ 44 | 'django.middleware.security.SecurityMiddleware', 45 | 'django.contrib.sessions.middleware.SessionMiddleware', 46 | 'django.middleware.common.CommonMiddleware', 47 | 'django.middleware.csrf.CsrfViewMiddleware', 48 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 49 | 'django.contrib.messages.middleware.MessageMiddleware', 50 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 51 | ] 52 | 53 | ROOT_URLCONF = 'LogginService.urls' 54 | 55 | TEMPLATES = [ 56 | { 57 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 58 | 'DIRS': [], 59 | 'APP_DIRS': True, 60 | 'OPTIONS': { 61 | 'context_processors': [ 62 | 'django.template.context_processors.debug', 63 | 'django.template.context_processors.request', 64 | 'django.contrib.auth.context_processors.auth', 65 | 'django.contrib.messages.context_processors.messages', 66 | ], 67 | }, 68 | }, 69 | ] 70 | 71 | WSGI_APPLICATION = 'LogginService.wsgi.application' 72 | 73 | 74 | # Database 75 | # https://docs.djangoproject.com/en/1.11/ref/settings/#databases 76 | 77 | DATABASES = { 78 | 'default': { 79 | 'ENGINE': 'django.db.backends.sqlite3', 80 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 81 | } 82 | } 83 | 84 | 85 | # Password validation 86 | # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators 87 | 88 | AUTH_PASSWORD_VALIDATORS = [ 89 | { 90 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 91 | }, 92 | { 93 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 94 | }, 95 | { 96 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 97 | }, 98 | { 99 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 100 | }, 101 | ] 102 | 103 | 104 | # Internationalization 105 | # https://docs.djangoproject.com/en/1.11/topics/i18n/ 106 | 107 | LANGUAGE_CODE = 'en-us' 108 | 109 | TIME_ZONE = 'UTC' 110 | 111 | USE_I18N = True 112 | 113 | USE_L10N = True 114 | 115 | USE_TZ = True 116 | 117 | 118 | # Static files (CSS, JavaScript, Images) 119 | # https://docs.djangoproject.com/en/1.11/howto/static-files/ 120 | 121 | STATIC_URL = '/static/' 122 | -------------------------------------------------------------------------------- /LogginService/LogginService/settings.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/LogginService/LogginService/settings.pyc -------------------------------------------------------------------------------- /LogginService/LogginService/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import include, re_path 2 | 3 | 4 | urlpatterns = [ 5 | ] -------------------------------------------------------------------------------- /LogginService/LogginService/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for LogginService project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "LogginService.settings") 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /LogginService/logger/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/LogginService/logger/__init__.py -------------------------------------------------------------------------------- /LogginService/logger/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/LogginService/logger/__init__.pyc -------------------------------------------------------------------------------- /LogginService/logger/__pycache__/__init__.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/LogginService/logger/__pycache__/__init__.cpython-311.pyc -------------------------------------------------------------------------------- /LogginService/logger/__pycache__/admin.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/LogginService/logger/__pycache__/admin.cpython-311.pyc -------------------------------------------------------------------------------- /LogginService/logger/__pycache__/apps.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/LogginService/logger/__pycache__/apps.cpython-311.pyc -------------------------------------------------------------------------------- /LogginService/logger/__pycache__/listener_user_created.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/LogginService/logger/__pycache__/listener_user_created.cpython-311.pyc -------------------------------------------------------------------------------- /LogginService/logger/__pycache__/models.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/LogginService/logger/__pycache__/models.cpython-311.pyc -------------------------------------------------------------------------------- /LogginService/logger/__pycache__/queue_listener.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/LogginService/logger/__pycache__/queue_listener.cpython-311.pyc -------------------------------------------------------------------------------- /LogginService/logger/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /LogginService/logger/admin.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/LogginService/logger/admin.pyc -------------------------------------------------------------------------------- /LogginService/logger/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class LoggerConfig(AppConfig): 5 | default_auto_field = "django.db.models.BigAutoField" 6 | name = "logger" 7 | -------------------------------------------------------------------------------- /LogginService/logger/management/commands/__pycache__/launch_queue_listener.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/LogginService/logger/management/commands/__pycache__/launch_queue_listener.cpython-311.pyc -------------------------------------------------------------------------------- /LogginService/logger/management/commands/__pycache__/launch_user_created_listener.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/LogginService/logger/management/commands/__pycache__/launch_user_created_listener.cpython-311.pyc -------------------------------------------------------------------------------- /LogginService/logger/management/commands/launch_queue_listener.py: -------------------------------------------------------------------------------- 1 | from django.core.management.base import BaseCommand 2 | from logger.queue_listener import UserCreatedListener 3 | class Command(BaseCommand): 4 | help = 'Launches Listener for user_created message : RaabitMQ' 5 | def handle(self, *args, **options): 6 | td = UserCreatedListener() 7 | td.start() 8 | self.stdout.write("Started Consumer Thread") -------------------------------------------------------------------------------- /LogginService/logger/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/LogginService/logger/migrations/__init__.py -------------------------------------------------------------------------------- /LogginService/logger/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | -------------------------------------------------------------------------------- /LogginService/logger/models.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/LogginService/logger/models.pyc -------------------------------------------------------------------------------- /LogginService/logger/queue_listener.py: -------------------------------------------------------------------------------- 1 | import json 2 | import pika 3 | import threading 4 | ROUTING_KEY = 'user.created.key' 5 | EXCHANGE = 'user_exchange' 6 | THREADS = 5 7 | 8 | class UserCreatedListener(threading.Thread): 9 | def __init__(self): 10 | threading.Thread.__init__(self) 11 | connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) 12 | self.channel = connection.channel() 13 | self.channel.exchange_declare(exchange=EXCHANGE, exchange_type='direct') 14 | # self.channel.queue_declare(queue=QUEUE_NAME, auto_delete=False) 15 | result = self.channel.queue_declare(queue='', exclusive=True) 16 | queue_name = result.method.queue 17 | self.channel.queue_bind(queue=queue_name, exchange=EXCHANGE, routing_key=ROUTING_KEY) 18 | self.channel.basic_qos(prefetch_count=THREADS*10) 19 | self.channel.basic_consume(queue=queue_name, on_message_callback=self.callback) 20 | 21 | def callback(self, channel, method, properties, body): 22 | #print(properties.content_type) 23 | #print(method) 24 | if(properties.content_type=="user_created_method"): 25 | message = json.loads(body) 26 | print(message) 27 | channel.basic_ack(delivery_tag=method.delivery_tag) 28 | 29 | def run(self): 30 | print ('Inside LogginService: Created Listener ') 31 | self.channel.start_consuming() 32 | 33 | -------------------------------------------------------------------------------- /LogginService/logger/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /LogginService/logger/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | 3 | # Create your views here. 4 | -------------------------------------------------------------------------------- /LogginService/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import sys 4 | 5 | if __name__ == "__main__": 6 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "LogginService.settings") 7 | try: 8 | from django.core.management import execute_from_command_line 9 | except ImportError: 10 | # The above import may fail for some other reason. Ensure that the 11 | # issue is really that Django is missing to avoid masking other 12 | # exceptions on Python 2. 13 | try: 14 | import django 15 | except ImportError: 16 | raise ImportError( 17 | "Couldn't import Django. Are you sure it's installed and " 18 | "available on your PYTHONPATH environment variable? Did you " 19 | "forget to activate a virtual environment?" 20 | ) 21 | raise 22 | execute_from_command_line(sys.argv) 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## What this Repo is for ? 2 | It contains source code for tutorial published at 3 | https://medium.com/@mansha99/microservices-in-python-django-rabbitmq-and-pika-fe1adb0c6a1a 4 | 5 | ### What it demonstrates? 6 | How to create Application following microservice architecture pattern using Django, RabbitMQ and Pika -------------------------------------------------------------------------------- /UserService/UserService/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/UserService/UserService/__init__.py -------------------------------------------------------------------------------- /UserService/UserService/__pycache__/__init__.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/UserService/UserService/__pycache__/__init__.cpython-311.pyc -------------------------------------------------------------------------------- /UserService/UserService/__pycache__/settings.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/UserService/UserService/__pycache__/settings.cpython-311.pyc -------------------------------------------------------------------------------- /UserService/UserService/__pycache__/urls.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/UserService/UserService/__pycache__/urls.cpython-311.pyc -------------------------------------------------------------------------------- /UserService/UserService/__pycache__/wsgi.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/UserService/UserService/__pycache__/wsgi.cpython-311.pyc -------------------------------------------------------------------------------- /UserService/UserService/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for UserService project. 3 | 4 | Generated by 'django-admin startproject' using Django 1.11.29. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.11/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/1.11/ref/settings/ 11 | """ 12 | 13 | import os 14 | 15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'z+0bwv-(94!)@_*mhu7*c=6fs=phc_etl^dy(_7&w9%mm!_g)e' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'userauth', 41 | 'rest_framework' 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'UserService.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'UserService.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/1.11/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.mysql', 81 | 'NAME': 'userservice_db', 82 | 'USER': 'root', 83 | 'PASSWORD': 'Pa55Word#$', 84 | 'HOST': 'localhost', 85 | 'PORT': '3306', 86 | } 87 | } 88 | 89 | 90 | # Password validation 91 | # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators 92 | 93 | AUTH_PASSWORD_VALIDATORS = [ 94 | { 95 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 96 | }, 97 | { 98 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 99 | }, 100 | { 101 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 102 | }, 103 | { 104 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 105 | }, 106 | ] 107 | 108 | 109 | # Internationalization 110 | # https://docs.djangoproject.com/en/1.11/topics/i18n/ 111 | 112 | LANGUAGE_CODE = 'en-us' 113 | 114 | TIME_ZONE = 'UTC' 115 | 116 | USE_I18N = True 117 | 118 | USE_L10N = True 119 | 120 | USE_TZ = True 121 | 122 | 123 | # Static files (CSS, JavaScript, Images) 124 | # https://docs.djangoproject.com/en/1.11/howto/static-files/ 125 | 126 | STATIC_URL = '/static/' 127 | -------------------------------------------------------------------------------- /UserService/UserService/urls.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from django.urls import path, include 3 | from rest_framework.routers import DefaultRouter 4 | from django.conf import settings 5 | from django.conf.urls.static import static 6 | 7 | 8 | 9 | urlpatterns = [ 10 | path('auth/', include('userauth.urls')), 11 | ] -------------------------------------------------------------------------------- /UserService/UserService/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for UserService project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "UserService.settings") 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /UserService/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import sys 4 | 5 | if __name__ == "__main__": 6 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "UserService.settings") 7 | try: 8 | from django.core.management import execute_from_command_line 9 | except ImportError: 10 | # The above import may fail for some other reason. Ensure that the 11 | # issue is really that Django is missing to avoid masking other 12 | # exceptions on Python 2. 13 | try: 14 | import django 15 | except ImportError: 16 | raise ImportError( 17 | "Couldn't import Django. Are you sure it's installed and " 18 | "available on your PYTHONPATH environment variable? Did you " 19 | "forget to activate a virtual environment?" 20 | ) 21 | raise 22 | execute_from_command_line(sys.argv) 23 | -------------------------------------------------------------------------------- /UserService/requirements.txt: -------------------------------------------------------------------------------- 1 | absl-py==1.4.0 2 | aniso8601==9.0.1 3 | asgiref==3.7.2 4 | astunparse==1.6.3 5 | cachetools==5.3.1 6 | certifi==2023.5.7 7 | charset-normalizer==3.1.0 8 | distlib==0.3.6 9 | Django==4.2.3 10 | django-environ==0.10.0 11 | django-extensions==3.2.3 12 | django-rest==0.8.7 13 | django-seed==0.3.1 14 | djangorestframework==3.14.0 15 | djangorestframework-simplejwt==5.2.2 16 | Faker==18.11.2 17 | filelock==3.12.2 18 | flatbuffers==23.5.26 19 | gast==0.4.0 20 | google-auth==2.21.0 21 | google-auth-oauthlib==1.0.0 22 | google-pasta==0.2.0 23 | graphene==3.2.2 24 | graphene-django==3.1.3 25 | graphql-core==3.2.3 26 | graphql-relay==3.2.0 27 | grpcio==1.56.0 28 | h5py==3.9.0 29 | idna==3.4 30 | keras==2.13.1rc0 31 | libclang==16.0.0 32 | Markdown==3.4.3 33 | MarkupSafe==2.1.3 34 | mysqlclient==2.2.0 35 | numpy==1.25.0 36 | oauthlib==3.2.2 37 | opt-einsum==3.3.0 38 | packaging==23.1 39 | pika==1.3.2 40 | platformdirs==3.8.1 41 | promise==2.3 42 | protobuf==4.23.3 43 | pyasn1==0.5.0 44 | pyasn1-modules==0.3.0 45 | PyJWT==2.7.0 46 | python-dateutil==2.8.2 47 | pytz==2023.3 48 | requests==2.31.0 49 | requests-oauthlib==1.3.1 50 | rsa==4.9 51 | six==1.16.0 52 | sqlparse==0.4.4 53 | tensorboard==2.13.0 54 | tensorboard-data-server==0.7.1 55 | tensorflow==2.13.0rc1 56 | tensorflow-estimator==2.13.0rc0 57 | tensorflow-macos==2.13.0rc1 58 | termcolor==2.3.0 59 | text-unidecode==1.3 60 | toposort==1.10 61 | typing_extensions==4.6.3 62 | urllib3==1.26.16 63 | virtualenv==20.23.1 64 | Werkzeug==2.3.6 65 | wrapt==1.15.0 66 | -------------------------------------------------------------------------------- /UserService/userauth/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/UserService/userauth/__init__.py -------------------------------------------------------------------------------- /UserService/userauth/__pycache__/__init__.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/UserService/userauth/__pycache__/__init__.cpython-311.pyc -------------------------------------------------------------------------------- /UserService/userauth/__pycache__/admin.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/UserService/userauth/__pycache__/admin.cpython-311.pyc -------------------------------------------------------------------------------- /UserService/userauth/__pycache__/apps.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/UserService/userauth/__pycache__/apps.cpython-311.pyc -------------------------------------------------------------------------------- /UserService/userauth/__pycache__/models.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/UserService/userauth/__pycache__/models.cpython-311.pyc -------------------------------------------------------------------------------- /UserService/userauth/__pycache__/producer_user_created.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/UserService/userauth/__pycache__/producer_user_created.cpython-311.pyc -------------------------------------------------------------------------------- /UserService/userauth/__pycache__/serializers.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/UserService/userauth/__pycache__/serializers.cpython-311.pyc -------------------------------------------------------------------------------- /UserService/userauth/__pycache__/urls.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/UserService/userauth/__pycache__/urls.cpython-311.pyc -------------------------------------------------------------------------------- /UserService/userauth/__pycache__/views.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/UserService/userauth/__pycache__/views.cpython-311.pyc -------------------------------------------------------------------------------- /UserService/userauth/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /UserService/userauth/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class UserauthConfig(AppConfig): 5 | default_auto_field = "django.db.models.BigAutoField" 6 | name = "userauth" 7 | -------------------------------------------------------------------------------- /UserService/userauth/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/UserService/userauth/migrations/__init__.py -------------------------------------------------------------------------------- /UserService/userauth/migrations/__pycache__/__init__.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mansha99/django-rabbitmq-microservice/6c654321d6f45fde9bd37baa8ccc379c3f754860/UserService/userauth/migrations/__pycache__/__init__.cpython-311.pyc -------------------------------------------------------------------------------- /UserService/userauth/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | -------------------------------------------------------------------------------- /UserService/userauth/producer_user_created.py: -------------------------------------------------------------------------------- 1 | import json 2 | import pika 3 | 4 | ROUTING_KEY = 'user.created.key' 5 | EXCHANGE = 'user_exchange' 6 | THREADS = 5 7 | class ProducerUserCreated: 8 | def __init__(self) -> None: 9 | # hearbeat = 600 indicates that after 600 seconds 10 | # the peer TCP connection should be considered unreachable 11 | # by RabbitMQ and client libraries 12 | # 13 | #blocked_connection_timeout=300 means after 300 seconds 14 | # the peer TCP connection is interrupted and dropped. 15 | self.connection = pika.BlockingConnection( 16 | pika.ConnectionParameters('localhost', heartbeat=600, blocked_connection_timeout=300) 17 | ) 18 | self.channel = self.connection.channel() 19 | 20 | # This method will be called inside view for sending RabbitMQ message 21 | # method here is same as properties.content_type in listener callback 22 | def publish(self,method, body): 23 | print('Inside UserService: Sending to RabbitMQ: ') 24 | print(body) 25 | properties = pika.BasicProperties(method) 26 | self.channel.basic_publish( 27 | exchange=EXCHANGE, routing_key=ROUTING_KEY, body=json.dumps(body), 28 | properties=properties) -------------------------------------------------------------------------------- /UserService/userauth/serializers.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | from django.contrib.auth.models import User 3 | from rest_framework.validators import UniqueValidator 4 | from django.contrib.auth.password_validation import validate_password 5 | 6 | 7 | class RegisterSerializer(serializers.ModelSerializer): 8 | email = serializers.EmailField( 9 | required=True, 10 | validators=[UniqueValidator(queryset=User.objects.all())] 11 | ) 12 | 13 | password = serializers.CharField(write_only=True, required=True, validators=[validate_password]) 14 | password2 = serializers.CharField(write_only=True, required=True) 15 | 16 | class Meta: 17 | model = User 18 | fields = ('id','username', 'password', 'password2', 'email', 'first_name', 'last_name') 19 | extra_kwargs = { 20 | 'first_name': {'required': True}, 21 | 'last_name': {'required': True} 22 | } 23 | 24 | def validate(self, attrs): 25 | if attrs['password'] != attrs['password2']: 26 | raise serializers.ValidationError({"password": "Password fields didn't match."}) 27 | 28 | return attrs 29 | 30 | def create(self, validated_data): 31 | user = User.objects.create( 32 | username=validated_data['username'], 33 | email=validated_data['email'], 34 | first_name=validated_data['first_name'], 35 | last_name=validated_data['last_name'] 36 | ) 37 | 38 | user.set_password(validated_data['password']) 39 | user.save() 40 | 41 | return user -------------------------------------------------------------------------------- /UserService/userauth/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /UserService/userauth/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | from userauth.views import RegisterView 3 | 4 | 5 | urlpatterns = [ 6 | path('register/', RegisterView.as_view(), name='auth_register'), 7 | ] -------------------------------------------------------------------------------- /UserService/userauth/views.py: -------------------------------------------------------------------------------- 1 | from django.contrib.auth.models import User 2 | from .serializers import RegisterSerializer 3 | from rest_framework import generics 4 | from .producer_user_created import ProducerUserCreated 5 | from rest_framework.response import Response 6 | from rest_framework import status 7 | import json 8 | producerUserCreated=ProducerUserCreated() 9 | class RegisterView(generics.CreateAPIView): 10 | queryset = User.objects.all() 11 | #permission_classes = (AllowAny,) 12 | serializer_class = RegisterSerializer 13 | def create(self, request, *args, **kwargs): 14 | serializer = self.get_serializer(data=request.data) 15 | serializer.is_valid(raise_exception=True) 16 | self.perform_create(serializer) 17 | producerUserCreated.publish("user_created_method",json.dumps(serializer.data)) 18 | headers = self.get_success_headers(serializer.data) 19 | return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) 20 | --------------------------------------------------------------------------------