diff --git a/apps/promotions/admin.py b/apps/promotions/admin.py index 7677367..2233e09 100644 --- a/apps/promotions/admin.py +++ b/apps/promotions/admin.py @@ -4,8 +4,11 @@ from django.contrib import admin from .models import Promotion, Plan, Event, Recipient, EventSaver +class PromotionAdmin(admin.ModelAdmin): + list_display = ['user_uuid', 'plan', 'promotion_amount', 'recipient', 'event', 'created_at'] + admin.site.register(Plan) -admin.site.register(Promotion) +admin.site.register(Promotion, PromotionAdmin) admin.site.register(Event) admin.site.register(EventSaver) admin.site.register(Recipient) diff --git a/apps/promotions/urls.py b/apps/promotions/urls.py index 51be05f..d64de68 100644 --- a/apps/promotions/urls.py +++ b/apps/promotions/urls.py @@ -10,9 +10,9 @@ from .views import ApplicationPromoteUserApiView, ApplicationEventSubmitAPIView, app_name = 'promotions' router = DefaultRouter() -router.register(r'application', views.ApplicationPromotionViewSet, basename='application_promotions') -router.register(r'events', views.ApplicationViewSet, basename='application-events') -router.register(r'plans', views.UserViewSet, basename='user') +# router.register(r'application', views.ApplicationPromotionViewSet, basename='application_promotions') +# router.register(r'events', views.ApplicationViewSet, basename='application-events') +# router.register(r'plans', views.UserViewSet, basename='user') # urlpatterns = diff --git a/apps/promotions/views.py b/apps/promotions/views.py index 2140cfe..8775077 100644 --- a/apps/promotions/views.py +++ b/apps/promotions/views.py @@ -8,6 +8,7 @@ from rest_framework.viewsets import GenericViewSet from apps.gooyal_oauth2.rest_framework import IsAuthenticatedOrTokenMatchesOASRequirements from apps.gooyal_oauth2.utils import get_application +from utils.exceptions import UnprocessableEntity from .models import Plan, Promotion, EventSaver from .serializers import PlanSerializer, PromotionSerializer, EventSerializer, PromoteSerializer from .tasks import analyze_event_task @@ -225,7 +226,10 @@ class ApplicationEventSubmitAPIView(CreateAPIView): data = serializer.validated_data application = get_application(self.request) event_saver: EventSaver = EventSaver.objects.filter(event_label=serializer.validated_data['label']).first() - event = event_saver.save_event(user=None, application=application, **serializer.validated_data) + try: + event = event_saver.save_event(user=None, application=application, **serializer.validated_data) + except Exception as e: + raise UnprocessableEntity(str(e)) analyze_event_task.delay(event.uuid) diff --git a/utils/exceptions.py b/utils/exceptions.py index f0b7e9c..807f9b5 100644 --- a/utils/exceptions.py +++ b/utils/exceptions.py @@ -52,3 +52,11 @@ class ErrorMiddleware: }, status=500 ) + + +from rest_framework.exceptions import APIException + +class UnprocessableEntity(APIException): + status_code = 422 + default_detail = 'The request was well-formed but cannot be processed due to semantic errors.' + default_code = 'unprocessable_entity'