Exemplo n.º 1
0
def library(request,
          page_number=settings.DEFAULT_START_PAGE, 
          order=settings.DEFAULT_ORDER_FIELD,
          dir=settings.DEFAULT_ORDER_DIRECTION):
    '''Users's library listing.  The page itself should not be cached although 
    individual items in the library should be cached at the model level.'''
    if not dir in settings.VALID_ORDER_DIRECTIONS:
        raise Exception("Direction %s was not in our list of known ordering directions" % dir)
    if not order in settings.VALID_ORDER_FIELDS:
        raise Exception("Field %s was not in our list of known ordering fields" % order)
    if dir == 'desc':
        order_computed = '-%s' % order
        reverse_dir = 'asc'
    else:
        order_computed = order
        reverse_dir = 'desc'
  
    if page_number is None:
        page_number = settings.DEFAULT_START_PAGE
    user = request.user
    form = EpubValidateForm()
    paginator = Paginator(EpubArchive.objects.filter(user_archive__user=user).order_by(order_computed).distinct(), settings.DEFAULT_NUM_RESULTS)
    try:
        page = paginator.page(page_number)
    except EmptyPage:
        page_number = settings.DEFAULT_START_PAGE
        # If we somehow got to an invalid page number, start over
        return HttpResponseRedirect(reverse('library-paginate', args=[page_number]))

    for d in page.object_list:
        if d.orderable_author == '':
            # Populate the author field if it was empty before
            d.orderable_author = d.safe_author()
            d.save()

    sysinfo = SystemInfo()
    return direct_to_template(request, 'index.html', {'documents':paginator, 
                                             'page':page,
                                             'form':form,
                                             'order':order,
                                             'dir':dir,
                                             'total_users':sysinfo.get_total_users(),
                                             'total_books':sysinfo.get_total_books(),
                                             'reverse_dir':reverse_dir,
                                             'order_text': order_fields[order],
                                             'order_direction': _('ascending') if dir == 'asc' else _('descending'),
                                             'order_adverb': _('alphabetically') if order != 'created_time' else _('by date value'),
                                             }
                              )