Example #1
0
def addBook(request):
    '''新加书籍, post request only'''
    if request.method != "POST":
        raise Http404
    
    authorName = request.REQUEST.get('authorName', None)
    authorDesc = request.REQUEST.get('authorDesc', None)
    bookName = request.REQUEST.get('bookName', None)
    bookPrice = request.REQUEST.get('bookPrice', None)
    bookIsbn = request.REQUEST.get('bookIsbn', None)
    bookPress = request.REQUEST.get('bookPress', None)
    bookDesc = request.REQUEST.get('bookDesc', None)
    bookBinding = request.REQUEST.get('bookBinding', None)
    bookPages = request.REQUEST.get('bookPages', None)
    bookSpic = request.REQUEST.get('bookSpic', None)
    bookMpic = request.REQUEST.get('bookMpic', None)
    bookLpic = request.REQUEST.get('bookLpic', None)
    bookPublishDate = request.REQUEST.get('bookPublishDate', None)
    stock = request.REQUEST.get('stock', None)
    category = request.REQUEST.get('category', None)
    
    author = Author(name=authorName, desc=authorDesc)
    author.save()
    
    price = float(''.join([ item for item in bookPrice if item in '1234567890.' ]))
    try:
        cate = Category.objects.get(label=category)
    except Category.DoesNotExist:
        raise Http404
    try:
        book = Book.objects.get(isbn=bookIsbn)
    except Book.DoesNotExist:
        book = Book()
    book.name=bookName
    book.author=author 
    book.price=price
    book.isbn=bookIsbn
    book.press=bookPress
    book.desc=bookDesc
    book.binding=bookBinding
    book.pages=bookPages
    book.spic=bookSpic
    book.mpic=bookMpic
    book.lpic=bookLpic 
    book.publish_date=bookPublishDate
    book.stock=stock
    book.category=cate
    book.save()
    utils.addMsg(request, messages.SUCCESS, '添加成功!')
    return HttpResponseRedirect('/manage/reg_book/')
Example #2
0
def regFromDouban(request):
    '''利用豆瓣API获取书籍信息加入数据库'''
    try:
        DOUBAN = "https://api.douban.com/v2/book/search?q="
        q = request.REQUEST.get('q', '')
        q = urllib.quote(q.decode('utf8').encode('utf8'))  
        DOUBAN += q
    
        req = urllib.urlopen(DOUBAN)
        resp = req.read()
        result = simplejson.loads(resp)
        books = result['books']
        
        
        for i in range(len(books)):
            author = Author()
            author.name = books[i]['author'][0]
            author.desc = books[i]['author_intro']
            author.save()
            
            try:
                book = Book.objects.get(isbn=books[i]['isbn13'])
            except Book.DoesNotExist:
                book = Book()
                book.name = books[i]['title']
                book.author = author
                book.price = float(''.join([ item for item in books[i]['price'] if item in '1234567890.' ]))
                if not books[i]['isbn13']:
                    book.isbn = books[i]['isbn10']
                else:
                    book.isbn = books[i]['isbn13']
                book.press = books[i]['publisher']
                book.desc = books[i]['summary']
                book.binding = books[i]['binding']
                book.pages = books[i]['pages']
                book.spic = _downloadImg(books[i]['images']['small'], 'small')   
                book.mpic = _downloadImg(books[i]['images']['medium'], 'medium')   
                book.lpic = _downloadImg(books[i]['images']['large'], 'large')   
                book.stock = 140
                book.publish_date = books[i]['pubdate']
                book.category = _getBookCate('letter')
                book.save()
                
        return HttpResponse('success')
    except Exception:
        return HttpResponse('error')