Skip to content
This repository was archived by the owner on Nov 6, 2022. It is now read-only.

Commit 3711108

Browse files
author
Hugo Osvaldo Barrera
committed
Provide a celery task for notification processing
1 parent dcf1dad commit 3711108

File tree

5 files changed

+44
-10
lines changed

5 files changed

+44
-10
lines changed

README.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,19 @@ AUTOPROCESS
7575

7676
**Required**
7777

78-
If set to ``True``, notifications will be processed as soon as they are
79-
received. Otherwise, it's up to the developer to process them.
78+
If set to ``'SYNC'``, notifications will be processed as soon as they are
79+
received. If set to ``'ASYNC'``, notifications will be processed asynchronously.
80+
Celery is expected to be configured and running for this to works.
81+
82+
If this variable is set to ``False``, notifications will not be processed and
83+
it will be up to the developer to call their ``process()`` method.
8084

8185
A signal is always fired when a notification has been created, and a common
82-
pattern if not auto-processing is to have a celery task to process them::
86+
pattern if not auto-processing is to use your own queue mechanism::
8387

8488
@receiver(notification_received)
8589
def process_notification(sender, **kwargs):
86-
tasks.process_notification.delay(notification=sender)
90+
add_to_some_queue(notification=sender)
8791

8892
SUCCESS_URL
8993
~~~~~~~~~~~

django_mercadopago/apps.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ class MercadoPagoConfig(AppConfig):
55
name = 'django_mercadopago'
66
label = 'mp'
77
verbose_name = 'MercadoPago'
8+
9+
def ready(self):
10+
from django_mercadopago import models, signals
11+
12+
signals.notification_received.connect(
13+
signals.process_new_notification,
14+
sender=models.Notification,
15+
)

django_mercadopago/signals.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1-
from django.dispatch import Signal
1+
import logging
22

3+
from django.conf import settings
4+
from django.dispatch import Signal
35

4-
payment_received = Signal(
5-
providing_args=['payment']
6-
)
76

7+
payment_received = Signal(providing_args=['payment'])
88
notification_received = Signal()
9+
10+
logger = logging.getLogger(__name__)
11+
12+
13+
def process_new_notification(sender, instance, **kwargs):
14+
# XXX: what if a user is being redireected and we expect the status to be
15+
# updated?
16+
17+
if settings.MERCADOPAGO['autoprocess'] == 'SYNC':
18+
instance.process()
19+
elif settings.MERCADOPAGO['autoprocess'] == 'ASYNC':
20+
from django_mercadopago import tasks
21+
tasks.process_notification.delay(instance.pk)
22+
else:
23+
logger.debug('Notification not auto-processed.')

django_mercadopago/tasks.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from celery import shared_task
2+
3+
from django_mercadopago import models
4+
5+
6+
@shared_task
7+
def process_notification(notification_id):
8+
notification = models.Notification.objects.get(pk=notification_id)
9+
notification.process()

django_mercadopago/views.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ def _create_notification(reference, topic, resource_id):
3434
},
3535
)
3636

37-
if settings.MERCADOPAGO['autoprocess']:
38-
notification.process()
3937
signals.notification_received.send(sender=notification)
4038

4139
return notification, created

0 commit comments

Comments
 (0)