Exemplo n.º 1
0
def index(request):
    context = {}
    query_string = ''
    found_entries = None
    entry_query = Q()
    try:
        # get tweets within the last 24 hours and that belong to AZ counties
        #date_from_utc = timezone.now() - timedelta(days=1)

        if ('q' in request.GET) and request.GET['q'].strip():
            query_string = request.GET['q']
            entry_query = get_query(query_string, ['text',])

        mood_by_geo = Tweet.objects.filter(
            county__in=AZ_COUNTIES #, created_dt__gte=date_from_utc,
            ).filter(entry_query).values(
            'county').annotate(
            avg_index=Avg(Case(When(sentiment_index__gt=0, then=1),
                               When(sentiment_index__lt=0, then=-1),
                               default=0.001,
                               output_field=DecimalField())),
            num_tweets=Count('county'))

        mood_by_geo_json = json.dumps(list(mood_by_geo))

        context = {'tweet_stats': mood_by_geo,
                   'mood_by_geo': mood_by_geo_json,
                   'query_string': query_string,}

    except Tweet.DoesNotExist:
        raise Http404("tweet does not exist")

    return render_to_response('index.html', context)
Exemplo n.º 2
0
def search(request):
    query_string = ''
    found_entries = None
    if ('q' in request.GET) and request.GET['q'].strip():
        query_string = request.GET['q']

        entry_query = get_query(query_string, ['title', 'body',])

        found_entries = Tweets.objects.filter(entry_query).order_by(
        '-pub_date')

    return render_to_response('search/search_results.html',
                          { 'query_string': query_string, 'found_entries': found_entries },
                          context_instance=RequestContext(request))