From e15cdd7fc10148671d95750a9650cd62b9fc8d70 Mon Sep 17 00:00:00 2001 From: Michael Hall Date: Mon, 29 Jan 2018 12:48:14 -0500 Subject: [PATCH] Require 3 characters or more before calling an API for Lookup fields. Limit returned results to 20 objects. Include 'no value' option in lookup field results --- events/forms.py | 3 + events/views.py | 12 ++-- .../get_together/places/create_place.html | 26 +++++---- .../get_together/teams/create_team.html | 12 ++-- .../get_together/teams/edit_team.html | 55 ++++++------------- get_together/views/__init__.py | 27 --------- get_together/views/places.py | 27 +++++++++ 7 files changed, 75 insertions(+), 87 deletions(-) diff --git a/events/forms.py b/events/forms.py index 10b43ce..9e6bac4 100644 --- a/events/forms.py +++ b/events/forms.py @@ -203,4 +203,7 @@ class NewPlaceForm(forms.ModelForm): widgets = { 'city': Lookup(source='/api/cities/', label='name'), } + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.fields['city'].required = True diff --git a/events/views.py b/events/views.py index 6491d10..54588fa 100644 --- a/events/views.py +++ b/events/views.py @@ -41,9 +41,9 @@ def places_list(request, *args, **kwargs): def country_list(request, *args, **kwargs): if "q" in request.GET: match = request.GET.get("q", "") - countries = Country.objects.filter(name__icontains=match) + countries = Country.objects.filter(name__icontains=match)[:20] else: - countries = Country.objects.all() + countries = Country.objects.all()[:20] serializer = CountrySerializer(countries, many=True) return Response(serializer.data) @@ -51,9 +51,9 @@ def country_list(request, *args, **kwargs): def spr_list(request, *args, **kwargs): if "q" in request.GET: match = request.GET.get("q", "") - sprs = SPR.objects.filter(name__icontains=match) + sprs = SPR.objects.filter(name__icontains=match)[:20] else: - sprs = SPR.objects.all() + sprs = SPR.objects.all()[:20] if "country" in request.GET and request.GET.get("country") is not "": sprs = sprs.filter(country=request.GET.get("country")) @@ -64,9 +64,9 @@ 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)[:20] else: - cities = City.objects.all() + cities = City.objects.all()[:20] if "spr" in request.GET and request.GET.get("spr") is not "": cities = cities.filter(spr=request.GET.get("spr")) diff --git a/get_together/templates/get_together/places/create_place.html b/get_together/templates/get_together/places/create_place.html index 1085352..65756bb 100644 --- a/get_together/templates/get_together/places/create_place.html +++ b/get_together/templates/get_together/places/create_place.html @@ -16,19 +16,23 @@ diff --git a/get_together/templates/get_together/teams/create_team.html b/get_together/templates/get_together/teams/create_team.html index 3d5e66a..50eebc1 100644 --- a/get_together/templates/get_together/teams/create_team.html +++ b/get_together/templates/get_together/teams/create_team.html @@ -16,6 +16,7 @@ $(document).ready(function(){ $("#city_search").keyup(function() { var searchText = this.value; + if (searchText.length < 3) return; $.getJSON("/api/cities/?q="+searchText, function(data) { var searchField = $("#city_search")[0]; var m = this.url.match(/q=([^&]+)/); @@ -25,10 +26,13 @@ $(document).ready(function(){ } var c = searchField.value if (c != q) return; - var selectField = $("#city_select"); - selectField.empty(); - $.each(data, function(){ - selectField.append('') + var selectField = $("#city_select"); + selectField.empty(); + var selected = " selected" + selectField.append('') + $.each(data, function(){ + selectField.append(''); + selected=""; }); }); }); diff --git a/get_together/templates/get_together/teams/edit_team.html b/get_together/templates/get_together/teams/edit_team.html index 5fff5f1..ae35283 100644 --- a/get_together/templates/get_together/teams/edit_team.html +++ b/get_together/templates/get_together/teams/edit_team.html @@ -14,53 +14,30 @@ {% block javascript %} diff --git a/get_together/views/__init__.py b/get_together/views/__init__.py index f005b99..0aa03a6 100644 --- a/get_together/views/__init__.py +++ b/get_together/views/__init__.py @@ -35,31 +35,4 @@ def home(request, *args, **kwards): else: return render(request, 'get_together/index.html') -def places_list(request, *args, **kwargs): - places = Place.objects.all() - context = { - 'places_list': places, - } - return render(request, 'get_together/places/list_places.html', context) - -def create_place(request): - if request.method == 'GET': - form = NewPlaceForm() - - context = { - 'place_form': form, - } - return render(request, 'get_together/places/create_place.html', context) - elif request.method == 'POST': - form = NewPlaceForm(request.POST) - if form.is_valid: - new_place = form.save() - return redirect('places') - else: - context = { - 'place_form': form, - } - return render(request, 'get_together/places/create_place.html', context) - else: - return redirect('home') diff --git a/get_together/views/places.py b/get_together/views/places.py index 88a8476..dc4b1bd 100644 --- a/get_together/views/places.py +++ b/get_together/views/places.py @@ -14,4 +14,31 @@ import datetime import simplejson # Create your views here. +def places_list(request, *args, **kwargs): + places = Place.objects.all() + context = { + 'places_list': places, + } + return render(request, 'get_together/places/list_places.html', context) + +def create_place(request): + if request.method == 'GET': + form = NewPlaceForm() + + context = { + 'place_form': form, + } + return render(request, 'get_together/places/create_place.html', context) + elif request.method == 'POST': + form = NewPlaceForm(request.POST) + if form.is_valid(): + new_place = form.save() + return redirect('places') + else: + context = { + 'place_form': form, + } + return render(request, 'get_together/places/create_place.html', context) + else: + return redirect('home')