コード例 #1
0
ファイル: tests.py プロジェクト: nlal/medaform.com
 def testLotto(self):
     times_chosen = {}
     for i in range(1000):
         chosen_links = choose_by_lotto(Link.objects.all())
     
         for link_id, score in chosen_links:
             if link_id in times_chosen:
                 times_chosen[link_id] += 1
             else:
                 times_chosen[link_id] = 1
     items = sorted(times_chosen.items())
     for key, value in items:
         print 'Link ' + str(key) + ' was chosen ' + str(value) + ' times.'
         for j in range(0, value, 10):
              sys.stdout.write('*')
         print '\n'
     print 'Chose ' + str(len(items)) + ' unique links.'
コード例 #2
0
ファイル: views.py プロジェクト: nlal/medaform.com
def index(request, tag_name=None, sort_by="lotto", page_num=1):
    #link_list = Link.objects.filter(date_submitted__gte=(datetime.now() - timedelta(hours=24))).annotate(score=SumWithDefault('votes__vote', default=0)).order_by('-score')
    page_num = int(page_num)
    start_rank = (page_num - 1) * default_links_per_page
    curr_page = next_page = prev_page = ""
    end = page_num*default_links_per_page;
    start = end-default_links_per_page;
    
    possible_links = Link.objects.all()

    if tag_name:
        proper_tag_name = tag_name.replace('-', ' ')
        tag = Tag.objects.get(name=proper_tag_name)
        possible_links = TaggedItem.objects.get_by_model(possible_links, tag)
        curr_page += '/tags/' + tag_name

    if sort_by == "lotto":
        recent_links = possible_links.order_by('-date_submitted')[:1000]
        links_by_lotto = choose_by_lotto(recent_links)
        links = [(Link.objects.get(pk=link_id), score) for link_id, score in links_by_lotto]

    elif sort_by == "new":
        recent_links = possible_links #.filter(date_submitted__gte=(datetime.now() - timedelta(hours=24)))
        new_links = recent_links.order_by('-date_submitted')[start:end]
        links = [(link, Vote.objects.get_score(link)['score']) for link in new_links]
        curr_page += '/new'
        if end < len(recent_links):
            next_page += curr_page + '/page' + str(page_num+1)
        if page_num > 1:
            prev_page += curr_page + '/page' + str(page_num-1)

    elif sort_by == "top":
        recent_links = possible_links #.filter(date_submitted__gte=(datetime.now() - timedelta(hours=24)))
        sorted_links = choose_by_score(recent_links)[start:end]
        links = [(Link.objects.get(pk=link_id), score) for link_id, score in sorted_links]
        curr_page +='/top'
        if end < len(recent_links):
            next_page += curr_page + '/page' + str(page_num+1)
        if page_num > 1:
            prev_page += curr_page + '/page' + str(page_num-1)

    if tag_name:
        return render_to_response('tag_index.html', RequestContext(request, {'links': links, 'tag_name':proper_tag_name, 'sort_by': sort_by, 'start_rank': start_rank, 'next_page': next_page, 'prev_page': prev_page}))
    return render_to_response('index.html', RequestContext(request, {'links': links, 'sort_by': sort_by, 'start_rank': start_rank, 'next_page': next_page, 'prev_page': prev_page}))