def fileIpCreate(request, file): client_ip, is_routable = get_client_ip(request) if request.data.get('usuario'): user = User.objects.get(id=request.data.get('usuario')) ip_file = IpsFiles(usuario=user) else: ip_file = IpsFiles() if client_ip is None: ip_file.ip = 0 ip_file.file = file ip_file.lat = 0 ip_file.lon = 0 else: # We got the client's IP address if is_routable: ip_file.ip = client_ip print( '-------------------------------------------------------------' ) print(GeoIP2.city(client_ip)['latitude']) ip_file.lat = GeoIP2.city(client_ip)['latitude'] ip_file.lon = GeoIP2.city(client_ip)['longitude'] ip_file.file = file ip_file.is_routeable = True else: ip_file.ip = client_ip ip_file.file = file ip_file.is_routeable = False ip_file.lat = 0 ip_file.lon = 0 ip_file.save()
def geoip_data(self): """Attempt to retrieve MaxMind GeoIP data based on visitor's IP.""" if not HAS_GEOIP or not TRACK_USING_GEOIP: return if not hasattr(self, '_geoip_data'): self._geoip_data = None try: gip = GeoIP(cache=GEOIP_CACHE_TYPE) self._geoip_data = gip.city(self.ip_address) except GeoIPException: msg = 'Error getting GeoIP data for IP "{0}"'.format( self.ip_address) log.exception(msg) return self._geoip_data
def detect_geo(self): if self.ip and self.counter == 0: try: # Django 1.9+ from django.contrib.gis.geoip2 import GeoIP2 as GeoIP from django.contrib.gis.geoip2 import GeoIP2Exception \ as GeoIPException except ImportError: # Django 1.8 from django.contrib.gis.geoip import GeoIP from django.contrib.gis.geoip import GeoIPException try: g = GeoIP() info = g.city(self.ip) or dict() for (k, v) in info.items(): setattr(self, 'ip_%s' % k, v) except GeoIPException: pass