本文整理汇总了Python中django.contrib.gis.utils.GeoIP.country方法的典型用法代码示例。如果您正苦于以下问题:Python GeoIP.country方法的具体用法?Python GeoIP.country怎么用?Python GeoIP.country使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.gis.utils.GeoIP
的用法示例。
在下文中一共展示了GeoIP.country方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test04_city
# 需要导入模块: from django.contrib.gis.utils import GeoIP [as 别名]
# 或者: from django.contrib.gis.utils.GeoIP import country [as 别名]
def test04_city(self):
"Testing GeoIP city querying methods."
g = GeoIP(country='<foo>')
addr = '130.80.29.3'
fqdn = 'chron.com'
for query in (fqdn, addr):
# Country queries should still work.
for func in (g.country_code, g.country_code_by_addr, g.country_code_by_name):
self.assertEqual('US', func(query))
for func in (g.country_name, g.country_name_by_addr, g.country_name_by_name):
self.assertEqual('United States', func(query))
self.assertEqual({'country_code' : 'US', 'country_name' : 'United States'},
g.country(query))
# City information dictionary.
d = g.city(query)
self.assertEqual('USA', d['country_code3'])
self.assertEqual('Houston', d['city'])
self.assertEqual('TX', d['region'])
self.assertEqual(713, d['area_code'])
geom = g.geos(query)
self.failIf(not isinstance(geom, GEOSGeometry))
lon, lat = (-95.3670, 29.7523)
lat_lon = g.lat_lon(query)
lat_lon = (lat_lon[1], lat_lon[0])
for tup in (geom.tuple, g.coords(query), g.lon_lat(query), lat_lon):
self.assertAlmostEqual(lon, tup[0], 4)
self.assertAlmostEqual(lat, tup[1], 4)
示例2: process_request
# 需要导入模块: from django.contrib.gis.utils import GeoIP [as 别名]
# 或者: from django.contrib.gis.utils.GeoIP import country [as 别名]
def process_request(self, request):
from django.conf import settings
from meegloo.international.models import Country
try:
from django.contrib.gis.utils import GeoIP
except ImportError:
request.country = Country.objects.get(
pk = getattr(settings, 'COUNTRY_ID')
)
return
g = GeoIP()
ip = request.META.get('HTTP_X_FORWARDED_FOR',
request.META.get('REMOTE_IP', request.META.get('REMOTE_ADDR'))
)
if ip != '127.0.0.1':
d = g.country(ip)
code = d['country_code']
request.country = Country.objects.get(code = code)
else:
pk = getattr(settings, 'COUNTRY_ID')
request.country = Country.objects.get(pk = pk)
示例3: get_country_code
# 需要导入模块: from django.contrib.gis.utils import GeoIP [as 别名]
# 或者: from django.contrib.gis.utils.GeoIP import country [as 别名]
def get_country_code(request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0]
else:
ip = request.META.get('REMOTE_ADDR')
if not ip:
return ''
g = GeoIP()
return g.country(ip)['country_code'] or ''
示例4: test03_country
# 需要导入模块: from django.contrib.gis.utils import GeoIP [as 别名]
# 或者: from django.contrib.gis.utils.GeoIP import country [as 别名]
def test03_country(self):
"Testing GeoIP country querying methods."
g = GeoIP(city='<foo>')
fqdn = 'www.google.com'
addr = '12.215.42.19'
for query in (fqdn, addr):
for func in (g.country_code, g.country_code_by_addr, g.country_code_by_name):
self.assertEqual('US', func(query))
for func in (g.country_name, g.country_name_by_addr, g.country_name_by_name):
self.assertEqual('United States', func(query))
self.assertEqual({'country_code' : 'US', 'country_name' : 'United States'},
g.country(query))
示例5: dashboard
# 需要导入模块: from django.contrib.gis.utils import GeoIP [as 别名]
# 或者: from django.contrib.gis.utils.GeoIP import country [as 别名]
def dashboard(request):
ip = request.META['REMOTE_ADDR']
geoip = GeoIP()
try:
country = geoip.country(ip)
except:
country = "Ghost"
else:
country = country['country_name']
try:
coords = geoip.coords(ip)
if not coords:
coords = "on Earth"
except:
coords = "on Earth"
location = {
'country': country,
'coords': coords
}
checkins = Checkin.objects.all()
obj_list = list()
for item in checkins:
item.geodata.transform(900913)
obj_list.append(json.loads(item.geodata.geojson))
obj_list = json.dumps(obj_list)
last_20 = Checkin.objects.all()[:19]
data = {
"checkins": last_20,
"count": Checkin.objects.all().count(),
"obj_list": obj_list,
"location": location
}
return render_to_response("index.html", data,
context_instance=RequestContext(request, {}))
示例6: loadfeeds
# 需要导入模块: from django.contrib.gis.utils import GeoIP [as 别名]
# 或者: from django.contrib.gis.utils.GeoIP import country [as 别名]
def loadfeeds(self):
# for now I keep this utility command very basic, much more refactoring in future
# now we start sorting the feeds
feeds = Feed.objects.all().filter(enabled=True)
for feed in feeds:
self.stdout.write('\n***Parsing feed %s' % feed.name.encode('utf-8'))
feed_parsed = feedparser.parse(feed.url_xml)
for item_parsed in feed_parsed.entries:
biased_link = item_parsed.link
# link must be restored to original (in some case it is: http://news.google.com/news...&url=http//www.example.com
self.stdout.write('Biased link: %s' % biased_link)
link = get_original_url(biased_link)
# then we unquote the url (ex http://voria.gr/index.php?module%3Dnews%26func%3Ddisplay%26sid%3D56406 becomes
# http://voria.gr/index.php?module=news&func=display&sid=56406
link = urllib.unquote(link)
if len(link)>255:
self.stdout.write('\nItem link is more than 255 chars!\n%s\n' % link)
link = link[:255]
# must import only items not already in the database (check URL of item)
items = Item.objects.filter(link=link)
if len(items)==0: # new item, must import it!
try:
self.stdout.write('\nImporting item %s' % item_parsed.title.encode('utf-8'))
# store item
item = Item()
item.title = item_parsed.title[:255].encode('utf-8')
item.summary = item_parsed.summary.encode('utf-8')
item.link = link
item.feed = feed
item.updated = datetime.datetime.fromtimestamp(mktime(item_parsed.updated_parsed))
# 0. domain
# first let's check domain country
parsed = urlparse.urlsplit(link)
domain_name = parsed.netloc.encode('utf-8')
g = GeoIP()
country_code = g.country(domain_name)['country_code']
self.stdout.write('\nDomain: %s, country code: %s' % (domain_name, country_code))
countries = Country.objects.filter(iso2=country_code)
country = None
if countries.count() > 0:
country = countries[0]
# check if if we need to add domain in db
domains = Domain.objects.all().filter(name=domain_name)
if not domains:
self.stdout.write('\nAdding a new domain to the system: %s for this item.' % domain_name)
domain = Domain()
domain.name = domain_name
domain.country = country
domain.save()
else:
domain = domains[0]
# add the item to the place
item.domain = domain
# save item
item.save()
# define text to be parsed
text2parse = item.title + item.summary
# 1. keywords
keywords = Keyword.objects.all()
for keyword in keywords:
if re.search(keyword.name, text2parse, re.IGNORECASE):
self.stdout.write('\n***Keyword %s is in this item.' % keyword.name)
#import ipdb;ipdb.set_trace()
keyword.item.add(item)
# 2. people
people = Person.objects.all()
for person in people:
if re.search(person.name, text2parse, re.IGNORECASE):
self.stdout.write('\n***Person %s is in this item.' % person.name)
#import ipdb;ipdb.set_trace()
person.item.add(item)
# 3. images
#url = item.link
#soup = bs(urlopen(url))
#parsed = list(urlparse.urlparse(url))
soup = bs(item.summary)
parsed = list(item.summary)
for img in soup.findAll("img"):
print img
alt = ''
if img.has_key('src'):
if img.has_key('alt'):
alt = img["alt"]
if img["src"].lower().startswith("http"):
#import ipdb;ipdb.set_trace()
src = img["src"]
else:
# TODO src extraction from relative url
src = urlparse.urlunparse(parsed)
print src
image = Image()
image.src = src
image.alt = alt
image.item = item
#.........这里部分代码省略.........
示例7: test04_city
# 需要导入模块: from django.contrib.gis.utils import GeoIP [as 别名]
# 或者: from django.contrib.gis.utils.GeoIP import country [as 别名]
def test04_city(self):
"Testing GeoIP city querying methods."
g = GeoIP(country="<foo>")
addr = "130.80.29.3"
fqdn = "chron.com"
for query in (fqdn, addr):
# Country queries should still work.
for func in (g.country_code, g.country_code_by_addr, g.country_code_by_name):
self.assertEqual("US", func(query))
for func in (g.country_name, g.country_name_by_addr, g.country_name_by_name):
self.assertEqual("United States", func(query))
self.assertEqual({"country_code": "US", "country_name": "United States"}, g.country(query))
# City information dictionary.
d = g.city(query)
self.assertEqual("USA", d["country_code3"])
self.assertEqual("Houston", d["city"])
self.assertEqual("TX", d["region"])
self.assertEqual(713, d["area_code"])
geom = g.geos(query)
self.failIf(not isinstance(geom, GEOSGeometry))
lon, lat = (-95.4152, 29.7755)
lat_lon = g.lat_lon(query)
lat_lon = (lat_lon[1], lat_lon[0])
for tup in (geom.tuple, g.coords(query), g.lon_lat(query), lat_lon):
self.assertAlmostEqual(lon, tup[0], 4)
self.assertAlmostEqual(lat, tup[1], 4)
示例8: test03_country
# 需要导入模块: from django.contrib.gis.utils import GeoIP [as 别名]
# 或者: from django.contrib.gis.utils.GeoIP import country [as 别名]
def test03_country(self):
"Testing GeoIP country querying methods."
g = GeoIP(city="<foo>")
fqdn = "www.google.com"
addr = "12.215.42.19"
for query in (fqdn, addr):
for func in (g.country_code, g.country_code_by_addr, g.country_code_by_name):
self.assertEqual("US", func(query))
for func in (g.country_name, g.country_name_by_addr, g.country_name_by_name):
self.assertEqual("United States", func(query))
self.assertEqual({"country_code": "US", "country_name": "United States"}, g.country(query))