Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download all country maps & Use OSM instead of google maps for locations #220

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ libsass
num2words
markdown>=3.0
martor>=1.5.1
django-modeltranslation
django-modeltranslation>=0.16.1
django-tinymce4-lite
python-slugify
django-microsoft-auth
Expand All @@ -39,4 +39,5 @@ sshtunnel
django-instagram
cairosvg
django-cleanup
easy-thumbnails
easy-thumbnails
django-location-field
15 changes: 10 additions & 5 deletions wbcore/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from martor.widgets import AdminMartorWidget
from django_google_maps import widgets as map_widgets
from django_google_maps import fields as map_fields
from wbcore.widgets import CustomLocationWidget
from schedule.models import Calendar
from easy_thumbnails.files import get_thumbnailer
from django_reverse_admin import ReverseModelAdmin
Expand Down Expand Up @@ -150,8 +151,7 @@ class JoinPageInlineModel(PermissionInlineModel):
model = JoinPage

formfield_overrides = {
models.TextField: {'widget': AdminMartorWidget},
map_fields.AddressField: {'widget': map_widgets.GoogleMapsAddressWidget(attrs={'data-map-type': 'roadmap'})},
models.TextField: {'widget': AdminMartorWidget}
}


Expand All @@ -177,8 +177,7 @@ class Media:

formfield_overrides = {
models.TextField: {'widget': AdminMartorWidget},
map_fields.AddressField: {'widget': map_widgets.GoogleMapsAddressWidget(attrs={'data-map-type': 'roadmap'})},
}
}

def formfield_for_manytomany(self, db_field, request, **kwargs):
if request.user.is_super_admin:
Expand Down Expand Up @@ -748,6 +747,8 @@ class ContentAdmin(MyAdmin):
class LocationAdmin(MyAdmin):

def get_queryset(self, request):
if request.user.is_superuser or request.user.is_super_admin:
return super().get_queryset(request)
event_locations = Location.objects.filter(event__host__in=request.user.hosts.all())
host_locations = Location.objects.filter(host__in=request.user.hosts.all())
project_locations = Location.objects.filter(project__hosts__in=request.user.hosts.all())
Expand All @@ -757,6 +758,11 @@ def get_queryset(self, request):
list_display = ('name', 'street', 'postal_code', 'city', 'get_country', 'geolocation', 'get_occurrences')
readonly_fields = ('get_occurrences',)

formfield_overrides = {
map_fields.GeoLocationField: {'widget': CustomLocationWidget(based_fields=['address'])}
}


def get_country(self, address):
return address.country.name

Expand All @@ -768,7 +774,6 @@ def link(self, type_name, pk, title, ):
def get_occurrences(self, location):
occurrences = []

print(location)
for event in location.event_set.all():
occurrences.append(self.link('Event', event.pk, event.title))

Expand Down
41 changes: 41 additions & 0 deletions wbcore/management/commands/download_country_maps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from django.core.management.base import BaseCommand, no_translations
import urllib.request
from django.conf import settings
from os import path, makedirs
from pathlib import Path
import json



class Command(BaseCommand):
"""
Creates slugs based on the name for each partner without slug.
"""
@no_translations
def handle(self, *args, **options):
env_path = Path(settings.ENV_PATH)
local_static_path = env_path.parent / 'wbcore' / 'static'
static_location = local_static_path if settings.DEBUG else Path(settings.SERVER_STATIC_ROOT)

path_highmaps_countries = static_location / 'highmaps' / 'countries'
bhemmer marked this conversation as resolved.
Show resolved Hide resolved
print('Save files in ' + str(path_highmaps_countries))
if not path.exists(path_highmaps_countries):
makedirs(path_highmaps_countries)

with open(str(path_highmaps_countries.parent) + '/world-robinson-highres.geo.json') as f:
world_data = json.load(f)

print('Beginning file downloads with urllib2...')
# Iterating through the json list
count = 0
for country in world_data['features']:
country_code = country['properties']['hc-key']
print('download country: ' + country['properties']['name'])
url = 'https://code.highcharts.com/mapdata/countries/{}/{}-all.js'.format(country_code, country_code)
try:
urllib.request.urlretrieve(url, str(path_highmaps_countries / country_code) + '.js')
count += 1
except urllib.error.HTTPError:
print('country {} with code {} cannot be downloaded'.format(country['properties']['name'], country_code))

print("created {} country files".format(count))
Loading