From 37111087e7feff7ac7116333f01c65027f73ffa5 Mon Sep 17 00:00:00 2001 From: Hugo Osvaldo Barrera Date: Sun, 9 Dec 2018 18:18:16 -0300 Subject: [PATCH 1/2] Provide a celery task for notification processing --- README.rst | 12 ++++++++---- django_mercadopago/apps.py | 8 ++++++++ django_mercadopago/signals.py | 23 +++++++++++++++++++---- django_mercadopago/tasks.py | 9 +++++++++ django_mercadopago/views.py | 2 -- 5 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 django_mercadopago/tasks.py diff --git a/README.rst b/README.rst index a69df3d..4530b2c 100644 --- a/README.rst +++ b/README.rst @@ -75,15 +75,19 @@ AUTOPROCESS **Required** -If set to ``True``, notifications will be processed as soon as they are -received. Otherwise, it's up to the developer to process them. +If set to ``'SYNC'``, notifications will be processed as soon as they are +received. If set to ``'ASYNC'``, notifications will be processed asynchronously. +Celery is expected to be configured and running for this to works. + +If this variable is set to ``False``, notifications will not be processed and +it will be up to the developer to call their ``process()`` method. A signal is always fired when a notification has been created, and a common -pattern if not auto-processing is to have a celery task to process them:: +pattern if not auto-processing is to use your own queue mechanism:: @receiver(notification_received) def process_notification(sender, **kwargs): - tasks.process_notification.delay(notification=sender) + add_to_some_queue(notification=sender) SUCCESS_URL ~~~~~~~~~~~ diff --git a/django_mercadopago/apps.py b/django_mercadopago/apps.py index c82049d..88bb8c1 100644 --- a/django_mercadopago/apps.py +++ b/django_mercadopago/apps.py @@ -5,3 +5,11 @@ class MercadoPagoConfig(AppConfig): name = 'django_mercadopago' label = 'mp' verbose_name = 'MercadoPago' + + def ready(self): + from django_mercadopago import models, signals + + signals.notification_received.connect( + signals.process_new_notification, + sender=models.Notification, + ) diff --git a/django_mercadopago/signals.py b/django_mercadopago/signals.py index 7f90517..9ebc1ba 100644 --- a/django_mercadopago/signals.py +++ b/django_mercadopago/signals.py @@ -1,8 +1,23 @@ -from django.dispatch import Signal +import logging +from django.conf import settings +from django.dispatch import Signal -payment_received = Signal( - providing_args=['payment'] -) +payment_received = Signal(providing_args=['payment']) notification_received = Signal() + +logger = logging.getLogger(__name__) + + +def process_new_notification(sender, instance, **kwargs): + # XXX: what if a user is being redireected and we expect the status to be + # updated? + + if settings.MERCADOPAGO['autoprocess'] == 'SYNC': + instance.process() + elif settings.MERCADOPAGO['autoprocess'] == 'ASYNC': + from django_mercadopago import tasks + tasks.process_notification.delay(instance.pk) + else: + logger.debug('Notification not auto-processed.') diff --git a/django_mercadopago/tasks.py b/django_mercadopago/tasks.py new file mode 100644 index 0000000..61790d4 --- /dev/null +++ b/django_mercadopago/tasks.py @@ -0,0 +1,9 @@ +from celery import shared_task + +from django_mercadopago import models + + +@shared_task +def process_notification(notification_id): + notification = models.Notification.objects.get(pk=notification_id) + notification.process() diff --git a/django_mercadopago/views.py b/django_mercadopago/views.py index 0e45a9f..d101dde 100644 --- a/django_mercadopago/views.py +++ b/django_mercadopago/views.py @@ -34,8 +34,6 @@ def _create_notification(reference, topic, resource_id): }, ) - if settings.MERCADOPAGO['autoprocess']: - notification.process() signals.notification_received.send(sender=notification) return notification, created From 0a7b0460f25adeac0d9f9bc883cddfa0d85726ff Mon Sep 17 00:00:00 2001 From: Hugo Osvaldo Barrera Date: Sun, 9 Dec 2018 18:19:55 -0300 Subject: [PATCH 2/2] List celery as an optional dependency --- README.rst | 5 +++++ setup.py | 1 + 2 files changed, 6 insertions(+) diff --git a/README.rst b/README.rst index 4530b2c..070f441 100644 --- a/README.rst +++ b/README.rst @@ -39,6 +39,11 @@ Installation should generally be done via pip:: pip install django-mercadopago +If you'll be using the asynchronous processing, celery is required. You'll need +to set up celery itself for your project as well, obviously:: + + pip install django-mercadopago[celery] + Configuration ------------- diff --git a/setup.py b/setup.py index cbf5692..a879767 100644 --- a/setup.py +++ b/setup.py @@ -16,6 +16,7 @@ setup_requires=['setuptools_scm'], extras_require={ 'fixtures': ['factory-boy'], + 'celery-task': ['celery'], }, classifiers=[ 'Development Status :: 4 - Beta',