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.
 
 
 
 
 
 

106 lines
3.1 KiB

  1. import random
  2. from datetime import timedelta
  3. from django.db import models, IntegrityError
  4. from django.contrib.auth.models import User
  5. from django.utils.translation import ugettext as _
  6. from django.utils import timezone
  7. from django_lcore.core import (
  8. LcoreUserProfileMethods,
  9. setup_sync_hooks,
  10. VPN_AUTH_STORAGE,
  11. )
  12. from payments.models import BaseSubUser
  13. prng = random.SystemRandom()
  14. def random_gift_code():
  15. charset = "123456789ABCDEFGHIJKLMNPQRSTUVWXYZ"
  16. return "".join([prng.choice(charset) for n in range(10)])
  17. class VPNUser(models.Model, BaseSubUser, LcoreUserProfileMethods):
  18. class Meta:
  19. verbose_name = _("VPN User")
  20. verbose_name_plural = _("VPN Users")
  21. user = models.OneToOneField(User, on_delete=models.CASCADE)
  22. notes = models.TextField(blank=True)
  23. expiration = models.DateTimeField(blank=True, null=True)
  24. last_expiry_notice = models.DateTimeField(blank=True, null=True)
  25. last_vpn_auth_fail_notice = models.DateTimeField(blank=True, null=True)
  26. notify_expiration = models.BooleanField(default=True)
  27. notify_vpn_auth_fail = models.BooleanField(default=True)
  28. trial_periods_given = models.IntegerField(default=0)
  29. last_vpn_auth = models.DateTimeField(blank=True, null=True)
  30. last_core_sync = models.DateTimeField(blank=True, null=True)
  31. referrer = models.ForeignKey(
  32. User, blank=True, null=True, on_delete=models.SET_NULL, related_name="referrals"
  33. )
  34. referrer_used = models.BooleanField(default=False)
  35. campaign = models.CharField(blank=True, null=True, max_length=64)
  36. def clear_fields(self):
  37. self.expiration = None
  38. self.last_expiry_notice = None
  39. self.last_vpn_auth = None
  40. self.last_core_sync = None
  41. self.referrer = None
  42. self.referrer_used = False
  43. self.campaign = None
  44. def on_payment_confirmed(self, payment):
  45. if self.referrer and not self.referrer_used:
  46. self.referrer.vpnuser.add_paid_months(
  47. 1,
  48. "referrer",
  49. f"rewarded for {self.user.username} (payment #{payment.id})",
  50. )
  51. self.referrer.vpnuser.save()
  52. self.referrer_used = True
  53. self.save()
  54. @property
  55. def subscr_is_failing(self):
  56. margin = timedelta(hours=24)
  57. if self.expiration < timezone.now() + margin:
  58. return True
  59. return False
  60. def lcore_sync(self):
  61. if VPN_AUTH_STORAGE == "inst":
  62. return
  63. from lambdainst.tasks import push_user
  64. push_user.delay(user_id=self.user.id)
  65. def notify_payment(self, payment):
  66. return
  67. def notify_subscription(self, sub):
  68. return
  69. def notify_refund(self, payment):
  70. return
  71. def __str__(self):
  72. return self.user.username
  73. setup_sync_hooks(User, VPNUser)
  74. # from django.db.models.signals import post_save
  75. # from django.dispatch import receiver
  76. # @receiver(post_save, sender=User)
  77. # def create_vpnuser(sender, instance, created, **kwargs):
  78. # if created:
  79. # try:
  80. # VPNUser.objects.create(user=instance)
  81. # except IntegrityError:
  82. # pass