django-cfg 1.2.21__py3-none-any.whl → 1.2.23__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- django_cfg/__init__.py +1 -1
- django_cfg/apps/newsletter/signals.py +9 -8
- django_cfg/apps/payments/__init__.py +8 -0
- django_cfg/apps/payments/admin/__init__.py +23 -0
- django_cfg/apps/payments/admin/api_keys_admin.py +347 -0
- django_cfg/apps/payments/admin/balance_admin.py +434 -0
- django_cfg/apps/payments/admin/currencies_admin.py +186 -0
- django_cfg/apps/payments/admin/filters.py +259 -0
- django_cfg/apps/payments/admin/payments_admin.py +142 -0
- django_cfg/apps/payments/admin/subscriptions_admin.py +227 -0
- django_cfg/apps/payments/admin/tariffs_admin.py +199 -0
- django_cfg/apps/payments/apps.py +22 -0
- django_cfg/apps/payments/config/__init__.py +87 -0
- django_cfg/apps/payments/config/module.py +162 -0
- django_cfg/apps/payments/config/providers.py +93 -0
- django_cfg/apps/payments/config/settings.py +136 -0
- django_cfg/apps/payments/config/utils.py +198 -0
- django_cfg/apps/payments/decorators.py +291 -0
- django_cfg/apps/payments/managers/__init__.py +22 -0
- django_cfg/apps/payments/managers/api_key_manager.py +35 -0
- django_cfg/apps/payments/managers/balance_manager.py +361 -0
- django_cfg/apps/payments/managers/currency_manager.py +32 -0
- django_cfg/apps/payments/managers/payment_manager.py +44 -0
- django_cfg/apps/payments/managers/subscription_manager.py +37 -0
- django_cfg/apps/payments/managers/tariff_manager.py +29 -0
- django_cfg/apps/payments/middleware/__init__.py +13 -0
- django_cfg/apps/payments/middleware/api_access.py +261 -0
- django_cfg/apps/payments/middleware/rate_limiting.py +216 -0
- django_cfg/apps/payments/middleware/usage_tracking.py +296 -0
- django_cfg/apps/payments/migrations/0001_initial.py +1003 -0
- django_cfg/apps/payments/migrations/__init__.py +1 -0
- django_cfg/apps/payments/models/__init__.py +67 -0
- django_cfg/apps/payments/models/api_keys.py +96 -0
- django_cfg/apps/payments/models/balance.py +209 -0
- django_cfg/apps/payments/models/base.py +30 -0
- django_cfg/apps/payments/models/currencies.py +138 -0
- django_cfg/apps/payments/models/events.py +73 -0
- django_cfg/apps/payments/models/payments.py +301 -0
- django_cfg/apps/payments/models/subscriptions.py +270 -0
- django_cfg/apps/payments/models/tariffs.py +102 -0
- django_cfg/apps/payments/serializers/__init__.py +56 -0
- django_cfg/apps/payments/serializers/api_keys.py +51 -0
- django_cfg/apps/payments/serializers/balance.py +59 -0
- django_cfg/apps/payments/serializers/currencies.py +55 -0
- django_cfg/apps/payments/serializers/payments.py +62 -0
- django_cfg/apps/payments/serializers/subscriptions.py +71 -0
- django_cfg/apps/payments/serializers/tariffs.py +56 -0
- django_cfg/apps/payments/services/__init__.py +65 -0
- django_cfg/apps/payments/services/billing/__init__.py +8 -0
- django_cfg/apps/payments/services/cache/__init__.py +15 -0
- django_cfg/apps/payments/services/cache/base.py +30 -0
- django_cfg/apps/payments/services/cache/simple_cache.py +135 -0
- django_cfg/apps/payments/services/core/__init__.py +17 -0
- django_cfg/apps/payments/services/core/balance_service.py +449 -0
- django_cfg/apps/payments/services/core/payment_service.py +393 -0
- django_cfg/apps/payments/services/core/subscription_service.py +616 -0
- django_cfg/apps/payments/services/internal_types.py +266 -0
- django_cfg/apps/payments/services/middleware/__init__.py +8 -0
- django_cfg/apps/payments/services/providers/__init__.py +19 -0
- django_cfg/apps/payments/services/providers/base.py +137 -0
- django_cfg/apps/payments/services/providers/cryptapi.py +262 -0
- django_cfg/apps/payments/services/providers/nowpayments.py +293 -0
- django_cfg/apps/payments/services/providers/registry.py +99 -0
- django_cfg/apps/payments/services/validators/__init__.py +8 -0
- django_cfg/apps/payments/signals/__init__.py +13 -0
- django_cfg/apps/payments/signals/api_key_signals.py +150 -0
- django_cfg/apps/payments/signals/payment_signals.py +127 -0
- django_cfg/apps/payments/signals/subscription_signals.py +196 -0
- django_cfg/apps/payments/urls.py +78 -0
- django_cfg/apps/payments/utils/__init__.py +42 -0
- django_cfg/apps/payments/utils/config_utils.py +243 -0
- django_cfg/apps/payments/utils/middleware_utils.py +228 -0
- django_cfg/apps/payments/utils/validation_utils.py +94 -0
- django_cfg/apps/payments/views/__init__.py +62 -0
- django_cfg/apps/payments/views/api_key_views.py +164 -0
- django_cfg/apps/payments/views/balance_views.py +75 -0
- django_cfg/apps/payments/views/currency_views.py +111 -0
- django_cfg/apps/payments/views/payment_views.py +111 -0
- django_cfg/apps/payments/views/subscription_views.py +135 -0
- django_cfg/apps/payments/views/tariff_views.py +131 -0
- django_cfg/apps/support/signals.py +16 -4
- django_cfg/apps/support/templates/support/chat/ticket_chat.html +1 -1
- django_cfg/core/config.py +6 -0
- django_cfg/models/revolution.py +14 -0
- django_cfg/modules/base.py +9 -0
- django_cfg/modules/django_email.py +42 -4
- django_cfg/modules/django_unfold/dashboard.py +20 -0
- {django_cfg-1.2.21.dist-info → django_cfg-1.2.23.dist-info}/METADATA +2 -1
- {django_cfg-1.2.21.dist-info → django_cfg-1.2.23.dist-info}/RECORD +92 -14
- {django_cfg-1.2.21.dist-info → django_cfg-1.2.23.dist-info}/WHEEL +0 -0
- {django_cfg-1.2.21.dist-info → django_cfg-1.2.23.dist-info}/entry_points.txt +0 -0
- {django_cfg-1.2.21.dist-info → django_cfg-1.2.23.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,361 @@
|
|
1
|
+
"""
|
2
|
+
User balance manager with atomic operations.
|
3
|
+
|
4
|
+
Following CRITICAL_REQUIREMENTS.md:
|
5
|
+
- Atomic balance updates
|
6
|
+
- Type safety
|
7
|
+
- Event sourcing
|
8
|
+
- Proper error handling
|
9
|
+
"""
|
10
|
+
|
11
|
+
from django.db import models, transaction
|
12
|
+
from django.utils import timezone
|
13
|
+
from decimal import Decimal
|
14
|
+
from typing import Optional, Dict, Any
|
15
|
+
import logging
|
16
|
+
|
17
|
+
logger = logging.getLogger(__name__)
|
18
|
+
|
19
|
+
|
20
|
+
class UserBalanceManager(models.Manager):
|
21
|
+
"""Manager for UserBalance with atomic operations."""
|
22
|
+
|
23
|
+
def get_or_create_balance(self, user) -> 'UserBalance':
|
24
|
+
"""Get or create user balance atomically."""
|
25
|
+
balance, created = self.get_or_create(
|
26
|
+
user=user,
|
27
|
+
defaults={
|
28
|
+
'amount_usd': Decimal('0'),
|
29
|
+
'reserved_usd': Decimal('0'),
|
30
|
+
'total_earned': Decimal('0'),
|
31
|
+
'total_spent': Decimal('0'),
|
32
|
+
}
|
33
|
+
)
|
34
|
+
|
35
|
+
if created:
|
36
|
+
logger.info(f"Created new balance for user {user.id}")
|
37
|
+
|
38
|
+
return balance
|
39
|
+
|
40
|
+
def add_funds(
|
41
|
+
self,
|
42
|
+
user,
|
43
|
+
amount_usd: Decimal,
|
44
|
+
description: str,
|
45
|
+
reference_id: Optional[str] = None,
|
46
|
+
payment=None
|
47
|
+
) -> Dict[str, Any]:
|
48
|
+
"""
|
49
|
+
Add funds to user balance atomically.
|
50
|
+
|
51
|
+
Returns:
|
52
|
+
Dict with operation result and transaction details
|
53
|
+
"""
|
54
|
+
if amount_usd <= 0:
|
55
|
+
raise ValueError("Amount must be positive")
|
56
|
+
|
57
|
+
with transaction.atomic():
|
58
|
+
# Get or create balance with row lock
|
59
|
+
balance = self.select_for_update().get_or_create_balance(user)
|
60
|
+
|
61
|
+
# Store old values for transaction record
|
62
|
+
old_balance = balance.amount_usd
|
63
|
+
old_earned = balance.total_earned
|
64
|
+
|
65
|
+
# Update balance
|
66
|
+
balance.amount_usd += amount_usd
|
67
|
+
balance.total_earned += amount_usd
|
68
|
+
balance.last_transaction_at = timezone.now()
|
69
|
+
balance.save()
|
70
|
+
|
71
|
+
# Create transaction record
|
72
|
+
from ..models.balance import Transaction
|
73
|
+
transaction_record = Transaction.objects.create(
|
74
|
+
user=user,
|
75
|
+
amount_usd=amount_usd,
|
76
|
+
transaction_type=Transaction.TypeChoices.CREDIT,
|
77
|
+
description=description,
|
78
|
+
payment=payment,
|
79
|
+
reference_id=reference_id,
|
80
|
+
balance_before=old_balance,
|
81
|
+
balance_after=balance.amount_usd,
|
82
|
+
metadata={
|
83
|
+
'total_earned_before': str(old_earned),
|
84
|
+
'total_earned_after': str(balance.total_earned),
|
85
|
+
}
|
86
|
+
)
|
87
|
+
|
88
|
+
logger.info(
|
89
|
+
f"Added ${amount_usd} to user {user.id} balance. "
|
90
|
+
f"New balance: ${balance.amount_usd}"
|
91
|
+
)
|
92
|
+
|
93
|
+
return {
|
94
|
+
'success': True,
|
95
|
+
'old_balance': old_balance,
|
96
|
+
'new_balance': balance.amount_usd,
|
97
|
+
'amount_added': amount_usd,
|
98
|
+
'transaction_id': str(transaction_record.id),
|
99
|
+
'balance_obj': balance
|
100
|
+
}
|
101
|
+
|
102
|
+
def debit_funds(
|
103
|
+
self,
|
104
|
+
user,
|
105
|
+
amount_usd: Decimal,
|
106
|
+
description: str,
|
107
|
+
reference_id: Optional[str] = None,
|
108
|
+
allow_overdraft: bool = False
|
109
|
+
) -> Dict[str, Any]:
|
110
|
+
"""
|
111
|
+
Debit funds from user balance atomically.
|
112
|
+
|
113
|
+
Args:
|
114
|
+
user: User object
|
115
|
+
amount_usd: Amount to debit (positive value)
|
116
|
+
description: Transaction description
|
117
|
+
reference_id: Optional reference ID
|
118
|
+
allow_overdraft: Allow negative balance
|
119
|
+
|
120
|
+
Returns:
|
121
|
+
Dict with operation result
|
122
|
+
"""
|
123
|
+
if amount_usd <= 0:
|
124
|
+
raise ValueError("Amount must be positive")
|
125
|
+
|
126
|
+
with transaction.atomic():
|
127
|
+
# Get balance with row lock
|
128
|
+
balance = self.select_for_update().get_or_create_balance(user)
|
129
|
+
|
130
|
+
# Check sufficient funds
|
131
|
+
if not allow_overdraft and balance.amount_usd < amount_usd:
|
132
|
+
from ..models.exceptions import InsufficientFundsError
|
133
|
+
from ..models.pydantic_models import MoneyAmount
|
134
|
+
from ..models import CurrencyChoices
|
135
|
+
|
136
|
+
raise InsufficientFundsError(
|
137
|
+
message=f"Insufficient funds: ${balance.amount_usd} < ${amount_usd}",
|
138
|
+
required_amount=MoneyAmount(amount=amount_usd, currency=CurrencyChoices.USD),
|
139
|
+
available_amount=MoneyAmount(amount=balance.amount_usd, currency=CurrencyChoices.USD),
|
140
|
+
user_id=user.id
|
141
|
+
)
|
142
|
+
|
143
|
+
# Store old values
|
144
|
+
old_balance = balance.amount_usd
|
145
|
+
old_spent = balance.total_spent
|
146
|
+
|
147
|
+
# Update balance
|
148
|
+
balance.amount_usd -= amount_usd
|
149
|
+
balance.total_spent += amount_usd
|
150
|
+
balance.last_transaction_at = timezone.now()
|
151
|
+
balance.save()
|
152
|
+
|
153
|
+
# Create transaction record
|
154
|
+
from ..models.balance import Transaction
|
155
|
+
transaction_record = Transaction.objects.create(
|
156
|
+
user=user,
|
157
|
+
amount_usd=-amount_usd, # Negative for debit
|
158
|
+
transaction_type=Transaction.TypeChoices.DEBIT,
|
159
|
+
description=description,
|
160
|
+
reference_id=reference_id,
|
161
|
+
balance_before=old_balance,
|
162
|
+
balance_after=balance.amount_usd,
|
163
|
+
metadata={
|
164
|
+
'total_spent_before': str(old_spent),
|
165
|
+
'total_spent_after': str(balance.total_spent),
|
166
|
+
'allow_overdraft': allow_overdraft,
|
167
|
+
}
|
168
|
+
)
|
169
|
+
|
170
|
+
logger.info(
|
171
|
+
f"Debited ${amount_usd} from user {user.id} balance. "
|
172
|
+
f"New balance: ${balance.amount_usd}"
|
173
|
+
)
|
174
|
+
|
175
|
+
return {
|
176
|
+
'success': True,
|
177
|
+
'old_balance': old_balance,
|
178
|
+
'new_balance': balance.amount_usd,
|
179
|
+
'amount_debited': amount_usd,
|
180
|
+
'transaction_id': str(transaction_record.id),
|
181
|
+
'balance_obj': balance
|
182
|
+
}
|
183
|
+
|
184
|
+
def hold_funds(
|
185
|
+
self,
|
186
|
+
user,
|
187
|
+
amount_usd: Decimal,
|
188
|
+
description: str,
|
189
|
+
reference_id: Optional[str] = None
|
190
|
+
) -> Dict[str, Any]:
|
191
|
+
"""
|
192
|
+
Hold funds (move from available to reserved).
|
193
|
+
|
194
|
+
Args:
|
195
|
+
user: User object
|
196
|
+
amount_usd: Amount to hold
|
197
|
+
description: Hold description
|
198
|
+
reference_id: Optional reference ID
|
199
|
+
|
200
|
+
Returns:
|
201
|
+
Dict with operation result
|
202
|
+
"""
|
203
|
+
if amount_usd <= 0:
|
204
|
+
raise ValueError("Amount must be positive")
|
205
|
+
|
206
|
+
with transaction.atomic():
|
207
|
+
# Get balance with row lock
|
208
|
+
balance = self.select_for_update().get_or_create_balance(user)
|
209
|
+
|
210
|
+
# Check sufficient available funds
|
211
|
+
if balance.amount_usd < amount_usd:
|
212
|
+
from ..models.exceptions import InsufficientFundsError
|
213
|
+
from ..models.pydantic_models import MoneyAmount
|
214
|
+
from ..models import CurrencyChoices
|
215
|
+
|
216
|
+
raise InsufficientFundsError(
|
217
|
+
message=f"Insufficient available funds for hold: ${balance.amount_usd} < ${amount_usd}",
|
218
|
+
required_amount=MoneyAmount(amount=amount_usd, currency=CurrencyChoices.USD),
|
219
|
+
available_amount=MoneyAmount(amount=balance.amount_usd, currency=CurrencyChoices.USD),
|
220
|
+
user_id=user.id
|
221
|
+
)
|
222
|
+
|
223
|
+
# Store old values
|
224
|
+
old_available = balance.amount_usd
|
225
|
+
old_reserved = balance.reserved_usd
|
226
|
+
|
227
|
+
# Move funds from available to reserved
|
228
|
+
balance.amount_usd -= amount_usd
|
229
|
+
balance.reserved_usd += amount_usd
|
230
|
+
balance.last_transaction_at = timezone.now()
|
231
|
+
balance.save()
|
232
|
+
|
233
|
+
# Create transaction record
|
234
|
+
from ..models.balance import Transaction
|
235
|
+
transaction_record = Transaction.objects.create(
|
236
|
+
user=user,
|
237
|
+
amount_usd=amount_usd,
|
238
|
+
transaction_type=Transaction.TypeChoices.HOLD,
|
239
|
+
description=description,
|
240
|
+
reference_id=reference_id,
|
241
|
+
balance_before=old_available,
|
242
|
+
balance_after=balance.amount_usd,
|
243
|
+
metadata={
|
244
|
+
'reserved_before': str(old_reserved),
|
245
|
+
'reserved_after': str(balance.reserved_usd),
|
246
|
+
'operation': 'hold_funds',
|
247
|
+
}
|
248
|
+
)
|
249
|
+
|
250
|
+
logger.info(
|
251
|
+
f"Held ${amount_usd} for user {user.id}. "
|
252
|
+
f"Available: ${balance.amount_usd}, Reserved: ${balance.reserved_usd}"
|
253
|
+
)
|
254
|
+
|
255
|
+
return {
|
256
|
+
'success': True,
|
257
|
+
'amount_held': amount_usd,
|
258
|
+
'available_balance': balance.amount_usd,
|
259
|
+
'reserved_balance': balance.reserved_usd,
|
260
|
+
'transaction_id': str(transaction_record.id),
|
261
|
+
'balance_obj': balance
|
262
|
+
}
|
263
|
+
|
264
|
+
def release_funds(
|
265
|
+
self,
|
266
|
+
user,
|
267
|
+
amount_usd: Decimal,
|
268
|
+
description: str,
|
269
|
+
reference_id: Optional[str] = None,
|
270
|
+
refund_to_available: bool = True
|
271
|
+
) -> Dict[str, Any]:
|
272
|
+
"""
|
273
|
+
Release held funds.
|
274
|
+
|
275
|
+
Args:
|
276
|
+
user: User object
|
277
|
+
amount_usd: Amount to release
|
278
|
+
description: Release description
|
279
|
+
reference_id: Optional reference ID
|
280
|
+
refund_to_available: If True, move to available; if False, remove entirely
|
281
|
+
|
282
|
+
Returns:
|
283
|
+
Dict with operation result
|
284
|
+
"""
|
285
|
+
if amount_usd <= 0:
|
286
|
+
raise ValueError("Amount must be positive")
|
287
|
+
|
288
|
+
with transaction.atomic():
|
289
|
+
# Get balance with row lock
|
290
|
+
balance = self.select_for_update().get_or_create_balance(user)
|
291
|
+
|
292
|
+
# Check sufficient reserved funds
|
293
|
+
if balance.reserved_usd < amount_usd:
|
294
|
+
raise ValueError(
|
295
|
+
f"Insufficient reserved funds: ${balance.reserved_usd} < ${amount_usd}"
|
296
|
+
)
|
297
|
+
|
298
|
+
# Store old values
|
299
|
+
old_available = balance.amount_usd
|
300
|
+
old_reserved = balance.reserved_usd
|
301
|
+
|
302
|
+
# Release funds
|
303
|
+
balance.reserved_usd -= amount_usd
|
304
|
+
if refund_to_available:
|
305
|
+
balance.amount_usd += amount_usd
|
306
|
+
else:
|
307
|
+
# Funds are consumed (e.g., for payment)
|
308
|
+
balance.total_spent += amount_usd
|
309
|
+
|
310
|
+
balance.last_transaction_at = timezone.now()
|
311
|
+
balance.save()
|
312
|
+
|
313
|
+
# Create transaction record
|
314
|
+
from ..models.balance import Transaction
|
315
|
+
transaction_record = Transaction.objects.create(
|
316
|
+
user=user,
|
317
|
+
amount_usd=amount_usd if refund_to_available else -amount_usd,
|
318
|
+
transaction_type=Transaction.TypeChoices.RELEASE,
|
319
|
+
description=description,
|
320
|
+
reference_id=reference_id,
|
321
|
+
balance_before=old_available,
|
322
|
+
balance_after=balance.amount_usd,
|
323
|
+
metadata={
|
324
|
+
'reserved_before': str(old_reserved),
|
325
|
+
'reserved_after': str(balance.reserved_usd),
|
326
|
+
'refund_to_available': refund_to_available,
|
327
|
+
'operation': 'release_funds',
|
328
|
+
}
|
329
|
+
)
|
330
|
+
|
331
|
+
action = "refunded to available" if refund_to_available else "consumed"
|
332
|
+
logger.info(
|
333
|
+
f"Released ${amount_usd} for user {user.id} ({action}). "
|
334
|
+
f"Available: ${balance.amount_usd}, Reserved: ${balance.reserved_usd}"
|
335
|
+
)
|
336
|
+
|
337
|
+
return {
|
338
|
+
'success': True,
|
339
|
+
'amount_released': amount_usd,
|
340
|
+
'refund_to_available': refund_to_available,
|
341
|
+
'available_balance': balance.amount_usd,
|
342
|
+
'reserved_balance': balance.reserved_usd,
|
343
|
+
'transaction_id': str(transaction_record.id),
|
344
|
+
'balance_obj': balance
|
345
|
+
}
|
346
|
+
|
347
|
+
def get_balance_summary(self, user) -> Dict[str, Any]:
|
348
|
+
"""Get comprehensive balance summary for user."""
|
349
|
+
balance = self.get_or_create_balance(user)
|
350
|
+
|
351
|
+
return {
|
352
|
+
'user_id': user.id,
|
353
|
+
'available_balance': balance.amount_usd,
|
354
|
+
'reserved_balance': balance.reserved_usd,
|
355
|
+
'total_balance': balance.total_balance,
|
356
|
+
'total_earned': balance.total_earned,
|
357
|
+
'total_spent': balance.total_spent,
|
358
|
+
'last_transaction_at': balance.last_transaction_at,
|
359
|
+
'created_at': balance.created_at,
|
360
|
+
'updated_at': balance.updated_at,
|
361
|
+
}
|
@@ -0,0 +1,32 @@
|
|
1
|
+
"""
|
2
|
+
Currency managers.
|
3
|
+
"""
|
4
|
+
|
5
|
+
from django.db import models
|
6
|
+
|
7
|
+
|
8
|
+
class CurrencyManager(models.Manager):
|
9
|
+
"""Manager for Currency model."""
|
10
|
+
|
11
|
+
def get_active_currencies(self):
|
12
|
+
"""Get active currencies."""
|
13
|
+
return self.filter(is_active=True)
|
14
|
+
|
15
|
+
def get_fiat_currencies(self):
|
16
|
+
"""Get fiat currencies."""
|
17
|
+
return self.filter(currency_type='fiat', is_active=True)
|
18
|
+
|
19
|
+
def get_crypto_currencies(self):
|
20
|
+
"""Get cryptocurrencies."""
|
21
|
+
return self.filter(currency_type='crypto', is_active=True)
|
22
|
+
|
23
|
+
|
24
|
+
class CurrencyNetworkManager(models.Manager):
|
25
|
+
"""Manager for CurrencyNetwork model."""
|
26
|
+
|
27
|
+
def get_active_networks(self, currency=None):
|
28
|
+
"""Get active networks."""
|
29
|
+
queryset = self.filter(is_active=True)
|
30
|
+
if currency:
|
31
|
+
queryset = queryset.filter(currency=currency)
|
32
|
+
return queryset
|
@@ -0,0 +1,44 @@
|
|
1
|
+
"""
|
2
|
+
Payment manager for UniversalPayment model.
|
3
|
+
"""
|
4
|
+
|
5
|
+
from django.db import models
|
6
|
+
|
7
|
+
|
8
|
+
class UniversalPaymentManager(models.Manager):
|
9
|
+
"""Manager for UniversalPayment model."""
|
10
|
+
|
11
|
+
def create_payment(self, user, amount_usd: float, currency_code: str, provider: str):
|
12
|
+
"""Create a new payment."""
|
13
|
+
payment = self.create(
|
14
|
+
user=user,
|
15
|
+
amount_usd=amount_usd,
|
16
|
+
currency_code=currency_code.upper(),
|
17
|
+
provider=provider,
|
18
|
+
status=self.model.PaymentStatus.PENDING
|
19
|
+
)
|
20
|
+
return payment
|
21
|
+
|
22
|
+
def get_pending_payments(self, user=None):
|
23
|
+
"""Get pending payments for user or all users."""
|
24
|
+
queryset = self.filter(status=self.model.PaymentStatus.PENDING)
|
25
|
+
if user:
|
26
|
+
queryset = queryset.filter(user=user)
|
27
|
+
return queryset
|
28
|
+
|
29
|
+
def get_completed_payments(self, user=None):
|
30
|
+
"""Get completed payments for user or all users."""
|
31
|
+
queryset = self.filter(status=self.model.PaymentStatus.COMPLETED)
|
32
|
+
if user:
|
33
|
+
queryset = queryset.filter(user=user)
|
34
|
+
return queryset
|
35
|
+
|
36
|
+
def get_failed_payments(self, user=None):
|
37
|
+
"""Get failed/expired payments for user or all users."""
|
38
|
+
queryset = self.filter(status__in=[
|
39
|
+
self.model.PaymentStatus.FAILED,
|
40
|
+
self.model.PaymentStatus.EXPIRED
|
41
|
+
])
|
42
|
+
if user:
|
43
|
+
queryset = queryset.filter(user=user)
|
44
|
+
return queryset
|
@@ -0,0 +1,37 @@
|
|
1
|
+
"""
|
2
|
+
Subscription managers.
|
3
|
+
"""
|
4
|
+
|
5
|
+
from django.db import models
|
6
|
+
from django.utils import timezone
|
7
|
+
|
8
|
+
|
9
|
+
class SubscriptionManager(models.Manager):
|
10
|
+
"""Manager for Subscription model."""
|
11
|
+
|
12
|
+
def get_active_subscriptions(self, user=None):
|
13
|
+
"""Get active subscriptions."""
|
14
|
+
queryset = self.filter(
|
15
|
+
status='active',
|
16
|
+
expires_at__gt=timezone.now()
|
17
|
+
)
|
18
|
+
if user:
|
19
|
+
queryset = queryset.filter(user=user)
|
20
|
+
return queryset
|
21
|
+
|
22
|
+
def get_expired_subscriptions(self, user=None):
|
23
|
+
"""Get expired subscriptions."""
|
24
|
+
queryset = self.filter(
|
25
|
+
expires_at__lte=timezone.now()
|
26
|
+
)
|
27
|
+
if user:
|
28
|
+
queryset = queryset.filter(user=user)
|
29
|
+
return queryset
|
30
|
+
|
31
|
+
|
32
|
+
class EndpointGroupManager(models.Manager):
|
33
|
+
"""Manager for EndpointGroup model."""
|
34
|
+
|
35
|
+
def get_active_groups(self):
|
36
|
+
"""Get active endpoint groups."""
|
37
|
+
return self.filter(is_active=True)
|
@@ -0,0 +1,29 @@
|
|
1
|
+
"""
|
2
|
+
Tariff managers.
|
3
|
+
"""
|
4
|
+
|
5
|
+
from django.db import models
|
6
|
+
|
7
|
+
|
8
|
+
class TariffManager(models.Manager):
|
9
|
+
"""Manager for Tariff model."""
|
10
|
+
|
11
|
+
def get_active_tariffs(self):
|
12
|
+
"""Get active tariffs."""
|
13
|
+
return self.filter(is_active=True).order_by('monthly_price')
|
14
|
+
|
15
|
+
def get_free_tariffs(self):
|
16
|
+
"""Get free tariffs."""
|
17
|
+
return self.filter(monthly_price=0, is_active=True)
|
18
|
+
|
19
|
+
def get_paid_tariffs(self):
|
20
|
+
"""Get paid tariffs."""
|
21
|
+
return self.filter(monthly_price__gt=0, is_active=True)
|
22
|
+
|
23
|
+
|
24
|
+
class TariffEndpointGroupManager(models.Manager):
|
25
|
+
"""Manager for TariffEndpointGroup model."""
|
26
|
+
|
27
|
+
def get_enabled_for_tariff(self, tariff):
|
28
|
+
"""Get enabled endpoint groups for tariff."""
|
29
|
+
return self.filter(tariff=tariff, is_enabled=True)
|
@@ -0,0 +1,13 @@
|
|
1
|
+
"""
|
2
|
+
Middleware for universal payments.
|
3
|
+
"""
|
4
|
+
|
5
|
+
from .api_access import APIAccessMiddleware
|
6
|
+
from .rate_limiting import RateLimitingMiddleware
|
7
|
+
from .usage_tracking import UsageTrackingMiddleware
|
8
|
+
|
9
|
+
__all__ = [
|
10
|
+
'APIAccessMiddleware',
|
11
|
+
'RateLimitingMiddleware',
|
12
|
+
'UsageTrackingMiddleware',
|
13
|
+
]
|