CCrypto VPN public website https://vpn.ccrypto.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

61 lines
2.2 KiB

  1. from django.core.management.base import BaseCommand
  2. from datetime import timedelta
  3. from django.db.models import Q, F
  4. from django.conf import settings
  5. from django.utils import timezone
  6. from django.template.loader import get_template
  7. from django.core.mail import send_mass_mail
  8. from constance import config as site_config
  9. from ccvpn.common import parse_integer_list
  10. from lambdainst.models import VPNUser
  11. ROOT_URL = settings.ROOT_URL
  12. SITE_NAME = settings.TICKETS_SITE_NAME
  13. def get_next_expirations(days=3):
  14. """ Gets users whose subscription will expire in some days """
  15. limit_date = timezone.now() + timedelta(days=days)
  16. users = VPNUser.objects.exclude(user__email__exact='')
  17. users = users.filter(expiration__gt=timezone.now()) # Not expired
  18. users = users.filter(expiration__lt=limit_date) # Expire in a few days
  19. # Make sure we dont send the notice twice
  20. users = users.filter(Q(last_expiry_notice__isnull=True)
  21. | Q(expiration__gt=F('last_expiry_notice')
  22. + timedelta(days=days)))
  23. return users
  24. class Command(BaseCommand):
  25. help = "Notify users near the end of their subscription"
  26. def handle(self, *args, **options):
  27. from_email = settings.DEFAULT_FROM_EMAIL
  28. for v in parse_integer_list(site_config.NOTIFY_DAYS_BEFORE):
  29. emails = []
  30. qs = get_next_expirations(v)
  31. users = list(qs)
  32. for u in users:
  33. # Ignore users with active subscriptions
  34. # They will get notified only if it gets cancelled (payments
  35. # processors will cancel after a few failed payments)
  36. if u.get_subscription():
  37. continue
  38. ctx = dict(site_name=SITE_NAME, user=u.user,
  39. exp=u.expiration, url=ROOT_URL)
  40. text = get_template('lambdainst/mail_expire_soon.txt').render(ctx)
  41. emails.append(("CCVPN Expiration", text, from_email, [u.user.email]))
  42. self.stdout.write("sending -%d days notify to %s ..." % (v, u.user.email))
  43. send_mass_mail(emails)
  44. qs.update(last_expiry_notice=timezone.now())