def show_contact(request, key): """ Shows details about a specific contact. It uses django's generic views. """ return object_detail(request, Contact.all(), key)
def delete_contacts(request): """ Deletes all the contacts from the current google username. This view also deletes the entities associated with the contacts that are being deleted. """ if request.method == 'POST': db.delete( Contact.all().filter("owner =", users.get_current_user()) ) db.delete( ContactmapsImport.all().filter("owner =", users.get_current_user()) ) return HttpResponseRedirect(reverse('contactmaps.views.list_contacts')) else: return render_to_response( request, 'contact_confirm_delete.html', {'object': {'name': 'All contacts', 'key': 'all'}} )
def list_contacts(request): """ The list of contacts. This view also fetches the cities that the current contacts live in and the geographical postion of those cities. """ # Set up the contacts query contacts = ( Contact.all(). filter("owner =", users.get_current_user()). order('creation_date') ) # Paginate using django's pagination tools paginator = Paginator(contacts, settings.PAGINATE_BY) page = request.GET.get('page', 1) try: page_number = int(page) except ValueError: if page == 'last': page_number = paginator.num_pages else: # Page is not 'last', nor can it be converted to an int. raise Http404 try: page_obj = paginator.page(page_number) except InvalidPage: raise Http404 # Get the cities that the contacts live in _cities = [] for contact in copy.deepcopy(page_obj.object_list): _cities.append(contact.city) cities = set(_cities) cities_list = [] if len(cities): # Construct the cities string for our query cities_str = u'(' for city in cities: cities_str += "'%s'," % city cities_str = cities_str.strip(',') cities_str += u')' # Create the cities query for city in City.gql("WHERE name IN %s" % cities_str): cities_list.append({ 'name': city.name, 'latitude': city.location.lat, 'longitude': city.location.lon }) # Create the context and render the template with pagination # This part is adapted from django.views.generic.list_detail.oject_list context = RequestContext(request, { 'object_list': page_obj.object_list, 'paginator': paginator, 'page_obj': page_obj, # Legacy template context stuff. New templates should use page_obj # to access this instead. 'is_paginated': page_obj.has_other_pages(), 'results_per_page': paginator.per_page, 'has_next': page_obj.has_next(), 'has_previous': page_obj.has_previous(), 'page': page_obj.number, 'next': page_obj.next_page_number(), 'previous': page_obj.previous_page_number(), 'first_on_page': page_obj.start_index(), 'last_on_page': page_obj.end_index(), 'pages': paginator.num_pages, 'hits': paginator.count, 'page_range': paginator.page_range, # The cities list 'cities_list': cities_list }) template = loader.get_template('contact_list.html') # Return the rendered template return HttpResponse(template.render(context))