Exemple #1
0
def book_input_auto():
    book_list = get_data('book_out.txt')
    from book.models import Book
    from book.models import Taxonomy
    
    for b in book_list:
        bs = None
        if b[6] != '':
            bs = Book.objects.filter(isbn=b[6]) 
        if bs is None or not bs.exists():
            bs = Book.objects.filter(name=b[0], version=b[7]) 
            if not bs.exists():
                try:
                    book = Book()
                    book.name = b[0]
                    book.subtitle = b[1]
                    if b[2] is None:
                        book.author = ''
                    else:
                        book.author = b[2]
                    if b[3] is None:
                        book.press = ''
                    else:
                        book.press = b[3]
                    book.price_ori = b[4]
                    if b[5] is None or b[5] == 0:
                        book.price_now = 0.4*b[4]
                    else:
                        book.price_now = b[5]
                    book.isbn = b[6]
                    book.version = b[7]
                    book.publication_date = b[8]
                    book.page_number = b[9]
                    book.size = b[10]
                    book.binding = b[11]
                    if b[12] == 'zh':
                        book.language = 'zh'
                    else:
                        book.language = 'en'
                    if b[13] is None:
                        book.translator = ''
                    else:
                        book.translator = b[13]
                    book.status = 4
                    book.save()
                    try:
                        if b[14] is not None and b[14] != '':
                            t = Taxonomy.objects.get(name=TAXONOMY_DICT[b[13]])
                            book.add_taxonomy(t)
                    except Exception, e:
                        print "Add taxonomy failed"
                        print e
                        print book_list.index(b)
                        print b
                        import pdb;pdb.set_trace()
                except Exception, e:
                    print e
                    print book_list.index(b)
                    print b
Exemple #2
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)