Exemplo n.º 1
0
def book_list(request):
    """
    Shows a list of all the books listed.
    Does pagination, sorting and filtering.
    
    Tests:
        - GETTest
        - SearchBookTest
        - SortBookTest
    """
    house_cleaning()
    # Filter for the search box
    if request.method == 'GET':
        filter_form = FilterForm(request.GET)
        if filter_form.is_valid():
            cd = filter_form.cleaned_data
            all_books = Book.objects.all()
            books = book_filter(cd['filter'], cd['field'], all_books)
        elif request.GET.has_key("sort_by") and request.GET.has_key("dir"):
            books = book_sort(request.GET["sort_by"], request.GET["dir"])
        else:
            books = Book.objects.all()

    # Filter according to permissions
    if not request.user.is_staff:
        # Non staff can only see books which are for sale.
        books = filter(lambda x: x.status == 'F', books)
    # Staff want to see the unsold books first so if we sort them ascending, that should do
    else:
        # This alphabet is the order in which book statuses should be displayed
        alphabet = "AFOPMTSD"
        # Sort by the index value of the book status in the alphabet
        books = sorted(books, key=lambda book: [alphabet.index(book.status)])

    # Pagination
    page_num = get_number(request.GET, 'page', PAGE_NUM)
    books_per_page = get_number(request.GET, 'per_page', PER_PAGE)

    paginator = Paginator(books, books_per_page)
    try:
        page_of_books = paginator.page(page_num)
    except (EmptyPage, InvalidPage):
        page_of_books = paginator.page(paginator.num_pages)

    # Template time
    if request.GET.get('dir', '') == 'asc': dir = 'desc'
    else: dir = 'asc'
    var_dict = {
        'books': page_of_books,
        'per_page': books_per_page,
        'page': page_num,
        'field': request.GET.get('field', 'any_field'),
        'filter_text': request.GET.get('filter', ''),
        'dir': dir
    }
    return rtr('books/book_list.html', var_dict, context_instance=RC(request))
Exemplo n.º 2
0
def setting_list(request):
    """
    Shows a list of all the application settings.
    Does pagination, sorting and filtering.
    
    Tests:
    """
    house_cleaning()
    # Filter for the search box
    if request.method == 'GET':
        filter_form = FilterForm(request.GET)
        if filter_form.is_valid():
            cd = filter_form.cleaned_data
            all_settings = AppSetting.objects.all()
            settings = settings_filter(cd['filter'], cd['field'], all_settings)
        elif request.GET.has_key("sort_by") and request.GET.has_key("dir"):
            settings = setting_sort(request.GET["sort_by"], request.GET["dir"])
        else:
            settings = AppSetting.objects.all()


# This filter for permissions was copied from the books view but probably isn't needed here
# Filter according to permissions
#    if not request.user.is_staff:
# Non staff can only see books which are for sale.
#        books = filter(lambda x: x.status == 'F', books)

# Pagination
    page_num = get_number(request.GET, 'page', PAGE_NUM)
    settings_per_page = get_number(request.GET, 'per_page', PER_PAGE)

    paginator = Paginator(settings, settings_per_page)
    try:
        page_of_settings = paginator.page(page_num)
    except (EmptyPage, InvalidPage):
        page_of_settings = paginator.page(paginator.num_pages)

    # Template time
    if request.GET.get('dir', '') == 'asc': dir = 'desc'
    else: dir = 'asc'
    var_dict = {
        'appsettings': page_of_settings,
        'per_page': settings_per_page,
        'page': page_num,
        'field': request.GET.get('field', 'any_field'),
        'filter_text': request.GET.get('filter', ''),
        'dir': dir
    }
    return rtr('appsettings/settings_list.html',
               var_dict,
               context_instance=RC(request))