# Path: apps/base/models.py
# class Timestampable(models.Model):
#     created_date = models.DateTimeField(auto_now_add=True)
#     modified_date = models.DateTimeField(auto_now=True)
# 
#     class Meta:
#         abstract = True
# 
# Path: apps/user/models.py
# class UserProfile(models.Model):
#     TRIAL_USER = 1
#     PREMIUM_USER = 2
#     USER_STATUS = (
#         (TRIAL_USER, 'Free User'),
#         (PREMIUM_USER, 'Premium User'),
#     )
#     user = models.OneToOneField(settings.AUTH_USER_MODEL,
#                                 related_name='profile', primary_key=True)
#     subscription = models.IntegerField(choices=USER_STATUS, default=TRIAL_USER)
#     expired_date = models.DateTimeField(auto_now_add=True)
#     token = models.CharField(blank=True, max_length=255)
# 
#     class Meta:
#         db_table = 'user_profile'
# 
#     def __str__(self):
#         return self.user.email


from django.core.exceptions import ValidationError
from django.db import models
from apps.base.models import Timestampable
from apps.user.models import UserProfile


class IP(Timestampable):
    address = models.URLField(max_length=255)
    vhost = models.CharField(blank=True, max_length=255, default='default')
    user_profile = models.ForeignKey(UserProfile)

    class Meta:
        db_table = 'ips'
        unique_together = ('user_profile', 'address', 'vhost',)

    def save(self, *args, **kwargs):
        if self.address and 'http' in self.address:
            self.address = self.address.split('://')[1].lower()
            if '/' in self.address:
                self.address = self.address.split('/')[0]
        return super().save(*args, **kwargs)

    def __str__(self):

# Path: apps/base/models.py
# class Timestampable(models.Model):
#     created_date = models.DateTimeField(auto_now_add=True)
#     modified_date = models.DateTimeField(auto_now=True)
# 
#     class Meta:
#         abstract = True
# 
# Path: apps/user/models.py
# class UserProfile(models.Model):
#     TRIAL_USER = 1
#     PREMIUM_USER = 2
#     USER_STATUS = (
#         (TRIAL_USER, 'Free User'),
#         (PREMIUM_USER, 'Premium User'),
#     )
#     user = models.OneToOneField(settings.AUTH_USER_MODEL,
#                                 related_name='profile', primary_key=True)
#     subscription = models.IntegerField(choices=USER_STATUS, default=TRIAL_USER)
#     expired_date = models.DateTimeField(auto_now_add=True)
#     token = models.CharField(blank=True, max_length=255)
# 
#     class Meta:
#         db_table = 'user_profile'
# 
#     def __str__(self):
#         return self.user.email


# Path: apps/ip/models.py
from django.core.exceptions import ValidationError
from django.db import models
from apps.base.models import Timestampable
from apps.user.models import UserProfile



class IP(Timestampable):
    address = models.URLField(max_length=255)
    vhost = models.CharField(blank=True, max_length=255, default='default')
    user_profile = models.ForeignKey(UserProfile)

    class Meta:
        db_table = 'ips'
        unique_together = ('user_profile', 'address', 'vhost',)

    def save(self, *args, **kwargs):
        if self.address and 'http' in self.address:
            self.address = self.address.split('://')[1].lower()
            if '/' in self.address:
                self.address = self.address.split('/')[0]
        return super().save(*args, **kwargs)

    def __str__(self):

        return self.address