Example #1
0
 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)
Example #2
0
    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"}
Example #3
0
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 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)
Example #5
0
    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)
Example #6
0
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")
Example #7
0
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()
Example #8
0
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")
Example #9
0
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")
Example #10
0
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())
Example #11
0
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))
Example #12
0
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})
Example #13
0
#获取当前文件的路径(运行脚本)
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]