# 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/base/views.py
# class BaseView(View):
#     """docstring for BaseView"""
#     @method_decorator(gzip_page)
#     def dispatch(self, request, *args, **kwargs):
#         return super().dispatch(request, *args, **kwargs)
# 
#     def get_context_data(self, **kwargs):
#         context = super().get_context_data(**kwargs)
#         return context
# 
# class LoginRequiredMixin(View):
#     """docstring for LoginRequiredMixin"""
#     @method_decorator(login_required)
#     def dispatch(self, request, *args, **kwargs):
#         return super().dispatch(request, *args, **kwargs)
# 
# Path: apps/ip/utils.py
# 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()


from django.db import IntegrityError
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse_lazy
from django.views.generic import (CreateView, ListView, DeleteView)
from django.http import HttpResponse
from django.core.validators import validate_email
from django.core.exceptions import ValidationError
from apps.ip.models import IP
from apps.base.views import (BaseView, LoginRequiredMixin)
from .utils import UtilsIP
import json



class IPAPIList(BaseView):
    def get(self, request, *args, **kwargs):
        util_ip = UtilsIP()
        result = util_ip.get_list_ip('')
        return HttpResponse(json.dumps(result))


class APIIPCreateView(BaseView):
    def post(self, request, *args, **kwargs):
        pass


class IPListView(BaseView, LoginRequiredMixin, ListView):
    """docstring for IPListView"""
    model = IP
    template_name = 'ip/index.html'

    def get_queryset(self):

# 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/base/views.py
# class BaseView(View):
#     """docstring for BaseView"""
#     @method_decorator(gzip_page)
#     def dispatch(self, request, *args, **kwargs):
#         return super().dispatch(request, *args, **kwargs)
# 
#     def get_context_data(self, **kwargs):
#         context = super().get_context_data(**kwargs)
#         return context
# 
# class LoginRequiredMixin(View):
#     """docstring for LoginRequiredMixin"""
#     @method_decorator(login_required)
#     def dispatch(self, request, *args, **kwargs):
#         return super().dispatch(request, *args, **kwargs)
# 
# Path: apps/ip/utils.py
# 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()


# Path: apps/ip/views.py
from django.db import IntegrityError
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse_lazy
from django.views.generic import (CreateView, ListView, DeleteView)
from django.http import HttpResponse
from django.core.validators import validate_email
from django.core.exceptions import ValidationError
from apps.ip.models import IP
from apps.base.views import (BaseView, LoginRequiredMixin)
from .utils import UtilsIP
import json




class IPAPIList(BaseView):
    def get(self, request, *args, **kwargs):
        util_ip = UtilsIP()
        result = util_ip.get_list_ip('')
        return HttpResponse(json.dumps(result))


class APIIPCreateView(BaseView):
    def post(self, request, *args, **kwargs):
        pass


class IPListView(BaseView, LoginRequiredMixin, ListView):
    """docstring for IPListView"""
    model = IP
    template_name = 'ip/index.html'

    def get_queryset(self):

        return IP.objects.filter(user_profile=self.request.user.profile).order_by('-id')