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
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
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
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
def search(request): """Given a search query argument, treats each token as another filter on the set of all books. Two types of filters are recognized: ISBN and strings.""" book_list = Book.objects.all() st = request.GET.get("s") if st: # Progressively whittle down the entire book_list. for token in st.split(): clean_isbn = cleanisbn(token) if len(clean_isbn) > 0: query = Q(isbn13__icontains=clean_isbn) query = query | Q(isbn10__icontains=clean_isbn) else: query = Q(title__icontains=token) query = query | Q(author__icontains=token) query = query | Q(course__dept__icontains=token) query = query | Q(course__num__icontains=token) book_list = book_list.filter(query) book_list = book_list.distinct().order_by('title') showofferings = any(book.hasOfferings() for book in book_list) showunoffered = any(not book.hasOfferings() for book in book_list) # Dictionary for displaying stuff on template data = { 'book_list': book_list, 'showofferings': showofferings, 'showunoffered': showunoffered, 'num_books': len(book_list), 'st': st } # Render to template return render_to_response(request, 'ptx/browsebooks.html', data)
def search(request): """Given a search query argument, treats each token as another filter on the set of all books. Two types of filters are recognized: ISBN and strings.""" book_list = Book.objects.all() st = request.GET.get("s") if st: # Progressively whittle down the entire book_list. for token in st.split(): clean_isbn = cleanisbn(token) if len(clean_isbn) > 0: query = Q(isbn13__icontains=clean_isbn) query = query | Q(isbn10__icontains=clean_isbn) else: query = Q(title__icontains=token) query = query | Q(author__icontains=token) query = query | Q(course__dept__icontains=token) query = query | Q(course__num__icontains=token) book_list = book_list.filter(query) book_list = book_list.distinct().order_by('title') showofferings = any(book.hasOfferings() for book in book_list) showunoffered = any(not book.hasOfferings() for book in book_list) # Dictionary for displaying stuff on template data = {'book_list': book_list, 'showofferings': showofferings, 'showunoffered': showunoffered, 'num_books': len(book_list), 'st': st} # Render to template return render_to_response(request, 'ptx/browsebooks.html', data)