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 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.º 3
0
def my_books(request):
    """
    Displays books the user has on hold
    and is selling, sorts by search box, filters, calculates total prices
    
    Tests: GETTest
    """
    #gets users books
    selling = Book.objects.filter(seller = request.user)  
    holding = Book.objects.filter(holder = request.user)    
    priceHold = 0
    priceSell = 0
    searched = False
    #calculate totals for book
    for book in holding:
        priceHold = book.price + priceHold         
    for book in selling:
        priceSell = book.price + priceSell
    
  
    # Filter for the search box
    if request.GET.has_key("filter") and request.GET.has_key("field"):
        # only run the filter if the GET args are there
        selling = book_filter(request.GET["filter"] , request.GET["field"],
                                  selling)
        holding = book_filter(request.GET["filter"] , request.GET["field"],
                                  holding)
        searched = True
    # Sorts results by request
    elif request.GET.has_key("sort_by") and request.GET.has_key("dir"):
        holding = book_sort(request.GET["sort_by"], request.GET["dir"])
        holding = holding.filter(holder = request.user)
    elif request.GET.has_key("sort_with") and request.GET.has_key("dir"):
        selling = book_sort(request.GET["sort_with"], request.GET["dir"])
        selling = selling.filter(seller = request.user)
   
    var_dict = {
         'sellP' : selling,
         'holdP' : holding,
         'priceH' : priceHold,
         'priceS' : priceSell,
         'field' : request.GET.get('field', 'any_field'),
         'filter_text' : request.GET.get('filter', ''),
         'search' : searched
    }             
    template = 'books/my_books.html'
    return rtr(template, var_dict, context_instance=RC(request))    
    
    # Save New User
    if action == "Save":
        role = request.POST.get("role", '')
        try:
            user = User.objects.get(id = student_id)
        except User.DoesNotExist:
            user = import_user(student_id)
            if user == None:
                message = "Invalid Student ID: %s" % student_id
                return tidy_error(request, message)
        if request.POST.get("role", '') == "Administrator":
            user.is_superuser = True
            user.is_staff = True
            user.save()
        else:
            user.is_superuser = False
            user.is_staff = True
            user.save()
        var_dict = {
            'user_name' : user.get_full_name(),
            'administrator' : user.is_superuser,
        }
        template = 'books/update_staff/saved.html'
        return rtr(template, var_dict, context_instance=RC(request))
Exemplo n.º 4
0
def my_books(request):
    """
    Displays books the user has on hold
    and is selling, sorts by search box, filters, calculates total prices
    
    Tests: GETTest
    """
    #gets users books
    selling = Book.objects.filter(seller=request.user)
    holding = Book.objects.filter(holder=request.user)
    priceHold = 0
    priceSell = 0
    searched = False
    #calculate totals for book
    for book in holding:
        priceHold = book.price + priceHold
    for book in selling:
        priceSell = book.price + priceSell

    # Filter for the search box
    if request.GET.has_key("filter") and request.GET.has_key("field"):
        # only run the filter if the GET args are there
        selling = book_filter(request.GET["filter"], request.GET["field"],
                              selling)
        holding = book_filter(request.GET["filter"], request.GET["field"],
                              holding)
        searched = True
    # Sorts results by request
    elif request.GET.has_key("sort_by") and request.GET.has_key("dir"):
        holding = book_sort(request.GET["sort_by"], request.GET["dir"])
        holding = holding.filter(holder=request.user)
    elif request.GET.has_key("sort_with") and request.GET.has_key("dir"):
        selling = book_sort(request.GET["sort_with"], request.GET["dir"])
        selling = selling.filter(seller=request.user)

    var_dict = {
        'sellP': selling,
        'holdP': holding,
        'priceH': priceHold,
        'priceS': priceSell,
        'field': request.GET.get('field', 'any_field'),
        'filter_text': request.GET.get('filter', ''),
        'search': searched
    }
    template = 'books/my_books.html'
    return rtr(template, var_dict, context_instance=RC(request))

    # Save New User
    if action == "Save":
        role = request.POST.get("role", '')
        try:
            user = User.objects.get(id=student_id)
        except User.DoesNotExist:
            user = import_user(student_id)
            if user == None:
                message = "Invalid Student ID: %s" % student_id
                return tidy_error(request, message)
        if request.POST.get("role", '') == "Administrator":
            user.is_superuser = True
            user.is_staff = True
            user.save()
        else:
            user.is_superuser = False
            user.is_staff = True
            user.save()
        var_dict = {
            'user_name': user.get_full_name(),
            'administrator': user.is_superuser,
        }
        template = 'books/update_staff/saved.html'
        return rtr(template, var_dict, context_instance=RC(request))