Skip to content

Commit b2a117e

Browse files
committed
Klubis#1131 - Zaračunavanje računov
1 parent 9ae4207 commit b2a117e

File tree

13 files changed

+302
-196
lines changed

13 files changed

+302
-196
lines changed

django_project_base/base/event.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
import datetime
33

44
from abc import ABC, abstractmethod
5-
from gettext import gettext
65

76
import swapper
87

98
from django.conf import settings
109
from django.shortcuts import get_object_or_404
10+
from django.utils.translation import gettext_lazy as _
1111
from rest_registration.settings import registration_settings
1212

1313
from django_project_base.constants import EMAIL_SENDER_ID_SETTING_NAME, SMS_SENDER_ID_SETTING_NAME
@@ -186,8 +186,9 @@ def trigger(self, payload=None, **kwargs):
186186
if to := getattr(settings, "ADMINS", getattr(settings, "MANAGERS", [])):
187187
SystemEMailNotificationWithListOfEmails(
188188
message=DjangoProjectBaseMessage(
189-
subject=gettext("Project settings action required"),
190-
body=f"{gettext('Action required for setting')} {payload.name} in project {payload.project.name}",
189+
subject=_("Project settings action required"),
190+
body=_("Action required for setting %(sett_name)s in project %(project_name).")
191+
% {"sett_name": payload.name, "project_name": payload.project.name},
191192
footer="",
192193
content_type=DjangoProjectBaseMessage.HTML,
193194
),
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from enum import Enum
2+
3+
4+
class LicenseType(Enum):
5+
EMAIL = "email"
6+
SMS = "sms"
7+
8+
9+
class LicenseTypeData:
10+
price: float
11+
12+
def __init__(self, price: float) -> None:
13+
self.price = price

django_project_base/licensing/logic.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
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
35
from django.contrib.contenttypes.models import ContentType
46
from django.db import connections
57
from django.db.models import Model, Sum
@@ -8,7 +10,16 @@
810
from dynamicforms.serializers import Serializer
911
from rest_framework.exceptions import PermissionDenied
1012

13+
from django_project_base.licensing.license_type_data import LicenseType
1114
from 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

1425
class 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

Comments
 (0)