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 bookLoading(self, num): start = 1 # except first row end = num self.progressBar(start, end, "Books data uploading") btLen = len(self.bookTags) - 1 rtLen = len(self.ratings) - 1 for row in self.booksInfo[1:num + 1]: # +1 because num+1-1=num bTitle = str(row[10]) bAuthor = str(row[7]).split(',') bRating = 0.0 book = Book(id=row[1], title=bTitle, rating=bRating) if row[22][8] != 's': image_dir = row[22].split("/")[3:] image_dir[-2] = image_dir[-2][:-1] + '{}' image_dir = '/'.join(image_dir) book.smallImage = image_dir.format("s") book.middleImage = image_dir.format("m") book.bigImage = image_dir.format("l") book.save() ## Rating counting step = rtLen // 2 maxIterations = rtLen // 2 cnt = 0 i = 1 up = False down = False while not up and not down and cnt < maxIterations: cnt += 1 if self.ratings[i][0] == row[1]: caret = i sum = 0.0 ratingCnt = 0.0 while caret > 1 and self.ratings[caret][0] == row[1]: caret -= 1 curRating = float(self.ratings[caret][2]) userId = int(self.ratings[caret][1]) try: curUser = User.objects.get(id=userId) myUser = MyUser.objects.get(user=curUser) br = BookRating(rtd_book=book, rating=curRating) br.save() curUser.save() myUser.rated_books.add(br) myUser.save() except ObjectDoesNotExist: self.warning( "User with id = {:d} does not exist".format( userId)) sum += curRating ratingCnt += 1.0 up = True caret = i while caret < rtLen and self.ratings[caret][0] == row[1]: curRating = float(self.ratings[caret][2]) userId = int(self.ratings[caret][1]) try: curUser = User.objects.get(id=userId) myUser = MyUser.objects.get(user=curUser) br = BookRating(rtd_book=book, rating=curRating) br.save() curUser.save() myUser.rated_books.add(br) myUser.save() except ObjectDoesNotExist: self.warning( "User with id = {:d} does not exist".format( userId)) sum += curRating ratingCnt += 1.0 caret += 1 down = True bRating = sum / ratingCnt elif int(self.ratings[i][0]) > int(row[1]): i -= step step //= 2 else: i += step step //= 2 book.rating = bRating try: bDate = int(float(row[8])) except ValueError: bDate = 0 book.date = bDate book.save() for auth in bAuthor: try: a = Author.objects.get(name__exact=auth) except ObjectDoesNotExist: a = Author(name=auth) a.save() book.author.add(a) ## Genre setting step = btLen // 2 maxIterations = btLen // 2 cnt = 0 i = 1 up = False down = False while not up and not down and cnt < maxIterations: cnt += 1 if self.bookTags[i][0] == row[1]: caret = i while caret > 1 and self.bookTags[caret][0] == row[1]: caret -= 1 try: gName = self.allGenres.get(self.bookTags[caret][1]) g = Genre.objects.get(name=gName) book.genre.add(g) except ObjectDoesNotExist: # nothing to do pass up = True caret = i while caret < btLen and self.bookTags[caret][0] == row[1]: try: gName = self.allGenres.get(self.bookTags[caret][1]) g = Genre.objects.get(name=gName) book.genre.add(g) except ObjectDoesNotExist: # nothing to do pass caret += 1 down = True elif int(self.bookTags[i][0]) > int(row[1]): i -= step step //= 2 else: i += step step //= 2 book.save() self.progressBar(start, end, "Books data uploading") start += 1
def load_books(): """ Transfers all books from the old database API for all programs into the new database creating objects of the Book model """ for post, program_id in NAClient().get_books(): if post['status'] == "published": try: post_parent_program = get_program(program_id) parent_program_books_homepage = get_content_homepage( post_parent_program, ProgramBooksPage, 'Books', ) book_slug = slugify(post['title']) new_book = Book.objects.filter(slug=book_slug).first() if not new_book and book_slug: new_book = Book( search_description='', seo_title='', depth=5, show_in_menus=False, slug=book_slug, title=post['title'], date=get_post_date(post['publish_at']), publication_cover_image=download_image( post['book_cover_image_url'], book_slug + "_cover_image.jpeg" ), subheading=post['sub_headline'], body=json.dumps([ { 'type': 'paragraph', 'value': post['content'] } ]), story_excerpt=get_summary(post['summary']), story_image=download_image( post['cover_image_url'], book_slug + "_image.jpeg" ), ) parent_program_books_homepage.add_child(instance=new_book) new_book.save() get_post_authors(new_book, post['authors']) connect_programs_to_post(new_book, post['programs']) elif new_book and book_slug and need_to_update_post(post['modified']): new_book.search_description = '' new_book.seo_title = '' new_book.depth = 5 new_book.date = get_post_date(post['publish_at']) new_book.show_in_menus = False new_book.slug = book_slug new_book.title = post['title'] new_book.body = json.dumps([ { 'type': 'paragraph', 'value': post['content'] } ]) new_book.publication_cover_image = download_image( post['book_cover_image_url'], book_slug + "_cover_image.jpeg" ) new_book.story_image = download_image( post['cover_image_url'], book_slug + "_image.jpeg" ) new_book.subheading=post['sub_headline'] new_book.save() get_post_authors(new_book, post['authors']) connect_programs_to_post(new_book, post['programs']) except django.db.utils.IntegrityError: pass
def postbox(request): if request.method == "POST": onedrive_data = json.loads(request.body) if onedrive_data['name'][-5] == '.': file_name, file_format = onedrive_data['name'][:-5], onedrive_data[ 'name'][-4:] elif onedrive_data['name'][-4] == '.': file_name, file_format = onedrive_data['name'][:-4], onedrive_data[ 'name'][-3:] else: h = HttpResponse('后缀名不合法') h.status = 404 return h if (file_format != 'epub') and (file_format != 'mobi') and ( file_format != 'azw3') and (file_format != 'pdf') and (file_format != 'kfx'): h = HttpResponse('后缀名不合法') h.status = 404 return h if Book.objects.filter(file_name=file_name): #如果存在这本书 b = Book.objects.get(file_name=file_name) b.save_url(onedrive_data['url'], file_format, onedrive_data['size']) b.save() else: #如果不存在这本书 douban_data = get_book_data(file_name) pprint(douban_data) b = Book() b.date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) if douban_data: b.file_name = file_name b.title = douban_data['title'] if douban_data['subtitle']: b.subtitle = douban_data['subtitle'] if douban_data['alt_title']: b.english_title = douban_data['alt_title'] b.save() #为添加作者准备 author = douban_data.get('author') if author: #如果作者不为空 if len(author) == 1: a_inf = douban_data['author_intro'] else: a_inf = douban_data['author_intro'].split('\n')[1::2] al = [] if len(a_inf) == len(douban_data['author']): a_inf = a_inf else: a_inf = [ '不详' for i in range(len(douban_data['author'])) ] for i in range(len(douban_data['author'])): d = {} d['name'] = douban_data['author'][i] d['information'] = a_inf[i] al.append(d) for l in al: a = None if not Author.objects.filter(name=l['name']): #添加作者 a = Author() a.name = l['name'] a.information = l['information'] a.save() a.book.add(b) else: a = Author.objects.get(name=l['name']) a.book.add(b) b.author.add(a) if douban_data.get('price'): b.price = douban_data['price'] b.cover = 'https://images.weserv.nl/?url=' + douban_data[ 'images']['small'][8:] b.large_cover = 'https://images.weserv.nl/?url=' + douban_data[ 'images']['large'][8:] b.summary = douban_data['summary'] b.catalog = douban_data['catalog'] if not Publisher.objects.filter( name=douban_data['publisher']): #添加出版社 p = Publisher(name=douban_data['publisher']) p.save() else: p = Publisher.objects.get(name=douban_data['publisher']) b.publisher.add(p) p.book.add(b) b.save() b.douban_id = douban_data['id'] if douban_data['isbn10']: b.isbn10 = douban_data['isbn10'] if douban_data['isbn13']: b.isbn13 = douban_data['isbn13'] if douban_data['pages']: b.pages = douban_data['pages'] if douban_data.get('tags'): b.save() for d_t in douban_data['tags']: t = Tag() t.name = d_t['name'] t.title = d_t['title'] t.save() t.book.add(b) b.tags.add(t) s = douban_data.get('series') if s and type(s) == type({}): b.save() s = Series() s.name = douban_data.get('series')['title'] s.save() s.book.add(b) b.series.add(s) tr = douban_data.get('translator') pprint(tr) if tr and type(tr) == type([]): b.save() for tr in tr: if not Translator.objects.filter(name=tr): t = Translator() t.name = tr t.save() t.book.add(b) b.translator.add(t) else: b.translator.add(Translator.objects.get(name=tr)) b.save_url(onedrive_data['url'], file_format, onedrive_data['size']) else: b.file_name = b.title = file_name b.save_url(onedrive_data['url'], file_format, onedrive_data['size']) b.save() return render(request, 'book/404.html') else: return render(request, 'book/404.html', status=404)