models.py 6.33 KB
Newer Older
1
from choicesenum import ChoicesEnum
2 3 4 5 6
from django.conf import settings
from django.db import models
from django.utils import timezone


7
class Legacy(ChoicesEnum):
8 9 10
    UNDEFINED = 0, "Undefined"


11
def create_missing_user_limits(user):
12 13
    for code in settings.LM_SERVICES.MAP.keys():
        is_defined = code != settings.LM_SERVICES.UNDEFINED
14
        is_exists = Limit.objects.filter(user=user, service=code).exists()
15
        if is_defined and not is_exists:
16 17
            Limit(user=user, service=code).save()

18

19 20
class Limit(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
21 22
    service = models.IntegerField(default=settings.LM_SERVICES.UNDEFINED,
                                  choices=settings.LM_SERVICES.choices())
23
    renewal_date = models.DateTimeField(default=None, blank=True, null=True)
24
    expiry_date = models.DateTimeField(default=None, blank=True, null=True)
25 26 27
    volume_total = models.DecimalField(default=0, decimal_places=2, max_digits=6)
    time_total = models.DecimalField(default=0, decimal_places=2, max_digits=6)
    is_active = models.BooleanField(default=False)
28 29 30
    created_date = models.DateTimeField(default=timezone.now)
    updated_date = models.DateTimeField(default=timezone.now)

31 32
    def service_label(self):

33
        return settings.LM_SERVICES.get_name_by_code(self.service)
34

35 36 37 38 39 40 41
    def active_label(self):

        if self.is_active:
            return "Yes"
        else:
            return "No"

42
    def endpoint_short_label(self):
43

44 45 46
        return self.endpoint_label("%Y-%m-%d")

    def endpoint_full_label(self):
47

48
        return self.endpoint_label("%Y-%m-%d %H:%M:%S %z")
49

50 51 52 53 54 55 56
    def endpoint_label(self, label_format):

        is_blank = self.renewal_date is None and self.expiry_date is None
        is_renewal = self.renewal_date is not None and self.expiry_date is None
        is_expiry = self.renewal_date is None and self.expiry_date is not None

        if is_blank:
57
            return "-"
58 59 60 61
        elif is_renewal:
            return self.renewal_date.strftime(label_format)
        elif is_expiry:
            return self.expiry_date.strftime(label_format)
62
        else:
63
            return "Invalid"
64 65 66

    def credit_label(self):

67
        if self.service == settings.LM_SERVICES.TUNNEL:
68
            return self.days_credit_label()
69
        else:
70
            return self.days_credit_label()
71 72 73 74

    def timedelta_to_daystring(self, delta):

        single_day = 60 * 60 * 24
75
        days = int(round(delta.total_seconds() / single_day))
David Seaward's avatar
David Seaward committed
76 77 78 79 80 81 82

        if days <= 62:
            return str(days) + " days"
        elif days <= 744:
            return str(days // 30) + " months"
        else:
            return str(days // 365) + " years"
83

84
    def days_credit_label(self):
85

86 87 88 89
        has_zero = self.expiry_date is None and self.renewal_date is None and self.time_total == 0
        has_credit_only = self.expiry_date is None and self.renewal_date is None and self.time_total > 0
        has_valid_expiry = self.expiry_date is not None and self.renewal_date is None and self.time_total == 0
        has_valid_renewal = self.expiry_date is None and self.renewal_date is not None and self.time_total == 0
90 91 92 93 94 95 96 97

        if has_zero:
            label = "-"
        elif has_credit_only:
            label = str(int(self.time_total)) + " days"
        elif has_valid_expiry:
            difference = self.expiry_date - timezone.now()
            label = self.timedelta_to_daystring(difference)
98 99 100
        elif has_valid_renewal:
            difference = self.renewal_date - timezone.now()
            label = self.timedelta_to_daystring(difference)
101 102 103 104
        else:
            label = "Invalid"

        return label
105

106

107
class ExternalBundle(models.Model):
108 109
    parser = models.IntegerField(default=settings.LM_PARSERS.UNDEFINED,
                                 choices=settings.LM_PARSERS.choices())
110
    external_key = models.CharField(max_length=30)
111 112
    service = models.IntegerField(default=settings.LM_SERVICES.UNDEFINED,
                                  choices=settings.LM_SERVICES.choices())
113 114 115 116 117 118 119
    time_credit = models.DecimalField(default=0, decimal_places=2, max_digits=6)
    volume_credit = models.DecimalField(default=0, decimal_places=2, max_digits=6)
    created_date = models.DateTimeField(default=timezone.now)
    updated_date = models.DateTimeField(default=timezone.now)


class ExternalCredit(models.Model):
120 121
    parser = models.IntegerField(default=settings.LM_PARSERS.UNDEFINED,
                                 choices=settings.LM_PARSERS.choices())
122 123 124 125 126 127 128
    external_key = models.CharField(max_length=30)
    label = models.CharField(max_length=30)
    bundle_key = models.CharField(max_length=30)
    bundle_label = models.CharField(max_length=30)
    quantity = models.DecimalField(default=1, decimal_places=2, max_digits=6)
    account_name = models.CharField(max_length=30, default="")
    additional_data = models.TextField(default="")
David Seaward's avatar
David Seaward committed
129
    error_message = models.TextField(default="")
130 131 132 133
    is_converted = models.BooleanField(default=False)
    created_date = models.DateTimeField(default=timezone.now)
    updated_date = models.DateTimeField(default=timezone.now)

134 135 136 137
    @property
    def parser_name(self):
        settings.LM_PARSERS.get_name_by_code(self.parser)

138 139
    @property
    def external_code(self):
140
        return self.parser_name + ":" + str(self.external_key)
141 142 143

    @property
    def external_bundle(self):
144
        return self.parser_name + ":" + str(self.external_key)
145

146 147 148

class Credit(models.Model):
    limit = models.ForeignKey(Limit)
149
    external = models.ForeignKey(ExternalCredit, blank=True, null=True, default=None)
150 151
    time_credit = models.DecimalField(default=0, decimal_places=2, max_digits=6)
    volume_credit = models.DecimalField(default=0, decimal_places=2, max_digits=6)
152 153 154 155 156 157
    old_expiry_date = models.DateTimeField(blank=True, null=True, default=None)
    old_time_total = models.DecimalField(blank=True, null=True, default=None, decimal_places=2, max_digits=6)
    old_volume_total = models.DecimalField(blank=True, null=True, default=None, decimal_places=2, max_digits=6)
    new_expiry_date = models.DateTimeField(blank=True, null=True, default=None)
    new_time_total = models.DecimalField(blank=True, null=True, default=None, decimal_places=2, max_digits=6)
    new_volume_total = models.DecimalField(blank=True, null=True, default=None, decimal_places=2, max_digits=6)
158 159
    created_date = models.DateTimeField(default=timezone.now)
    updated_date = models.DateTimeField(default=timezone.now)