Example #1
0
def main():
    ret_list = []
    tweets = []
    display_tweets = []
    models_to_show = ALL_TOP_V_SHOW
    month_list_entity = ds.get_saved_months('month_list')
    if month_list_entity != None:
        months_to_show = month_list_entity.month_list
    else:
        months_to_show = ["201512"]
        print "####months_to_show not found, use default:%s"%months_to_show
    if request.method == 'POST':
        month = request.form.get("month","")
        model = request.form.get("model","")
    else:
        #use latest month when GET by default
        month = months_to_show[0]
        model = models_to_show[0]

    if model in ALL_TOP_V_ALIAS_DICT.keys():
        models_alias = ALL_TOP_V_ALIAS_DICT[model]
    else:
        models_alias = [model]

    for model_query in models_alias:
        #get the tweets per model, month from db
        entities = ds.get_all_entities_bymodelmonth(model_query, month)
        for i in entities:
            #gmap uses [lat,lng], while twitter uses [lng,lat], here's using [lng,lat]
            tmp_coordinate = [(i.coordinate[0]),(i.coordinate[1])]
            #WAR for heat in middle of US! remove the 'if' after Dec.
            if cmp([-95.712891, 37.09024], tmp_coordinate) != 0:
                ret_list.append(tmp_coordinate)
            tweets.append([i.month, i.place, i.text])
    if len(tweets) > 10:
        display_tweets = random.sample(tweets, 10)
    return render_template('index.html', coor_data=ret_list, tweet_list=display_tweets, \
                        model_list=models_to_show, month_list=months_to_show, model=model, month=month, \
                        total_num=len(tweets))
Example #2
0
def trends():
    set_tab = 0
    data_list_trends_brand = []
    selling_data_list_trends = []
    monthly_total_alltype_list = []
    monthly_total_list = []
    per_model_total_ytd = 0
    per_brand_total_ytd = 0
    models_to_show = TREND_TOP_V
    brands_to_show = TREND_TOP_V_BRAND
    tmp_months_to_show = []
    tmp_month_list = []
    month_list_entity = ds.get_saved_months('month_list')
    if month_list_entity != None:
        tmp_month_list = month_list_entity.month_list
        #default to use latest year
        current_year = tmp_month_list[0][:4]
    if current_year == None:
        current_year = '2015'
    if request.method == 'POST':
        model_full = request.form.get("model","")
        brand = request.form.get("brand","")
        current_year = request.form.get("year","")
        if model_full == "":
            model_full = models_to_show[0]
        if brand != "":
            set_tab = 1
        else:
            brand = brands_to_show[0]
    else:
        model_full = models_to_show[0]
        brand = brands_to_show[0]
    tmp_months_to_show = []
    if tmp_month_list != []:
        tmp_year_list = []
        years_show_list = []
        for m in tmp_month_list:
            tmp_year_list.append(m[:4])
            if current_year in m:
                tmp_months_to_show.append(m)
        tmp_year_list = list(set(tmp_year_list))
        tmp_year_list.sort(reverse=True)
#        print tmp_year_list
        years_show_list = tmp_year_list
    else:
        tmp_months_to_show = ['201501'] 

    if model_full == "top 30 in total":
        per_model_total_ytd = 0
    for tmp_month in reversed(tmp_months_to_show):
        data_dict_brand = {}
        brand_total = 0
        data_dict_brand['month'] = str(tmp_month)
        data_entities_brand_a_month = ds.get_monthly_total_perbrand_bybrandmonth(brand, tmp_month)
        for i in data_entities_brand_a_month:
            brand_total += i.monthly_total_no
        per_brand_total_ytd += brand_total
        data_dict_brand['brand_total'] = brand_total
        data_list_trends_brand.append(data_dict_brand)
        if model_full == "top 30 in total":
            monthly_total_alltype_entities = ds.get_monthly_total_pertype_bytypemonth("all",tmp_month)
            for i in monthly_total_alltype_entities:
                monthly_total_alltype_list.append({'month':str(tmp_month),'monthly_total_no':i.monthly_total_no})
                per_model_total_ytd += i.monthly_total_no
#    print data_list_trends_brand
#    print monthly_total_alltype_list

    model =' '.join(model_full.split()[1:])
    for tmp_month in reversed(tmp_months_to_show):
        selling_data_entities_model = ds.get_all_entities_bymodelmonth(model, tmp_month)
        if selling_data_entities_model != None:
            for i in selling_data_entities_model:
                per_model_total_ytd = i.selling_no_ytd
                selling_data_list_trends.append(json.dumps(i.to_dict()))
#    print selling_data_list_trends

    return render_template('selling_trends.html', data_list_trends = selling_data_list_trends, \
            monthly_total_list = monthly_total_alltype_list, model = model_full, models_show = models_to_show, \
            per_model_total_ytd = per_model_total_ytd, per_brand_total_ytd = per_brand_total_ytd, \
            data_list_trends_brand = data_list_trends_brand, brand = brand, brands_show = brands_to_show, \
            years_show = years_show_list, year = current_year,set_tab = set_tab)