Beispiel #1
0
def book_input_auto():
    book_list = get_data('book_out.txt')
    from book.models import Book
    from book.models import Taxonomy
    
    for b in book_list:
        bs = None
        if b[6] != '':
            bs = Book.objects.filter(isbn=b[6]) 
        if bs is None or not bs.exists():
            bs = Book.objects.filter(name=b[0], version=b[7]) 
            if not bs.exists():
                try:
                    book = Book()
                    book.name = b[0]
                    book.subtitle = b[1]
                    if b[2] is None:
                        book.author = ''
                    else:
                        book.author = b[2]
                    if b[3] is None:
                        book.press = ''
                    else:
                        book.press = b[3]
                    book.price_ori = b[4]
                    if b[5] is None or b[5] == 0:
                        book.price_now = 0.4*b[4]
                    else:
                        book.price_now = b[5]
                    book.isbn = b[6]
                    book.version = b[7]
                    book.publication_date = b[8]
                    book.page_number = b[9]
                    book.size = b[10]
                    book.binding = b[11]
                    if b[12] == 'zh':
                        book.language = 'zh'
                    else:
                        book.language = 'en'
                    if b[13] is None:
                        book.translator = ''
                    else:
                        book.translator = b[13]
                    book.status = 4
                    book.save()
                    try:
                        if b[14] is not None and b[14] != '':
                            t = Taxonomy.objects.get(name=TAXONOMY_DICT[b[13]])
                            book.add_taxonomy(t)
                    except Exception, e:
                        print "Add taxonomy failed"
                        print e
                        print book_list.index(b)
                        print b
                        import pdb;pdb.set_trace()
                except Exception, e:
                    print e
                    print book_list.index(b)
                    print b
Beispiel #2
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 #3
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)