# Path: geoip/mixins.py
# class GeoIpMixin(object):
#     gi = pygeoip.GeoIP(os.path.join(BASE_DIR, 'GeoIP.dat'))
# 
#     def ip_lookup(self, ip):
#         code = self.gi.country_code_by_addr(ip).lower()
#         if not code:
#             return ['unknown', 'Unknown']
#         return [self.gi.country_code_by_addr(ip).lower(),
#                 self.gi.country_name_by_addr(ip)]


import datetime
from django.conf import settings
from django.db import models
from django.utils import timezone
from timezone_field.fields import TimeZoneField
from ua_parser import user_agent_parser
from geoip.mixins import GeoIpMixin




class Website(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    name = models.CharField(max_length=255)
    url = models.URLField()
    timezone = TimeZoneField()

    def __unicode__(self):
        return self.name

    def view_count_today(self):
        today_midnight = timezone.now().astimezone(self.timezone).replace(hour=0, minute=0, second=0)
        return len(PageView.objects.filter(session__website=self,
                                           view_timestamp__gt=today_midnight))



# Path: geoip/mixins.py
# class GeoIpMixin(object):
#     gi = pygeoip.GeoIP(os.path.join(BASE_DIR, 'GeoIP.dat'))
# 
#     def ip_lookup(self, ip):
#         code = self.gi.country_code_by_addr(ip).lower()
#         if not code:
#             return ['unknown', 'Unknown']
#         return [self.gi.country_code_by_addr(ip).lower(),
#                 self.gi.country_name_by_addr(ip)]


# Path: core/models.py
import datetime
from django.conf import settings
from django.db import models
from django.utils import timezone
from timezone_field.fields import TimeZoneField
from ua_parser import user_agent_parser
from geoip.mixins import GeoIpMixin





class Website(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    name = models.CharField(max_length=255)
    url = models.URLField()
    timezone = TimeZoneField()

    def __unicode__(self):
        return self.name

    def view_count_today(self):
        today_midnight = timezone.now().astimezone(self.timezone).replace(hour=0, minute=0, second=0)
        return len(PageView.objects.filter(session__website=self,
                                           view_timestamp__gt=today_midnight))



class TrackerUser(GeoIpMixin, models.Model):