diff --git a/accounts/api/views.py b/accounts/api/views.py index 83333ff..549c7c7 100644 --- a/accounts/api/views.py +++ b/accounts/api/views.py @@ -708,18 +708,18 @@ class GoogleLoginAPIView(APIView): print("Created new user") message = "Details updated" - try: - ReferralCode.create_referral_code_for_user_manager( - principal=principal, principal_type=principal.principal_type - ) - except Exception as e: - print("ReferralCode: E-", e) - error_response = { - "status": status.HTTP_400_BAD_REQUEST, - "message": constants.FAILURE, - "errors": str(e), - } - return ApiResponse.error(**error_response) + try: + ReferralCode.create_referral_code_for_user_manager( + principal=principal, principal_type=principal.principal_type + ) + except Exception as e: + print("ReferralCode: E-", e) + error_response = { + "status": status.HTTP_400_BAD_REQUEST, + "message": constants.FAILURE, + "errors": str(e), + } + return ApiResponse.error(**error_response) if referral_code: already_register_through_referral = ReferralRecord.objects.filter( diff --git a/manage_notifications/utils.py b/manage_notifications/utils.py index ce7382a..ea450e8 100644 --- a/manage_notifications/utils.py +++ b/manage_notifications/utils.py @@ -87,50 +87,83 @@ def send_notification(title, message, image_url=None, eligible_principals=None): return response +# def get_eligible_principals_for_notification(push_notification): +# principal_type = push_notification.principal_type +# notification_category = push_notification.notification_category + +# if principal_type == PrincipalType.BOTH: +# # If BOTH is selected, fetch users categorized under both EVENT_USER and EVENT_MANAGER +# eligible_principals = list( +# IAmPrincipal.objects.filter( +# Q(principal_type__name=PrincipalType.EVENT_USER) +# | Q(principal_type__name=PrincipalType.EVENT_MANAGER), +# notifications_principal__notification_category=notification_category, +# notifications_principal__is_enabled=True, +# ).values_list("player_id", flat=True) +# ) + +# elif principal_type == PrincipalType.EVENT_USER: +# # Fetch only EVENT_USER principals +# eligible_principals = list( +# IAmPrincipal.objects.filter( +# principal_type__name=PrincipalType.EVENT_USER, +# notifications_principal__notification_category=notification_category, +# notifications_principal__is_enabled=True, +# ).values_list("player_id", flat=True) +# ) + +# elif principal_type == PrincipalType.EVENT_MANAGER: +# # Fetch only EVENT_MANAGER principals +# eligible_principals = list( +# IAmPrincipal.objects.filter( +# principal_type__name=PrincipalType.EVENT_MANAGER, +# notifications_principal__notification_category=notification_category, +# notifications_principal__is_enabled=True, +# ).values_list("player_id", flat=True) +# ) + +# return eligible_principals + + def get_eligible_principals_for_notification(push_notification): principal_type = push_notification.principal_type notification_category = push_notification.notification_category if principal_type == PrincipalType.BOTH: # If BOTH is selected, fetch users categorized under both EVENT_USER and EVENT_MANAGER - eligible_principals = list( - IAmPrincipal.objects.filter( + eligible_principals = [ + player_id + for player_id in IAmPrincipal.objects.filter( Q(principal_type__name=PrincipalType.EVENT_USER) | Q(principal_type__name=PrincipalType.EVENT_MANAGER), notifications_principal__notification_category=notification_category, notifications_principal__is_enabled=True, ).values_list("player_id", flat=True) - ) - - # event_manager_principals = IAmPrincipal.objects.filter( - # principal_type__name=PrincipalType.EVENT_MANAGER, - # notifications_principal__notification_category=notification_category, - # notifications_principal__is_enabled=True, - # ) - - # # Combine the QuerySets. Use | operator for OR query (union) and distinct() to avoid duplicates. - # eligible_principals = ( - # event_user_principals | event_manager_principals - # ) + if player_id is not None + ] elif principal_type == PrincipalType.EVENT_USER: # Fetch only EVENT_USER principals - eligible_principals = list( - IAmPrincipal.objects.filter( + eligible_principals = [ + player_id + for player_id in IAmPrincipal.objects.filter( principal_type__name=PrincipalType.EVENT_USER, notifications_principal__notification_category=notification_category, notifications_principal__is_enabled=True, ).values_list("player_id", flat=True) - ) + if player_id is not None + ] elif principal_type == PrincipalType.EVENT_MANAGER: # Fetch only EVENT_MANAGER principals - eligible_principals = list( - IAmPrincipal.objects.filter( + eligible_principals = [ + player_id + for player_id in IAmPrincipal.objects.filter( principal_type__name=PrincipalType.EVENT_MANAGER, notifications_principal__notification_category=notification_category, notifications_principal__is_enabled=True, ).values_list("player_id", flat=True) - ) + if player_id is not None + ] return eligible_principals diff --git a/manage_referrals/forms.py b/manage_referrals/forms.py index 950f801..8427dbf 100644 --- a/manage_referrals/forms.py +++ b/manage_referrals/forms.py @@ -4,6 +4,7 @@ from .models import ( ReferralRecordReward, GoodTimeCoins, ReferralTracking, + ReferralCode, ) @@ -69,3 +70,14 @@ class ReferralTrackingForm(forms.ModelForm): "user_agent": forms.TextInput(attrs={"class": "form-control"}), "device_model": forms.TextInput(attrs={"class": "form-control"}), } + + +class ReferralCodeForm(forms.ModelForm): + class Meta: + model = ReferralCode + fields = ["id", "principal", "principal_type", "referral_code"] + widgets = { + "principal": forms.Select(attrs={"class": "form-control"}), + "principal_type": forms.Select(attrs={"class": "form-control"}), + "referral_code": forms.TextInput(attrs={"class": "form-control"}), + } diff --git a/manage_referrals/urls.py b/manage_referrals/urls.py index 3d14636..c5ff84f 100644 --- a/manage_referrals/urls.py +++ b/manage_referrals/urls.py @@ -4,6 +4,12 @@ from . import views app_name = "manage_referrals" urlpatterns = [ + # Referral Code + path( + "referral-code/list/", + views.ReferralCodeView.as_view(), + name="code_list", + ), # Referral Record path( "referral-record/list/", diff --git a/manage_referrals/views.py b/manage_referrals/views.py index 63cbca5..2dd98fc 100644 --- a/manage_referrals/views.py +++ b/manage_referrals/views.py @@ -12,6 +12,7 @@ from manage_referrals.forms import ( ReferralTrackingForm, ) from manage_referrals.models import ( + ReferralCode, ReferralRecord, ReferralRecordReward, GoodTimeCoins, @@ -102,6 +103,23 @@ class ReferralRecordView(LoginRequiredMixin, generic.ListView): return context +class ReferralCodeView(LoginRequiredMixin, generic.ListView): + page_name = resource_action.RESOURCE_MANAGE_REFERRALS + resource = resource_action.RESOURCE_MANAGE_REFERRALS + action = resource_action.ACTION_READ + model = ReferralCode + template_name = "manage_referrals/code_list.html" + context_object_name = "code" + + def get_queryset(self): + return super().get_queryset().filter(active=True, deleted=False) + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context["page_name"] = self.page_name + return context + + class ReferralRecordDeleteView(LoginRequiredMixin, generic.View): page_name = resource_action.RESOURCE_MANAGE_REFERRALS resource = resource_action.RESOURCE_MANAGE_REFERRALS diff --git a/templates/manage_referrals/code_list.html b/templates/manage_referrals/code_list.html new file mode 100644 index 0000000..59d7ec8 --- /dev/null +++ b/templates/manage_referrals/code_list.html @@ -0,0 +1,137 @@ +{% extends 'layout/base_template.html' %} +{% load static %} +{% block stylesheet %} + +{% include "cdn_through_html/datatable_cdn_css.html" %} + +{% endblock %} + +{% block content %} + +