Initial commit: igny8 project

This commit is contained in:
igny8
2025-11-09 10:27:02 +00:00
commit 60b8188111
27265 changed files with 4360521 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
"""
Billing module for credit management and usage tracking
"""

View File

@@ -0,0 +1,43 @@
"""
Billing Module Admin
"""
from django.contrib import admin
from igny8_core.admin.base import AccountAdminMixin
from .models import CreditTransaction, CreditUsageLog
@admin.register(CreditTransaction)
class CreditTransactionAdmin(AccountAdminMixin, admin.ModelAdmin):
list_display = ['id', 'account', 'transaction_type', 'amount', 'balance_after', 'description', 'created_at']
list_filter = ['transaction_type', 'created_at', 'account']
search_fields = ['description', 'account__name']
readonly_fields = ['created_at']
date_hierarchy = 'created_at'
def get_account_display(self, obj):
"""Safely get account name"""
try:
account = getattr(obj, 'account', None)
return account.name if account else '-'
except:
return '-'
get_account_display.short_description = 'Account'
@admin.register(CreditUsageLog)
class CreditUsageLogAdmin(AccountAdminMixin, admin.ModelAdmin):
list_display = ['id', 'account', 'operation_type', 'credits_used', 'cost_usd', 'model_used', 'created_at']
list_filter = ['operation_type', 'created_at', 'account', 'model_used']
search_fields = ['account__name', 'model_used']
readonly_fields = ['created_at']
date_hierarchy = 'created_at'
def get_account_display(self, obj):
"""Safely get account name"""
try:
account = getattr(obj, 'account', None)
return account.name if account else '-'
except:
return '-'
get_account_display.short_description = 'Account'

View File

@@ -0,0 +1,9 @@
from django.apps import AppConfig
class BillingConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'igny8_core.modules.billing'
verbose_name = 'Billing'

View File

@@ -0,0 +1,22 @@
"""
Credit Cost Constants
"""
CREDIT_COSTS = {
'clustering': {
'base': 1, # 1 credit per 30 keywords
'per_keyword': 1 / 30,
},
'ideas': {
'base': 1, # 1 credit per idea
},
'content': {
'base': 3, # 3 credits per full blog post
},
'images': {
'base': 1, # 1 credit per image
},
'reparse': {
'base': 1, # 1 credit per reparse
},
}

View File

@@ -0,0 +1,14 @@
"""
Billing Exceptions
"""
class InsufficientCreditsError(Exception):
"""Raised when account doesn't have enough credits"""
pass
class CreditCalculationError(Exception):
"""Raised when credit calculation fails"""
pass

View File

@@ -0,0 +1,2 @@
# Management commands for billing module

View File

@@ -0,0 +1,2 @@
# Management commands

View File

@@ -0,0 +1,147 @@
"""
Management command to get account plan limits
Usage: python manage.py get_account_limits dev@igny8.com scale@igny8.com
"""
from django.core.management.base import BaseCommand
from igny8_core.auth.models import Account, Plan, User
from django.db.models import Q
class Command(BaseCommand):
help = 'Get plan limits for specified accounts'
def add_arguments(self, parser):
parser.add_argument('emails', nargs='+', type=str, help='Email addresses of accounts to check')
def handle(self, *args, **options):
emails = options['emails']
# Table header
print("\n" + "="*120)
print("PLAN LIMITS COMPARISON TABLE")
print("="*120 + "\n")
# Get all limit fields from Plan model
limit_fields = {
'General': [
('max_users', 'Max Users'),
('max_sites', 'Max Sites'),
],
'Planner': [
('max_keywords', 'Max Keywords'),
('max_clusters', 'Max Clusters'),
('max_content_ideas', 'Max Content Ideas'),
('daily_cluster_limit', 'Daily Cluster Limit'),
],
'Writer': [
('monthly_word_count_limit', 'Monthly Word Count Limit'),
('daily_content_tasks', 'Daily Content Tasks'),
],
'Images': [
('monthly_image_count', 'Monthly Image Count'),
('daily_image_generation_limit', 'Daily Image Generation Limit'),
],
'AI Credits': [
('monthly_ai_credit_limit', 'Monthly AI Credit Limit'),
('monthly_cluster_ai_credits', 'Monthly Cluster AI Credits'),
('monthly_content_ai_credits', 'Monthly Content AI Credits'),
('monthly_image_ai_credits', 'Monthly Image AI Credits'),
],
}
accounts_data = []
for email in emails:
account = Account.objects.filter(users__email=email).first()
if not account:
self.stdout.write(self.style.WARNING(f'Account not found for {email}'))
accounts_data.append({
'email': email,
'account': None,
'plan': None,
'limits': {}
})
continue
plan = account.plan if account else None
limits = {}
if plan:
for category, fields in limit_fields.items():
limits[category] = {}
for field_name, field_display in fields:
value = getattr(plan, field_name, None)
limits[category][field_name] = value
# Add effective credits
limits['AI Credits']['credits_per_month'] = plan.get_effective_credits_per_month()
accounts_data.append({
'email': email,
'account': account,
'plan': plan,
'limits': limits
})
# Print table header
header = f"{'Limit Category':<20} {'Limit Name':<45} "
for acc_data in accounts_data:
email_short = acc_data['email'].split('@')[0].upper()
header += f"{email_short:<20} "
print(header)
print("-" * 120)
# Print table rows
for category, fields in limit_fields.items():
print(f"\n{self.style.BOLD(category.upper())}")
for field_name, field_display in fields:
row = f" {field_display:<43} "
for acc_data in accounts_data:
if acc_data['plan']:
value = acc_data['limits'].get(category, {}).get(field_name, 'N/A')
row += f"{str(value):<20} "
else:
row += f"{'NO PLAN':<20} "
print(row)
# Print effective credits
print(f"\n {'Credits Per Month (Effective)':<43} ", end="")
for acc_data in accounts_data:
if acc_data['plan']:
value = acc_data['limits'].get('AI Credits', {}).get('credits_per_month', 'N/A')
print(f"{str(value):<20} ", end="")
else:
print(f"{'NO PLAN':<20} ", end="")
print()
# Print account details
print("\n" + "="*120)
print("ACCOUNT DETAILS")
print("="*120 + "\n")
for acc_data in accounts_data:
print(f"Email: {acc_data['email']}")
if acc_data['account']:
print(f" Account Name: {acc_data['account'].name}")
print(f" Account Slug: {acc_data['account'].slug}")
if acc_data['plan']:
print(f" Plan Name: {acc_data['plan'].name}")
print(f" Plan Slug: {acc_data['plan'].slug}")
print(f" Plan Price: ${acc_data['plan'].price}")
print(f" Plan ID: {acc_data['plan'].id}")
else:
print(f" Plan: {self.style.ERROR('NO PLAN ASSIGNED')}")
else:
print(f" Account: {self.style.ERROR('NOT FOUND')}")
print()
# Summary
print("="*120)
print("SUMMARY")
print("="*120)
print(f"Total accounts checked: {len(emails)}")
accounts_with_plans = sum(1 for acc in accounts_data if acc['plan'])
print(f"Accounts with plans: {accounts_with_plans}")
print(f"Accounts without plans: {len(emails) - accounts_with_plans}")
print()

View File

@@ -0,0 +1,59 @@
# Generated by Django 5.2.8 on 2025-11-07 10:37
import django.core.validators
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
('igny8_core_auth', '0008_passwordresettoken_alter_industry_options_and_more'),
]
operations = [
migrations.CreateModel(
name='CreditTransaction',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('updated_at', models.DateTimeField(auto_now=True)),
('transaction_type', models.CharField(choices=[('purchase', 'Purchase'), ('subscription', 'Subscription Renewal'), ('refund', 'Refund'), ('deduction', 'Usage Deduction'), ('adjustment', 'Manual Adjustment')], db_index=True, max_length=20)),
('amount', models.IntegerField(help_text='Positive for additions, negative for deductions')),
('balance_after', models.IntegerField(help_text='Credit balance after this transaction')),
('description', models.CharField(max_length=255)),
('metadata', models.JSONField(default=dict, help_text='Additional context (AI call details, etc.)')),
('created_at', models.DateTimeField(auto_now_add=True)),
('tenant', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_set', to='igny8_core_auth.tenant')),
],
options={
'db_table': 'igny8_credit_transactions',
'ordering': ['-created_at'],
'indexes': [models.Index(fields=['tenant', 'transaction_type'], name='igny8_credi_tenant__c4281c_idx'), models.Index(fields=['tenant', 'created_at'], name='igny8_credi_tenant__69abd3_idx')],
},
),
migrations.CreateModel(
name='CreditUsageLog',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('updated_at', models.DateTimeField(auto_now=True)),
('operation_type', models.CharField(choices=[('clustering', 'Keyword Clustering'), ('ideas', 'Content Ideas Generation'), ('content', 'Content Generation'), ('images', 'Image Generation'), ('reparse', 'Content Reparse')], db_index=True, max_length=50)),
('credits_used', models.IntegerField(validators=[django.core.validators.MinValueValidator(0)])),
('cost_usd', models.DecimalField(blank=True, decimal_places=4, max_digits=10, null=True)),
('model_used', models.CharField(blank=True, max_length=100)),
('tokens_input', models.IntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(0)])),
('tokens_output', models.IntegerField(blank=True, null=True, validators=[django.core.validators.MinValueValidator(0)])),
('related_object_type', models.CharField(blank=True, max_length=50)),
('related_object_id', models.IntegerField(blank=True, null=True)),
('metadata', models.JSONField(default=dict)),
('created_at', models.DateTimeField(auto_now_add=True)),
('tenant', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_set', to='igny8_core_auth.tenant')),
],
options={
'db_table': 'igny8_credit_usage_logs',
'ordering': ['-created_at'],
'indexes': [models.Index(fields=['tenant', 'operation_type'], name='igny8_credi_tenant__3545ed_idx'), models.Index(fields=['tenant', 'created_at'], name='igny8_credi_tenant__4aee99_idx'), models.Index(fields=['tenant', 'operation_type', 'created_at'], name='igny8_credi_tenant__a00d57_idx')],
},
),
]

View File

@@ -0,0 +1,72 @@
"""
Billing Models for Credit System
"""
from django.db import models
from django.core.validators import MinValueValidator
from igny8_core.auth.models import AccountBaseModel
class CreditTransaction(AccountBaseModel):
"""Track all credit transactions (additions, deductions)"""
TRANSACTION_TYPE_CHOICES = [
('purchase', 'Purchase'),
('subscription', 'Subscription Renewal'),
('refund', 'Refund'),
('deduction', 'Usage Deduction'),
('adjustment', 'Manual Adjustment'),
]
transaction_type = models.CharField(max_length=20, choices=TRANSACTION_TYPE_CHOICES, db_index=True)
amount = models.IntegerField(help_text="Positive for additions, negative for deductions")
balance_after = models.IntegerField(help_text="Credit balance after this transaction")
description = models.CharField(max_length=255)
metadata = models.JSONField(default=dict, help_text="Additional context (AI call details, etc.)")
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = 'igny8_credit_transactions'
ordering = ['-created_at']
indexes = [
models.Index(fields=['account', 'transaction_type']),
models.Index(fields=['account', 'created_at']),
]
def __str__(self):
account = getattr(self, 'account', None)
return f"{self.get_transaction_type_display()} - {self.amount} credits - {account.name if account else 'No Account'}"
class CreditUsageLog(AccountBaseModel):
"""Detailed log of credit usage per AI operation"""
OPERATION_TYPE_CHOICES = [
('clustering', 'Keyword Clustering'),
('ideas', 'Content Ideas Generation'),
('content', 'Content Generation'),
('images', 'Image Generation'),
('reparse', 'Content Reparse'),
]
operation_type = models.CharField(max_length=50, choices=OPERATION_TYPE_CHOICES, db_index=True)
credits_used = models.IntegerField(validators=[MinValueValidator(0)])
cost_usd = models.DecimalField(max_digits=10, decimal_places=4, null=True, blank=True)
model_used = models.CharField(max_length=100, blank=True)
tokens_input = models.IntegerField(null=True, blank=True, validators=[MinValueValidator(0)])
tokens_output = models.IntegerField(null=True, blank=True, validators=[MinValueValidator(0)])
related_object_type = models.CharField(max_length=50, blank=True) # 'keyword', 'cluster', 'task'
related_object_id = models.IntegerField(null=True, blank=True)
metadata = models.JSONField(default=dict)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = 'igny8_credit_usage_logs'
ordering = ['-created_at']
indexes = [
models.Index(fields=['account', 'operation_type']),
models.Index(fields=['account', 'created_at']),
models.Index(fields=['account', 'operation_type', 'created_at']),
]
def __str__(self):
account = getattr(self, 'account', None)
return f"{self.get_operation_type_display()} - {self.credits_used} credits - {account.name if account else 'No Account'}"

View File

@@ -0,0 +1,65 @@
"""
Serializers for Billing Models
"""
from rest_framework import serializers
from .models import CreditTransaction, CreditUsageLog
from igny8_core.auth.models import Account
class CreditTransactionSerializer(serializers.ModelSerializer):
transaction_type_display = serializers.CharField(source='get_transaction_type_display', read_only=True)
class Meta:
model = CreditTransaction
fields = [
'id', 'transaction_type', 'transaction_type_display', 'amount',
'balance_after', 'description', 'metadata', 'created_at'
]
read_only_fields = ['created_at', 'account']
class CreditUsageLogSerializer(serializers.ModelSerializer):
operation_type_display = serializers.CharField(source='get_operation_type_display', read_only=True)
class Meta:
model = CreditUsageLog
fields = [
'id', 'operation_type', 'operation_type_display', 'credits_used',
'cost_usd', 'model_used', 'tokens_input', 'tokens_output',
'related_object_type', 'related_object_id', 'metadata', 'created_at'
]
read_only_fields = ['created_at', 'account']
class CreditBalanceSerializer(serializers.Serializer):
"""Serializer for credit balance response"""
credits = serializers.IntegerField()
plan_credits_per_month = serializers.IntegerField()
credits_used_this_month = serializers.IntegerField()
credits_remaining = serializers.IntegerField()
class UsageSummarySerializer(serializers.Serializer):
"""Serializer for usage summary response"""
period = serializers.DictField()
total_credits_used = serializers.IntegerField()
total_cost_usd = serializers.DecimalField(max_digits=10, decimal_places=2)
by_operation = serializers.DictField()
by_model = serializers.DictField()
class LimitCardSerializer(serializers.Serializer):
"""Serializer for individual limit card"""
title = serializers.CharField()
limit = serializers.IntegerField()
used = serializers.IntegerField()
available = serializers.IntegerField()
unit = serializers.CharField()
category = serializers.CharField()
percentage = serializers.FloatField()
class UsageLimitsSerializer(serializers.Serializer):
"""Serializer for usage limits response"""
limits = LimitCardSerializer(many=True)

View File

@@ -0,0 +1,161 @@
"""
Credit Service for managing credit transactions and deductions
"""
from django.db import transaction
from django.utils import timezone
from .models import CreditTransaction, CreditUsageLog
from .constants import CREDIT_COSTS
from .exceptions import InsufficientCreditsError, CreditCalculationError
from igny8_core.auth.models import Account
class CreditService:
"""Service for managing credits"""
@staticmethod
def check_credits(account, required_credits):
"""
Check if account has enough credits.
Args:
account: Account instance
required_credits: Number of credits required
Raises:
InsufficientCreditsError: If account doesn't have enough credits
"""
if account.credits < required_credits:
raise InsufficientCreditsError(
f"Insufficient credits. Required: {required_credits}, Available: {account.credits}"
)
@staticmethod
@transaction.atomic
def deduct_credits(account, amount, operation_type, description, metadata=None, cost_usd=None, model_used=None, tokens_input=None, tokens_output=None, related_object_type=None, related_object_id=None):
"""
Deduct credits and log transaction.
Args:
account: Account instance
amount: Number of credits to deduct
operation_type: Type of operation (from CreditUsageLog.OPERATION_TYPE_CHOICES)
description: Description of the transaction
metadata: Optional metadata dict
cost_usd: Optional cost in USD
model_used: Optional AI model used
tokens_input: Optional input tokens
tokens_output: Optional output tokens
related_object_type: Optional related object type
related_object_id: Optional related object ID
Returns:
int: New credit balance
"""
# Check sufficient credits
CreditService.check_credits(account, amount)
# Deduct from account.credits
account.credits -= amount
account.save(update_fields=['credits'])
# Create CreditTransaction
CreditTransaction.objects.create(
account=account,
transaction_type='deduction',
amount=-amount, # Negative for deduction
balance_after=account.credits,
description=description,
metadata=metadata or {}
)
# Create CreditUsageLog
CreditUsageLog.objects.create(
account=account,
operation_type=operation_type,
credits_used=amount,
cost_usd=cost_usd,
model_used=model_used or '',
tokens_input=tokens_input,
tokens_output=tokens_output,
related_object_type=related_object_type or '',
related_object_id=related_object_id,
metadata=metadata or {}
)
return account.credits
@staticmethod
@transaction.atomic
def add_credits(account, amount, transaction_type, description, metadata=None):
"""
Add credits (purchase, subscription, etc.).
Args:
account: Account instance
amount: Number of credits to add
transaction_type: Type of transaction (from CreditTransaction.TRANSACTION_TYPE_CHOICES)
description: Description of the transaction
metadata: Optional metadata dict
Returns:
int: New credit balance
"""
# Add to account.credits
account.credits += amount
account.save(update_fields=['credits'])
# Create CreditTransaction
CreditTransaction.objects.create(
account=account,
transaction_type=transaction_type,
amount=amount, # Positive for addition
balance_after=account.credits,
description=description,
metadata=metadata or {}
)
return account.credits
@staticmethod
def calculate_credits_for_operation(operation_type, **kwargs):
"""
Calculate credits needed for an operation.
Args:
operation_type: Type of operation
**kwargs: Operation-specific parameters
Returns:
int: Number of credits required
Raises:
CreditCalculationError: If calculation fails
"""
if operation_type not in CREDIT_COSTS:
raise CreditCalculationError(f"Unknown operation type: {operation_type}")
cost_config = CREDIT_COSTS[operation_type]
if operation_type == 'clustering':
# 1 credit per 30 keywords
keyword_count = kwargs.get('keyword_count', 0)
credits = max(1, int(keyword_count * cost_config['per_keyword']))
return credits
elif operation_type == 'ideas':
# 1 credit per idea
idea_count = kwargs.get('idea_count', 1)
return cost_config['base'] * idea_count
elif operation_type == 'content':
# 3 credits per content piece
content_count = kwargs.get('content_count', 1)
return cost_config['base'] * content_count
elif operation_type == 'images':
# 1 credit per image
image_count = kwargs.get('image_count', 1)
return cost_config['base'] * image_count
elif operation_type == 'reparse':
# 1 credit per reparse
return cost_config['base']
return cost_config['base']

View File

@@ -0,0 +1,16 @@
"""
URL patterns for billing module
"""
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import CreditBalanceViewSet, CreditUsageViewSet, CreditTransactionViewSet
router = DefaultRouter()
router.register(r'credits/balance', CreditBalanceViewSet, basename='credit-balance')
router.register(r'credits/usage', CreditUsageViewSet, basename='credit-usage')
router.register(r'credits/transactions', CreditTransactionViewSet, basename='credit-transactions')
urlpatterns = [
path('', include(router.urls)),
]

View File

@@ -0,0 +1,465 @@
"""
ViewSets for Billing API
"""
from rest_framework import viewsets, status, permissions
from rest_framework.decorators import action
from rest_framework.response import Response
from django.db.models import Sum, Count, Q
from django.utils import timezone
from datetime import timedelta
from decimal import Decimal
from igny8_core.api.base import AccountModelViewSet
from igny8_core.api.pagination import CustomPageNumberPagination
from igny8_core.api.authentication import JWTAuthentication, CSRFExemptSessionAuthentication
from .models import CreditTransaction, CreditUsageLog
from .serializers import (
CreditTransactionSerializer, CreditUsageLogSerializer,
CreditBalanceSerializer, UsageSummarySerializer, UsageLimitsSerializer
)
from .services import CreditService
from .exceptions import InsufficientCreditsError
class CreditBalanceViewSet(viewsets.ViewSet):
"""
ViewSet for credit balance operations
"""
permission_classes = [permissions.IsAuthenticated]
authentication_classes = [JWTAuthentication, CSRFExemptSessionAuthentication]
@action(detail=False, methods=['get'])
def balance(self, request):
"""Get current credit balance and usage"""
account = getattr(request, 'account', None)
if not account:
user = getattr(request, 'user', None)
if user:
account = getattr(user, 'account', None)
if not account:
return Response(
{'error': 'Account not found'},
status=status.HTTP_400_BAD_REQUEST
)
# Get plan credits per month
plan_credits_per_month = account.plan.credits_per_month if account.plan else 0
# Calculate credits used this month
now = timezone.now()
start_of_month = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
credits_used_this_month = CreditUsageLog.objects.filter(
account=account,
created_at__gte=start_of_month
).aggregate(total=Sum('credits_used'))['total'] or 0
credits_remaining = account.credits
data = {
'credits': account.credits,
'plan_credits_per_month': plan_credits_per_month,
'credits_used_this_month': credits_used_this_month,
'credits_remaining': credits_remaining,
}
serializer = CreditBalanceSerializer(data)
return Response(serializer.data)
class CreditUsageViewSet(viewsets.ReadOnlyModelViewSet):
"""
ViewSet for credit usage logs
"""
queryset = CreditUsageLog.objects.all()
serializer_class = CreditUsageLogSerializer
permission_classes = [permissions.IsAuthenticated]
authentication_classes = [JWTAuthentication, CSRFExemptSessionAuthentication]
pagination_class = CustomPageNumberPagination
filter_backends = []
def get_queryset(self):
"""Get usage logs for current account"""
account = getattr(self.request, 'account', None)
if not account:
user = getattr(self.request, 'user', None)
if user:
account = getattr(user, 'account', None)
if not account:
return CreditUsageLog.objects.none()
queryset = CreditUsageLog.objects.filter(account=account)
# Filter by operation type
operation_type = self.request.query_params.get('operation_type')
if operation_type:
queryset = queryset.filter(operation_type=operation_type)
# Filter by date range
start_date = self.request.query_params.get('start_date')
end_date = self.request.query_params.get('end_date')
if start_date:
queryset = queryset.filter(created_at__gte=start_date)
if end_date:
queryset = queryset.filter(created_at__lte=end_date)
return queryset.order_by('-created_at')
@action(detail=False, methods=['get'])
def summary(self, request):
"""Get usage summary for date range"""
account = getattr(request, 'account', None)
if not account:
user = getattr(request, 'user', None)
if user:
account = getattr(user, 'account', None)
if not account:
return Response(
{'error': 'Account not found'},
status=status.HTTP_400_BAD_REQUEST
)
# Get date range from query params
start_date = request.query_params.get('start_date')
end_date = request.query_params.get('end_date')
# Default to current month if not provided
now = timezone.now()
if not start_date:
start_date = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
else:
from django.utils.dateparse import parse_datetime
start_date = parse_datetime(start_date) or start_date
if not end_date:
end_date = now
else:
from django.utils.dateparse import parse_datetime
end_date = parse_datetime(end_date) or end_date
# Get usage logs in date range
usage_logs = CreditUsageLog.objects.filter(
account=account,
created_at__gte=start_date,
created_at__lte=end_date
)
# Calculate totals
total_credits_used = usage_logs.aggregate(total=Sum('credits_used'))['total'] or 0
total_cost_usd = usage_logs.aggregate(total=Sum('cost_usd'))['total'] or Decimal('0.00')
# Group by operation type
by_operation = {}
for operation_type, _ in CreditUsageLog.OPERATION_TYPE_CHOICES:
operation_logs = usage_logs.filter(operation_type=operation_type)
credits = operation_logs.aggregate(total=Sum('credits_used'))['total'] or 0
cost = operation_logs.aggregate(total=Sum('cost_usd'))['total'] or Decimal('0.00')
count = operation_logs.count()
if credits > 0 or count > 0:
by_operation[operation_type] = {
'credits': credits,
'cost': float(cost),
'count': count
}
# Group by model
by_model = {}
model_stats = usage_logs.values('model_used').annotate(
credits=Sum('credits_used'),
cost=Sum('cost_usd'),
count=Count('id')
).filter(model_used__isnull=False).exclude(model_used='')
for stat in model_stats:
model = stat['model_used']
by_model[model] = {
'credits': stat['credits'] or 0,
'cost': float(stat['cost'] or Decimal('0.00'))
}
data = {
'period': {
'start': start_date.isoformat() if hasattr(start_date, 'isoformat') else str(start_date),
'end': end_date.isoformat() if hasattr(end_date, 'isoformat') else str(end_date),
},
'total_credits_used': total_credits_used,
'total_cost_usd': float(total_cost_usd),
'by_operation': by_operation,
'by_model': by_model,
}
serializer = UsageSummarySerializer(data)
return Response(serializer.data)
@action(detail=False, methods=['get'], url_path='limits', url_name='limits')
def limits(self, request):
"""Get plan limits and current usage statistics"""
# Try multiple ways to get account
account = getattr(request, 'account', None)
if not account:
user = getattr(request, 'user', None)
if user and user.is_authenticated:
# Try to get account from user - refresh from DB to ensure we have latest
try:
from igny8_core.auth.models import User as UserModel
# Refresh user from DB to get account relationship
user = UserModel.objects.select_related('account', 'account__plan').get(id=user.id)
account = user.account
# Also set it on request for future use
request.account = account
except (AttributeError, UserModel.DoesNotExist, Exception) as e:
account = None
# Debug logging
import logging
logger = logging.getLogger(__name__)
logger.info(f'Limits endpoint - User: {getattr(request, "user", None)}, Account: {account}, Account has plan: {account.plan if account else False}')
if not account:
logger.warning(f'No account found in limits endpoint')
# Return empty limits instead of error - frontend will show "no data" message
return Response({'limits': []})
plan = account.plan
if not plan:
# Return empty limits instead of error - allows frontend to show "no plan" message
return Response({'limits': []})
# Import models
from igny8_core.modules.planner.models import Keywords, Clusters, ContentIdeas
from igny8_core.modules.writer.models import Tasks, Images
from igny8_core.auth.models import User, Site
# Get current month boundaries
now = timezone.now()
start_of_month = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
start_of_day = now.replace(hour=0, minute=0, second=0, microsecond=0)
# Calculate usage statistics
limits_data = []
# Planner Limits
keywords_count = Keywords.objects.filter(account=account).count()
clusters_count = Clusters.objects.filter(account=account).count()
content_ideas_count = ContentIdeas.objects.filter(account=account).count()
clusters_today = Clusters.objects.filter(account=account, created_at__gte=start_of_day).count()
limits_data.extend([
{
'title': 'Keywords',
'limit': plan.max_keywords or 0,
'used': keywords_count,
'available': max(0, (plan.max_keywords or 0) - keywords_count),
'unit': 'keywords',
'category': 'planner',
'percentage': (keywords_count / (plan.max_keywords or 1)) * 100 if plan.max_keywords else 0
},
{
'title': 'Clusters',
'limit': plan.max_clusters or 0,
'used': clusters_count,
'available': max(0, (plan.max_clusters or 0) - clusters_count),
'unit': 'clusters',
'category': 'planner',
'percentage': (clusters_count / (plan.max_clusters or 1)) * 100 if plan.max_clusters else 0
},
{
'title': 'Content Ideas',
'limit': plan.max_content_ideas or 0,
'used': content_ideas_count,
'available': max(0, (plan.max_content_ideas or 0) - content_ideas_count),
'unit': 'ideas',
'category': 'planner',
'percentage': (content_ideas_count / (plan.max_content_ideas or 1)) * 100 if plan.max_content_ideas else 0
},
{
'title': 'Daily Cluster Limit',
'limit': plan.daily_cluster_limit or 0,
'used': clusters_today,
'available': max(0, (plan.daily_cluster_limit or 0) - clusters_today),
'unit': 'per day',
'category': 'planner',
'percentage': (clusters_today / (plan.daily_cluster_limit or 1)) * 100 if plan.daily_cluster_limit else 0
},
])
# Writer Limits
tasks_today = Tasks.objects.filter(account=account, created_at__gte=start_of_day).count()
tasks_month = Tasks.objects.filter(account=account, created_at__gte=start_of_month)
word_count_month = tasks_month.aggregate(total=Sum('word_count'))['total'] or 0
limits_data.extend([
{
'title': 'Monthly Word Count',
'limit': plan.monthly_word_count_limit or 0,
'used': word_count_month,
'available': max(0, (plan.monthly_word_count_limit or 0) - word_count_month),
'unit': 'words',
'category': 'writer',
'percentage': (word_count_month / (plan.monthly_word_count_limit or 1)) * 100 if plan.monthly_word_count_limit else 0
},
{
'title': 'Daily Content Tasks',
'limit': plan.daily_content_tasks or 0,
'used': tasks_today,
'available': max(0, (plan.daily_content_tasks or 0) - tasks_today),
'unit': 'per day',
'category': 'writer',
'percentage': (tasks_today / (plan.daily_content_tasks or 1)) * 100 if plan.daily_content_tasks else 0
},
])
# Image Limits
images_month = Images.objects.filter(account=account, created_at__gte=start_of_month).count()
images_today = Images.objects.filter(account=account, created_at__gte=start_of_day).count()
limits_data.extend([
{
'title': 'Monthly Images',
'limit': plan.monthly_image_count or 0,
'used': images_month,
'available': max(0, (plan.monthly_image_count or 0) - images_month),
'unit': 'images',
'category': 'images',
'percentage': (images_month / (plan.monthly_image_count or 1)) * 100 if plan.monthly_image_count else 0
},
{
'title': 'Daily Image Generation',
'limit': plan.daily_image_generation_limit or 0,
'used': images_today,
'available': max(0, (plan.daily_image_generation_limit or 0) - images_today),
'unit': 'per day',
'category': 'images',
'percentage': (images_today / (plan.daily_image_generation_limit or 1)) * 100 if plan.daily_image_generation_limit else 0
},
])
# AI Credits
credits_used_month = CreditUsageLog.objects.filter(
account=account,
created_at__gte=start_of_month
).aggregate(total=Sum('credits_used'))['total'] or 0
# Get credits by operation type
cluster_credits = CreditUsageLog.objects.filter(
account=account,
operation_type='clustering',
created_at__gte=start_of_month
).aggregate(total=Sum('credits_used'))['total'] or 0
content_credits = CreditUsageLog.objects.filter(
account=account,
operation_type='content',
created_at__gte=start_of_month
).aggregate(total=Sum('credits_used'))['total'] or 0
image_credits = CreditUsageLog.objects.filter(
account=account,
operation_type='image',
created_at__gte=start_of_month
).aggregate(total=Sum('credits_used'))['total'] or 0
plan_credits = plan.monthly_ai_credit_limit or plan.credits_per_month or 0
limits_data.extend([
{
'title': 'Monthly AI Credits',
'limit': plan_credits,
'used': credits_used_month,
'available': max(0, plan_credits - credits_used_month),
'unit': 'credits',
'category': 'ai',
'percentage': (credits_used_month / plan_credits * 100) if plan_credits else 0
},
{
'title': 'Content AI Credits',
'limit': plan.monthly_content_ai_credits or 0,
'used': content_credits,
'available': max(0, (plan.monthly_content_ai_credits or 0) - content_credits),
'unit': 'credits',
'category': 'ai',
'percentage': (content_credits / (plan.monthly_content_ai_credits or 1)) * 100 if plan.monthly_content_ai_credits else 0
},
{
'title': 'Image AI Credits',
'limit': plan.monthly_image_ai_credits or 0,
'used': image_credits,
'available': max(0, (plan.monthly_image_ai_credits or 0) - image_credits),
'unit': 'credits',
'category': 'ai',
'percentage': (image_credits / (plan.monthly_image_ai_credits or 1)) * 100 if plan.monthly_image_ai_credits else 0
},
{
'title': 'Cluster AI Credits',
'limit': plan.monthly_cluster_ai_credits or 0,
'used': cluster_credits,
'available': max(0, (plan.monthly_cluster_ai_credits or 0) - cluster_credits),
'unit': 'credits',
'category': 'ai',
'percentage': (cluster_credits / (plan.monthly_cluster_ai_credits or 1)) * 100 if plan.monthly_cluster_ai_credits else 0
},
])
# General Limits
users_count = User.objects.filter(account=account).count()
sites_count = Site.objects.filter(account=account).count()
limits_data.extend([
{
'title': 'Users',
'limit': plan.max_users or 0,
'used': users_count,
'available': max(0, (plan.max_users or 0) - users_count),
'unit': 'users',
'category': 'general',
'percentage': (users_count / (plan.max_users or 1)) * 100 if plan.max_users else 0
},
{
'title': 'Sites',
'limit': plan.max_sites or 0,
'used': sites_count,
'available': max(0, (plan.max_sites or 0) - sites_count),
'unit': 'sites',
'category': 'general',
'percentage': (sites_count / (plan.max_sites or 1)) * 100 if plan.max_sites else 0
},
])
# Return data directly - serializer validation not needed for read-only endpoint
return Response({'limits': limits_data})
class CreditTransactionViewSet(viewsets.ReadOnlyModelViewSet):
"""
ViewSet for credit transaction history
"""
queryset = CreditTransaction.objects.all()
serializer_class = CreditTransactionSerializer
permission_classes = [permissions.IsAuthenticated]
authentication_classes = [JWTAuthentication, CSRFExemptSessionAuthentication]
pagination_class = CustomPageNumberPagination
def get_queryset(self):
"""Get transactions for current account"""
account = getattr(self.request, 'account', None)
if not account:
user = getattr(self.request, 'user', None)
if user:
account = getattr(user, 'account', None)
if not account:
return CreditTransaction.objects.none()
queryset = CreditTransaction.objects.filter(account=account)
# Filter by transaction type
transaction_type = self.request.query_params.get('transaction_type')
if transaction_type:
queryset = queryset.filter(transaction_type=transaction_type)
return queryset.order_by('-created_at')