コード例 #1
0
def _create_biblio_object(biblio_element, publication_object):
    """
    Takes an XML biblio-record element lifted from a Symplectic Publication
    file (which contains the FULL info about each biblio-record unlike a
    User XML file) extracts full info about that biblio-record,
    loads/creates biblio-object, populates it with this new info and saves
    it
    """
    # ++++++BIBLIOGRAPHIC-RECORD++++++
    # check Bibliographical-record Element
    # print "        creating", biblio_element

    if biblio_element is None:
        # print "       giving up in _create_biblio_object"
        return
    # make blank Biblio Object
    biblio_object = BibliographicRecord()

    # Bibliographic-record sub-elements (used to read XML)

    if biblio_element is not None:
        bibliometric_data_element = biblio_element.find(
            SYMPLECTIC_NAMESPACE + 'bibliometric-data'
            )
        bibliographic_data_element = biblio_element.find(
            SYMPLECTIC_NAMESPACE + 'bibliographic-data'
            )
    if bibliographic_data_element is not None:
        native_element = bibliographic_data_element.find(
            SYMPLECTIC_NAMESPACE + 'native'
            )
    if native_element is not None:
        authors_subtree = native_element.find(SYMPLECTIC_NAMESPACE + 'authors')
        keywords_subtree = native_element.find(
            SYMPLECTIC_NAMESPACE + 'keywords'
            )
    # bibliographic-record attribs
    if biblio_element is not None:
        biblio_object.data_source = biblio_element.get('data-source', '')
        biblio_object.id_at_source = biblio_element.get('id-at-source', '')
        biblio_object.verification_status = _get_element_text(
            biblio_element.find(SYMPLECTIC_NAMESPACE + 'verification-status')
        )
    # bibliometric data
    if bibliometric_data_element is not None:
        biblio_object.times_cited = _get_element_text(
            bibliometric_data_element.find(
                SYMPLECTIC_NAMESPACE + 'times-cited'
                )
            )
        biblio_object.reference_count = _get_element_text(
            bibliometric_data_element.find(
                SYMPLECTIC_NAMESPACE + 'reference-count'
                )
            )
    # native
    if native_element is not None:

        attr_names = [
            'abstract', 'associated-authors', 'awarded-date', 'begin-page',
            'book-author-type', 'commissioning-body', 'confidential', 'doi',
            'edition', 'editors', 'end-page', 'filed-date', 'finish-date',
            'isbn-10', 'isbn-13', 'issn', 'issue', 'journal',
            'journal-article-type', 'language', 'location', 'medium',
            'name-of-conference', 'notes', 'number', 'number-of-pages',
            'number-of-pieces', 'parent-title', 'patent-number', 'pii',
            'place-of-publication', 'publication-date', 'publication-status',
            'publication-status', 'series', 'start-date', 'title', 'version',
            'volume'
            ]

        for attr_name in attr_names:
            element = native_element.find(SYMPLECTIC_NAMESPACE + attr_name)
            attr_value = _get_element_text(element)
            setattr(biblio_object, attr_name.replace("-", "_"), attr_value)

    # authors
    if authors_subtree is not None:
        biblio_object.authors = ''
        author_list = []
        for author_element in authors_subtree.getchildren():
            name = _get_element_text(
                author_element.find(SYMPLECTIC_NAMESPACE + 'name')
                )
            initials = _get_element_text(
                author_element.find(SYMPLECTIC_NAMESPACE + 'initials')
                )
            author_list.append(unicode(name) + ' ' + unicode(initials))
        biblio_object.authors = ", ".join(author_list)
        print biblio_object.authors
        #derived authors
        biblio_object.number_of_authors = len(author_list)
        if len(author_list) > 0:
            biblio_object.first_author = author_list[0]
        if len(author_list) > 1:
            biblio_object.last_author = author_list[-1]
    # keywords
    if keywords_subtree is not None:
        biblio_object.keywords = ''
        for keyword_element in keywords_subtree.getchildren():
            biblio_object.keywords = "|".join([
                biblio_object.keywords,
                unicode(keyword_element.text)
                ])
    # link bibliographic-record object and passed-in publication object
    biblio_object.publication = publication_object
    # save
    # print "        going to save biblio_object", biblio_object, \
        # "publication_id",  biblio_object.publication_id

    biblio_object.save()

    # ++++++URLS++++++
    # delete all existing URLs for this biblio-record
    biblio_object.urls.all().delete()
    # URL elements are held in a subtree
    url_subtree = biblio_element.find(SYMPLECTIC_NAMESPACE + 'urls')
    # check if any url elements in subtree
    if url_subtree is None or len(url_subtree) < 1:
        return
    # for each url element in subtree
    for url_element in url_subtree.getchildren():
        _create_url_object(url_element, biblio_object)