44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
from datetime import timedelta
|
|
from django.conf import settings
|
|
from django.utils import timezone
|
|
from inventory.tasks import send_email
|
|
from django.contrib.auth import get_user_model
|
|
from django.core.management.base import BaseCommand
|
|
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = "Deactivates expired user plans"
|
|
|
|
def handle(self, *args, **options):
|
|
users_without_plan = User.objects.filter(
|
|
is_active=True,
|
|
userplan=None,
|
|
dealer__isnull=False,
|
|
date_joined__lte=timezone.now() - timedelta(days=7),
|
|
)
|
|
|
|
count = users_without_plan.count()
|
|
for user in users_without_plan:
|
|
user.is_active = False
|
|
user.save()
|
|
subject = "Your account has been deactivated"
|
|
message = """
|
|
Hello {},\n
|
|
Your account has been deactivated, please contact us at {} if you have any questions.
|
|
|
|
Regards,\n
|
|
Tenhal Team
|
|
""".format(user.dealer.name, settings.DEFAULT_FROM_EMAIL)
|
|
from_email = settings.DEFAULT_FROM_EMAIL
|
|
recipient_list = user.email
|
|
send_email(from_email, recipient_list, subject, message)
|
|
|
|
self.stdout.write(
|
|
self.style.SUCCESS(
|
|
f"Successfully deactivated {count} dealers who created account but dont have userplan"
|
|
)
|
|
)
|