|
@@ -22,34 +22,43 @@ class ConfirmRegion(TemplateView):
|
|
request.encoding = 'utf-8'
|
|
request.encoding = 'utf-8'
|
|
try:
|
|
try:
|
|
ip = CommonService.get_ip_address(request)
|
|
ip = CommonService.get_ip_address(request)
|
|
- device_domain_data = {'ip': ip}
|
|
|
|
|
|
+ device_domain_qs = DeviceDomainModel.objects.filter(ip=ip)
|
|
|
|
|
|
|
|
+ # 获取国家编码
|
|
ip_addr_qs = IPAddr.objects.filter(ip=ip).values('country_code')
|
|
ip_addr_qs = IPAddr.objects.filter(ip=ip).values('country_code')
|
|
if ip_addr_qs.exists():
|
|
if ip_addr_qs.exists():
|
|
country_code = ip_addr_qs[0]['country_code']
|
|
country_code = ip_addr_qs[0]['country_code']
|
|
else:
|
|
else:
|
|
ip_qs = IPQuery(ip)
|
|
ip_qs = IPQuery(ip)
|
|
|
|
+ district = ip_qs.district
|
|
country_code = ip_qs.country_id
|
|
country_code = ip_qs.country_id
|
|
|
|
+ # ip地址信息存表
|
|
|
|
+ IPAddr.objects.create(ip=ip, country_code=country_code, district=district)
|
|
|
|
|
|
logger = logging.getLogger('info')
|
|
logger = logging.getLogger('info')
|
|
logger.info('设备获取域名---ip:{},country_code:{}'.format(ip, country_code))
|
|
logger.info('设备获取域名---ip:{},country_code:{}'.format(ip, country_code))
|
|
|
|
+
|
|
if country_code:
|
|
if country_code:
|
|
- device_domain_data['country_name'] = country_code
|
|
|
|
- device_request_url = CountryModel.objects.filter(country_code=country_code).\
|
|
|
|
|
|
+ country_qs = CountryModel.objects.filter(country_code=country_code).\
|
|
values('region__api', 'region__push_api')
|
|
values('region__api', 'region__push_api')
|
|
- if device_request_url.exists():
|
|
|
|
- api = device_request_url[0]['region__api']
|
|
|
|
- push_api = device_request_url[0]['region__push_api']
|
|
|
|
- device_domain_data['api'] = api
|
|
|
|
- DeviceDomainModel.objects.create(**device_domain_data)
|
|
|
|
|
|
+ if country_qs.exists():
|
|
|
|
+ api = country_qs[0]['region__api']
|
|
|
|
+ push_api = country_qs[0]['region__push_api']
|
|
|
|
+
|
|
|
|
+ if not device_domain_qs.exists():
|
|
|
|
+ DeviceDomainModel.objects.create(ip=ip, country_name=country_code, api=api)
|
|
|
|
+
|
|
return response.json(0, {'request_api_url': api, 'push_api': push_api})
|
|
return response.json(0, {'request_api_url': api, 'push_api': push_api})
|
|
|
|
|
|
- # 不存在默认返回美洲地区api。
|
|
|
|
- api = RegionModel.objects.filter(continent_code='NA').values('api', 'push_api')
|
|
|
|
- device_domain_data['country_name'] = 'NA'
|
|
|
|
- device_domain_data['api'] = api[0]['api']
|
|
|
|
- DeviceDomainModel.objects.create(**device_domain_data)
|
|
|
|
- return response.json(0, {'request_api_url': api[0]['api'], 'push_api': api[0]['push_api']})
|
|
|
|
|
|
+ # 不存在则返回美洲域名
|
|
|
|
+ region_qs = RegionModel.objects.filter(continent_code='NA').values('api', 'push_api')
|
|
|
|
+ api = region_qs[0]['api']
|
|
|
|
+ push_api = region_qs[0]['push_api']
|
|
|
|
+
|
|
|
|
+ if not device_domain_qs.exists():
|
|
|
|
+ DeviceDomainModel.objects.create(ip=ip, country_name='NA', api=api)
|
|
|
|
+
|
|
|
|
+ return response.json(0, {'request_api_url': api, 'push_api': push_api})
|
|
except Exception as e:
|
|
except Exception as e:
|
|
print(e)
|
|
print(e)
|
|
return response.json(0, {'request_api_url': 'https://www.dvema.com', 'push_api': 'https://push.dvema.com'})
|
|
return response.json(0, {'request_api_url': 'https://www.dvema.com', 'push_api': 'https://push.dvema.com'})
|