Skip to content

Commit

Permalink
creating Order API - custom signal
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmalshahabudeen committed Apr 26, 2023
1 parent 30ad6e7 commit 9fff8aa
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 3 deletions.
3 changes: 3 additions & 0 deletions core/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
class CoreConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'core'

def ready(self) -> None:
import core.signals.handlers
6 changes: 6 additions & 0 deletions core/signals/handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.dispatch import receiver
from store.signals import order_created

@receiver(order_created)
def on_order_created(sender, **kwargs):
print(kwargs['order'])
2 changes: 1 addition & 1 deletion store/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ class StoreConfig(AppConfig):
name = 'store'

def ready(self) -> None:
import store.signals
import store.signals.handlers
2 changes: 2 additions & 0 deletions store/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.db import transaction
from store.models import Customer, Product, Collection, Review, Cart, CartItem, Order, OrderItem
from rest_framework import serializers
from .signals import order_created

class SimpleProductSerializer(serializers.ModelSerializer):
class Meta:
Expand Down Expand Up @@ -164,4 +165,5 @@ def save(self, **kwargs):
OrderItem.objects.bulk_create(order_items)
#deleting cart
CartItem.objects.filter(pk=cart_id).delete()
order_created.send_robust(self.__class__, order=order)
return order
3 changes: 3 additions & 0 deletions store/signals/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.dispatch import Signal

order_created = Signal()
2 changes: 1 addition & 1 deletion store/signals.py → store/signals/handlers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import Customer
from ..models import Customer

@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_customer_for_new_user(sender, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion store/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def history(self, request, pk):
return Response('ok')

class OrderViewSet(ModelViewSet):
http_method_names = ['get', 'patch', 'delete', 'head', 'option']
http_method_names = ['get', 'post', 'patch', 'delete', 'head', 'option']
# queryset = Order.objects.all()
# serializer_class = OrderSerializers
# permission_classes = [IsAuthenticated]
Expand Down

0 comments on commit 9fff8aa

Please sign in to comment.