Esempio n. 1
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)
Esempio n. 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)
Esempio n. 3
0
def update_book(xml):
    "Executes xml, creates book or updates inf about it."
    entity_id = get_id(xml)
    now_created = False

    if entity_id:
        # Modify existing book
        book = Book.objects.get(id=entity_id)
    else:
        # Create new book
        book = Book()
        now_created = True

    title = get_tag_text(xml, 'title')
    lang = get_tag_text(xml, 'lang')

    lang = get_language(lang)

    try:
        credit_str = get_tag_text(xml, 'credit')
        if credit_str:
            book.credit = int(credit_str)
    except ValueError, ex:
        raise InputDataServerException(ex)