def add(db_session, data, username, **kwargs): logger.debug(LogMsg.START, username) genre = data.get('genre', []) if genre and len(genre) > 0: check_enums(genre, Genre) logger.debug(LogMsg.ENUM_CHECK, {'genre': genre}) model_instance = Book() populate_basic_data(model_instance, username, data.get('tags')) logger.debug(LogMsg.POPULATING_BASIC_DATA) model_instance.title = data.get('title') model_instance.edition = data.get('edition') model_instance.pub_year = data.get('pub_year') model_instance.type = data.get('type') model_instance.genre = genre model_instance.images = data.get('images') model_instance.files = data.get('files') model_instance.language = data.get('language') model_instance.rate = data.get('rate') model_instance.description = data.get('description') model_instance.pages = data.get('pages') model_instance.duration = data.get('duration') model_instance.size = data.get('size') model_instance.isben = data.get('isben') model_instance.description = data.get('description') model_instance.from_editor = data.get('from_editor') model_instance.press = data.get('press') logger.debug(LogMsg.PERMISSION_CHECK, username) validate_permissions_and_access(username, db_session, 'BOOK_ADD', model=model_instance) logger.debug(LogMsg.PERMISSION_VERIFIED) db_session.add(model_instance) logger.debug(LogMsg.DB_ADD) add_connector(model_instance.id, data.get('unique_code'), db_session) logger.debug(LogMsg.UNIQUE_CONNECTOR_ADDED, { 'book_id': model_instance.id, 'unique_constraint': data.get('unique_code') }) price = data.get('price', None) if price: add_price_internal(price, model_instance.id, db_session, username) logger.info(LogMsg.END) return model_instance
def add_book(d): book = Book() book.title = d.get('title') book.authors = d.get('authors') book.publisher = d.get('publisher', "") book.isbn = d.get('isbn', "") book.tags = d.get('tags', "") book.description = d.get('description', "") book.copies_available = int(d.get('copies_available', 1)) book.save()
def addBook(request, userId): user = User.objects.get(pk=userId) book = Book(originOwner=user, currentOwner=user) book.name = request.POST['name'] book.author = request.POST['author'] book.kind = request.POST['kind'] book.description = request.POST['description'] book.picture = request.FILES['picture'] book.save() return HttpResponse(200)
def addBook(request,userId): user = User.objects.get(pk=userId) book = Book(originOwner=user,currentOwner=user) book.name = request.POST['name'] book.author = request.POST['author'] book.kind = request.POST['kind'] book.description = request.POST['description'] book.picture = request.FILES['picture'] book.save() return HttpResponse(200)
def process_book(self, book_json, request): """ Process single book json, creates Book, Category, Author object if doesn't exist Args: book_json (JSON): single book json request (Request): related request object Raises: LoadBookException: Obligatory field not found """ volume_id = book_json.get("id", None) if not volume_id: raise LoadBookException("Volume id not found") try: Book.objects.get(volume_id=volume_id) # Book already in the database return except Book.DoesNotExist: pass volume_info = book_json.get("volumeInfo", None) if not volume_info: raise LoadBookException("Volume info not found") title = volume_info.get("title", None) if not title: raise LoadBookException("Title not found") authors = volume_info.get("authors", []) if not authors: raise LoadBookException("Author not found") categories = volume_info.get("categories", []) if not categories: raise LoadBookException("Category not found") description = book_json.get("description", '') image = self.get_thumbnail(volume_info) new_book = Book() new_book.title = title new_book.description = description if image: new_book.google_image_link = image new_book.request = request new_book.volume_id = volume_id new_book.save() new_book.authors.add(*self.get_authors_list(authors)) new_book.categories.add(*self.get_categories_list(categories)) new_book.save()
def update_book_info(cls, book: Book, info_to_update: Dict[str, str]): author_data = {} for author in info_to_update['author']: temp = author.split(' ') author_data['name'], author_data['surname'] = temp[0], temp[-1] author = cls.add_author_to_db(author_data) book.author.add(author) if info_to_update.get('genre'): book.genre = cls.add_or_get_genre(info_to_update.get('genre')) book.description = info_to_update.get('annotation') book.save()
def handle(self, *args, **options): faker = Faker("pl_PL") authors = Author.objects.all() for i in range(10): book = Book() book.title = faker.text(max_nb_chars=50) book.pub_date = faker.past_date() book.description = faker.text(max_nb_chars=400) book.save() book.author.add(choice(authors)) book.save() self.stdout.write(self.style.SUCCESS(f'Create: {str(book)}'))
def save(self, data, request): """ Create new Book instance and save Args: data (BookForm data): Data validated by BookForm request (request): Original request with image file """ authors_string = data['authors'] categories_string = data['categories'] authors = self.get_authors(authors_string) categories = self.get_categories(categories_string) new_book = Book() new_book.title = data['title'] new_book.description = data['description'] new_book.image = request.FILES.get("image", None) new_book.save() new_book.authors.add(*authors) new_book.categories.add(*categories) new_book.save()
def get(self, request, *args, **kwargs): value = request.GET.get('value') context = {'value': value} try: if value is not None: api_url = f'https://www.googleapis.com/books/v1/volumes?q={value}&maxResults=5' api_request = requests.get(api_url) json_data = api_request.json() added_books = 0 no_added_books = 0 if 'items' in json_data: for item in json_data['items']: authors = if_api_exists(item['volumeInfo'], 'authors') categories = if_api_exists(item['volumeInfo'], 'categories') title = if_api_exists(item['volumeInfo'], 'title') description = if_api_exists(item['volumeInfo'], 'description') api_key = item['id'] thumbnail = if_api_exists(item['volumeInfo'], 'imageLinks') if not Book.objects.filter(api_key=api_key).exists(): create_author = None authors_array = [] for author in authors: if not Author.objects.filter( name=author).exists(): create_author = Author.objects.create( name=author) create_author.save() authors_array.append(create_author) else: create_author = Author.objects.get( name=author) authors_array.append(create_author) create_category = None categories_array = [] for category in categories: if not Category.objects.filter( name=category).exists(): create_category = Category.objects.create( name=category) categories_array.append(create_category) else: create_category = Category.objects.get( name=category) categories_array.append(create_category) create_book = Book() create_book.save() for author in authors_array: create_book.authors.add(author) for category in categories_array: create_book.categories.add(category) create_book.title = title create_book.description = description create_book.api_key = api_key if thumbnail is not False: create_book.thumbnail = thumbnail create_book.save() added_books += 1 else: no_added_books += 1 else: context['no_results'] = True context['added_books'] = added_books context['no_added_books'] = no_added_books except: context['exception'] = True return render(request, 'import_view.html', context)