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

This commit is contained in:
Michael Hall 2018-09-27 17:42:23 -04:00
parent d35355b535
commit 3d74d9395c
7 changed files with 32 additions and 5 deletions

View file

@ -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)

View file

@ -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:

View file

@ -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'),
),
]

View file

@ -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

View file

@ -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'
)

View file

@ -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()

View file

@ -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");
}