1
|
# -*- coding: utf-8 -*-
|
2
|
#
|
3
|
# UnivNautes
|
4
|
# Copyright (C) 2014 Entr'ouvert
|
5
|
#
|
6
|
# This program is free software: you can redistribute it and/or modify it under
|
7
|
# the terms of the GNU Affero General Public License as published by the Free
|
8
|
# Software Foundation, either version 3 of the License, or (at your option) any
|
9
|
# later version.
|
10
|
#
|
11
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
12
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13
|
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
14
|
# details.
|
15
|
#
|
16
|
# You should have received a copy of the GNU Affero General Public License
|
17
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
18
|
|
19
|
# django init
|
20
|
import os
|
21
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
|
22
|
|
23
|
import sys
|
24
|
import pfconfigxml
|
25
|
import urllib2
|
26
|
import json
|
27
|
import math
|
28
|
|
29
|
from authentic2.saml.models import LibertyProvider
|
30
|
|
31
|
import logging
|
32
|
from logging.handlers import SysLogHandler
|
33
|
|
34
|
logger = logging.getLogger()
|
35
|
logger.setLevel(logging.INFO)
|
36
|
syslog = SysLogHandler(address='/var/run/log', facility='local4')
|
37
|
formatter = logging.Formatter('sp/geo[%(process)s] (%(levelname)s) %(message)s')
|
38
|
syslog.setFormatter(formatter)
|
39
|
logger.addHandler(syslog)
|
40
|
|
41
|
def laxfloat(x):
|
42
|
try:
|
43
|
return float(x)
|
44
|
except:
|
45
|
return 666
|
46
|
|
47
|
idps = {}
|
48
|
|
49
|
sp = pfconfigxml.get_sp()
|
50
|
for url in sp.get('ui', {}).get('geolocations', []):
|
51
|
try:
|
52
|
logger.info('download geo locations from %s' % str(url))
|
53
|
geolist = json.load(urllib2.urlopen(url))
|
54
|
except urllib2.HTTPError as e:
|
55
|
logger.warn('Error loading JSON data source (%s)' % str(e))
|
56
|
except urllib2.URLError as e:
|
57
|
logger.warn('Error loading JSON data source (%s)' % str(e))
|
58
|
except ValueError as e:
|
59
|
logger.warn('Error reading JSON data source output (%s)' % str(e))
|
60
|
else:
|
61
|
for idp in geolist:
|
62
|
entityid = idp.get('entityID')
|
63
|
if not entityid:
|
64
|
logger.warn('idp with no entityID ?')
|
65
|
continue
|
66
|
geos = idp.get('geo')
|
67
|
if not geos:
|
68
|
logger.info('idp with no geo info (%s)' % entityid)
|
69
|
continue
|
70
|
if isinstance(geos, dict):
|
71
|
geos = [geos]
|
72
|
dots = set()
|
73
|
for geo in geos:
|
74
|
if not isinstance(geo, dict) \
|
75
|
or not 'lat' in geo \
|
76
|
or not 'lon' in geo:
|
77
|
logger.warn('geo is not a lat/lon dict (%s)' % entityid)
|
78
|
continu
|
79
|
dots.add((laxfloat(geo['lat']), laxfloat(geo['lon'])))
|
80
|
if entityid not in idps:
|
81
|
idps[entityid] = dots
|
82
|
else:
|
83
|
idps[entityid].update(dots)
|
84
|
|
85
|
geoinfos = []
|
86
|
|
87
|
n = 0
|
88
|
for idp in LibertyProvider.objects.all():
|
89
|
if idp.entity_id in idps:
|
90
|
geos = list(idps[idp.entity_id])
|
91
|
else:
|
92
|
logger.warn('idp %s do not have geoloc, randomize it' % idp.entity_id)
|
93
|
geos = [(47.0+2.0*math.sin(n), 2.5+3.0*math.cos(n))]
|
94
|
n += 1
|
95
|
geoinfos.append({
|
96
|
'slug': idp.slug,
|
97
|
'entityid': idp.entity_id,
|
98
|
'geo': geos
|
99
|
})
|
100
|
|
101
|
tempfilename = '/var/tmp/univnautes-geoinfo.js-%s' % os.getpid()
|
102
|
|
103
|
with open(tempfilename, 'wb') as f:
|
104
|
f.write('geoinfos = ')
|
105
|
json.dump(geoinfos, f, indent=2)
|
106
|
f.write('\n')
|
107
|
|
108
|
os.rename(tempfilename, sys.argv[1])
|
109
|
|