Initial commit
This commit is contained in:
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
|
||||
from accounts.models import PremiumEntitlement
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Grant permanent Lifetime Premium access to a user."
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument("identifier", help="Username or email address")
|
||||
parser.add_argument(
|
||||
"--source",
|
||||
choices=[choice[0] for choice in PremiumEntitlement.SOURCE_CHOICES],
|
||||
default=PremiumEntitlement.SOURCE_ADMIN,
|
||||
)
|
||||
parser.add_argument("--order-reference", default="")
|
||||
parser.add_argument("--notes", default="")
|
||||
|
||||
def handle(self, *args, **options):
|
||||
identifier = options["identifier"].strip()
|
||||
user_model = get_user_model()
|
||||
user = user_model.objects.filter(username=identifier).first()
|
||||
if user is None:
|
||||
user = user_model.objects.filter(email__iexact=identifier).first()
|
||||
if user is None:
|
||||
raise CommandError(f"No user found for {identifier!r}.")
|
||||
|
||||
entitlement, created = PremiumEntitlement.objects.get_or_create(
|
||||
user=user,
|
||||
defaults={
|
||||
"source": options["source"],
|
||||
"order_reference": options["order_reference"],
|
||||
"notes": options["notes"],
|
||||
},
|
||||
)
|
||||
if not created:
|
||||
entitlement.source = options["source"]
|
||||
entitlement.order_reference = (
|
||||
options["order_reference"] or entitlement.order_reference
|
||||
)
|
||||
entitlement.notes = options["notes"] or entitlement.notes
|
||||
entitlement.revoked_at = None
|
||||
entitlement.save(
|
||||
update_fields=["source", "order_reference", "notes", "revoked_at"]
|
||||
)
|
||||
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(f"Lifetime Premium is active for {user}.")
|
||||
)
|
||||
Reference in New Issue
Block a user