Beispiel #1
0
def app():
    """Create and configure a new app instance for each test."""

    path_to = os.path.join(os.path.dirname(os.path.dirname(__file__)),
                           'test.db')
    app = create_app({
        'TESTING': True,
        'SQLALCHEMY_DATABASE_URI': 'sqlite:///' + path_to,
        'SQLALCHEMY_TRACK_MODIFICATIONS': False
    })

    # create the database and load test data
    with app.app_context():
        db.create_all()

        author1 = Author(first_name='First', last_name='Author')
        author2 = Author(first_name='Second', last_name='Author')
        db.session.add(author1)
        db.session.add(author2)
        Book(author=author1, title='Test book', published=datetime(2000, 1, 1))
        Book(author=author2, title='Test book', published=datetime(2000, 1, 1))

        db.session.commit()

        yield app

        db.session.remove()
        db.drop_all()

    return app
Beispiel #2
0
def test_get_request_with_no_id(client):
    rs = client.get("/request")

    assert rs.status_code == 200

    ret_dict = rs.json
    assert ret_dict["data"] == []

    new_book = Book(title="4H work week")
    req1 = BookRequest(email="*****@*****.**", book=new_book)
    req2 = BookRequest(email="*****@*****.**", book=new_book)
    db.session.add_all([new_book, req1, req2])

    new_book = Book(title="6H work week")
    req1 = BookRequest(email="*****@*****.**", book=new_book)
    req2 = BookRequest(email="*****@*****.**", book=new_book)
    db.session.add_all([new_book, req1, req2])

    db.session.commit()

    rs = client.get("/request")

    ret_dict = rs.json
    assert len(ret_dict["data"]) == 4
    assert ret_dict["data"][0]["email"] == "*****@*****.**"  # may fail, fix this
Beispiel #3
0
    def test_can_search_books_by_categories(self):
        com = Categories()
        com.name = 'Computer'
        com.save()
        math = Categories()
        math.name = 'Math'
        math.save()

        first_book = Book()
        first_book.name = 'Data Structures and Algorithms in Python'
        first_book.score = 3.8
        first_book.save()
        first_book.categories.add(com)

        second_book = Book()
        second_book.name = 'Data Structures and Algorithms with Java'
        second_book.score = 4.0
        second_book.save()
        second_book.categories.add(com)

        third_book = Book()
        third_book.name = 'Discrete Math'
        third_book.score = 4.5
        third_book.save()
        third_book.categories.add(com, math)

        fourth_book = Book()
        fourth_book.name = 'Math II'
        fourth_book.score = 3.0
        fourth_book.save()
        fourth_book.categories.add(math)

        fifth_book = Book()
        fifth_book.name = 'A byte of Python'
        fifth_book.score = 3.5
        fifth_book.save()
        fifth_book.categories.add(com)

        saved_cat = Categories.objects.all()
        self.assertEqual(saved_cat[0], com)
        self.assertEqual(saved_cat[1], math)

        saved_book = Book.objects.all()
        self.assertEqual(saved_book.count(), 5)

        search_book = Book.objects.filter(name__contains="Data")
        self.assertEqual(search_book.count(), 2)

        search_cate = Categories.objects.filter(name__contains="Com")[0]
        self.assertEqual(search_cate.book_set.all().count(), 4)

        book_in_cate = search_cate.book_set.filter(name__contains="Data")
        self.assertEqual(search_book.count(), 2)

        book_in_two_cate = Book.objects.filter(name__contains="Discrete")[0]
        self.assertEqual(book_in_two_cate.categories.all().count(), 2)
Beispiel #4
0
def seed_db():
    """
    Insert some seed data. Use with care.
    """
    new_book = Book(title="4H work week")
    req1 = BookRequest(email="*****@*****.**", book=new_book)
    req2 = BookRequest(email="*****@*****.**", book=new_book)
    db.session.add_all([new_book, req1, req2])

    new_book = Book(title="6H work week")
    req1 = BookRequest(email="*****@*****.**", book=new_book)
    req2 = BookRequest(email="*****@*****.**", book=new_book)
    db.session.add_all([new_book, req1, req2])

    db.session.commit()
Beispiel #5
0
def save_book(request):
	# print('In save book ')
	print(request.POST)  #WSGI request : post
	b_name = request.POST.get('name')
	b_author = request.POST.get('author')
	b_price = request.POST.get('price')
	b_qty = request.POST.get('qty')
	b_pub = request.POST.get('pub')
	if b_pub == "on":
		flag = True
	else:
		flag = False 
	if request.POST.get('id'):
		book_obj= Book.objects.get(id=request.POST.get('id'))
		book_obj.name = b_name
		book_obj.author = b_author
		book_obj.price = b_price
		book_obj.qty = b_qty 
		book_obj.is_published = flag
		book_obj.save() 
		return redirect ('welcome') 
	else:
		b = Book(name = b_name, author= b_author ,qty= b_qty,price= b_price,is_published=flag)
		b.save()
		return redirect ('welcome') 
Beispiel #6
0
    def create_or_update(self, request, *args, **kwargs):
        post_params = dict(copy(request.POST))
        create = strtobool(post_params.pop('create', True)[0])
        book = Book(**post_params)

        result = {
            'success': False,
            'error_message': '',
        }

        try:
            book.file = request.FILES['file']
        except KeyError:
            result['error_message'] = ('Файл с книгой не обнаружен!')
            return Response(result)
        book.cover = request.FILES.get('cover', None)
        book.pk = kwargs.pop('pk')
        book.loaded_by_id = request.user.profile.id

        if create:
            try:
                book.save()
                FavouriteBooks.objects.create(
                    profile_id=request.user.profile.id,
                    book_id=book.id,
                )
            except IntegrityError as ex:
                result['error_message'] = 'Книга с подобным id уже загружена!'
                return Response(result)
        else:
            book.loaded_date = datetime.now()
            book.save()
            result['success'] = True

        return Response(result)
Beispiel #7
0
def addbook(request):
    #第一种方式
    #Book.objects.create(name="python",price=99,author="yuan",pub_date="2018-12-12")
    #第二中方式
    book = Book(name="python", price=99, author="yuan", pub_date="2018-12-12")
    book.save()
    return redirect("/index")
Beispiel #8
0
def save_book(book):

    element = Book(title=book['title'],
                   subtitle=book['subtitle'],
                   description=book['description'],
                   previewLink=book['previewLink'],
                   imagen=book['imagen'],
                   ISBN=book['ISBN'])
    element.save()

    for author in book['authors']:

        item = Author.objects.filter(name=author)
        print(len(item))

        if len(item) < 1:
            item = Author(name=author)
            item.save()
        else:
            item = item.first()

        element.ref_author.add(item)

    for category in book['categories']:

        item = Category.objects.filter(name=category)
        if len(item) < 1:
            item = Category(name=category)
            item.save()
        else:
            item = item.first()
        element.ref_category.add(item)

    return element
Beispiel #9
0
def add():
    form = request.form
    name = form.get('name')
    author = form.get('author')
    book = Book(name=name, author=author, date_created=datetime.now())
    book.save()
    return jsonify(status='success')
Beispiel #10
0
    def save_book_info_to_db(self, info):
        '''保存书本信息到数据库'''
        logging.info('保存<<{}>>信息到数据库'.format(info['name']))

        author = self.save_or_get_author_db(info)

        book = self.book
        if not self.book:
            lock.acquire()
            try:
                books = Book.normal.filter(title=info['name'],
                                           author=author,
                                           book_type=self.book_type)
                if books.count() > 0:
                    book = books[0]
                    for book in books[1:]:
                        book.delete()
                else:
                    book = Book()

                if not book.desc:
                    book.book_type = self.book_type
                    book.title = info.get('name')
                    book.author = author
                    book.desc = info.get('desc')
                    book.markeup = info.get('markeup')
                    book.origin_addr = self.url
                    book.on_shelf = self.on_shelf
                    book.save()
            finally:
                lock.release()
        self.book = book
        return book
Beispiel #11
0
 def test_empty(self):
     #given
     #when
     book = Book()
     #then
     self.assertEqual(book.title, '')
     self.assertEqual(book.ISBN, '')
Beispiel #12
0
    async def handler(self):
        res = self.do_request(self.url, self.headers)
        book_info_list = self.parser(res)
        count = 0
        author, _ = Author.normal.get_or_create(name='未知')
        exist = Book.normal.filter(
            origin_addr__in=[info['url']
                             for info in book_info_list]).values('origin_addr')
        exist_url = [i['origin_addr'] for i in exist]
        need_url = [i for i in book_info_list if i['url'] not in exist_url]
        books = []
        for idx, info in enumerate(need_url, 1):
            logging.info('新自动插入书{}/{}条: {}  {}'.format(idx, len(need_url),
                                                       info['title'],
                                                       info['url']))
            book = Book(on_shelf=False,
                        author=author,
                        book_type=BOOK_TYPE_DESC.Novel,
                        title=info['title'][:60],
                        markup=info['label'][:100],
                        origin_addr=info['url'][:200])
            books.append(book)

            if len(books) >= 500:
                Book.normal.bulk_create(books)
                books = []
        Book.normal.bulk_create(books)
Beispiel #13
0
    def handler_all_book(self, book_info_list):
        logging.info("自动插入书本信息,即将处理{}条数据".format(len(book_info_list)))
        count = 0
        author, _ = Author.normal.get_or_create(name='未知')
        exist = Book.normal.filter(
            origin_addr__in=[info['url']
                             for info in book_info_list]).values('origin_addr')
        exist_url = [i['origin_addr'] for i in exist]
        need_url = [i for i in book_info_list if i['url'] not in exist_url]
        books = []
        for idx, info in enumerate(need_url, 1):
            logging.info('新自动插入书{}/{}条: {}  {}'.format(idx, len(need_url),
                                                       info['title'],
                                                       info['url']))
            if info.get('author', None):
                retry = 5
                while retry > 0:
                    try:
                        author, _ = Author.normal.get_or_create(
                            name=info['author'])
                    except:
                        retry -= 1
            book = Book(on_shelf=False,
                        author=author,
                        book_type=BOOK_TYPE_DESC.Novel,
                        title=info['title'][:60],
                        markup=info['label'][:100],
                        origin_addr=info['url'][:200])
            books.append(book)

            if len(books) >= 500:
                Book.normal.bulk_create(books)
                books = []
        Book.normal.bulk_create(books)
Beispiel #14
0
def populate():
    print('Populating books...', end='')
    titles = [
        '穿在身上的 --草地恐龍時代毛衣',
        '穿在身上的 --星空刺繡暴龍長袖T恤',
        '穿在身上的 --手繪躲貓貓長袖T恤',
        '穿在身上的 --刺繡小鹿坑條毛衣',
        '穿在腿上的 --前口袋棉麻寬褲 ( 灰綠色 / 深灰色 )',
        '穿在腿上的 --刺繡扣復古高腰七分裙 ( 灰 / 藍 )',
        '其他其他的小東西 --窗台上的祕密戒指',
        '其他其他的小東西 --解不開結的戒指',
        '其他其他的小東西 --窗台的祕密戒指',
        '其他其他的小東西 --諾亞之心戒指',
        '裝錢的錢包 --水松木長夾( 彩色 / 原色 )',
        '裝錢的錢包 --手染掛頸手機零錢收納包 ( 海洋 / 夕陽 / 沙漠 )',
    ]
    materials = ['棉麻', 'Cotton, Polyester', 'Portuguese Cork', '銀']

    Book.objects.all().delete()
    for title in titles:
        book = Book()
        book.title = title
        n = random.randint(0, len(materials) - 1)
        book.material = materials[n]
        book.pubdate = datetime.datetime.today()
        book.price = 690
        book.save()
    print('done')
Beispiel #15
0
def mebook(startPage, endPage):
    for i in range(startPage, endPage + 1):
        url = 'http://mebook.cc/page/' + str(i)
        html = requests.get(url)
        selector = etree.HTML(html.text)
        endText = selector.xpath('//*[@id="container"]/div/h2/text()')
        if len(endText) > 0 and '传说' in endText[0]:
            print('mebook done')
            return  # 结束
        lis = selector.xpath('//*[@id="primary"]/ul/li')
        zz_page = True
        for li in lis:
            theBook = BookInfo(li)
            if (theBook.isBook()):
                bs = Book.objects.filter(bookname=theBook.bookName,
                                         zz=theBook.zz,
                                         gxsj=theBook.gxrq)
                if (len(bs) > 0):
                    continue
                zz_page = False
                db = DoubanBook(theBook.bookName, theBook.zz)
                [tags, rating, bookUrl, summary] = (db.getBookInfo())
                #print(theBook.bookName, theBook.zz, tags, rating, bookUrl, summary)
                try:
                    rating = float(rating)
                except:
                    rating = 0
                bb = Book(bookname = theBook.bookName, zz=theBook.zz, gxsj = theBook.gxrq\
                        , tags=','.join(tags), rating=rating, cclink=theBook.booklink\
                        , dblink=bookUrl, bz=summary, zzFlag=0)
                bb.save()
                #print(theBook.bookName + ' ' + theBook.zz + ' ' + theBook.gxrq)
        print('the %d page done' % i)
        if (zz_page):
            return
Beispiel #16
0
def create_books():
    with open('data/all_book.csv', 'r') as f:
        reader = csv.reader(f)
        for book_detail in reader:
            book_name = book_detail[0]
            book_score = float(book_detail[1])
            book_cat = book_detail[-1].split(',')  # Split category

            try:
                if Book.objects.filter(
                        name__exact=book_name):  # Check for an existing book
                    raise NameDuplicate(book_name)
                else:
                    b = Book(name=book_name, score=book_score)
                    b.save()

                    for c in book_cat:
                        if Categories.objects.filter(
                                name__exact=c
                        ):  # Check for an existing category
                            b.categories.add(Categories.objects.get(name=c))
                        else:
                            Categories(name=c).save()
                            b.categories.add(Categories.objects.get(name=c))

            except NameDuplicate as n:
                print('NameDuplicate: There is an existing ' + n.name +
                      ' book')
Beispiel #17
0
def index(request):
    '''
    操作rom
    1.所有的模型类都有个objects属性,就是一个manager对象,用于进行数据操作管理
    2.添加数据
        2.1 模型类。objects.create(属性=值)
                返回生成的对象----》记录
        2.2 添加数据
            对象 = 模型类()
            对象。属性=值
            对象。save()保存数据
        2.3 ,模型类。objects.get_or_crate(属性=值)
            如果有就获取,没有就创造
    3查看数据
            模型类。objects.all() 查看所有数据
            模型类。objects.filter(条件)条件查询,相当于where
            模型类。objects.get(条件)只能获取一条数据
    4修改数据
            4.1 模型类。object。filter(条件).update(属性=新属性值)
                一次更新多条
            4.2 先查询出对象  ---一次更新一条
                对象。属性=值
                对象。属性=值
                对象。属性=值
                对象。save()
        5 删除数据
            查询偶delete()
    :param request:
    :return:
    '''
    rs = Book.objects.create(name='红楼梦', author='曹雪芹', price=9.9)
    print(rs)
    book = Book()
    book.name = '红楼梦'
    book.author = '曹雪芹'
    book.price = 99
    book.save()
    book = Book.objects.get_or_create(name='三国演义', author='施耐庵', price=88)
    print(book)

    #查看数据
    # books=Book.objects.all()
    # content = {
    #     "books":books
    # }
    # book = Book.objects.get(id=1)
    # book = Book.objects.get(name='红楼梦')  如果存在多条数据,返回列表[]
    # books = Book.objects.filter(name='红楼梦').first()#获取第一条
    # print(book)

    #更新数据
    # rs = Book.objects.filter(name='红楼梦').update(name='红楼2')
    # print(rs)
    # book = Book.objects.get(id=1)
    # book.name='红楼3'
    # book.save()
    book = Book.objects.get(id=1)
    book.delete()
    return HttpResponse("bookModel")
Beispiel #18
0
def test_get_request_with_id_present(client):
    new_book = Book(title="4H work week")
    req1 = BookRequest(email="*****@*****.**", book=new_book)
    db.session.add_all([new_book, req1])
    db.session.commit()

    rs = client.get("/request/%d" % req1.id)
    assert rs.status_code == 200
Beispiel #19
0
    def test_add_book_equals_isbn(self):
        #given
        book1 = Book()
        book2 = Book()

        #when
        book1.title = "What's up?"
        book1.ISBN = "123123123123"
        book1.save()

        book2.title = "What's up?"
        book2.ISBN = "123123123123"
        book2.save()

        #then
        books_by_ISBN = Book.objects.filter(ISBN="123123123123")
        self.assertEqual(len(books_by_ISBN), 1)
Beispiel #20
0
def bulk_create_test():
    start = time.time()
    book_objects = []
    for i in range(990000):
        book_objects.append(Book())
    Book.objects.bulk_create(book_objects)
    process_time = time.time() - start
    print(process_time)
def save_book_from_row(book_row):
    book = Book()
    book.id = book_row[0]
    book.title = book_row[1]
    # book.category=book_row[2]
    book.author = book_row[3]
    book.publication = book_row[4]
    book.category = book_row[5]
    book.save()
Beispiel #22
0
 def add_book(self, json_object):
     book = Book()
     book.name = json_object['name']
     book.save(
     )  # must be saved to assign id (used to establish a many-to-many relationship)
     book.author.set(json_object['author'])
     book.description = json_object['description']
     book.genre.set(json_object['genre'])
     book.user.set(json_object['user'])
     book.save()
Beispiel #23
0
 def tests(self):
     Book(id=1, title='Title', rating=0.0).save()
     links = [
         '/admin/', '/register/', '/login/', '/', '/books', '/books/p0',
         '/book/1', '/book/2', '/user/1'
     ]
     codes = [200, 200, 200, 200, 200, 200, 200, 200, 404]
     for link, code in zip(links, codes):
         self.assertEqual(
             self.client.get(link, follow=True).status_code, code)
Beispiel #24
0
    def setUp(self):
        """ Create a user object to be used by the tests """
        time_mock = datetime.datetime(2017, 4, 10, 12, 00, tzinfo=pytz.utc)
        with mock.patch('django.utils.timezone.now') as mock_time:
            mock_time.return_value = time_mock
            self.user = CustomUser(id=111, email='*****@*****.**', password='******', first_name='fname',
                                   middle_name='mname',
                                   last_name='lname')
            self.user.save()
            self.user_free = CustomUser(id=222, email='*****@*****.**', password='******', first_name='2fname',
                                        middle_name='2mname',
                                        last_name='2lname')
            self.user_free.save()

            self.author1 = Author(id=101, name="author1", surname="s1", patronymic="p1")
            self.author1.save()

            self.author2 = Author(id=102, name="author2", surname="s2", patronymic="p2")
            self.author2.save()

            self.book1 = Book(id=101, name="book1", description="description1", count=1)
            self.book1.save()
            self.book1.authors.add(self.author1)
            self.book1.save()

            self.book2 = Book(id=102, name="book2", description="description2")
            self.book2.save()
            self.book2.authors.add(self.author2)
            self.book2.save()

            self.book3 = Book(id=103, name="book3", description="description3")
            self.book3.save()
            self.book3.authors.add(self.author1)
            self.book3.authors.add(self.author2)
            self.book3.save()

            self.order1 = Order(id=101, user=self.user, book=self.book1, plated_end_at=TEST_DATE)
            self.order1.save()
            self.order2 = Order(id=102, user=self.user, book=self.book2, plated_end_at=TEST_DATE)
            self.order2.save()
            self.order3 = Order(id=103, user=self.user, book=self.book3, end_at=TEST_DATE_END, plated_end_at=TEST_DATE)
            self.order3.save()
Beispiel #25
0
def fake():
    db.create_all()
    books = [
        {
            'title': 'My Neighbor Totoro',
            'year': '1988'
        },
        {
            'title': 'Dead Poets Society',
            'year': '1989'
        },
        {
            'title': 'A Perfect World',
            'year': '1993'
        },
        {
            'title': 'Leon',
            'year': '1994'
        },
        {
            'title': 'Mahjong',
            'year': '1996'
        },
        {
            'title': 'Swallowtail Butterfly',
            'year': '1996'
        },
        {
            'title': 'King of Comedy',
            'year': '1999'
        },
        {
            'title': 'Devils on the Doorstep',
            'year': '1999'
        },
        {
            'title': 'WALL-E',
            'year': '2008'
        },
        {
            'title': 'The Pork of Music',
            'year': '2012'
        },
    ]
    all_books = [
        Book(title=book['title'], year=book['year']) for book in books
    ]
    db.session.add_all(all_books)
    try:
        db.session.commit()
        click.echo('Success.')
    except:
        db.session.rollback()
    click.echo('Done.')
    def test_cannot_save_empty_title_books(self):
        publisher = Publisher.objects.create(name='Pub',
                                             country='China',
                                             website='http://www.pub.com')
        classfication = Classfication.objects.create()

        book = Book(classfication=classfication, publisher=publisher)
        book.title = ''
        with self.assertRaises(ValidationError):
            book.save()
            book.full_clean()
Beispiel #27
0
def add_fk1():
    from book.models import Publisher, Book

    p = Publisher.objects.get(name='机械工业出版社')

    b = Book()
    b.name = 'Python 高并发实战'
    b.publisher_state = 2
    b.desc = 'Good'
    b.publisher = p  # ForeignKey
    b.save()
Beispiel #28
0
def get_book_inf(xml, messages=None):
    '''
    Reads information from xml, finds or creates the book author,
    the book files, annotations, etc

    Returns tupel of 
    (NOT saved models.Book with information about it in its fileds,
    list of authors, list of book files, list of annotations)

    Or raises InputDataServerException, if the book title not found.
    '''
    if messages == None:
        messages = []

    book = Book(title='', lang='')
    authors = []
    book_files = []
    annotations = []

    # unused flag
    #is_author_created = False

    for node in xml.getchildren():
        if node.tag == 'title':
            book.title = strip_str(node.text)

        if node.tag == 'lang':
            book.language = get_language(node.text)

        if node.tag == 'authors' and book.title:
            (authors, is_author_created) = get_authors(node, messages)

        if node.tag == 'files' and book.title:
            book_files = get_files(node)

        if node.tag == 'annotation' and book.title:
            annotation_txt = strip_str(node.text)
            if annotation_txt:
                annotation = \
                    Annotation.objects.get_or_create(name=annotation_txt)[0]
                annotations.append(annotation)

    # if there are not the title of the book, raise exception
    if not book.title:
        analyzer_log.warning('In request there is not the title of the book')
        raise InputDataServerException(
            'In request there is not the title of the book')
    # if there are not the book_file, raise exception
    elif not book_files:
        analyzer_log.warning('In request there is not the book_file')
        raise InputDataServerException('In request there is not the book_file')

    return (book, authors, book_files, annotations)
Beispiel #29
0
def add_book(request):
    response = {}
    try:
        book = Book(book_name=request.GET.get('book_name'))
        book.save()
        response['msg'] = 'success'
        response['error_num'] = 0
    except Exception as e:
        response['msg'] = str(e)
        response['error_num'] = 1

    return JsonResponse(response)
Beispiel #30
0
def test_post_request_without_valid_email(client):
    new_book = Book(title="4H work week")
    db.session.add_all([new_book])
    db.session.commit()

    rs = client.post("/request",
                     json={
                         "email": "[invalid!email]",
                         "title": "4H work week"
                     })

    assert rs.status_code == 400