Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Pédagogies Multimodales
Phonographe
Commits
d8f2352b
Commit
d8f2352b
authored
Aug 07, 2022
by
Sylvain Coulange
🌼
Browse files
ajout addStat country
parent
48a5061b
Changes
2
Hide whitespace changes
Inline
Side-by-side
users/models.py
View file @
d8f2352b
...
...
@@ -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
)
users/views.py
View file @
d8f2352b
...
...
@@ -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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment