def add_chapters(book_instance, chapters, status, this_user): for chapter in chapters: new_chapter = Chapter(book=book_instance, text_id=chapter['text'], number=chapter['number'], part=chapter['part']) new_chapter.save() # If the current user is the owner of the chapter-document, make sure # that everyone with access to the book gets at least read access. if this_user == new_chapter.text.owner: for bar in BookAccessRight.objects.filter(book=book_instance): if len(new_chapter.text.accessright_set.filter( user=bar.user)) == 0: AccessRight.objects.create( document_id=new_chapter.text.id, user_id=bar.user.id, rights='read', ) if this_user != book_instance.owner and len( new_chapter.text.accessright_set.filter( user=book_instance.owner)) == 0: AccessRight.objects.create( document_id=new_chapter.text.id, user_id=book_instance.owner.id, rights='read', ) return status
def update_book(request, pk): if request.method == 'POST': updatebookform = UpdateBookForm(request.POST) if updatebookform.is_valid(): chapter = Chapter() chapter.book_from = Book.objects.get(pk=pk) chapter.volume_from = updatebookform.cleaned_data['volume_from'] chapter.name = updatebookform.cleaned_data['name'] chapter.contents = updatebookform.cleaned_data['content'] word_num = float(process_chapter_num(updatebookform.cleaned_data['content'])) chapter.word_num = word_num chapter.save() signals.book_updated_signal.send( sender=chapter.__class__, chapter=chapter, book=Book.objects.get(pk=pk), ) return redirect(reverse('author:index')) else: updatebookform = UpdateBookForm() updatebookform.fields['volume_from'].queryset = Volume.objects.filter(book_from__pk=pk) context = dict() context['updatebookform'] = updatebookform context['book'] = Book.objects.get(pk=pk) return render(request, 'author_area/manage/update_book.html', context)
def handler_content(self, content, chapter: Chapter): logging.info("处理--{}--<<{}>>{},正文信息:{}...".format( self.wait_done, chapter.book, chapter, content[:10])) if chapter.book_type == BOOK_TYPE_DESC.Comic: imgs = [] for key in content.keys(): imgs.insert(int(key), content[key]) img_objs = self.save_image(imgs, IMAGE_TYPE_DESC.CHAPER_CONTENT, self.headers) # 如果能获取到所有img对象则保存 if len(img_objs) and None not in img_objs: content = img_objs try: if not content: raise OSError chapter.save_content(content) chapter.active = True chapter.save() except OSError: logging.error("处理<<{}>>单章节正文信息 失败 : {}".format( chapter.book, chapter)) chapter.active = False chapter.save() pass
async def handler_content(self, res, chapter: Chapter): content = self.parser(res) logging.info('处理<<{}>>{},正文信息:{}...'.format(chapter.book, chapter, content[:15])) if chapter.book_type == BOOK_TYPE_DESC.Comic: imgs = [] for key in content.keys(): imgs.insert(int(key), content[key]) img_objs = await self.save_image(imgs, IMAGE_TYPE_DESC.CHAPER_CONTENT, self.headers) # 如果能获取到所有img对象则保存 if len(img_objs) and None not in img_objs: content = img_objs try: chapter.save_content(content) chapter.active = True chapter.save() except OSError: logging.error('处理<<{}>>单章节正文信息 失败 : {}'.format( chapter.book, chapter)) chapter.active = False chapter.save() pass
def save_chapter_list_to_db(self, chapter_dick_list): """保存章节信息到数据库""" new_urls = self.check_chapters(chapter_dick_list) logging.info("即将保存《{}》的{}条新章节到数据库".format(self.book, len(new_urls))) need_create = [] for index, chapter_dict in enumerate(chapter_dick_list, 0): chapter_title = list(chapter_dict.keys())[0] chapter_link = list(chapter_dict.values())[0] if chapter_link in new_urls: chapter = Chapter( title=chapter_title, origin_addr=chapter_link, order=index, book_type=self.book.book_type, book_id=self.book.id, number=index, ) need_create.append(chapter) if len(need_create) >= 200: self.bulk_create_chapter(need_create) need_create = [] self.bulk_create_chapter(need_create)
def add_chapters(book_instance, chapters, status, this_user): for chapter in chapters: new_chapter = Chapter( book=book_instance, text_id=chapter["text"], number=chapter["number"], part=chapter["part"] ) new_chapter.save() # If the current user is the owner of the chapter-document, make sure that everyone with access to the book gets at least read access. if this_user == new_chapter.text.owner: for bar in BookAccessRight.objects.filter(book=book_instance): if len(new_chapter.text.accessright_set.filter(user=bar.user)) == 0: AccessRight.objects.create(text_id=new_chapter.text.id, user_id=bar.user.id, rights="r") if ( this_user != book_instance.owner and len(new_chapter.text.accessright_set.filter(user=book_instance.owner)) == 0 ): AccessRight.objects.create(text_id=new_chapter.text.id, user_id=book_instance.owner.id, rights="r") return status
def book(request, book_id): page = int(request.GET.get('page', 1)) count = int(request.GET.get('count', 6)) start = (page - 1) * count book = Book.objects.get(id=book_id) latest_chapter = Chapter.get_the_latest_chapter_by_book(book_id) chapters = Chapter.get_chapters(book_id, start, count) total_chapters_count = Chapter.get_chapter_count_by_book(book_id) if page > 1: prev_page = page - 1 if page * count < total_chapters_count: next_page = page + 1 if total_chapters_count % count == 0: page_count = total_chapters_count / count else: page_count = total_chapters_count / count + 1 return render_to_response('book_intro.html', locals())
def make_chapter_public(self, request, queryset): from django.db.models import Max antal=0 for userchapter in queryset: highest_index = Chapter.objects.aggregate(Max('index')) index = highest_index['index__max']+1 chapter = Chapter(title=userchapter.title, summary=userchapter.summary, summary_html=userchapter.summary_html, body=userchapter.body, body_html=userchapter.body_html, mod_date=userchapter.pub_date, pub_date=userchapter.pub_date, index=index, author=userchapter.author, user_created=True, visible=True, picture=userchapter.picture, picture_description=userchapter.picture_description, tags_string=userchapter.tags_string) chapter.save() userchapter.delete() antal+=1 self.message_user(request, '%s successfully made public.' % antal)
def _save_all_chapter_db(self, comic, chapter_list): logger.info('_save_all_chapter_db for comic') for index, chapter_dict in enumerate(chapter_list, 0): chapter_title = list(chapter_dict.keys())[0] chapter_link = list(chapter_dict.values())[0] logger.info('{}_ -chapter-__{}'.format(chapter_title, chapter_link)) chapter_obj = Chapter.normal.filter(book=comic, book_type=BOOK_TYPE_DESC.Comic, title=chapter_title).first() if not chapter_obj: chapter_obj = Chapter() chapter_obj.comic = comic chapter_obj.title = chapter_title chapter_obj.order = index chapter_obj.origin_addr = chapter_link chapter_obj.save()
def _save_all_chapter_db(self, book, chapter_list): logger.info('_save_all_chapter_db') for index, chapter_dict in enumerate(chapter_list, 0): chapter_title = list(chapter_dict.keys())[0] chapter_link = list(chapter_dict.values())[0] logger.info('{}_ -chapter-__{}'.format(chapter_title, chapter_link)) chapter_obj = Chapter.normal.filter(book=book, title=chapter_title).first() if not chapter_obj: chapter_obj = Chapter() chapter_obj.book = book chapter_obj.title = chapter_title chapter_obj.order = index chapter_obj.origin_addr = chapter_link chapter_obj.save()
def save_book(book_url): bookInfo = download(book_url) chapter_list = bookInfo['chapters'] Novel.objects.create(name=bookInfo['name'], description=bookInfo['description'], imgSrc=bookInfo['imgSrc'], author=bookInfo['author'], biqugePath=bookInfo['biqugePath'], updateTime=bookInfo['updateTime'], latestChapter=bookInfo['latestChapter']) book_id = Novel.objects.get(name=bookInfo['name']).id querySetList = [] for chapter in chapter_list: querySetList.append( Chapter(novel_id_id=book_id, no=chapter_list.index(chapter) + 1, name=chapter['name'], context_url=chapter['context_url'])) Chapter.objects.bulk_create(querySetList)
def update_book(book_url): bookInfo = download(book_url) chapters = bookInfo['chapters'] lastChapterOnSearch = chapters[-1] Novel_now = Novel.objects.filter(name=bookInfo['name']) # 当前操作的小说对象 Novel_now.update(updateTimeOnServer=timezone.now()) novel_id = Novel_now.values('id')[0]['id'] lastChapterOnDb = list( Chapter.objects.filter(novel_id=novel_id).values())[-1] if lastChapterOnSearch['name'] != lastChapterOnDb['name']: Novel_now.update(latestChapter=bookInfo['latestChapter'], updateTime=bookInfo['updateTime']) index = lastChapterOnDb['no'] len = chapters.__len__() queryList = [] for i in range(index, len): queryList.append( Chapter(novel_id_id=novel_id, no=i + 1, name=chapters[i]['name'], context_url=chapters[i]['context_url'])) Chapter.objects.bulk_create(queryList)