def add_fk1(): from book.models import Publisher, Book p = Publisher.objects.get(name='机械工业出版社') b = Book() b.name = 'Python 高并发实战' b.publisher_state = 2 b.desc = 'Good' b.publisher = p # ForeignKey b.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 do_import(data, if_book_exists='continue'): """import a book in the database. Expects a dict of the following format: { 'publisher': (u'oreilly', u"Farnham ; O'Reilly, 2002 printing, c2003."), 'authors': [(u'ray_randy_j', u'Ray, Randy J.'), (u'kulchenko_pavel', u'Kulchenko, Pavel')], 'subjects': [(u'perl_computer_program_language', u'Perl (Computer program language)'), (u'internet_programming', u'Internet programming')], 'language': u'eng', 'title': u'Programming Web services with Perl', 'ddc': u'5.2762', 'long_title': None, 'edition_info': u'', 'summary': None } which other importers have to deliver. if_book_exists may be eighter one of 'continue', 'overwrite', 'error' """ try: book = Book.objects.get(isbn=data['isbn']) except Book.DoesNotExist: book = Book(isbn=data['isbn'], title=data['title'], long_title=data.get('long_title'), language=data.get('language'), ddc=data.get('ddc'), edition_info=data.get('edition_info'), summary=data.get('summary')) try: publisher = Publisher.objects.get(publisher_id=data['publisher'][0]) except Publisher.DoesNotExist: publisher = Publisher(publisher_id=data['publisher'][0], name=data['publisher'][1]) publisher.save() book.publisher = publisher for authordata in data['authors']: try: author = Author.objects.get(author_id=authordata[0]) except Author.DoesNotExist: author = Author(author_id=authordata[0], name=authordata[1]) author.save() book.authors.add(author) for subjectdata in data['subjects']: try: subject = Subject.objects.get(subject_id=subjectdata[0]) except Subject.DoesNotExist: subject = Subject(subject_id=subjectdata[0], name=subjectdata[1]) subject.save() book.subjects.add(subject) book.save() else: if if_book_exists.lower() == "error": raise ValueError("Book %s already exists!" % data['isbn']) elif if_book_exists.lower() == "overwrite": book.delete() do_import(data) elif if_book_exists.lower() != "continue": raise ValueError("if_book_exists must be eighter one of 'continue', 'overwrite', 'error'")
def populate(): print('Populating bkName ... ', end='') Book.objects.all().delete() i = 1 for bkName in bkNames: book = Book() book.bkName = bkName book.authorName = 'at' + str(i) book.publisher = 'pub' + str(i) book.pubversion = 1 book.price = i * 100 book.inventory = 10 i += 1 book.save() print('done')
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 populate(): print('Populating Book...', end='') titles = [ 'python', '小王子', 'Java', '黑子的籃球', 'Django', '管理數學', '計概', 'c++', 'vb', '少年陰陽師' ] authornames = ['王一', '王二', '王三'] Book.objects.all().delete() for title in titles: book = Book() book.title = title book.authorname = authornames[random.randint(0, len(authornames) - 1)] book.publisher = book.authorname book.date = datetime.datetime.today() book.version = '1' book.price = 1000 book.save() print('done')
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 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 create_book(request): ''' @note: 添加图书信息 ''' models = request.POST.get('models', '{}') models_json = json.loads(models) print models_json try: datalist = [] for data in models_json: if Book.objects.filter(title=data['title']): return HttpResponse(json.dumps({'result':False, 'message': u'书籍 %s 的信息已存在!' % data['title']})) else: print "create" print data['publication_date'] print data['authors'] print data['publisher'] publisher = Publisher.objects.filter(name=data['publisher'])[0] print publisher book = Book() book.title = data['title'] book.publication_date = gt(data['publication_date']) book.publisher = publisher book.price = data['price'] book.save() #解析authors并添加 authors = data['authors'].split(',') authorlist = [] for name in authors: authorlist.append(Author.objects.filter(name=name)[0]) print authorlist for author in authorlist: book.authors.add(author) datalist.append(data) except Exception, e: print e return HttpResponse(json.dumps({'result':False, 'message': u'添加记录失败, %s' % e}))
def create_book(request): """ @note: 添加图书信息 """ models = request.POST.get("models", "{}") models_json = json.loads(models) print models_json try: datalist = [] for data in models_json: if Book.objects.filter(title=data["title"]): return HttpResponse(json.dumps({"result": False, "message": u"书籍 %s 的信息已存在!" % data["title"]})) else: print "create" print data["publication_date"] print data["authors"] print data["publisher"] publisher = Publisher.objects.filter(name=data["publisher"])[0] print publisher book = Book() book.title = data["title"] book.publication_date = gt(data["publication_date"]) book.publisher = publisher book.price = data["price"] book.save() # 解析authors并添加 authors = data["authors"].split(",") authorlist = [] for name in authors: authorlist.append(Author.objects.filter(name=name)[0]) print authorlist for author in authorlist: book.authors.add(author) datalist.append(data) except Exception, e: print e return HttpResponse(json.dumps({"result": False, "message": u"添加记录失败, %s" % e}))
from book.models import Book from supplier.models import Supplier, SupplierStack from db_tools.data.book_data import raw_data # 先创建书库,将book_data中的所有书添加到这个书库中 book_stack = SupplierStack() book_stack.stack_name = "中国卖书有限公司书库" supplier = Supplier() supplier.supplier_name = "中国卖书有限公司" supplier.id = 1 book_stack.supplier = supplier supplier.save() book_stack.save() print("书库信息录入成功!") for book_detail in raw_data: book = Book() book.book_name = book_detail["book_name"] book.publisher = book_detail["publisher"] book.writer = book_detail["writer"] book.price = book_detail["price"] book.number = book_detail["number"] book.book_stack = book_stack book.save() print("图书信息录入成功!")