many fixes of backeend and fronteend

This commit is contained in:
IGNY8 VPS (Salman)
2025-12-06 16:41:35 +00:00
parent a0eee0df42
commit bfb07947ea
19 changed files with 638 additions and 19 deletions

View File

@@ -56,6 +56,11 @@ class AccountAdmin(AccountAdminMixin, admin.ModelAdmin):
pass
return qs.none()
def has_delete_permission(self, request, obj=None):
if obj and getattr(obj, 'slug', '') == 'aws-admin':
return False
return super().has_delete_permission(request, obj)
@admin.register(Subscription)
class SubscriptionAdmin(AccountAdminMixin, admin.ModelAdmin):

View File

@@ -0,0 +1,42 @@
from django.core.management.base import BaseCommand
from django.utils import timezone
from igny8_core.auth.models import Account, Site, Sector
from igny8_core.business.planning.models import Clusters, Keywords, ContentIdeas
from igny8_core.business.content.models import Tasks, Content, Images
class Command(BaseCommand):
help = "Permanently delete soft-deleted records whose retention window has expired."
def handle(self, *args, **options):
now = timezone.now()
total_deleted = 0
models = [
Account,
Site,
Sector,
Clusters,
Keywords,
ContentIdeas,
Tasks,
Content,
Images,
]
for model in models:
qs = model.all_objects.filter(is_deleted=True, restore_until__lt=now)
if model is Account:
qs = qs.exclude(slug='aws-admin')
count = qs.count()
if count:
qs.delete()
total_deleted += count
self.stdout.write(self.style.SUCCESS(f"Purged {count} {model.__name__} record(s)."))
if total_deleted == 0:
self.stdout.write("No expired soft-deleted records to purge.")
else:
self.stdout.write(self.style.SUCCESS(f"Total purged: {total_deleted}"))

View File

@@ -0,0 +1,93 @@
from django.db import migrations, models
import django.db.models.deletion
from django.core.validators import MinValueValidator, MaxValueValidator
class Migration(migrations.Migration):
dependencies = [
('igny8_core_auth', '0005_account_owner_nullable'),
]
operations = [
migrations.AddField(
model_name='account',
name='delete_reason',
field=models.CharField(blank=True, max_length=255, null=True),
),
migrations.AddField(
model_name='account',
name='deleted_at',
field=models.DateTimeField(blank=True, db_index=True, null=True),
),
migrations.AddField(
model_name='account',
name='deleted_by',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='igny8_core_auth.user'),
),
migrations.AddField(
model_name='account',
name='deletion_retention_days',
field=models.PositiveIntegerField(default=14, help_text='Retention window (days) before soft-deleted items are purged', validators=[MinValueValidator(1), MaxValueValidator(365)]),
),
migrations.AddField(
model_name='account',
name='is_deleted',
field=models.BooleanField(db_index=True, default=False),
),
migrations.AddField(
model_name='account',
name='restore_until',
field=models.DateTimeField(blank=True, db_index=True, null=True),
),
migrations.AddField(
model_name='sector',
name='delete_reason',
field=models.CharField(blank=True, max_length=255, null=True),
),
migrations.AddField(
model_name='sector',
name='deleted_at',
field=models.DateTimeField(blank=True, db_index=True, null=True),
),
migrations.AddField(
model_name='sector',
name='deleted_by',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='igny8_core_auth.user'),
),
migrations.AddField(
model_name='sector',
name='is_deleted',
field=models.BooleanField(db_index=True, default=False),
),
migrations.AddField(
model_name='sector',
name='restore_until',
field=models.DateTimeField(blank=True, db_index=True, null=True),
),
migrations.AddField(
model_name='site',
name='delete_reason',
field=models.CharField(blank=True, max_length=255, null=True),
),
migrations.AddField(
model_name='site',
name='deleted_at',
field=models.DateTimeField(blank=True, db_index=True, null=True),
),
migrations.AddField(
model_name='site',
name='deleted_by',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='igny8_core_auth.user'),
),
migrations.AddField(
model_name='site',
name='is_deleted',
field=models.BooleanField(db_index=True, default=False),
),
migrations.AddField(
model_name='site',
name='restore_until',
field=models.DateTimeField(blank=True, db_index=True, null=True),
),
]

View File

@@ -5,6 +5,7 @@ from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils.translation import gettext_lazy as _
from django.core.validators import MinValueValidator, MaxValueValidator
from igny8_core.common.soft_delete import SoftDeletableModel, SoftDeleteManager
class AccountBaseModel(models.Model):
@@ -52,7 +53,7 @@ class SiteSectorBaseModel(AccountBaseModel):
super().save(*args, **kwargs)
class Account(models.Model):
class Account(SoftDeletableModel):
"""
Account/Organization model for multi-account support.
"""
@@ -76,6 +77,11 @@ class Account(models.Model):
plan = models.ForeignKey('igny8_core_auth.Plan', on_delete=models.PROTECT, related_name='accounts')
credits = models.IntegerField(default=0, validators=[MinValueValidator(0)])
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='trial')
deletion_retention_days = models.PositiveIntegerField(
default=14,
validators=[MinValueValidator(1), MaxValueValidator(365)],
help_text="Retention window (days) before soft-deleted items are purged",
)
# Billing information
billing_email = models.EmailField(blank=True, null=True, help_text="Email for billing notifications")
@@ -99,6 +105,9 @@ class Account(models.Model):
models.Index(fields=['status']),
]
objects = SoftDeleteManager()
all_objects = models.Manager()
def __str__(self):
return self.name
@@ -107,6 +116,15 @@ class Account(models.Model):
# System accounts bypass all filtering restrictions
return self.slug in ['aws-admin', 'default-account', 'default']
def soft_delete(self, user=None, reason=None, retention_days=None):
if self.is_system_account():
from django.core.exceptions import PermissionDenied
raise PermissionDenied("System account cannot be deleted.")
return super().soft_delete(user=user, reason=reason, retention_days=retention_days)
def delete(self, using=None, keep_parents=False):
return self.soft_delete()
class Plan(models.Model):
"""
@@ -202,7 +220,7 @@ class Subscription(models.Model):
class Site(AccountBaseModel):
class Site(SoftDeletableModel, AccountBaseModel):
"""
Site model - Each account can have multiple sites based on their plan.
Each site belongs to ONE industry and can have 1-5 sectors from that industry.
@@ -274,6 +292,9 @@ class Site(AccountBaseModel):
blank=True,
help_text="SEO metadata: meta tags, Open Graph, Schema.org"
)
objects = SoftDeleteManager()
all_objects = models.Manager()
class Meta:
db_table = 'igny8_sites'
@@ -409,7 +430,7 @@ class SeedKeyword(models.Model):
return f"{self.keyword} ({self.industry.name} - {self.sector.name})"
class Sector(AccountBaseModel):
class Sector(SoftDeletableModel, AccountBaseModel):
"""
Sector model - Each site can have 1-5 sectors.
Sectors are site-specific instances that reference an IndustrySector template.
@@ -436,6 +457,9 @@ class Sector(AccountBaseModel):
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='active')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
objects = SoftDeleteManager()
all_objects = models.Manager()
class Meta:
db_table = 'igny8_sectors'