generate tile suggestions (#17317)
This commit is contained in:
parent
8ec3d1ee35
commit
8e07f69768
|
@ -16,13 +16,20 @@
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import datetime
|
||||
import random
|
||||
import re
|
||||
import urllib2
|
||||
|
||||
from django import template
|
||||
from django.conf import settings
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.text import slugify
|
||||
|
||||
from combo.apps.dashboard.models import DashboardCell
|
||||
from combo.apps.maps.models import MapLayer
|
||||
from combo.data.models import ConfigJsonCell
|
||||
from combo.public.views import render_cell
|
||||
from combo.utils import requests
|
||||
|
||||
register = template.Library()
|
||||
|
||||
|
@ -135,3 +142,83 @@ def as_producer(slug):
|
|||
return {'slug': collectivity,
|
||||
'label': settings.KNOWN_SERVICES['hobo'].get('hobo-%s' % collectivity, {'title': ''})['title']}
|
||||
return {'slug': 'grandlyon', 'label': 'Grand Lyon'}
|
||||
|
||||
|
||||
|
||||
@register.assignment_tag
|
||||
def get_suggestions(request, cell, user_data, places_data):
|
||||
tile_data = []
|
||||
addresses = []
|
||||
city = user_data.get('city') or user_data.get('address_city')
|
||||
if city:
|
||||
# get commune tile for the user city
|
||||
maplayer = MapLayer.objects.get(slug='mairie')
|
||||
data_result = requests.get(maplayer.geojson_url, timeout=2,
|
||||
without_user=True, cache_duration=300).json()
|
||||
city_slug = slugify(city)
|
||||
if data_result.get('features'):
|
||||
for feature in data_result['features']:
|
||||
if 'Annexe' in feature['properties']['nom']:
|
||||
continue
|
||||
if city_slug in slugify(feature['properties']['nom']):
|
||||
tile_data.append({'key': maplayer.slug,
|
||||
'properties': feature['properties']})
|
||||
break
|
||||
|
||||
if random.random() < 0.6:
|
||||
tile_data.append({'key': 'airquality'})
|
||||
if random.random() < 0.3:
|
||||
tile_data.append({'key': 'pollen'})
|
||||
|
||||
if user_data.get('address_street'):
|
||||
if not user_data.get('address_number'):
|
||||
user_data['address_number'] = ''
|
||||
addresses.append('%(address_number)s %(address_street)s, %(address_city)s, France' % user_data)
|
||||
|
||||
if places_data:
|
||||
for place_data in places_data.get('data'):
|
||||
addresses.append('%(adresse)s, %(ville)s, France' % place_data['content'])
|
||||
|
||||
coords = []
|
||||
nominatim_url = 'https://nominatim.entrouvert.org'
|
||||
for address in addresses:
|
||||
url = '%s/search?q=%s&accept-language=fr&format=json' % (nominatim_url, urllib2.quote(address))
|
||||
search_result = requests.get(url, timeout=2, without_user=True,
|
||||
cache_duration=300).json()
|
||||
if not search_result:
|
||||
continue
|
||||
coords.append({'lon': search_result[0]['lon'], 'lat': search_result[0]['lat']})
|
||||
|
||||
for coord in coords:
|
||||
lat1, lat2 = float(coord['lat']) - 0.008, float(coord['lat']) + 0.008
|
||||
lon1, lon2 = float(coord['lon']) - 0.006, float(coord['lon']) + 0.006
|
||||
for maplayer in MapLayer.objects.filter(slug__in=('velov', 'piscine', 'tcl')):
|
||||
url = maplayer.geojson_url + '&BBOX=%s,%s,%s,%s' % (lat1, lon1, lat2, lon2)
|
||||
data_result = requests.get(url, timeout=2, without_user=True,
|
||||
cache_duration=300).json()
|
||||
if not data_result.get('features'):
|
||||
continue
|
||||
for feature in random.sample(
|
||||
data_result.get('features'), min(len(data_result.get('features')), 2)):
|
||||
tile_data.append({'key': maplayer.slug,
|
||||
'properties': feature['properties']})
|
||||
|
||||
dashboard = DashboardCell.objects.all()[0]
|
||||
cells = []
|
||||
seen = {}
|
||||
for data in tile_data:
|
||||
cell = ConfigJsonCell(key=data['key'], order=1,
|
||||
page_id=cell.page_id, placeholder='_auto_tile')
|
||||
cell_form_keys = [x['varname'] for x in settings.JSON_CELL_TYPES[cell.key].get('form') or {}]
|
||||
cell.parameters = {}
|
||||
for key in cell_form_keys:
|
||||
cell.parameters[key] = data['properties'].get(key)
|
||||
cell_uid = repr((data['key'], cell.parameters))
|
||||
if cell_uid in seen:
|
||||
continue
|
||||
seen[cell_uid] = True
|
||||
cell.save()
|
||||
cells.append(render_cell(request, cell=cell).content)
|
||||
|
||||
random.shuffle(cells)
|
||||
return cells[:5]
|
||||
|
|
|
@ -262,7 +262,16 @@ JSON_CELL_TYPES = {
|
|||
]
|
||||
},
|
||||
"cut-profile": {
|
||||
"url": "[idp_url]api/users/[user_nameid]/",
|
||||
"name": "Profil CUT",
|
||||
"url": "[idp_url]api/users/[user_nameid]/",
|
||||
"name": "Profil CUT",
|
||||
},
|
||||
"suggestions": {
|
||||
"name": "Suggestions",
|
||||
"url": "[idp_url]api/users/[user_nameid]/",
|
||||
"additional-data": [
|
||||
{"key": "places",
|
||||
"url": "[passerelle_url]jsondatastore/lieux-favoris/data/?name_id=[user_nameid]"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue