def details(request, token): clicks = list(Click.findall(token=token)) # Sort clicks.sort(key=lambda click: click.click_id) short_link = ShortLink.find(token=token) # Get data for the charts browsers = get_data_for_charts(clicks, "browser") browsers.append("Browsers") os = get_data_for_charts(clicks, "os") os.append("OS") country = get_data_for_charts(clicks, "location") country.append("Country") not_json_data = (browsers, os, country) #Serialize json_data = json.dumps(not_json_data) data = { 'shortlink': short_link, 'clicks': clicks, 'json_data': json_data } return render_to_response('simple/details.html', data, context_instance=RequestContext(request))
def test_click_findall(self): url = "http://xlarrakoetxea.org" clicks = set() # Save sl = ShortLink(url=url) sl.save() # Clicks for i in range(random.randrange(0, 5)): c = Click(token=sl.token, os="linux") c.save() clicks.add(c) clicks2 = Click.findall(sl.token) self.assertEquals(len(clicks), len(clicks2)) for i in clicks: clicks2_aux = set(clicks2) for j in clicks2_aux: if i == j: clicks2.remove(j) self.assertEquals(0, len(clicks2))
def links_info(request, token): sl = ShortLink.find(token=token) clicks = Click.findall(token) # get browsers: browsers = {} os = {} languages = {} countries = {} countries_map = {} # The countries map doesn't have unknown dates_tmp = {} dates = [] date_format = "Date({0}, {1}, {2})" for i in clicks: # Browsers try: browsers[i.browser] += 1 except KeyError: browsers[i.browser] = 1 # Operative Systems try: os[i.os] += 1 except KeyError: os[i.os] = 1 # Languages (Browser) try: languages[i.language] += 1 except KeyError: languages[i.language] = 1 # Countries try: countries[i.location] += 1 if i.location: countries_map[i.location] += 1 except KeyError: countries[i.location] = 1 if i.location: countries_map[i.location] = 1 # dates dt = unix_to_datetime(i.click_date) dt_str = date_format.format(dt.year, dt.month-1, dt.day) try: dates_tmp[dt_str] += 1 except KeyError: dates_tmp[dt_str] = 1 # Fill the dates until now now = datetime_now_utc() temp_date = unix_to_datetime(sl.creation_date) # If the date doesn't have enough days in between then create a new range # of dates with more days (For graph visualization) rel_delta = relativedelta(now, temp_date) if rel_delta.year == 0 and rel_delta.month == 0 and rel_delta.day < 5 or\ rel_delta.hours < 24: try: days = MINIMUN_DAYS_FOR_CHART-rel_delta.day except TypeError: days = MINIMUN_DAYS_FOR_CHART now = now + relativedelta(days=days) while (temp_date.day != now.day or temp_date.month != now.month or temp_date.year != now.year): dt_str = date_format.format(temp_date.year, temp_date.month-1, temp_date.day) try: dates.append((dt_str, dates_tmp[dt_str])) except KeyError: dates.append((dt_str, 0)) temp_date += relativedelta(days=1) # Change None for unknown for the countries try: countries[_("Unknown")] = countries[None] del countries[None] except KeyError: pass context = { 'browser_data': pie_chart_json_transform("Browsers", browsers), 'os_data': pie_chart_json_transform("Operative systems", os), 'languages_data': pie_chart_json_transform("Languages", languages), 'countries_data': pie_chart_json_transform("Countries", countries), 'countries_map_data': pie_chart_json_transform("Countries", countries_map), 'dates_data': single_linechart_json_transform_with_list("Clicks", "Days", dates), 'short_link': sl } return render_to_response('links/link_info.html', context, context_instance=RequestContext(request))