Silas Baronda committed 815dabc

* Now creates if there is no record of the book or gets if there is record of book.
* Refactored books/views add_book so it actually doesn't call create twice.

  • Participants
  • Parent commits 855662e

Comments (0)

Files changed (2)

File books/

     notes = models.TextField(blank=True)
     #checked_out_by = models.ManyToManyField(User, blank=True)
-    def save(self):
+    def save(self, force_insert=''):
         from import Book as Book2
         book = Book2(self.isbn)
         if not book.get_xml():

File books/

 def add_book(request):
     if request.method == 'POST':
       errors = []
-      from book import Book
       isbn = param(request, 'isbn')
       if len(isbn) != 10:
           errors.append("ISBN must be 10 chars")
           #return HttpResponseServerError("ISBN must be 10 chars")
-      book = Book(isbn)
-      if not book.get_xml():
-          errors.append("No ISBN with that number found: %s" % isbn)
-          #return HttpResponseNotFound("No ISBN with that number found.")
       if errors:
         return render_to_response('book/add_book.html', {'errors': errors})
-      book_data = book.parse_xml()
       from library.books.models import Book
-      b = Book(title=book_data['Title'], slug=book_data['id'], publisher=book_data['Publisher'], author=book_data['Author'], isbn=book_data['ISBN'])
-      return render_to_response('book/add_book.html', {'errors': errors, 'book': b})
+      book, created = Book.objects.get_or_create(isbn=isbn)
+      return render_to_response('book/add_book.html', {'errors': errors, 'book': book})
       return render_to_response('book/add_book.html', {})