Beispiel #1
0
def store_city_from_contact(contact):
    """
    This function receives a contact parameter, which is an
    entity of type 'Contact', and stores it in the DataStore 
    along with is geographical location.
    """
    # If we don't have the contact's city yet, save it, along with its geo info
    total_city_records = (
        City.all().
        filter('name =', contact.city).
        filter('state =', contact.state).
        filter('country = ', contact.country).
        count()
    )
    if total_city_records == 0 and contact.city is not None:
        location = get_geoinfo(contact.city)
        
        # Store the city only if its geo location is available.
        if location:
            City(
                name=contact.city,
                state=contact.state,
                country=contact.country,
                location=db.GeoPt(location['latitude'], location['longitude'])
            ).put()
Beispiel #2
0
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))