From 3d74d9395c4501a12e37594f137a1accf69b9973 Mon Sep 17 00:00:00 2001 From: Michael Hall Date: Thu, 27 Sep 2018 17:42:23 -0400 Subject: [PATCH] If an event doesn't have latlng from a Place, use the Place's city's latlng before the Team's. Also add population field to City and sort dynamic lookups by population size to show bigger cities first --- events/admin.py | 2 +- events/management/commands/load_cities.py | 2 +- events/migrations/0047_add_city_population.py | 18 ++++++++++++++++++ events/models/events.py | 8 ++++++-- events/models/locale.py | 3 +++ events/views.py | 2 +- .../get_together/places/create_place.html | 2 ++ 7 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 events/migrations/0047_add_city_population.py diff --git a/events/admin.py b/events/admin.py index 4d5a4c2..d248389 100644 --- a/events/admin.py +++ b/events/admin.py @@ -95,7 +95,7 @@ admin.site.register(SPR, SPRAdmin) class CityAdmin(admin.ModelAdmin): raw_id_fields = ('spr',) - list_display = ('name', 'spr', 'latitude', 'longitude') + list_display = ('name', 'spr', 'latitude', 'longitude', 'population') list_filter =('spr__country',) search_fields = ('name', 'spr__name') admin.site.register(City, CityAdmin) diff --git a/events/management/commands/load_cities.py b/events/management/commands/load_cities.py index c571969..d57755e 100644 --- a/events/management/commands/load_cities.py +++ b/events/management/commands/load_cities.py @@ -52,7 +52,7 @@ class Command(BaseCommand): City.objects.update_or_create( name=city[NAME], spr=spr, - defaults={'tz': city[TIMEZONE], 'longitude': city[LONGITUDE], 'latitude': city[LATITUDE]}) + defaults={'tz': city[TIMEZONE], 'population': city[POPULATION], 'longitude': city[LONGITUDE], 'latitude': city[LATITUDE]}) except Exception as e: print("Warning: Failed to load city %s for %s (%s)" % (city[NAME], spr, e)) else: diff --git a/events/migrations/0047_add_city_population.py b/events/migrations/0047_add_city_population.py new file mode 100644 index 0000000..f8c8452 --- /dev/null +++ b/events/migrations/0047_add_city_population.py @@ -0,0 +1,18 @@ +# Generated by Django 2.0 on 2018-09-27 21:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('events', '0046_require_sponsor_logo'), + ] + + operations = [ + migrations.AddField( + model_name='city', + name='population', + field=models.IntegerField(default=0, help_text='Population'), + ), + ] diff --git a/events/models/events.py b/events/models/events.py index e23895d..60b303a 100644 --- a/events/models/events.py +++ b/events/models/events.py @@ -201,8 +201,12 @@ def update_event_searchable(event): if (event.place is not None): searchable.location_name = str(event.place.city) searchable.venue_name = event.place.name - searchable.longitude = event.place.longitude or None - searchable.latitude = event.place.latitude or None + if event.place.longitude is not None and event.place.latitude is not None: + searchable.longitude = event.place.longitude + searchable.latitude = event.place.latitude + elif event.place.city is not None: + searchable.longitude = event.place.city.longitude + searchable.latitude = event.place.city.latitude else: searchable.location_name = event.team.location_name diff --git a/events/models/locale.py b/events/models/locale.py index 86678ec..d6e70d9 100644 --- a/events/models/locale.py +++ b/events/models/locale.py @@ -96,6 +96,7 @@ class City(models.Model): tz = models.CharField(max_length=32, verbose_name=_('Default Timezone'), default='UTC', choices=[(tz, tz) for tz in pytz.all_timezones], blank=False, null=False, help_text=_('The most commonly used timezone for this Team.')) longitude = models.FloatField(help_text=_('Longitude in Degrees East'), null=True, blank=True) latitude = models.FloatField(help_text=_('Latitude in Degrees North'), null=True, blank=True) + population = models.IntegerField(help_text=_('Population'), null=False, blank=False, default=0) @property def short_name(self): @@ -125,6 +126,8 @@ class CitySerializer(serializers.ModelSerializer): 'short_name', 'spr', 'tz', + 'latitude', + 'longitude', 'slug', 'display' ) diff --git a/events/views.py b/events/views.py index b6335d5..0cacd7d 100644 --- a/events/views.py +++ b/events/views.py @@ -66,7 +66,7 @@ def spr_list(request, *args, **kwargs): def city_list(request, *args, **kwargs): if "q" in request.GET: match = request.GET.get("q", "") - cities = City.objects.filter(name__icontains=match) + cities = City.objects.filter(name__icontains=match).order_by('-population') else: cities = City.objects.all() diff --git a/get_together/templates/get_together/places/create_place.html b/get_together/templates/get_together/places/create_place.html index ecfc860..114a7ff 100644 --- a/get_together/templates/get_together/places/create_place.html +++ b/get_together/templates/get_together/places/create_place.html @@ -220,6 +220,8 @@ $(document).ready(function(){ }); }, select: function( event, ui ) { + $("#id_latitude").val(ui.data.latitude); + $("#id_longitude").val(ui.data.longitude); $("#id_tz").val(ui.data.tz); $("#id_tz").selectmenu("refresh"); }