# Path: web/models.py
# class Quota(models.Model):
#     account = models.ForeignKey(ProxyAccount, on_delete=models.CASCADE)
#     enabled = models.BooleanField(default=True)
#     last_trigged = models.DateTimeField(auto_now_add=True, blank=True)
#     comment = models.CharField(max_length=140, default='')
#     
#     is_alias_of = models.ForeignKey('self', on_delete=models.CASCADE, default=-1)       # clone type and param from this one
#     type = models.CharField(default='Unconfigured', max_length=20)
#     param = JSONField(default={})
# 
#     synced = False
#     alias_target_enabled = True
# 
#     def update_from_alias(self, forced=False):
#         '''Copy type and param from the alias target, if is_alias_of is set.
#         
#         Call this function before reading type or param.
#         '''
#         if not forced and self.synced: 
#             return
#         self.synced = True
#         if self.is_alias_of_id != -1 :
#             a = self.is_alias_of
#             self.type = a.type
#             self.param = a.param
#             self.alias_target_enabled = a.enabled
#     
#     @property
#     def is_really_enabled(self):
#         self.update_from_alias()
#         return self.alias_target_enabled and self.enabled and self.account.enabled
# 
#     @property
#     def module(self):
#         self.update_from_alias()
#         return getQuotaModule(self.type)
#     
#     @property
#     def name(self):
#         return self.module.FRIENDLY_NAME
#     
#     def descript(self, is_admin=False):
#         return self.module.descript(self, is_admin)
# 
#     def trig(self):
#         self.account.enabled = False
#         self.account.save()
#         self.reset()
# 
#     def reset(self):
#         from django.utils import timezone
#         self.last_trigged = timezone.now()
#         self.save()
# 
#     def is_exceeded(self):
#         try:
#             return self.module.is_exceeded(self)
#         except Exception as e:
#             from core.util import print_exception
#             print_exception(e)
#             print('Failed to judge Quota %d status', self.pk)
#             return False
# 
# class TrafficStat(models.Model):
#     account = models.ForeignKey(ProxyAccount, on_delete=models.CASCADE)
#     time = models.DateTimeField(auto_now_add=True, blank=True)
#     amount = models.IntegerField()


from django import forms
from django.db.models import Sum
from web.models import Quota, TrafficStat

FRIENDLY_NAME = 'Traffic'


class Form(forms.Form):
    traffic = forms.DecimalField(label='Traffic (MB)')


def descript(q, is_admin=False):
    '''return user-friendly strings descripting status of one account quota '''
    par_traffic = float(q.param.get('traffic', '1024768'))

# Path: web/models.py
# class Quota(models.Model):
#     account = models.ForeignKey(ProxyAccount, on_delete=models.CASCADE)
#     enabled = models.BooleanField(default=True)
#     last_trigged = models.DateTimeField(auto_now_add=True, blank=True)
#     comment = models.CharField(max_length=140, default='')
#     
#     is_alias_of = models.ForeignKey('self', on_delete=models.CASCADE, default=-1)       # clone type and param from this one
#     type = models.CharField(default='Unconfigured', max_length=20)
#     param = JSONField(default={})
# 
#     synced = False
#     alias_target_enabled = True
# 
#     def update_from_alias(self, forced=False):
#         '''Copy type and param from the alias target, if is_alias_of is set.
#         
#         Call this function before reading type or param.
#         '''
#         if not forced and self.synced: 
#             return
#         self.synced = True
#         if self.is_alias_of_id != -1 :
#             a = self.is_alias_of
#             self.type = a.type
#             self.param = a.param
#             self.alias_target_enabled = a.enabled
#     
#     @property
#     def is_really_enabled(self):
#         self.update_from_alias()
#         return self.alias_target_enabled and self.enabled and self.account.enabled
# 
#     @property
#     def module(self):
#         self.update_from_alias()
#         return getQuotaModule(self.type)
#     
#     @property
#     def name(self):
#         return self.module.FRIENDLY_NAME
#     
#     def descript(self, is_admin=False):
#         return self.module.descript(self, is_admin)
# 
#     def trig(self):
#         self.account.enabled = False
#         self.account.save()
#         self.reset()
# 
#     def reset(self):
#         from django.utils import timezone
#         self.last_trigged = timezone.now()
#         self.save()
# 
#     def is_exceeded(self):
#         try:
#             return self.module.is_exceeded(self)
#         except Exception as e:
#             from core.util import print_exception
#             print_exception(e)
#             print('Failed to judge Quota %d status', self.pk)
#             return False
# 
# class TrafficStat(models.Model):
#     account = models.ForeignKey(ProxyAccount, on_delete=models.CASCADE)
#     time = models.DateTimeField(auto_now_add=True, blank=True)
#     amount = models.IntegerField()


# Path: quota/TrafficQuota.py
from django import forms
from django.db.models import Sum
from web.models import Quota, TrafficStat


FRIENDLY_NAME = 'Traffic'


class Form(forms.Form):
    traffic = forms.DecimalField(label='Traffic (MB)')


def descript(q, is_admin=False):
    '''return user-friendly strings descripting status of one account quota '''
    par_traffic = float(q.param.get('traffic', '1024768'))

    current_used = TrafficStat.objects                          \