def _autocomplete_response(request, place_choices, term, limit=4): ''' Return a Python dict with sorted autocomplete responses. ''' match_status = [] for p in place_choices: name = p.name.lower().strip() if name and name.startswith(term): match_status.append(1) elif name and any(word.startswith(term) for word in name.split()): match_status.append(2) else: match_status.append(3) results = [] for _, p in sorted(zip(match_status, place_choices))[:limit]: try: thumb = get_cached_thumbnail(p.image, 'small') if p.image else None except IOError: thumb = None image_url = thumb.url if thumb else '/static/img/defaults/default-place.png' results.append({ 'id': p.id, 'name': p.name, 'image_url': image_url, 'address': p.location.address if p.location else '', 'selected': render_safe('orgadmin/ac_place_selected.html', place=p, context_instance=RequestContext(request)) }) return results
def page_edit_event(request, id=None): ''' Edit an Event. If id is None, the form is for a new Event entry. ''' org = _get_current_org(request.user) initial = {} if id is not None: instance = get_object_or_404(Event, id=id) if not org or not org_owns(org, instance): return HttpResponseForbidden() else: instance = None if not org: return _redirect_home(request, notification_type=2) if org and org.establishments.count() == 1: initial['place'] = org.establishments.all()[0].id if request.POST or request.FILES: form = SimpleEventForm(data=request.POST, files=request.FILES, instance=instance, initial=initial) if form.is_valid(): event = form.save() Role.objects.get_or_create(role_type='owner', organization=org, event=event) return redirect('orgadmin-listevents') else: form = SimpleEventForm(instance=instance, initial=initial) # TODO: awesome "initial_selected" hack for autocomplete display!!!! match = re.search('name="place" value="(\d+)"', form.as_ul()) if match: try: initial_place = Place.objects.get(id=match.group(1)) initial_selected = render_safe('orgadmin/ac_place_selected.html', context_instance=RequestContext(request), place=initial_place) except Place.DoesNotExist: initial_selected = None else: initial_selected = None context = ManagePageContext(request, content_dict=dict( form=form, tag_names=[t.name for t in Tag.objects.all()], newplace_form=SimplePlaceForm(prefix='newplace', initial={'state': 'PA', 'postcode': '15213', 'town': 'Pittsburgh'}), initial_selected=initial_selected )) return render_to_response('orgadmin/page_event_edit.html', context_instance=context)
def newplace_form_submission(request): if request.POST: print request.POST form = SimplePlaceForm(data=request.POST, prefix='newplace') if form.is_valid(): # if the form didn't give at least a place name or addres, don't save the junk if form.cleaned_data['name'] != '' or form.cleaned_data['address'] != '': place = form.save(commit=False) place.listed = False # save manully, including inner model reassign hack place.location.save() place.location = place.location place.save() return { 'id': place.id, 'name': place.name, 'address': place.location.address, 'selected': render_safe('orgadmin/ac_place_selected.html', place=place, context_instance=RequestContext(request)) } return False