def sort_items_by_page_views(all_items, item_module_name): # TODO(KGD): Cache this reordering of ``items`` for a period of time today = datetime.datetime.now() start_date = today - datetime.timedelta(days=settings.SPC['hit_horizon']) page_order = get_pagehits(item_module_name, start_date=start_date, end_date=today) page_order.sort(reverse=True) #``page_order`` is a list of tuples; the 2nd entry in each tuple is the # primary key, that must exist in ``items_pk``. all_items = list(all_items) items_pk = [item.pk for item in all_items] entry_order = [] count_list = [] for count, pk in page_order: try: idx = items_pk.index(pk) except ValueError: pass else: items_pk[idx] = None entry_order.append(all_items[idx]) count_list.append(count) # Items that have never been viewed get added to the bottom: for idx, pk in enumerate(items_pk): if pk is not None: entry_order.append(all_items[idx]) count_list.append(0) return entry_order, count_list
def view_item(request, submission, revision): """ Shows a submitted item to web users. The ``slug`` is always ignored, but appears in the URLs for the sake of search engines. The revision, if specified >= 0 will show the particular revision of the item, rather than the latest revision (default). """ create_hit(request, submission) permalink = settings.SPC['short_URL_root'] + str(submission.id) + '/' + \ str(revision.rev_id+1) + '/' latest_link = settings.SPC['short_URL_root'] + str(submission.id) + '/' pageviews = get_pagehits('submission', start_date=datetime.datetime.now()\ -datetime.timedelta(days=settings.SPC['hit_horizon']), item_pk=submission.id) package_files = [] if submission.sub_type == 'package': # Update the repo to the version required submission.fileset.checkout_revision(revision.hash_id) package_files = submission.fileset.list_iterator() return render_to_response( 'submission/item.html', {}, context_instance=RequestContext( request, { 'item': revision, 'tag_list': revision.tags.all(), 'permalink': permalink, 'latest_link': latest_link, 'pageviews': pageviews, 'pageview_days': settings.SPC['hit_horizon'], 'package_files': package_files, }))
def view_item(request, submission, revision): """ Shows a submitted item to web users. The ``slug`` is always ignored, but appears in the URLs for the sake of search engines. The revision, if specified >= 0 will show the particular revision of the item, rather than the latest revision (default). """ create_hit(request, submission) permalink = settings.SPC['short_URL_root'] + str(submission.id) + '/' + \ str(revision.rev_id+1) + '/' latest_link = settings.SPC['short_URL_root'] + str(submission.id) + '/' pageviews = get_pagehits('submission', start_date=datetime.datetime.now()\ -datetime.timedelta(days=settings.SPC['hit_horizon']), item_pk=submission.id) package_files = [] if submission.sub_type == 'package': # Update the repo to the version required submission.fileset.checkout_revision(revision.hash_id) package_files = list(submission.fileset.list_iterator()) return render_to_response('submission/item.html', {}, context_instance=RequestContext(request, {'item': revision, 'tag_list': revision.tags.all(), 'permalink': permalink, 'latest_link': latest_link, 'pageviews': pageviews, 'pageview_days': settings.SPC['hit_horizon'], 'package_files': package_files, }))
def most_viewed(field, num=5): """ Get the most viewed items from the Submission model """ top_items = get_pagehits(field) top_items.sort(reverse=True) out = [] for score, pk in top_items[:num]: out.append(Submission.objects.get(id=pk)) out[-1].score = score return out
def most_viewed(field, num=5): """ Get the most viewed items from the Submission model """ top_items = get_pagehits(field) top_items.sort(reverse=True) out = [] for score, pk in top_items[:num]: try: out.append(Submission.objects.get(pk=pk)) out[-1].score = score except (Submission.DoesNotExist, IndexError): pass return out