Creditsupdates in fotoer wdigets adn hoemapge and singe site settigns page #Run MIgration 0033 #MAJOR
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
# Generated by Django 5.2.10 on 2026-01-12 06:45
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('billing', '0032_historicalaimodelconfig_landscape_size_and_more'),
|
||||
('igny8_core_auth', '0020_fix_historical_account'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='creditusagelog',
|
||||
name='site',
|
||||
field=models.ForeignKey(blank=True, help_text='Site where the operation was performed', null=True, on_delete=django.db.models.deletion.CASCADE, to='igny8_core_auth.site'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='creditusagelog',
|
||||
index=models.Index(fields=['site', 'created_at'], name='igny8_credi_site_id_b628ed_idx'),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='creditusagelog',
|
||||
index=models.Index(fields=['account', 'site', 'created_at'], name='igny8_credi_tenant__ca31e1_idx'),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,90 @@
|
||||
# Generated by Django 5.2.10 on 2026-01-12 07:02
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def backfill_site_id(apps, schema_editor):
|
||||
"""
|
||||
Backfill site_id for existing CreditUsageLog records by looking up
|
||||
the related objects (Content, Images, ContentIdeas, Clusters).
|
||||
"""
|
||||
CreditUsageLog = apps.get_model('billing', 'CreditUsageLog')
|
||||
Content = apps.get_model('writer', 'Content')
|
||||
Images = apps.get_model('writer', 'Images')
|
||||
ContentIdeas = apps.get_model('planner', 'ContentIdeas')
|
||||
Clusters = apps.get_model('planner', 'Clusters')
|
||||
|
||||
# Backfill for content records
|
||||
content_logs = CreditUsageLog.objects.filter(
|
||||
site__isnull=True,
|
||||
related_object_type='content',
|
||||
related_object_id__isnull=False
|
||||
)
|
||||
for log in content_logs:
|
||||
try:
|
||||
content = Content.objects.get(id=log.related_object_id)
|
||||
log.site_id = content.site_id
|
||||
log.save(update_fields=['site'])
|
||||
except Content.DoesNotExist:
|
||||
pass
|
||||
|
||||
# Backfill for image records
|
||||
image_logs = CreditUsageLog.objects.filter(
|
||||
site__isnull=True,
|
||||
related_object_type='image',
|
||||
related_object_id__isnull=False
|
||||
)
|
||||
for log in image_logs:
|
||||
try:
|
||||
image = Images.objects.get(id=log.related_object_id)
|
||||
log.site_id = image.site_id
|
||||
log.save(update_fields=['site'])
|
||||
except Images.DoesNotExist:
|
||||
pass
|
||||
|
||||
# Backfill for content idea records
|
||||
idea_logs = CreditUsageLog.objects.filter(
|
||||
site__isnull=True,
|
||||
related_object_type='content_idea',
|
||||
related_object_id__isnull=False
|
||||
)
|
||||
for log in idea_logs:
|
||||
try:
|
||||
idea = ContentIdeas.objects.get(id=log.related_object_id)
|
||||
log.site_id = idea.site_id
|
||||
log.save(update_fields=['site'])
|
||||
except ContentIdeas.DoesNotExist:
|
||||
pass
|
||||
|
||||
# Backfill for cluster records
|
||||
cluster_logs = CreditUsageLog.objects.filter(
|
||||
site__isnull=True,
|
||||
related_object_type='cluster',
|
||||
related_object_id__isnull=False
|
||||
)
|
||||
for log in cluster_logs:
|
||||
try:
|
||||
cluster = Clusters.objects.get(id=log.related_object_id)
|
||||
log.site_id = cluster.site_id
|
||||
log.save(update_fields=['site'])
|
||||
except Clusters.DoesNotExist:
|
||||
pass
|
||||
|
||||
|
||||
def reverse_backfill(apps, schema_editor):
|
||||
"""Reverse migration - set site_id back to NULL for backfilled records."""
|
||||
# We don't reverse this as we can't distinguish which were backfilled
|
||||
pass
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('billing', '0033_add_site_to_credit_usage_log'),
|
||||
('writer', '0016_images_unique_position_constraint'),
|
||||
('planner', '0008_soft_delete'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(backfill_site_id, reverse_backfill),
|
||||
]
|
||||
@@ -165,6 +165,14 @@ class CreditUsageViewSet(AccountModelViewSet):
|
||||
created_at__gte=start_date,
|
||||
created_at__lte=end_date
|
||||
)
|
||||
|
||||
# Filter by site if provided
|
||||
site_id = request.query_params.get('site_id')
|
||||
if site_id:
|
||||
try:
|
||||
usage_logs = usage_logs.filter(site_id=int(site_id))
|
||||
except (ValueError, TypeError):
|
||||
pass
|
||||
|
||||
# Calculate totals
|
||||
total_credits_used = usage_logs.aggregate(total=Sum('credits_used'))['total'] or 0
|
||||
|
||||
Reference in New Issue
Block a user