Exemplo n.º 1
0
    def clean_isbn(self):
        '''Converts the user inputted isbn into a valid ISBN-13, or
           raises a validation error if the isbn does not exist or is
           incorrectly formatted'''

        # check formatting
        isbn = cleanisbn(self.cleaned_data['isbn'])
        if isbn == '':
            raise forms.ValidationError('The ISBN you entered is not valid')

        # check if book exists either here at PTX or at amazon
        try:
            if len(isbn) == 13:
                book = Book.objects.get(isbn13=isbn)
            elif len(isbn) == 10:
                book = Book.objects.get(isbn10=isbn)

        except Book.DoesNotExist:
            # book does not exist in PTX, so try fetching it from amazon
            book = book_details(isbn)
            if book == None:
                raise forms.ValidationError(
                    'A book with this ISBN could not be found')
            book.save()

        return book.isbn13
Exemplo n.º 2
0
    def clean_isbn(self):
        '''Converts the user inputted isbn into a valid ISBN-13, or
           raises a validation error if the isbn does not exist or is
           incorrectly formatted'''

        # check formatting
        isbn = cleanisbn(self.cleaned_data['isbn'])
        if isbn == '':
            raise forms.ValidationError('The ISBN you entered is not valid')

        # check if book exists either here at PTX or at amazon
        try:
            if len(isbn) == 13:
                book = Book.objects.get(isbn13=isbn)
            elif len(isbn) == 10:
                book = Book.objects.get(isbn10=isbn)

        except Book.DoesNotExist:
            # book does not exist in PTX, so try fetching it from amazon
            book = book_details(isbn)
            if book == None:
                raise forms.ValidationError('A book with this ISBN could not be found')
            book.save()

        return book.isbn13
Exemplo n.º 3
0
def process_add(request, form_data):
    '''Returns the added book if add is successful, None if the field is blank, 
    or raises Book.DoesNotExist if no such book is found'''

    form = AddForm(form_data)

    if not form.is_valid():
        raise Book.DoesNotExist

    # Clean up the ISBN, or stop if there has been no ISBN entered
    isbn = form.cleaned_data['add']
    if len(isbn) == 0: return None
    isbn = cleanisbn(isbn)

    # Get the book, or raise the exception that it does not exist
    if len(isbn) == 13: 
        try:
            book = Book.objects.get(isbn13=isbn)
        except Book.DoesNotExist:
            book = book_details(isbn)
            if book != None: book.save()
            else: raise Book.DoesNotExist

    elif len(isbn) == 10: 
        try:
            book = Book.objects.get(isbn10=isbn)
        except Book.DoesNotExist:
            book = book_details(isbn)
            if book != None: book.save()
            else: raise Book.DoesNotExist

    else:
        raise Book.DoesNotExist

    # Check that the item is not already in the wishlist
    user, created = User.objects.get_or_create(net_id=request.user.username)
    req_list = Request.objects.filter(user=user,
            status='o', book=book)
    if len(req_list) > 0:
        raise AlreadyInWishlist

    req = Request(user=user, book = book, status = 'o', maxprice = 0)
    req.save()

    return book
Exemplo n.º 4
0
def process_add(request, form_data):
    '''Returns the added book if add is successful, None if the field is blank, 
    or raises Book.DoesNotExist if no such book is found'''

    form = AddForm(form_data)

    if not form.is_valid():
        raise Book.DoesNotExist

    # Clean up the ISBN, or stop if there has been no ISBN entered
    isbn = form.cleaned_data['add']
    if len(isbn) == 0: return None
    isbn = cleanisbn(isbn)

    # Get the book, or raise the exception that it does not exist
    if len(isbn) == 13:
        try:
            book = Book.objects.get(isbn13=isbn)
        except Book.DoesNotExist:
            book = book_details(isbn)
            if book != None: book.save()
            else: raise Book.DoesNotExist

    elif len(isbn) == 10:
        try:
            book = Book.objects.get(isbn10=isbn)
        except Book.DoesNotExist:
            book = book_details(isbn)
            if book != None: book.save()
            else: raise Book.DoesNotExist

    else:
        raise Book.DoesNotExist

    # Check that the item is not already in the wishlist
    user, created = User.objects.get_or_create(net_id=request.user.username)
    req_list = Request.objects.filter(user=user, status='o', book=book)
    if len(req_list) > 0:
        raise AlreadyInWishlist

    req = Request(user=user, book=book, status='o', maxprice=0)
    req.save()

    return book
Exemplo n.º 5
0
def process(request, step, ticket):
    # this function only processes POST requests
    if request.method != 'POST':
        raise PermissionDenied()

    step = int(step)

    if step == ChooseCourseForm.PROC:
        form = ChooseCourseForm(request.POST)

        if form.is_valid():
            # break down the course input
            course = form.cleaned_data['course']
            m = COURSE.match(course).groups()
            course_dept = m[0].upper()
            course_num = int(m[1])

            # does it exist? if not, add it
            # TODO: MORE CHECKING HERE FOR INVALID DEPARTMENTS, ETC
            course_list = Course.objects.filter(dept__exact=course_dept,
                                                num__exact=course_num)
            if len(course_list) == 1:
                c = course_list[0]
            elif len(course_list) == 0:
                c = Course(dept=course_dept, num=course_num)
                c.save()
            else:
                # This can't really happen
                raise PermissionDenied

            # save the internal course id for later
            request.session['request_course_' + ticket] = c

            # show the list of books for the course
            return render_form(ChooseBookForm(c), '', ticket, request)

        else:
            return render_form(ChooseCourseForm(request.POST), '', ticket,
                               request)

    elif step == ChooseBookForm.PROC:
        course = request.session['request_course_' + ticket]
        form = ChooseBookForm(course, request.POST)

        if form.is_valid():
            isbn_from_db = form.cleaned_data['isbn_from_db']
            isbn_from_user = form.cleaned_data['isbn_from_user']

            if isbn_from_db != None:
                # book exists already, let the user list this book
                request.session['request_book_' + ticket] = isbn_from_db
                return render_form(RequestForm(), '', ticket, request)

            elif isbn_from_user != None:
                # check if book exists in DB, just not associated with that class
                if Book.objects.filter(isbn13=isbn_from_user).count() == 0:
                    # book does not exist, let the user create it
                    #form = AddBookForm()
                    #form.fields['isbn13'].initial = isbn_from_user
                    bookinfo = book_details(isbn_from_user)
                    if bookinfo == None:
                        return render_form(form, '', ticket, request)
                    else:
                        set_ticket_attr(request, ticket, 'bookinfo', bookinfo)
                        return render_bookinfo(bookinfo, ticket, request)
                else:
                    # book needs new class reference added.
                    book = Book.objects.get(isbn13=isbn_from_user)
                    book.course.add(course)
                    book.save()
                    request.session['request_book_' + ticket] = book
                    return render_form(RequestForm(), '', ticket, request)

            else:
                # no book chosen, go back and make the user choose a book
                return render_form(form, '', ticket, request)

        else:
            return render_form(form, '', ticket, request)

    elif step == PROCESS_ADD_BOOK:
        bookinfo = get_ticket_attr(request, ticket, 'bookinfo')

        # TODO: MAKE SURE AGAIN THAT THE ISBN IS NOT ALREADY IN THE DATABASE
        book = Book(isbn13=bookinfo['isbn13'],
                    isbn10=bookinfo['isbn10'],
                    title=bookinfo['title'],
                    desc='',
                    author=bookinfo['author'],
                    edition=bookinfo['edition'],
                    year=bookinfo['year'],
                    publisher=bookinfo['publisher'],
                    list_price=0,
                    imagename=bookinfo['img_name'])

        course = request.session['request_course_' + ticket]
        book.course.add(course)
        book.save()

        request.session['request_book_' + ticket] = book

        return render_form(RequestForm(), '', ticket, request)

    elif step == RequestForm.PROC:
        form = RequestForm(request.POST)
        if form.is_valid():
            f = form.cleaned_data

            book = request.session['request_book_' + ticket]
            user, created = User.objects.get_or_create(
                net_id=request.user.username)
            maxprice = form.cleaned_data['maxprice']
            the_request = Request(book=book, user=user, maxprice=maxprice)

            the_request.save()

            reset_request_session(request, ticket)

            # redirect to thank you page
            return HttpResponseRedirect('/request/thankyou?b=%s' % (book))

        else:
            return render_form(form, '', ticket, request)

    return HttpResponse(step)
Exemplo n.º 6
0
def process(request, step, ticket):
    # this function only processes POST requests
    if request.method != 'POST':
        raise PermissionDenied()

    step = int(step)

    if step == ChooseCourseForm.PROC:
        form = ChooseCourseForm(request.POST)

        if form.is_valid():
            # break down the course input
            course = form.cleaned_data['course']
            m = COURSE.match(course).groups()
            course_dept = m[0].upper()
            course_num = int(m[1])

            # does it exist? if not, add it
            # TODO: MORE CHECKING HERE FOR INVALID DEPARTMENTS, ETC
            course_list = Course.objects.filter(dept__exact=course_dept, num__exact=course_num)
            if len(course_list) == 1:
                c = course_list[0]
            elif len(course_list) == 0:
                c = Course(dept=course_dept, num=course_num)
                c.save()
            else:
                # This can't really happen
                raise PermissionDenied

            # save the internal course id for later
            request.session['request_course_' + ticket] = c

            # show the list of books for the course
            return render_form(ChooseBookForm(c), '', ticket, request)

        else:
            return render_form(ChooseCourseForm(request.POST), '', ticket, request)

    elif step == ChooseBookForm.PROC:
        course = request.session['request_course_' + ticket]
        form = ChooseBookForm(course, request.POST)

        if form.is_valid():
            isbn_from_db = form.cleaned_data['isbn_from_db']
            isbn_from_user = form.cleaned_data['isbn_from_user']

            if isbn_from_db != None:
                # book exists already, let the user list this book
                request.session['request_book_' + ticket] = isbn_from_db
                return render_form(RequestForm(), '', ticket, request)

            elif isbn_from_user != None:
                # check if book exists in DB, just not associated with that class
                if Book.objects.filter(isbn13=isbn_from_user).count() == 0:
                    # book does not exist, let the user create it
                    #form = AddBookForm()
                    #form.fields['isbn13'].initial = isbn_from_user
                    bookinfo = book_details(isbn_from_user)
                    if bookinfo == None:
                        return render_form(form, '', ticket, request)
                    else:
                        set_ticket_attr(request, ticket, 'bookinfo', bookinfo)
                        return render_bookinfo(bookinfo, ticket, request)
                else:
                    # book needs new class reference added.
                    book = Book.objects.get(isbn13=isbn_from_user)
                    book.course.add(course)
                    book.save()
                    request.session['request_book_' + ticket] = book
                    return render_form(RequestForm(), '', ticket, request)

            else:
                # no book chosen, go back and make the user choose a book
                return render_form(form, '', ticket, request)

        else:
            return render_form(form, '', ticket, request)

    elif step == PROCESS_ADD_BOOK:
        bookinfo = get_ticket_attr(request, ticket, 'bookinfo')

        # TODO: MAKE SURE AGAIN THAT THE ISBN IS NOT ALREADY IN THE DATABASE
        book = Book(
                isbn13  = bookinfo['isbn13'],
                isbn10  = bookinfo['isbn10'],
                title   = bookinfo['title'],
                desc  = '',
                author  = bookinfo['author'],
                edition  = bookinfo['edition'],
                year   = bookinfo['year'],
                publisher   = bookinfo['publisher'],
                list_price   = 0,
                imagename = bookinfo['img_name'])

        course = request.session['request_course_' + ticket]
        book.course.add(course)
        book.save()

        request.session['request_book_' + ticket] = book

        return render_form(RequestForm(), '', ticket, request)

    elif step == RequestForm.PROC:
        form = RequestForm(request.POST)
        if form.is_valid():
            f = form.cleaned_data

            book = request.session['request_book_' + ticket]
            user, created = User.objects.get_or_create(net_id=request.user.username)
            maxprice = form.cleaned_data['maxprice']
            the_request = Request(
                    book=book,
                    user=user,
                    maxprice=maxprice)

            the_request.save()

            reset_request_session(request, ticket)

            # redirect to thank you page
            return HttpResponseRedirect('/request/thankyou?b=%s' % (book))

        else:
            return render_form(form, '', ticket, request)

    return HttpResponse(step)