Example #1
0
def accept_suggested_book(request, pk):
    """View function for accepting a SuggestedBook and creating a Book."""
    suggested_book = get_object_or_404(SuggestedBook, pk=pk)

    # if the request is a GET (the user requests the /suggestions/<book>/accept url)
    # create a Book from the SuggestedBook object and remove SuggestedBook
    if request.method == 'GET':
        # check to see if author already exists
        author = Author.objects.filter(name=suggested_book.author).first()
        if not author:
            author = Author(name = suggested_book.author)
            author.save()

        Book(
            title = suggested_book.title,
            author = author,
            url = suggested_book.url,
            description = suggested_book.description
        ).save()

        suggested_book.delete()

        messages.success(request, 'You accepted the suggested book.')

    # redirect to a new URL:
    return HttpResponseRedirect(request.GET.get("next"))
Example #2
0
def add(request):
    if request.POST and request.POST['Name'] and request.POST['Title'] and request.POST['ISBN_PK'] and request.POST['AuthorID_FK']:
        post = request.POST
        new_author=Author(
            AuthorID_FK = post['AuthorID_FK'],
            Name = post['Name'],
            Age = post['Age'],
            Country = post['Country'],
            )
        tmp = list(Author.objects.filter(Name=post['Name']))
        tmp0 = list(Author.objects.filter(AuthorID_FK=post['AuthorID_FK']))
        if tmp0 != []:
            return render_to_response("addauerror.html")
        else:
            if tmp == [] :
                new_author.save()
            else:
                new_author=Author.objects.filter(Name=post['Name'])[0]
        new_book = Book(
            ISBN_PK = post["ISBN_PK"],
            Title = post["Title"],
            AuthorID_FK = new_author,
            Publisher = post["Publisher"],
            PublishDate = post["PublishDate"],   
            Price = post["Price"],
            )
        tmp1 = list(Book.objects.filter(ISBN_PK=post['ISBN_PK']))
        if tmp1!=[]:
            return render_to_response("addboerror.html")
        else:
            new_book.save()
        return render_to_response("addbosuccess.html")
    else:
        return render_to_response("addboerror1.html")
Example #3
0
def updateauthor(request):
    if request.method == "POST":
        title = request.POST["Title"]
        authorid = request.POST["AuthorID"]
        name = request.POST["Name"]
        age = request.POST["Age"]
        country = request.POST["Country"]
        
        try:
            Author.objects.get(AuthorID = authorid)
        except:
            newauthor = Author(AuthorID = authorid,
                           Name = name,
                           Age = age,
                           Country = country)
            newauthor.save()
            Book.objects.filter(Title = title).update(AuthorID = newauthor)
            return  HttpResponseRedirect("/booklist/")
        else:
            author = Author.objects.get(AuthorID = authorid)
            author.Name = name
            author.Age = age
            author.Country = country
            author.save()
            Book.objects.filter(Title = title).update(AuthorID = author)
            return  HttpResponseRedirect("/booklist/")
Example #4
0
 def handle(self, *args, **options):
     path = options['path']
     with open(path, 'r') as f:
         for i, name in enumerate(f.readlines()):
             if i == 0:
                 continue
             author = Author(name=self.clear_string(name))
             author.save()
Example #5
0
 def post(self, request):
     a = Author(name=request.POST['author_name'])
     try:
         a.save()
     except Exception as e:
         output = "Whoops! There was an error: " + str(e)
     else:
         output = 'Success! We added an author named: ' + a.name
     return HttpResponse(output)
Example #6
0
def addwriter(request):
	AUTHOR = Author()
	AUTHOR.AuthorID = request.POST['author_ID']
	AUTHOR.Age = request.POST['author_age']
	AUTHOR.Country = request.POST['author_country']
	AUTHOR.Name = request.POST['author_name']
	AUTHOR.save()
	AuthorList = Author.objects.all()
	return render_to_response('addbook.html') 
Example #7
0
def add_author(request):
    if request.POST:
        post = request.POST
        new_author = Author(
            AuthorID=post["AuthorID"],
            Name = post["Name"],
            Age = post["Age"],
            Country = post["Country"])
        new_author.save()
    return render_to_response("add_author.html")
Example #8
0
class AuthorTest(TestCase):
    def setUp(self):
        self.author = Author(name='John Doe')
        self.author.save()

    def test_create(self):
        'Author may be saved'
        self.assertEqual(1, self.author.pk)

    def test_str(self):
        'Author string representation may be the name.'
        self.assertEqual('John Doe', str(self.author))
Example #9
0
    def init_author(self):
        # Remove existing Author models from the DB.
        Author.objects.all().delete()

        first_names = ["Fred", "George", "Juan", "Pablo", "Rohit", "Graham", "Jessica"]
        last_names = ["Salvador", "Gonzales", "Miller", "Walker", "Crofford", "Adams"]
        for i in range(1, 100):
            # Create a random name for the author.
            name = self.rand_join(' ', first_names, last_names)
            # Save the author in the DB.
            author = Author(name=name)
            author.save()
Example #10
0
def add_author(request):
    if request.method == 'GET':
        output = FORM_HTML % request.path
    else:
        a = Author(name=request.POST['author_name'])
        try:
            a.save()
        except Exception as e:
            output = "Whoops! There was an error: " + str(e)
        else:
            output = 'Success! We added an author named: ' + a.name

    return HttpResponse(output)
Example #11
0
def AddBook2(request):
	AUTHOR = Author()
	AUTHOR.AulthorID = request.POST['author_ID']
	AUTHOR.Age = request.POST['author_age']
	AUTHOR.Country = request.POST['author_country']
	AUTHOR.Name = request.POST['author_name']
	AUTHOR.save()
	BOOK = Book()
	BOOK.Title = request.POST['book_name']
	BOOK.AulthorID_id = request.POST['author_ID']
	BOOK.Publisher =  request.POST['book_publisher']
	BOOK.PublishDate = request.POST['book_date']
	BOOK.Price = float(request.POST['book_price'])
	BOOK.save()
	BookList = Book.objects.all()
	return render_to_response('BookList.html',{'BookList':BookList})
Example #12
0
def addauthor(request):
    if request.POST and request.POST['Name'] and request.POST['AuthorID_FK']:
        post = request.POST
        new_author=Author(
            AuthorID_FK = post['AuthorID_FK'],
            Name = post['Name'],
            Age = post['Age'],
            Country = post['Country'],
            )
        tmp2 = list(Author.objects.filter(AuthorID_FK=post['AuthorID_FK']))
        if tmp2!=[]:
            return render_to_response("addauerror.html")
        else:
            new_author.save()
        return render_to_response("addausuccess.html")
    else:
        return render_to_response("addauerror1.html")
Example #13
0
def insertauthor(request):
    if request.method == "POST":
        authorID = request.POST["AuthorID"]
        newauthor = Author(AuthorID = request.POST["AuthorID"],
                           Name = request.POST["Name"],
                           Age = request.POST["Age"],
                           Country = request.POST["Country"])
        newauthor.save()
        newbook = Book(ISBN = request.POST["ISBN"],
                       Title = request.POST["Title"],
                       AuthorID = Author.objects.get(AuthorID = authorID),
                       Publisher = request.POST["Publisher"],
                       PublishDate = request.POST["PublishDate"],
                       Price = request.POST["Price"])
        newbook.save()
        return  HttpResponseRedirect("/booklist/")
    return render_to_response("insertauthor.html")
Example #14
0
class BookTest(TestCase):
    def setUp(self):
        self.author = Author(name='John Doe')
        self.author.save()

        self.category = Category(description='Comedy', )
        self.category.save()

        self.book = Book(title='Any book',
                         author=self.author,
                         category=self.category,
                         pages=900,
                         description='',
                         published_in=datetime.today())
        self.book.save()

    def test_create(self):
        'Book instance may be saved'
        self.assertEqual(1, self.book.pk)

    def test_str(self):
        'Book string representation may be the name.'
        self.assertEqual('Any book', str(self.book))
Example #15
0
    def handle(self, *args, **options):
        BookCheckout.objects.all().delete()

        Book.objects.all().delete()
        with open(
                r"C:\Users\Brandon\code\class_raccoon\Code\Brandon\Django\labs\library\management\commands\books.json",
                "r") as f:
            books = json.loads(f.read())
        for book in books:
            author = book['author']
            if not Author.objects.filter(author=author).exists():
                author = Author(author=author)
                author.save()
            else:
                author = Author.objects.get(author=author)
            title = book['title']
            year_published = book['year']
            url = book['link']
            book = Book(title=title,
                        year_published=year_published,
                        author=author,
                        url=url)
            book.save()
Example #16
0
 def handle(self, *args, **options):
     BooksCheckedOut.objects.all().delete()
     Book.objects.all().delete()
     with open("/Users/salamandersmith/Desktop/class_raccoon/Code/Alex/django/mysite/library/management/commands/books.json", "r") as f:
         books = json.loads(f.read())
     for book in books:
         author = book['author']
         if not Author.objects.filter(name=author).exists():
             author = Author(name=author)
             author.save()
         else:
            author = Author.objects.get(name=author)
         #print(f"Author: {author.name}")
         title = book['title']
         #print(f"Title: {title}")
         year_published = book['year']
         #print(f"Year: {year_published}")
         pages = book['pages']
         link = book['link']
         country = book['country']
         language = book['language']
         book = Book(title=title, year_published=year_published, pages=pages, link=link, country=country, language=language)
         book.save()
         book.authors.add(author)