def upload_auth(request): print request.POST book_name = request.POST['book_name'] author_name = request.POST['author_name'] description = request.POST['description'] file = request.FILES['file'] tag = request.POST['tag'] category = request.POST['category'] print book_name, author_name, description, file, category book = Books(book_name=book_name, author_name=author_name, description=description, thumbnail=file, tag=tag, category=category) book.save() print str(book.thumbnail) filename = str(book.thumbnail).split('.')[0] print filename cmd = 'gs -sDEVICE=pngalpha -o Media/' + filename + '.png -sDEVICE=pngalpha -dLastPage=1 Media/' + str( book.thumbnail) print cmd os.system(cmd) return HttpResponseRedirect('/books/uploadbook')
def Add(request): if request.POST: post = request.POST auth = Author.objects.get(AuthorID = post['aid']) b = Books(ISBN=post['is'],Title=post['tit'],AuthorID=auth,Publisher=post['pb'],Publisher_Date=post['pbd'],Price=post['pri']) b.save() return Search(request)
def create_book(self, data): """ Functionality: Params: Response: """ try: book = Books.objects.filter(name=data["name"])[:1].get() except: book = None if book is None: try: book = Books( name=data["name"], cover_image=data["cover_image"], category=Category.objects.get(pk=data["category_id"]) ) authors_list = list() authors = data["authors"].split(",") for author_id in authors: author = Author.objects.get(pk=author_id) authors_list.append(author) book.save() book.authors.add(*authors_list) # import pdb;pdb.set_trace() serializer = BookSerializer(book) return serializer.data except: return {"error_code": 404, "message": "Invalid Author/Category"} else: return {"message": "Already Exists"}
def test_get_book_page(self): author = Author(name="Test author name", photo="test.jpg") author.save() book = Books(title="Test for book", price=9.99, author_id=author.id, photo_main="test.jpg") book.save() response = self.client.get("/books/book/1") self.assertEqual(response.status_code, 200)
def create_books(): '''创建图书''' c = Category.objects.get(name='计算机') with open('./data/books.csv', 'r', newline='', encoding='utf-8') as f: reader = csv.DictReader(f) for i in reader: print(dict(i)) obj = Books(**dict(i), category=c) obj.save()
def test_create_book(self): book = Books(title='Test Book', description='Some test content.') self.assertEqual(book.title, 'Test Book') self.assertEqual(book.description, "Some test content.") self.assertFalse(book.photo_main) self.assertFalse(book.price) self.assertTrue(book.available)
def test_create_book(self): book = Books(title='Book', description='Some test content description.') self.assertEqual(book.title, 'Book') self.assertEqual(book.description, "Some test content description.") self.assertFalse(book.book_image) self.assertFalse(book.price) self.assertTrue(book.available)
def _save_to_disk(seft, books): tmp_table_name = seft.book_table + ".tmp" with open(tmp_table_name, mode="w") as file: writer = csv.DictWriter(file, fieldnames=Books.schema()) writer.writerows(books) os.remove(seft.book_table) os.rename(tmp_table_name, seft.book_table)
def create(ctx, name, author, year, publisher, topic): """Creates a Book""" book = Books(name, author, year, publisher, topic) book_service = BooksServices(ctx.obj["books_table"]) try: book_service.create_book(book) click.echo("The book was added") except: click.echo("Something failed. Please try again")
def add_from_form(request): message = "Enter data for all fields" title = request.GET.get('title') author = request.GET.get('author') pages = request.GET.get('pages') isbn = request.GET.get('isbn') newBook = Books(title=title, author=author, pages=pages, isbn=isbn) books_list = Books.objects.all() if len(title) == 0 or len(author) == 0 or len(pages) == 0 or len(isbn) == 0: t = loader.get_template('books/index.html') c = Context({'books_list': books_list, 'message' : message}) return HttpResponse(t.render(c)) else: newBook.save() t = loader.get_template('books/index.html') c = Context({'books_list': books_list,}) return HttpResponse(t.render(c))
def upload_auth(request): print request.POST book_name=request.POST['book_name'] author_name=request.POST['author_name'] description=request.POST['description'] file=request.FILES['file'] tag=request.POST['tag'] category=request.POST['category'] print book_name,author_name,description,file,category book=Books(book_name=book_name,author_name=author_name,description=description,thumbnail=file,tag=tag,category=category) book.save() print str(book.thumbnail) filename = str(book.thumbnail).split('.')[0] print filename cmd = 'gs -sDEVICE=pngalpha -o Media/'+filename+'.png -sDEVICE=pngalpha -dLastPage=1 Media/'+str(book.thumbnail) print cmd os.system(cmd) return HttpResponseRedirect('/books/uploadbook')
def add_books(request): if request.method == 'POST': x = BookForm(request.POST) if x.is_valid(): book_name_user = x.cleaned_data['book_name'] book_title_user = x.cleaned_data['book_title'] book_date = x.cleaned_data['insert_date'] book_object = Books(name=book_name_user, title=book_title_user, date=book_date) book_object.save() # will save the data from the form to database return HttpResponseRedirect('listbook') else: x = BookForm(request.POST) return render(request, 'add_book.html', {'form': x})
def delete(ctx, book_id): """Delete a Book""" book_service = BooksServices(ctx.obj["books_table"]) books_list = book_service.list_books() book = [book for book in books_list if book["uid"] == book_id] if book: book = Books(**book[0]) book_service.delete_book(book) click.echo("Book deleted") else: click.echo("Book not found")
def edit(ctx, book_uid): """Edits a Book""" book_service = BooksServices(ctx.obj["books_table"]) books_list = book_service.list_books() book = [book for book in books_list if book["uid"] == book_uid] if book: book = _update_book_flow(Books(**book[0])) book_service.edit_book(book) click.echo("Book updated") else: click.echo("Book not found")
def _print_list(book_list): table = BeautifulTable() headers = [name.upper() for name in Books.schema()] table.column_headers = headers for book_item in book_list: table.append_row([ book_item["name"], book_item["author"], book_item["year"], book_item["publisher"], book_item["topic"], book_item["uid"] ]) # This was an effort for padding the columns # max_len = 0 # for i in table["NAME"]: # if len(str(i)) >= max_len: # max_len = len(str(i)) # table.set_padding_widths(5) # # table.left_padding_widths["NAME"] = mayor - len("NAME") # # table.right_padding_widths["NAME"] = mayor - len("NAME") click.echo(table)
# Create your views here. from books.models import Books from django.http import HttpResponse from django.shortcuts import render_to_response b1=Books(name='Linux Commands Line And Shell Scripting',author ='Richard Blum',uploader='Koosha',upload_date='1391-12-17' ,rate='0',comment='Usefull Book For People Who Wants to Learn linux', url_pdf='home/koosha/Desktop/PDF/linuxcommand.pdf',email='*****@*****.**') b1.save() b2=Books(name='Programming and Customizing AVR',author='Dhananjay V.Gadre',uploader='Koosha',upload_date='1391-12-17' ,rate='0',comment='Fast Way to Learn AVR Programming ', url_pdf='home/koosha/Desktop/PDF/AVR.pdf',email='*****@*****.**') b2.save() def download_page(request): # first_books() #Books.objects.all().delete() books_list=Books.objects.all() """b_list=[] b_list.append(books_list[0]) b_list.append(books_list[1]) b_list.append""" return render_to_response('download.html',{'books_list':books_list})
def list_books(seft): with open(seft.book_table, mode="r") as file: reader = csv.DictReader(file, fieldnames=Books.schema()) return list(reader)
def create_book(seft, book): with open(seft.book_table, mode="a") as file: write = csv.DictWriter(file, fieldnames=Books.schema()) write.writerow(book.to_dict())
#获取当前文件的路径(运行脚本) pwd = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) #获取项目的跟目录 sys.path.append(pwd) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djangoVue.settings") #独立使用django的model import django django.setup() from books.models import Books, BooksCategory, BooksImage from db_tools.data.product_data import row_data for books_detail in row_data: books = Books() books.name = books_detail["name"] #前端中是“¥232”,数据库中是float类型,所以要替换掉 books.market_price = float( int(books_detail["market_price"].replace("¥", "").replace("元", ""))) books.shop_price = float( int(books_detail["sale_price"].replace("¥", "").replace("元", ""))) books.books_brief = books_detail["desc"] if books_detail[ "desc"] is not None else "" books.books_desc = books_detail["books_desc"] if books_detail[ "books_desc"] is not None else "" # 取第一张作为封面图 books.books_front_image = books_detail["images"][0] if books_detail[ "images"] else "" #取最后一个 category_name = books_detail["categorys"][-1]
def addBook(request): genres = Genre.objects.all() publishing_houses = Publishing_house.objects.all() authors = Autors.objects.all() if request.method == "POST": title = request.POST.get('title') id_author = request.POST.get('id_author') text = request.POST.get('text') count = request.POST.get('count') count_page = request.POST.get('count_page') year_publish = request.POST.get('year_publish') publishing_house = request.POST.get('publishing_house') genre = request.POST.get('genre') intro_images = '' view = 0 like = 0 disslike = 0 print("do") if request.FILES: if request.FILES.get('intro_file'): url = 'static/image/' + request.FILES.get('intro_file').name handle_uploaded_file(request.FILES.get('intro_file'), url) intro_images = '/' + url newBook = Books() newBook.title = title newBook.id_author = Autors.objects.get(id=id_author) newBook.text = text newBook.count = count newBook.count_page = count_page newBook.year_publish = year_publish newBook.id_publishing_house = Publishing_house.objects.get( id=publishing_house) newBook.id_genre = Genre.objects.get(id=genre) newBook.intro_images = intro_images newBook.like = like newBook.disslike = disslike newBook.view = view newBook.save() return redirect("/admins/") return render(request, 'myAdmin/bookadd.html', locals())