-
-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added basic "auto" command, can be ran on cron. Next going to add Pub…
…lic API version Signed-off-by: Trey <[email protected]>
- Loading branch information
Showing
4 changed files
with
155 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import uuid | ||
from django.core.management.base import BaseCommand | ||
from backend.service.maintenance.expire.run import expire_and_cleanup_objects | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Runs automation scripts to make sure objects are up to date, expired objects are deleted, etc. | ||
""" | ||
|
||
def handle(self, *args, **kwargs): | ||
self.stdout.write("Running expire + cleanup script...") | ||
self.stdout.write(expire_and_cleanup_objects()) |
42 changes: 42 additions & 0 deletions
42
backend/migrations/0065_remove_invoiceurl_never_expire_passwordsecret_active_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Generated by Django 5.1.1 on 2024-10-02 20:00 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("backend", "0064_remove_invoice_payment_status_invoice_status"), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name="invoiceurl", | ||
name="never_expire", | ||
), | ||
migrations.AddField( | ||
model_name="passwordsecret", | ||
name="active", | ||
field=models.BooleanField(default=True), | ||
), | ||
migrations.AlterField( | ||
model_name="invoice", | ||
name="status", | ||
field=models.CharField(choices=[("draft", "Draft"), ("pending", "Pending"), ("paid", "Paid")], default="draft", max_length=10), | ||
), | ||
migrations.AlterField( | ||
model_name="invoiceurl", | ||
name="expires", | ||
field=models.DateTimeField(blank=True, help_text="When the item will expire", null=True, verbose_name="Expires"), | ||
), | ||
migrations.AlterField( | ||
model_name="passwordsecret", | ||
name="expires", | ||
field=models.DateTimeField(blank=True, help_text="When the item will expire", null=True, verbose_name="Expires"), | ||
), | ||
migrations.AlterField( | ||
model_name="teaminvitation", | ||
name="expires", | ||
field=models.DateTimeField(blank=True, help_text="When the item will expire", null=True, verbose_name="Expires"), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from datetime import timedelta | ||
from typing import Type | ||
|
||
from django.db import models | ||
from django.db.models import QuerySet | ||
|
||
from backend.models import TeamInvitation, InvoiceURL, PasswordSecret | ||
|
||
from django.utils import timezone | ||
|
||
""" | ||
Every model MUST have the field "expires" as: | ||
expires = models.DateTimeField(null=True, blank=True) | ||
""" | ||
|
||
|
||
def expire_and_cleanup_objects() -> str: | ||
deactivated_items: int = 0 | ||
deleted_items: int = 0 | ||
|
||
model_list: list[Type[models.Model]] = [TeamInvitation, InvoiceURL, PasswordSecret] | ||
|
||
now = timezone.now() | ||
|
||
for model in model_list: | ||
# Delete objects that have been inactive and expired for more than 14 days | ||
over_14_days_expired: QuerySet[models.Model] = model.all_objects.filter(expires__lte=now - timedelta(days=14)) | ||
deleted_items += over_14_days_expired.count() | ||
over_14_days_expired.delete() | ||
|
||
# Deactivate expired items that got missed | ||
to_deactivate: QuerySet[models.Model] = model.all_objects.filter(expires__lte=now, active=True) | ||
|
||
deactivated_items += to_deactivate.count() | ||
to_deactivate.update(active=False) | ||
|
||
return f"Deactivated {deactivated_items} objects and deleted {deleted_items} objects." |