Ejemplo n.º 1
0
 def _save_or_get_author_db(self, info):
     author = Author.normal.filter(name=info['author_name']).first()
     if not author:
         author = Author()
     author.name = info['author_name']
     author.save()
     return author
Ejemplo n.º 2
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
Ejemplo n.º 3
0
def update_author(xml):
    "Executes xml, creates author or updates inf about it."
    entity_id = get_id(xml)

    if entity_id is None:
        # Create new author
        author = Author()
    else:
        # Modify existing author
        author = Author.objects.get(id=entity_id)

    full_name = get_tag_text(xml, 'full_name')

    if full_name:
        author.name = full_name
    
    if not author.name:
        raise IntegrityError("'full_name' can't be empty")

    try:
        credit_str = get_tag_text(xml, 'credit')
        if credit_str:
            credit = int(credit_str)
            author.credit = credit
    except ValueError, ex:
        raise InputDataServerException(ex)
Ejemplo n.º 4
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
Ejemplo n.º 5
0
def insert_author(request):
    if request.POST:
        post = request.POST
        try:
            author_remain = Author.objects.filter(AuthorID = post["author_id"])[0]
            message="this author id exists "            
            return render_to_response("mistake.html",{'message': message})
        except IndexError:
            new_author = Author( AuthorID= post["author_id"],Name = post["author_name"] ,Age=post["age"], Country=post["country"])    
            new_author.save()
            return render_to_response("success.html")
    return render_to_response("insert_author.html")
Ejemplo n.º 6
0
def addbook(request):
	if 'addsub' in request.GET:
		temp_add = request.GET['addsub']
	if 'again' in request.GET:
		again = request.GET['again']
	if 'add_Author' in request.GET:
		add_Author = request.GET['add_Author']
	if temp_add:
		add_ISBN = request.GET['ISBN']
		add_Title = request.GET['Title']
		add_Name = request.GET['Author_name']
		add_Publisher = request.GET['Publisher']
		add_PublishDate = request.GET['PublishDate']
		add_Price = request.GET['Price']
		if not add_ISBN:
			return render_to_response('addbook.html',{'ret': '请输入完整信息!', 'add_book':'书籍信息:'})
		if not add_Title:
			return render_to_response('addbook.html',{'ret': '请输入完整信息!', 'add_book':'书籍信息:'})
		if not add_Name:
			return render_to_response('addbook.html',{'ret': '请输入完整信息!', 'add_book':'书籍信息:'})
		if not add_Publisher:
			return render_to_response('addbook.html',{'ret': '请输入完整信息!', 'add_book':'书籍信息:'})
		if not add_PublishDate:
			return render_to_response('addbook.html',{'ret': '请输入完整信息!', 'add_book':'书籍信息:'})
		if not add_Price:
			return render_to_response('addbook.html',{'ret': '请输入完整信息!', 'add_book':'书籍信息:'})
		for temp_author in Author.objects.all():
			if(add_Name,temp_author.Name)==0:
				p = Book( ISBN = add_ISBN, Title = add_Title, AuthorID = temp_author, Publisher = add_Publisher, PublishDate = add_PublishDate, Price = add_Price)
				p.save()
				return render_to_response('addbook.html',{'ret': '已添加书籍!', 'add_book':'书籍信息:'})
		return render_to_response('addbook.html',{'ret':'No this author', 'add_author':'作者信息:'})
		
	if add_Author:
		add_AuthorName = request.GET['Name']
		add_AuthorID = request.GET['AuthorID']
		add_Age = request.GET['Age']
		add_Country = request.GET['Country']
		if not add_AuthorName:
			return render_to_response('addbook.html',{'ret': '请输入完整信息!', 'add_author':'作者信息:'})
		if not add_AuthorID:
			return render_to_response('addbook.html',{'ret': '请输入完整信息!', 'add_author':'作者信息:'})
		if not add_Age:
			return render_to_response('addbook.html',{'ret': '请输入完整信息!', 'add_author':'作者信息:'})
		if not add_Country:
			return render_to_response('addbook.html',{'ret': '请输入完整信息!', 'add_author':'作者信息:'})
		k = Author(AuthorID = add_AuthorID, Name = add_Name, Age = add_Age, Country = add_Country)
		k.save()
		return render_to_response('addbook.html',{'ret': '已添加作者!', 'add_book':'书籍信息:'})
	if again:
		return render_to_response('addbook.html',{'add_book':'书籍信息:'})
	return render_to_response('addbook.html',{'add_book':'书籍信息:'})
Ejemplo n.º 7
0
def add_author(request):
	book_list = Book.objects.all()
	author_list = Author.objects.all()
	if request.POST:
		post = request.POST
		new_author = Author(
				AuthorID_PK = post["AuthorID_PK"],
				Name = post["Name"],
				Age = post["Age"],
				Country = post["Country"],
				)
		new_author.save()
		return HttpResponseRedirect('/add_book/')
	return render_to_response('add_author.html')
Ejemplo n.º 8
0
def do_import(data, if_book_exists='continue'):
    """import a book in the database. 
    
    Expects a dict of the following format:
{ 'publisher': (u'oreilly', u"Farnham ; O'Reilly, 2002 printing, c2003."), 'authors': [(u'ray_randy_j', u'Ray, Randy J.'), (u'kulchenko_pavel', u'Kulchenko, Pavel')], 'subjects': [(u'perl_computer_program_language', u'Perl (Computer program language)'), (u'internet_programming', u'Internet programming')], 'language': u'eng', 'title': u'Programming Web services with Perl', 'ddc': u'5.2762', 'long_title': None, 'edition_info': u'', 'summary': None }
    which other importers have to deliver.

    if_book_exists may be eighter one of 'continue', 'overwrite', 'error'
    """

    try:
        book = Book.objects.get(isbn=data['isbn'])
    except Book.DoesNotExist:
        book = Book(isbn=data['isbn'], title=data['title'], 
                    long_title=data.get('long_title'), language=data.get('language'), 
                    ddc=data.get('ddc'), edition_info=data.get('edition_info'), 
                    summary=data.get('summary'))  
        try:
            publisher = Publisher.objects.get(publisher_id=data['publisher'][0])
        except Publisher.DoesNotExist:
            publisher = Publisher(publisher_id=data['publisher'][0], name=data['publisher'][1])
            publisher.save()
        book.publisher = publisher

        for authordata in data['authors']:
            try:
                author = Author.objects.get(author_id=authordata[0])
            except Author.DoesNotExist:
                author = Author(author_id=authordata[0], name=authordata[1])
                author.save()
            book.authors.add(author)

        for subjectdata in data['subjects']:
            try:
                subject = Subject.objects.get(subject_id=subjectdata[0])
            except Subject.DoesNotExist:
                subject = Subject(subject_id=subjectdata[0], name=subjectdata[1])
                subject.save()
            book.subjects.add(subject)

        book.save()
    else:
        if if_book_exists.lower() == "error":
            raise ValueError("Book %s already exists!" % data['isbn'])
        elif if_book_exists.lower() == "overwrite":
            book.delete()
            do_import(data)
        elif if_book_exists.lower() != "continue":
            raise ValueError("if_book_exists must be eighter one of 'continue', 'overwrite', 'error'")
Ejemplo n.º 9
0
    def test_author(self):
        """ tests author response"""
        client = Client()
        author = Author(name="Author")
        author.save()
        author_id = author.id

        response = client.get('/author.atom/id%s/' %(author_id,))
        print 'status code for good author request in atom', response.status_code
        self.failUnlessEqual(response.status_code, 200)
        
        response = client.get('/author/id%s/' %(author_id,))
        print 'status code for good author request in xhtml', response.status_code
        self.failUnlessEqual(response.status_code, 200)

        response = client.get('/author.atom/id%s/' %(author_id + 1,))
        print 'status code for bad author request in atom', response.status_code
        self.failUnlessEqual(response.status_code, 404)                          

        response = client.get('/author/id%s/' %(author_id + 1,))
        print 'status code for bad author request in xhtml', response.status_code
        self.failUnlessEqual(response.status_code, 404)                          
Ejemplo n.º 10
0
    def bookLoading(self, num):
        start = 1  # except first row
        end = num
        self.progressBar(start, end, "Books data uploading")
        btLen = len(self.bookTags) - 1
        rtLen = len(self.ratings) - 1
        for row in self.booksInfo[1:num + 1]:  # +1 because num+1-1=num
            bTitle = str(row[10])
            bAuthor = str(row[7]).split(',')
            bRating = 0.0

            book = Book(id=row[1], title=bTitle, rating=bRating)
            if row[22][8] != 's':
                image_dir = row[22].split("/")[3:]
                image_dir[-2] = image_dir[-2][:-1] + '{}'
                image_dir = '/'.join(image_dir)
                book.smallImage = image_dir.format("s")
                book.middleImage = image_dir.format("m")
                book.bigImage = image_dir.format("l")
            book.save()
            ## Rating counting
            step = rtLen // 2
            maxIterations = rtLen // 2
            cnt = 0
            i = 1
            up = False
            down = False
            while not up and not down and cnt < maxIterations:
                cnt += 1
                if self.ratings[i][0] == row[1]:
                    caret = i
                    sum = 0.0
                    ratingCnt = 0.0

                    while caret > 1 and self.ratings[caret][0] == row[1]:
                        caret -= 1
                        curRating = float(self.ratings[caret][2])
                        userId = int(self.ratings[caret][1])
                        try:
                            curUser = User.objects.get(id=userId)
                            myUser = MyUser.objects.get(user=curUser)
                            br = BookRating(rtd_book=book, rating=curRating)
                            br.save()
                            curUser.save()
                            myUser.rated_books.add(br)
                            myUser.save()
                        except ObjectDoesNotExist:
                            self.warning(
                                "User with id = {:d} does not exist".format(
                                    userId))
                        sum += curRating
                        ratingCnt += 1.0

                    up = True
                    caret = i
                    while caret < rtLen and self.ratings[caret][0] == row[1]:
                        curRating = float(self.ratings[caret][2])
                        userId = int(self.ratings[caret][1])
                        try:
                            curUser = User.objects.get(id=userId)
                            myUser = MyUser.objects.get(user=curUser)
                            br = BookRating(rtd_book=book, rating=curRating)
                            br.save()
                            curUser.save()
                            myUser.rated_books.add(br)
                            myUser.save()
                        except ObjectDoesNotExist:
                            self.warning(
                                "User with id = {:d} does not exist".format(
                                    userId))
                        sum += curRating
                        ratingCnt += 1.0
                        caret += 1
                    down = True
                    bRating = sum / ratingCnt
                elif int(self.ratings[i][0]) > int(row[1]):
                    i -= step
                    step //= 2
                else:
                    i += step
                    step //= 2

            book.rating = bRating

            try:
                bDate = int(float(row[8]))
            except ValueError:
                bDate = 0
            book.date = bDate

            book.save()

            for auth in bAuthor:
                try:
                    a = Author.objects.get(name__exact=auth)
                except ObjectDoesNotExist:
                    a = Author(name=auth)
                    a.save()
                book.author.add(a)

            ## Genre setting
            step = btLen // 2
            maxIterations = btLen // 2
            cnt = 0
            i = 1
            up = False
            down = False
            while not up and not down and cnt < maxIterations:
                cnt += 1
                if self.bookTags[i][0] == row[1]:
                    caret = i
                    while caret > 1 and self.bookTags[caret][0] == row[1]:
                        caret -= 1
                        try:
                            gName = self.allGenres.get(self.bookTags[caret][1])
                            g = Genre.objects.get(name=gName)
                            book.genre.add(g)
                        except ObjectDoesNotExist:
                            # nothing to do
                            pass
                    up = True
                    caret = i
                    while caret < btLen and self.bookTags[caret][0] == row[1]:
                        try:
                            gName = self.allGenres.get(self.bookTags[caret][1])
                            g = Genre.objects.get(name=gName)
                            book.genre.add(g)
                        except ObjectDoesNotExist:
                            # nothing to do
                            pass
                        caret += 1
                    down = True
                elif int(self.bookTags[i][0]) > int(row[1]):
                    i -= step
                    step //= 2
                else:
                    i += step
                    step //= 2

            book.save()
            self.progressBar(start, end, "Books data uploading")
            start += 1
Ejemplo n.º 11
0
def upd_all_book(book_parser):
    """更新所有小说信息"""
    #global g_books
    #bt = time.time()
    for l in book_parser.book_list:
        l['Alias'] = l['Name'] + '_' + l['Author']
        l['Alias_Host'] = l['Alias'] + '_' + book_parser.host_name
        l['Alias_Author'] = l['Author'] + '_' + book_parser.host_name
        l['is_new_book'] = config.Yes
        l['is_new_bookinfo'] = config.Yes
        l['is_new_contentinfo'] = config.Yes
        l['is_new_author'] = config.Yes
        for b in config.g_books:
            if (l['Alias'].decode(config.SYS_ENCODING)==b['Alias']):
                l['book_id'] = b['id']
                l['is_new_book'] = config.No
                break

        if (l['is_new_book']):
            for a in config.g_authors:
                if (l['Alias_Author'].decode(config.SYS_ENCODING)==a['Alias']):
                    #print l['Author'].decode(config.SYS_ENCODING), a['Name']
                    l['author_id'] = a['id']
                    l['is_new_author'] = config.No
                    break
        for bi in config.g_bookinfos:
            if (l['Alias_Host'].decode(config.SYS_ENCODING)==bi['Alias']):
                l['bookinfo_id'] = bi['id']
                l['is_new_bookinfo'] = config.No
                break
        #if (l.has_key('bookinfo_id')) and (config.g_contentinfos.has_key(l['bookinfo_id'])):
        try:
            if (config.g_contentinfos[l['bookinfo_id']] == l['Content_Url']):
                l['is_new_contentinfo'] = config.No
            #else:
                #config.g_contentinfos[l['bookinfo_id']] = l['Content_Url']
        except KeyError:
            pass

    #print "step 1 use time: %f" % (time.time()-bt)
    #bt = time.time()
    k = 0
    for b in book_parser.book_list:
        if (book_parser.is_origin):
          if (b.has_key('is_new_book') and (b['is_new_book'])):
            if (b.has_key('is_new_author') and (b['is_new_author'])):
                author = Author()
                author.Name = b['Author'].decode(config.SYS_ENCODING)
                author.Alias = b['Alias_Author'].decode(config.SYS_ENCODING)
                author.save()
                b['author_id'] = author.id
                config.g_authors.append(model_to_dict(author))
            book = Book()
            book.Name = b['Name'].decode(config.SYS_ENCODING)
            book.Alias = b['Alias'].decode(config.SYS_ENCODING)
            book.author_id = b['author_id']
            book.save()
            config.g_books.append(model_to_dict(book))
            b['book_id'] = book.id
            b['is_new_book'] = config.No
            b['is_new_author'] = config.No
        if (b.has_key('is_new_bookinfo') and b.has_key('is_new_book')):
          if ((b['is_new_bookinfo']) and (not b['is_new_book'])):
            bookinfo = BookInfo()
            bookinfo.book_id = b['book_id']
            bookinfo.HostName = book_parser.host_name 
            bookinfo.BookUrl = b['Book_Url']
            bookinfo.Alias = b['Alias_Host'].decode(config.SYS_ENCODING)
            bookinfo.LastContent = b['Content'].decode(config.SYS_ENCODING)
            bookinfo.ContentUrl = b['Content_Url']
            bookinfo.LastUpdated = time.time()
            bookinfo.save()
            config.g_bookinfos.append(model_to_dict(bookinfo))
            b['bookinfo_id'] = bookinfo.id
            b['is_new_bookinfo'] = config.No
          """
          else:
            bookinfo = BookInfo.objects.get(id=b['bookinfo_id'])
            bookinfo.LastContent = b['Content'].decode(config.SYS_ENCODING)
            bookinfo.ContentUrl = b['Content_Url']
            bookinfo.LastUpdated = time.strftime('%Y-%m-%d %H:%M',
                                                 time.localtime())
            bookinfo.save()
          """
        if (b.has_key('is_new_contentinfo') and b.has_key('is_new_book')):
          if ((b['is_new_contentinfo']) and (not b['is_new_book'])):
            k = k + 1
            cinfo = ContentInfo()
            cinfo.bookinfo_id = b['bookinfo_id']
            cinfo.LastContent = b['Content'].decode(config.SYS_ENCODING)
            cinfo.ContentUrl = b['Content_Url']
            cinfo.LastUpdated = time.strftime('%Y-%m-%d %H:%M',
                                                 time.localtime())
            cinfo.save()
            config.g_contentinfos[b['bookinfo_id']] = b['Content_Url']
            b['is_new_contentinfo'] = config.No
Ejemplo n.º 12
0
def postbox(request):
    if request.method == "POST":
        onedrive_data = json.loads(request.body)
        if onedrive_data['name'][-5] == '.':
            file_name, file_format = onedrive_data['name'][:-5], onedrive_data[
                'name'][-4:]
        elif onedrive_data['name'][-4] == '.':
            file_name, file_format = onedrive_data['name'][:-4], onedrive_data[
                'name'][-3:]
        else:
            h = HttpResponse('后缀名不合法')
            h.status = 404
            return h
        if (file_format != 'epub') and (file_format != 'mobi') and (
                file_format != 'azw3') and (file_format !=
                                            'pdf') and (file_format != 'kfx'):
            h = HttpResponse('后缀名不合法')
            h.status = 404
            return h
        if Book.objects.filter(file_name=file_name):  #如果存在这本书
            b = Book.objects.get(file_name=file_name)
            b.save_url(onedrive_data['url'], file_format,
                       onedrive_data['size'])
            b.save()
        else:  #如果不存在这本书
            douban_data = get_book_data(file_name)
            pprint(douban_data)
            b = Book()
            b.date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            if douban_data:
                b.file_name = file_name
                b.title = douban_data['title']
                if douban_data['subtitle']:
                    b.subtitle = douban_data['subtitle']
                if douban_data['alt_title']:
                    b.english_title = douban_data['alt_title']
                b.save()  #为添加作者准备
                author = douban_data.get('author')
                if author:  #如果作者不为空
                    if len(author) == 1:
                        a_inf = douban_data['author_intro']
                    else:
                        a_inf = douban_data['author_intro'].split('\n')[1::2]
                    al = []
                    if len(a_inf) == len(douban_data['author']):
                        a_inf = a_inf
                    else:
                        a_inf = [
                            '不详' for i in range(len(douban_data['author']))
                        ]
                    for i in range(len(douban_data['author'])):
                        d = {}
                        d['name'] = douban_data['author'][i]
                        d['information'] = a_inf[i]
                        al.append(d)
                    for l in al:
                        a = None
                        if not Author.objects.filter(name=l['name']):  #添加作者
                            a = Author()
                            a.name = l['name']
                            a.information = l['information']
                            a.save()
                            a.book.add(b)
                        else:
                            a = Author.objects.get(name=l['name'])
                            a.book.add(b)
                        b.author.add(a)
                if douban_data.get('price'):
                    b.price = douban_data['price']
                b.cover = 'https://images.weserv.nl/?url=' + douban_data[
                    'images']['small'][8:]
                b.large_cover = 'https://images.weserv.nl/?url=' + douban_data[
                    'images']['large'][8:]
                b.summary = douban_data['summary']
                b.catalog = douban_data['catalog']
                if not Publisher.objects.filter(
                        name=douban_data['publisher']):  #添加出版社
                    p = Publisher(name=douban_data['publisher'])
                    p.save()
                else:
                    p = Publisher.objects.get(name=douban_data['publisher'])
                b.publisher.add(p)
                p.book.add(b)
                b.save()
                b.douban_id = douban_data['id']
                if douban_data['isbn10']:
                    b.isbn10 = douban_data['isbn10']
                if douban_data['isbn13']:
                    b.isbn13 = douban_data['isbn13']
                if douban_data['pages']:
                    b.pages = douban_data['pages']
                if douban_data.get('tags'):
                    b.save()
                    for d_t in douban_data['tags']:
                        t = Tag()
                        t.name = d_t['name']
                        t.title = d_t['title']
                        t.save()
                        t.book.add(b)
                        b.tags.add(t)
                s = douban_data.get('series')
                if s and type(s) == type({}):
                    b.save()
                    s = Series()
                    s.name = douban_data.get('series')['title']
                    s.save()
                    s.book.add(b)
                    b.series.add(s)
                tr = douban_data.get('translator')
                pprint(tr)
                if tr and type(tr) == type([]):
                    b.save()
                    for tr in tr:
                        if not Translator.objects.filter(name=tr):
                            t = Translator()
                            t.name = tr
                            t.save()
                            t.book.add(b)
                            b.translator.add(t)
                        else:
                            b.translator.add(Translator.objects.get(name=tr))
                b.save_url(onedrive_data['url'], file_format,
                           onedrive_data['size'])
            else:
                b.file_name = b.title = file_name
                b.save_url(onedrive_data['url'], file_format,
                           onedrive_data['size'])
        b.save()
        return render(request, 'book/404.html')
    else:
        return render(request, 'book/404.html', status=404)