Exemplo n.º 1
0
def add_book(request):
    context = dict()
    if request.method == "POST":
        if "url" in request.POST:
            url = request.POST["url"]
            context = parse_book_by_isbn(url)
            return render(request, "book/add.html", context)
        if "isbn" in request.POST:
            isbn = request.POST["isbn"]
            context = parse_book_by_isbn(isbn)
            book = Book()
            book.title = context["title"]
            book.publisher = context["publisher"]
            book.isbn = context["isbn"]
            book.url = context["url"]
            book.ispersonal = int(True)
            book.ownerid = request.user.id
            book.summary = context["summary"]
            book.price = context["price"]
            book.numraters = 0
            book.averageRate = 3
            book.created_date = datetime.datetime.now()
            book.updated_date = datetime.datetime.now()
            book.author = context["author"][0]
            book.authorinfo = context["author_intro"][0:4090]
            # print book.authorinfo
            book.catelog = context["catalog"][0:4090]
            book.pubdate = context["pubdate"]
            book.ispublic = int(True)
            book.imgurl = context["images"]
            book.city = Dper.objects.get(user_id=request.user.id).city
            book.bookcount = 1
            book.status = 1
            # Dper.objects.filter(id=request.user.id)[0].city
            book.save()

            # save tags of book
            for val in context["tags"]:
                tag = Tag()
                tag.value = val
                tag.createdate = datetime.datetime.now()
                tag.save()
                rel = Booktag()
                rel.tagid = tag.id
                rel.bookid = book.id
                rel.save()
            return redirect("/book/library", context)
    else:
        form = BookForm()
        context["form"] = form
        return render(request, "book/add.html", context)