from django.db import models
from django.conf import settings
from .utils import create_shortcode
from .validators import validate_url

# Create your models here.

MINIMUM_LENGTH = getattr(settings, 'MINIMUM_LENGTH', 6)

class Shortner(models.Model):
    url = models.CharField(max_length=300, validators=[validate_url])
    shortCode = models.CharField(max_length=MINIMUM_LENGTH, unique=True, blank=True)
    active = models.BooleanField(default=True)
    timestamp = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def save(self, *args, **kwargs):
        if self.shortCode is None or self.shortCode == '':
            self.shortCode = create_shortcode(self)
        if 'http://' not in self.url:
            self.url = f'http://{self.url}'
        super(Shortner, self).save(*args, **kwargs)

    def __str__(self):
        return self.url
from django.db import models
from django.conf import settings
from .utils import create_shortcode
from .validators import validate_url

# Create your models here.

MINIMUM_LENGTH = getattr(settings, 'MINIMUM_LENGTH', 6)

class Shortner(models.Model):
    url = models.CharField(max_length=300, validators=[validate_url])
    shortCode = models.CharField(max_length=MINIMUM_LENGTH, unique=True, blank=True)
    active = models.BooleanField(default=True)
    timestamp = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def save(self, *args, **kwargs):
        if self.shortCode is None or self.shortCode == '':
            self.shortCode = create_shortcode(self)
        if 'http://' not in self.url:
            self.url = f'http://{self.url}'
        super(Shortner, self).save(*args, **kwargs)

    def __str__(self):
        return self.url
# Create your models here.