Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def location(request):
"""
Transform an IP address into an approximate location.
"""
ip = utils.resolve(app_settings.IP_RESOLVER, request)
try:
location = GeoIP() and GeoIP().city(ip)
except:
# Handle 127.0.0.1 and not found IPs
return ugettext('unknown')
if location and location['country_name']:
if location['city']:
return '%s, %s' % (location['city'], location['country_name'])
else:
return location['country_name']
return ugettext('unknown')
def create_session(self, request, user):
ip = utils.resolve(app_settings.IP_RESOLVER, request)
device = utils.resolve(app_settings.DEVICE_RESOLVER, request)
location = utils.resolve(app_settings.LOCATION_RESOLVER, request)
user_agent = request.META.get('HTTP_USER_AGENT', '')
user_agent = user_agent[:200] if user_agent else user_agent
try:
with transaction.atomic():
obj = self.create(
user=user,
session_key=request.session.session_key,
ip=ip,
user_agent=user_agent,
device=device,
location=location,
expiration_date=request.session.get_expiry_date(),