1- from typing import List , Optional
1+ from enum import Enum
2+ from typing import List , Optional , Union
23
4+ from django .conf import settings
35from django .contrib .contenttypes .models import ContentType
46from django .db import connections
57from django .db .models import Model , Sum
810from dynamicforms .serializers import Serializer
911from rest_framework .exceptions import PermissionDenied
1012
13+ from django_project_base .licensing .license_type_data import LicenseType
1114from django_project_base .licensing .models import LicenseAccessUse
15+ from django_project_base .notifications .base .channels .channel import Channel
16+
17+
18+ def license_price (license_type : Union [LicenseType , Enum ]):
19+ license_type_data = settings .DJANGO_PROJECT_BASE_LICENSE_TYPE_DATA .get (license_type )
20+ if not license_type_data :
21+ raise f"No data for license type { license_type .value } "
22+ return license_type_data .price
1223
1324
1425class LicenseUsageReport (Serializer ):
@@ -87,7 +98,7 @@ def log(
8798 content_type = ContentType .objects .get_for_model (model = record ._meta .model )
8899 used = (
89100 LicenseAccessUse .objects .using (self .db )
90- .filter (user_id = str (user_profile_pk ), content_type = content_type )
101+ .filter (user_id = str (user_profile_pk ))
91102 .aggregate (Sum ("amount" ))
92103 .get ("amount__sum" , None )
93104 or 0
@@ -98,7 +109,11 @@ def log(
98109
99110 chl_prices = {i .name : i .notification_price for i in ChannelIdentifier .supported_channels ()}
100111 for used_channel in list (set (list (items .keys ()))):
101- used += chl_prices .get (used_channel , 0 ) * items .get (used_channel , 0 )
112+ if isinstance (used_channel , Channel ):
113+ used_channel_name = used_channel .name
114+ else :
115+ used_channel_name = used_channel
116+ used += chl_prices .get (used_channel_name , 0 ) * items .get (used_channel , 0 )
102117
103118 if not kwargs .get ("is_system_notification" ) and used >= 0 :
104119 raise PermissionDenied (gettext ("Your license is consumed. Please contact support." ))
0 commit comments