You cannot 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
Python

8 years ago
import random
from datetime import timedelta
from django.db import models, IntegrityError
8 years ago
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from django.utils import timezone
from django_lcore.core import (
LcoreUserProfileMethods,
setup_sync_hooks,
VPN_AUTH_STORAGE,
)
8 years ago
from payments.models import BaseSubUser
8 years ago
prng = random.SystemRandom()
def random_gift_code():
charset = "123456789ABCDEFGHIJKLMNPQRSTUVWXYZ"
return "".join([prng.choice(charset) for n in range(10)])
8 years ago
class VPNUser(models.Model, BaseSubUser, LcoreUserProfileMethods):
8 years ago
class Meta:
verbose_name = _("VPN User")
verbose_name_plural = _("VPN Users")
user = models.OneToOneField(User, on_delete=models.CASCADE)
notes = models.TextField(blank=True)
expiration = models.DateTimeField(blank=True, null=True)
last_expiry_notice = models.DateTimeField(blank=True, null=True)
last_vpn_auth_fail_notice = models.DateTimeField(blank=True, null=True)
8 years ago
notify_expiration = models.BooleanField(default=True)
notify_vpn_auth_fail = models.BooleanField(default=True)
8 years ago
trial_periods_given = models.IntegerField(default=0)
last_vpn_auth = models.DateTimeField(blank=True, null=True)
last_core_sync = models.DateTimeField(blank=True, null=True)
8 years ago
referrer = models.ForeignKey(
User, blank=True, null=True, on_delete=models.SET_NULL, related_name="referrals"
)
8 years ago
referrer_used = models.BooleanField(default=False)
campaign = models.CharField(blank=True, null=True, max_length=64)
8 years ago
def clear_fields(self):
self.expiration = None
self.last_expiry_notice = None
self.last_vpn_auth = None
self.last_core_sync = None
self.referrer = None
self.referrer_used = False
self.campaign = None
8 years ago
def on_payment_confirmed(self, payment):
if self.referrer and not self.referrer_used:
self.referrer.vpnuser.add_paid_months(
1,
"referrer",
f"rewarded for {self.user.username} (payment #{payment.id})",
)
8 years ago
self.referrer.vpnuser.save()
self.referrer_used = True
self.save()
@property
def subscr_is_failing(self):
margin = timedelta(hours=24)
if self.expiration < timezone.now() + margin:
return True
return False
def lcore_sync(self):
if VPN_AUTH_STORAGE == "inst":
return
from lambdainst.tasks import push_user
push_user.delay(user_id=self.user.id)
def notify_payment(self, payment):
return
8 years ago
def notify_subscription(self, sub):
return
8 years ago
def notify_refund(self, payment):
return
8 years ago
def __str__(self):
return self.user.username
8 years ago
setup_sync_hooks(User, VPNUser)
8 years ago
# from django.db.models.signals import post_save
# from django.dispatch import receiver
# @receiver(post_save, sender=User)
# def create_vpnuser(sender, instance, created, **kwargs):
# if created:
# try:
# VPNUser.objects.create(user=instance)
# except IntegrityError:
# pass