示例#1
0
def add_new_book(request):
    """
    Tests:
        - GETTest
        - AddNewBookTest
        - SecurityTest
        - NotAllowedTest
    """
    if not request.method == 'POST':
        t = loader.get_template('405.html')
        c = RC(request)
        return HttpResponseNotAllowed(t.render(c), ['POST'])
    # User must be staff or admin to get to this page
    if not request.user.is_staff:
        t = loader.get_template('403.html')
        c = RC(request)
        return HttpResponseForbidden(t.render(c))
    if request.POST.get("Action", '') == 'Add':
        form = NewBookForm(request.POST)
        if form.is_valid():
            # This came from the add_book view, and we need to
            # create a book and a metabook
            barcode = form.cleaned_data['barcode']
            price = form.cleaned_data['price']
            sid = form.cleaned_data['seller']
            author = form.cleaned_data['author']
            title = form.cleaned_data['title']
            ed = form.cleaned_data['edition']
            dept = form.cleaned_data['department']
            course_num = form.cleaned_data['course_number']

            metabook = MetaBook(barcode=barcode, author=author, title=title, edition=ed)
            metabook.save()
            goc = Course.objects.get_or_create
            course, created = goc(department=dept, number=course_num)
            metabook.courses.add(course)
            metabook.save()
            try:
                seller = User.objects.get(pk=sid)
            except User.DoesNotExist:
                seller = import_user(sid)
                if seller == None:
                    message = "Invalid Student ID: %s" % sid
                    return tidy_error(request, message)
            book = Book(seller=seller, price=Decimal(price), metabook=metabook)
            book.status = 'F'
            book.save()
            Log(book=book, who=request.user, action='A').save()

            var_dict = {
                'title' : metabook.title,
                'author' : metabook.author,
                'seller_name' : seller.get_full_name(),
                'book_id' : book.id,
            }
            template = 'books/update_book/added.html'
            return rtr(template, var_dict, context_instance=RC(request))
        var_dict = {'form' : form}
        template = 'books/add_new_book.html'
        return rtr(template, var_dict, context_instance=RC(request))
示例#2
0
def add_book(request):
    """
    Tests:
        - GETTest
        - SecurityTest
    """
    # User must be staff or admin to get to this page
    if not request.user.is_staff:
        t = loader.get_template('403.html')
        c = RC(request)
        return HttpResponseForbidden(t.render(c))
    if request.method == "POST":
        form = BookForm(request.POST)
        if form.is_valid():
            student_id = form.cleaned_data['seller']
            price = form.cleaned_data['price']
            barcode = form.cleaned_data['barcode']
            try:
                metabook = MetaBook.objects.get(barcode=barcode)
            except MetaBook.DoesNotExist: 
                initial = {
                    'barcode' : barcode,
                    'seller' : student_id,
                    'price' : price,
                    'edition' : '1',
                }
                form = NewBookForm(initial=initial)
                var_dict = {'form' : form}
                template = 'books/add_new_book.html'
                return rtr(template, var_dict, context_instance=RC(request))
            try:
                seller = User.objects.get(id=student_id)
            except User.DoesNotExist:
                seller = import_user(student_id)
                if seller == None:
                    message = "Invalid Student ID: %s" % student_id
                    return tidy_error(request, message)
            book = Book(price=price, status="F", metabook=metabook, seller=seller)
            book.save()
            Log(book=book, who=request.user, action='A').save()
            var_dict = {
                'title' : metabook.title,
                'book_id' : book.id
            }
            template = 'books/update_book/added.html'
            return rtr(template, var_dict, context_instance=RC(request))
        # the form isn't valid. send the user back.
        var_dict = {'form' : form}
        template = 'books/add_book.html'
        return rtr(template, var_dict, context_instance=RC(request))
    else:
        # the user is hitting the page for the first time
        form = BookForm()
        var_dict = {'form' : form}
        template = 'books/add_book.html'
        return rtr(template, var_dict, context_instance=RC(request))
示例#3
0
def add_book(request):
    """
    Tests:
        - GETTest
        - SecurityTest
    """
    # User must be staff or admin to get to this page
    if not request.user.is_staff:
        t = loader.get_template('403.html')
        c = RC(request)
        return HttpResponseForbidden(t.render(c))
    if request.method == "POST":
        form = BookForm(request.POST)
        if form.is_valid():
            student_id = form.cleaned_data['seller']
            price = form.cleaned_data['price']
            barcode = form.cleaned_data['barcode']
            try:
                metabook = MetaBook.objects.get(barcode=barcode)
            except MetaBook.DoesNotExist:
                initial = {
                    'barcode': barcode,
                    'seller': student_id,
                    'price': price,
                    'edition': '1',
                }
                form = NewBookForm(initial=initial)
                var_dict = {'form': form}
                template = 'books/add_new_book.html'
                return rtr(template, var_dict, context_instance=RC(request))
            try:
                seller = User.objects.get(id=student_id)
            except User.DoesNotExist:
                seller = import_user(student_id)
                if seller == None:
                    message = "Invalid Student ID: %s" % student_id
                    return tidy_error(request, message)
            book = Book(price=price,
                        status="F",
                        metabook=metabook,
                        seller=seller)
            book.save()
            Log(book=book, who=request.user, action='A').save()
            var_dict = {'title': metabook.title, 'book_id': book.id}
            template = 'books/update_book/added.html'
            return rtr(template, var_dict, context_instance=RC(request))
        # the form isn't valid. send the user back.
        var_dict = {'form': form}
        template = 'books/add_book.html'
        return rtr(template, var_dict, context_instance=RC(request))
    else:
        # the user is hitting the page for the first time
        form = BookForm()
        var_dict = {'form': form}
        template = 'books/add_book.html'
        return rtr(template, var_dict, context_instance=RC(request))
示例#4
0
    def setUp(self):
        self.client.login(username=TEST_USERNAME, password=PASSWORD)
        self.get_data = {'field': '', 'filter': ''}

        self.course = Course(department='ENGL', number='103')
        self.course.save()

        metabook = MetaBook(title=self.TITLE, author=self.AUTHOR)
        metabook.barcode = self.BARCODE
        metabook.edition = self.EDITION
        metabook.save()
        metabook.courses.add(self.course)

        seller = User.objects.get(pk=3)

        self.book = Book(metabook=metabook, seller=seller)
        self.book.price = Decimal('1.01')
        self.book.save()
示例#5
0
    def setUp(self):
        self.client.login(username=TEST_USERNAME, password=PASSWORD)
        self.get_data = {"field": "", "filter": ""}

        self.course = Course(department="ENGL", number="103")
        self.course.save()

        metabook = MetaBook(title=self.TITLE, author=self.AUTHOR)
        metabook.barcode = self.BARCODE
        metabook.edition = self.EDITION
        metabook.save()
        metabook.courses.add(self.course)

        seller = User.objects.get(pk=3)

        self.book = Book(metabook=metabook, seller=seller)
        self.book.price = Decimal("1.01")
        self.book.save()
示例#6
0
class SearchBookTest(TestCase):
    fixtures = ["test_empty.json"]
    TITLE = "The Silmarillion"
    AUTHOR = "J.R.R. Tolkien"
    BARCODE = "9780618391110"
    EDITION = 1

    def setUp(self):
        self.client.login(username=TEST_USERNAME, password=PASSWORD)
        self.get_data = {"field": "", "filter": ""}

        self.course = Course(department="ENGL", number="103")
        self.course.save()

        metabook = MetaBook(title=self.TITLE, author=self.AUTHOR)
        metabook.barcode = self.BARCODE
        metabook.edition = self.EDITION
        metabook.save()
        metabook.courses.add(self.course)

        seller = User.objects.get(pk=3)

        self.book = Book(metabook=metabook, seller=seller)
        self.book.price = Decimal("1.01")
        self.book.save()

    # Barcode
    def test_barcode(self):
        """ Searching for barcodes in book list should work """
        self.get_data["field"] = "barcode"
        self.get_data["filter"] = self.BARCODE
        response = self.client.get("/books/", self.get_data)
        self.assertContains(response, self.TITLE)

    def test_barcode_anyfield(self):
        """ Searching for barcodes in any field """
        self.get_data["field"] = "any_field"
        self.get_data["filter"] = self.BARCODE
        response = self.client.get("/books/", self.get_data)
        self.assertContains(response, self.TITLE)

    # Title
    def test_title(self):
        """ Searching for title """
        self.get_data["field"] = "title"
        self.get_data["filter"] = self.TITLE
        response = self.client.get("/books/", self.get_data)
        self.assertContains(response, self.AUTHOR)

    def test_title_anyfield(self):
        """ Searching for title in any field """
        self.get_data["field"] = "any_field"
        self.get_data["filter"] = self.TITLE
        response = self.client.get("/books/", self.get_data)
        self.assertContains(response, self.AUTHOR)

    # Author
    def test_author(self):
        """ Searching for author """
        self.get_data["field"] = "author"
        self.get_data["filter"] = self.AUTHOR
        response = self.client.get("/books/", self.get_data)
        self.assertContains(response, self.TITLE)

    def test_author_anyfield(self):
        """ Searching for author in Any Field """
        self.get_data["field"] = "any_field"
        self.get_data["filter"] = self.AUTHOR
        response = self.client.get("/books/", self.get_data)
        self.assertContains(response, self.TITLE)

    # Course Code
    def test_course_code(self):
        """ Searching for course code """
        self.get_data["field"] = "course_code"
        self.get_data["filter"] = self.course.code()
        response = self.client.get("/books/", self.get_data)
        self.assertContains(response, self.AUTHOR)

    def test_course_code_anyfield(self):
        """ Searching for course code in any field """
        self.get_data["field"] = "any_field"
        self.get_data["filter"] = self.course.code()
        response = self.client.get("/books/", self.get_data)
        self.assertContains(response, self.AUTHOR)

    # Ref #
    def test_refno(self):
        """ Searching for course code """
        self.get_data["field"] = "ref_no"
        self.get_data["filter"] = self.EDITION
        response = self.client.get("/books/", self.get_data)
        self.assertContains(response, self.AUTHOR)

    def test_refno_anyfield(self):
        """ Searching for course code in any field """
        self.get_data["field"] = "any_field"
        self.get_data["filter"] = self.EDITION
        response = self.client.get("/books/", self.get_data)
        self.assertContains(response, self.AUTHOR)
示例#7
0
class SearchBookTest(TestCase):
    fixtures = ['test_empty.json']
    TITLE = 'The Silmarillion'
    AUTHOR = 'J.R.R. Tolkien'
    BARCODE = '9780618391110'
    EDITION = 1

    def setUp(self):
        self.client.login(username=TEST_USERNAME, password=PASSWORD)
        self.get_data = {'field': '', 'filter': ''}

        self.course = Course(department='ENGL', number='103')
        self.course.save()

        metabook = MetaBook(title=self.TITLE, author=self.AUTHOR)
        metabook.barcode = self.BARCODE
        metabook.edition = self.EDITION
        metabook.save()
        metabook.courses.add(self.course)

        seller = User.objects.get(pk=3)

        self.book = Book(metabook=metabook, seller=seller)
        self.book.price = Decimal('1.01')
        self.book.save()

    # Barcode
    def test_barcode(self):
        """ Searching for barcodes in book list should work """
        self.get_data['field'] = 'barcode'
        self.get_data['filter'] = self.BARCODE
        response = self.client.get('/books/', self.get_data)
        self.assertContains(response, self.TITLE)

    def test_barcode_anyfield(self):
        """ Searching for barcodes in any field """
        self.get_data['field'] = 'any_field'
        self.get_data['filter'] = self.BARCODE
        response = self.client.get('/books/', self.get_data)
        self.assertContains(response, self.TITLE)

    # Title
    def test_title(self):
        """ Searching for title """
        self.get_data['field'] = 'title'
        self.get_data['filter'] = self.TITLE
        response = self.client.get('/books/', self.get_data)
        self.assertContains(response, self.AUTHOR)

    def test_title_anyfield(self):
        """ Searching for title in any field """
        self.get_data['field'] = 'any_field'
        self.get_data['filter'] = self.TITLE
        response = self.client.get('/books/', self.get_data)
        self.assertContains(response, self.AUTHOR)

    # Author
    def test_author(self):
        """ Searching for author """
        self.get_data['field'] = 'author'
        self.get_data['filter'] = self.AUTHOR
        response = self.client.get('/books/', self.get_data)
        self.assertContains(response, self.TITLE)

    def test_author_anyfield(self):
        """ Searching for author in Any Field """
        self.get_data['field'] = 'any_field'
        self.get_data['filter'] = self.AUTHOR
        response = self.client.get('/books/', self.get_data)
        self.assertContains(response, self.TITLE)

    # Course Code
    def test_course_code(self):
        """ Searching for course code """
        self.get_data['field'] = 'course_code'
        self.get_data['filter'] = self.course.code()
        response = self.client.get('/books/', self.get_data)
        self.assertContains(response, self.AUTHOR)

    def test_course_code_anyfield(self):
        """ Searching for course code in any field """
        self.get_data['field'] = 'any_field'
        self.get_data['filter'] = self.course.code()
        response = self.client.get('/books/', self.get_data)
        self.assertContains(response, self.AUTHOR)

    # Ref #
    def test_refno(self):
        """ Searching for course code """
        self.get_data['field'] = 'ref_no'
        self.get_data['filter'] = self.EDITION
        response = self.client.get('/books/', self.get_data)
        self.assertContains(response, self.AUTHOR)

    def test_refno_anyfield(self):
        """ Searching for course code in any field """
        self.get_data['field'] = 'any_field'
        self.get_data['filter'] = self.EDITION
        response = self.client.get('/books/', self.get_data)
        self.assertContains(response, self.AUTHOR)
示例#8
0
def add_new_book(request):
    """
    Tests:
        - GETTest
        - AddNewBookTest
        - SecurityTest
        - NotAllowedTest
    """
    if not request.method == 'POST':
        t = loader.get_template('405.html')
        c = RC(request)
        return HttpResponseNotAllowed(t.render(c), ['POST'])
    # User must be staff or admin to get to this page
    if not request.user.is_staff:
        t = loader.get_template('403.html')
        c = RC(request)
        return HttpResponseForbidden(t.render(c))
    if request.POST.get("Action", '') == 'Add':
        form = NewBookForm(request.POST)
        if form.is_valid():
            # This came from the add_book view, and we need to
            # create a book and a metabook
            barcode = form.cleaned_data['barcode']
            price = form.cleaned_data['price']
            sid = form.cleaned_data['seller']
            author = form.cleaned_data['author']
            title = form.cleaned_data['title']
            ed = form.cleaned_data['edition']
            dept = form.cleaned_data['department']
            course_num = form.cleaned_data['course_number']

            metabook = MetaBook(barcode=barcode,
                                author=author,
                                title=title,
                                edition=ed)
            metabook.save()
            goc = Course.objects.get_or_create
            course, created = goc(department=dept, number=course_num)
            metabook.courses.add(course)
            metabook.save()
            try:
                seller = User.objects.get(pk=sid)
            except User.DoesNotExist:
                seller = import_user(sid)
                if seller == None:
                    message = "Invalid Student ID: %s" % sid
                    return tidy_error(request, message)
            book = Book(seller=seller, price=Decimal(price), metabook=metabook)
            book.status = 'F'
            book.save()
            Log(book=book, who=request.user, action='A').save()

            var_dict = {
                'title': metabook.title,
                'author': metabook.author,
                'seller_name': seller.get_full_name(),
                'book_id': book.id,
            }
            template = 'books/update_book/added.html'
            return rtr(template, var_dict, context_instance=RC(request))
        var_dict = {'form': form}
        template = 'books/add_new_book.html'
        return rtr(template, var_dict, context_instance=RC(request))