from django.conf import settings
from django.db import models
import os


class Tfile(models.Model):
    PERM_PUBLIC = 1
    PERM_CONFIDENTIAL = 2
    PERM_CLASSIFIED = 3
    PERM_TYPES = (
        (PERM_PUBLIC, 'Public'),
        (PERM_CONFIDENTIAL, 'Confidential'),
        (PERM_CLASSIFIED, 'Classified'),
    )

    LIMIT_NONE = 'N'
    LIMIT_TIME = 'T'
    LIMIT_ACCESS = 'A'
    LIMIT_TYPES = (
        (LIMIT_NONE, 'None'),
        (LIMIT_TIME, 'Time'),
        (LIMIT_ACCESS, 'Access'),
    )

    uid = models.CharField(primary_key=True, max_length=32)
    name = models.CharField(max_length=255)
    size = models.BigIntegerField()
    perm = models.PositiveSmallIntegerField(choices=PERM_TYPES)
    qid = models.CharField(max_length=6, null=True, blank=True)
    access_count = models.IntegerField(default=0)
    limit = models.CharField(max_length=1, choices=LIMIT_TYPES)
    time_limit = models.DateTimeField(null=True, blank=True)
    access_limit = models.IntegerField(default=0)
    upload_time = models.DateTimeField(auto_now_add=True)
    last_access_time = models.DateTimeField(auto_now=True)

    def __str__(self):
        return '{self.name}'.format(self=self)

    def abs_path(self):
        return os.path.join(settings.TRASH_PATH, self.uid)

    class Meta:
        verbose_name_plural = "tfiles"


class Penalty(models.Model):
    ip = models.GenericIPAddressField(unique=True)
    amount = models.IntegerField()
    last_time = models.DateTimeField(auto_now=True)

    def __str__(self):
        return '{self.ip}: {self.amount}'.format(self=self)

    class Meta:
        verbose_name_plural = "penalties"
