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
def save_book_info_to_db(self, info): '''保存书本信息到数据库''' logging.info('保存<<{}>>信息到数据库'.format(info['name'])) author = self.save_or_get_author_db(info) book = self.book if not self.book: lock.acquire() try: books = Book.normal.filter(title=info['name'], author=author, book_type=self.book_type) if books.count() > 0: book = books[0] for book in books[1:]: book.delete() else: book = Book() if not book.desc: book.book_type = self.book_type book.title = info.get('name') book.author = author book.desc = info.get('desc') book.markeup = info.get('markeup') book.origin_addr = self.url book.on_shelf = self.on_shelf book.save() finally: lock.release() self.book = book return book
def index(request): ''' 操作rom 1.所有的模型类都有个objects属性,就是一个manager对象,用于进行数据操作管理 2.添加数据 2.1 模型类。objects.create(属性=值) 返回生成的对象----》记录 2.2 添加数据 对象 = 模型类() 对象。属性=值 对象。save()保存数据 2.3 ,模型类。objects.get_or_crate(属性=值) 如果有就获取,没有就创造 3查看数据 模型类。objects.all() 查看所有数据 模型类。objects.filter(条件)条件查询,相当于where 模型类。objects.get(条件)只能获取一条数据 4修改数据 4.1 模型类。object。filter(条件).update(属性=新属性值) 一次更新多条 4.2 先查询出对象 ---一次更新一条 对象。属性=值 对象。属性=值 对象。属性=值 对象。save() 5 删除数据 查询偶delete() :param request: :return: ''' rs = Book.objects.create(name='红楼梦', author='曹雪芹', price=9.9) print(rs) book = Book() book.name = '红楼梦' book.author = '曹雪芹' book.price = 99 book.save() book = Book.objects.get_or_create(name='三国演义', author='施耐庵', price=88) print(book) #查看数据 # books=Book.objects.all() # content = { # "books":books # } # book = Book.objects.get(id=1) # book = Book.objects.get(name='红楼梦') 如果存在多条数据,返回列表[] # books = Book.objects.filter(name='红楼梦').first()#获取第一条 # print(book) #更新数据 # rs = Book.objects.filter(name='红楼梦').update(name='红楼2') # print(rs) # book = Book.objects.get(id=1) # book.name='红楼3' # book.save() book = Book.objects.get(id=1) book.delete() return HttpResponse("bookModel")
def save_book_from_row(book_row): book = Book() book.id = book_row[0] book.title = book_row[1] # book.category=book_row[2] book.author = book_row[3] book.publication = book_row[4] book.category = book_row[5] book.save()
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)
def book_add(request): response_data = {} response_data['result'] = 'error' # Requests req = json.loads(request.body.decode('utf-8')) book_item = Book() book_item.isbn = req['isbn'] book_item.title = req['title'] book_item.author = req['author'] book_item.translator = req['translator'] book_item.edition = req['edition'] book_item.pubhouse = req['pubhouse'] book_item.pubtime = req['pubtime'] book_item.summary = req['summary'] book_item.context = req['context'] book_item.clc = req['clc'] book_item.price = req['price'] # add cate book_item.save() for item in req['category']: try: # print(item['id']) cate = Category.objects.get(id=item['id']) book_item.category.add(cate) except: pass # add tag for item in req['tag']: try: tag = Tag.objects.get(id=item['id']) book_item.tag.add(tag) except: pass # Save book try: book_item.save() # Response Data response_data['result'] = 'ok' response_data['book_id'] = book_item.bookid except: response_data['message'] = 'Fail to save data.' return JsonResponse(response_data)
def add_book(request): context = dict() if request.method == 'POST': if 'url' in request.POST: url = request.POST['url'] context = parse_book_by_url(url) return render(request, 'book/add.html', context) if 'bookid' in request.POST: bookid = request.POST['bookid'] context = parse_book_by_id(bookid) book = Book() book.title = context['title'] book.publisher = context['publisher'] book.isbn = context['isbn10'] if 'isbn10' in context else context[ 'isbn13'] 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.pubdate = context['pubdate'] book.ispublic = int(True) book.imgurl = context['images'] 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)
def add_book(request): context = dict() if request.method == 'POST': if 'url' in request.POST: url = request.POST['url'] context = parse_book_by_url(url) return render(request, 'book/add.html', context) if 'bookid' in request.POST: bookid = request.POST['bookid'] context = parse_book_by_id(bookid) book = Book() book.title = context['title'] book.publisher = context['publisher'] book.isbn = context['isbn10'] if 'isbn10' in context else context['isbn13'] 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.pubdate = context['pubdate'] book.ispublic = int(True) book.imgurl = context['images'] 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)
def create_book(request): if request.method == 'POST': createbookform = CreateBookForm(request.POST, request.FILES) if createbookform.is_valid(): book = Book() book.category = createbookform.cleaned_data['category'] book.name = createbookform.cleaned_data['name'] book.cover = createbookform.cleaned_data['cover'] book.summary = createbookform.cleaned_data['summary'] book.author = request.user.author book.save() return redirect(reverse('author:update_book', args=[book.pk])) else: print(createbookform.errors) else: createbookform = CreateBookForm() context = dict() context['createbookform'] = createbookform return render(request, 'author_area/manage/create_book.html', context)
def populate(): print('Populating articles and comments ... ', end='') Book.objects.all().delete() Review.objects.all().delete() for title, content in zip(titles, contents): book = Book() book.title = title book.content = content book.author = authors book.publisher = publishers book.publicationDate = publicationDate book.price = 999 book.save() for review, score in zip(reviews, scores): Review.objects.create(book=book, review=review, score=score) print('done')
def populate(): print('Populating book...', end='') titles = ['如何像電腦科學家一樣思考', '10 分鐘內學好 Python', '簡單學習 Django' '如何像電腦科學家一樣思考2', '10 分鐘內學好 Python2', '簡單學習 Django2' '如何像電腦科學家一樣思考3', '10 分鐘內學好 Python3', '簡單學習 Django3' '如何像電腦科學家一樣思考4', '10 分鐘內學好 Python4', '簡單學習 Django4'] authors =['張三','李四','王五','趙六','錢七'] Book.objects.all().delete() for title in titles: book = Book() book.title = title n = random.randint(0,len(authors)-1) book.author = authors[n] book.publisher = book.author book.time = datetime.datetime.today() book.version = '1' book.price = 1000 book.save() print('done')
def populate(): print('Populating Book...', end='') titles = [ '如何像電腦科學家一樣思考', '10 分鐘內學好 Python', '簡單學習 Django' '如何像電腦科學家一樣思考2', '10 分鐘內學好 Python2', '簡單學習 Django2' '如何像電腦科學家一樣思考3', '10 分鐘內學好 Python3', '簡單學習 Django3' '如何像電腦科學家一樣思考4', '10 分鐘內學好 Python4', '簡單學習 Django4' ] authors = ["張三", "李四", "王五", "趙六", "錢七"] Book.objects.all().delete() for title in titles: book = Book() book.title = title n = random.randint(0, len(authors) - 1) book.author = authors[n] book.publisher = book.author book.pubDate = datetime.datetime.today() book.price = 1000 book.save() print('done')
def addbook(request): if request.method == 'POST': # user = cache.get('shuji') # print(user) user = request.user bookname = request.POST.get('bookname') author = request.POST.get('author') binding = request.POST.get('binding') publisher = request.POST.get('publisher') pubdate = request.POST.get('pubdate') price = request.POST.get('price') pages = request.POST.get('pages') isbn = request.POST.get('isbn') summary = request.POST.get('summary') image = request.FILES.get('image') book = Book() book.bookname = bookname book.author = author book.binding = binding book.publisher = publisher book.pubdate = pubdate book.price = price book.pages = pages book.isbn = isbn book.summary = summary book.image = image book.user_id_id = user.id book.save() user = User.objects.filter(pk=user.id).first() user.beans += 1 user.save() # recent = Book.objects.filter(user_id_id=user.id) recent = Book.objects.all() return render(request,'index.html',{'recent':recent,'user':user})
def save_book(self, title, author): book = Book() book.title = title book.author = author book.save()
def add_doc_form(request): """ Displays form for converting and adding book in doc """ if request.method == "POST": form = DocCreateForm(request.POST, request.FILES) if form.is_valid(): temp_name = tempfile.mkstemp()[1] cd = form.cleaned_data properties = BookProperties(temp_name, cd['title'].encode("utf-8")) properties.language = cd['language'] properties.author = request.user.first_name + ' ' + request.user.last_name properties.subject = cd['subject'].encode("utf-8") properties.description = cd['description'].encode("utf-8") properties.genre = cd['type'].encode("utf-8") properties.date = cd['date'].encode("utf-8") properties.rights = cd['rights'].encode("utf-8") properties.author = request.user.first_name + " " + request.user.last_name file = request.FILES['file'] doc_file_name = tempfile.mkstemp()[1] doc_file = open(doc_file_name, 'wb') doc_file.write(file.read()) doc_file.close() doc_parser = DocParser(doc_file_name, properties) out_file_name = tempfile.mkdtemp() + "tmp.html" doc_parser.parse() try: language = Language.objects.get(short=cd['language']) except: return render_response(request, "epub/add_doc_form.html", {'errors': [_('No such language.')], 'user': request.user, 'menu_on': True, 'form': form}) lang_code = LANG_CODE[0] for lang_code in LANG_CODE: if lang_code[0] == cd['language']: break book_model = Book(language=language, title = cd['title'].encode("utf-8"), pagelink="") try: author = Author.objects.get(name=request.user.first_name + ' ' + request.user.last_name) except: author = Author.objects.create(name=request.user.first_name + ' ' + request.user.last_name) book_model.save() book_model.author = [author] ebook = EpubBook.objects.create(book=book_model) print "creating" ebook.name.save(cd['title'].encode("utf-8") + ".epub", _ExistingFile(temp_name)) try: book_file = BookFile(link="/" + MEDIA_URL + "/uploads/" + quote(cd['title'].encode("utf-8")) + '.epub') book_file.type = 'epub' book_file.save() except: return render_response(request, "epub/add_doc_form.html", {'errors': [_("Book name should be unique")], 'menu_on': True, 'form': form}) book_model.book_file = [book_file] book_model.save() ebook.save() hrr = HttpResponseRedirect("/book/id%d" % book_model.id) return hrr else: form = DocCreateForm() return render_response(request, "epub/add_doc_form.html", {'user': request.user, 'menu_on': True, 'form': form})