# Path: apps/ip/models.py
# 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


from .models import IP


class UtilsIP(object):
    def get_list_ip(self, vhost):
        obj_vhost = {}
        obj_vhost['default'] = []

        if not vhost:
            list_ip = IP.objects.values_list('vhost', 'address').distinct()
            for ip in list_ip:
                key = ip[0]
                if key:
                    if not key in obj_vhost:
                        obj_vhost[key] = []

                    if ip[1] not in obj_vhost[key]:
                        obj_vhost[key].append(ip[1])
                else:
                    obj_vhost['default'].append(ip[1])
            return obj_vhost
        else:

# Path: apps/ip/models.py
# 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


# Path: apps/ip/utils.py
from .models import IP



class UtilsIP(object):
    def get_list_ip(self, vhost):
        obj_vhost = {}
        obj_vhost['default'] = []

        if not vhost:
            list_ip = IP.objects.values_list('vhost', 'address').distinct()
            for ip in list_ip:
                key = ip[0]
                if key:
                    if not key in obj_vhost:
                        obj_vhost[key] = []

                    if ip[1] not in obj_vhost[key]:
                        obj_vhost[key].append(ip[1])
                else:
                    obj_vhost['default'].append(ip[1])
            return obj_vhost
        else:

            return IP.objects.values_list('address', 'vhost').distinct()