Commit d8f2352b authored by Sylvain Coulange's avatar Sylvain Coulange
Browse files

ajout addStat country

parent 48a5061b
......@@ -59,4 +59,5 @@ class appstats(models.Model):
module = models.CharField(max_length=30)
ip = models.CharField(max_length=30)
agent = models.CharField(max_length=200, default='Anonymous')
country = models.CharField(max_length=50, default='')
lang = models.CharField(max_length=10)
......@@ -15,7 +15,7 @@ from .models import Trace, SynthVocTrace, SynthVocRecord, appstats
from .decorators import unauthenticated_user, allowed_users
from user_agents import parse
import json, datetime, csv, os
import json, datetime, csv, os, requests
import numpy as np
......@@ -605,14 +605,28 @@ def addStat(request):
module = colis["module"]
lang = colis["lang"]
newStat = appstats()
if get_client_ip(request)!="127.0.0.1":
newStat = appstats()
newStat.app = app
newStat.module = module
newStat.ip = get_client_ip(request)
newStat.agent = parse(request.META['HTTP_USER_AGENT'])
newStat.lang = lang
if newStat.ip!="127.0.0.1": newStat.save()
newStat.app = app
newStat.module = module
newStat.ip = get_client_ip(request)
newStat.agent = parse(request.META['HTTP_USER_AGENT'])
newStat.country = getCountry(get_client_ip(request))
newStat.lang = lang
newStat.save()
return JsonResponse({})
def getCountry(ip):
endpoint = f'https://ipinfo.io/{ip}/json'
response = requests.get(endpoint, verify = True)
if response.status_code != 200:
return 'Status:', response.status_code, 'Problem with the request. Exiting.'
exit()
return JsonResponse({})
\ No newline at end of file
data = response.json()
print(data)
return data['country']
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment