def bibtex_import(filename): parser = bib_in.Parser() bib_data = parser.parse_file(filename) writer = bib_out.Writer(encoding='ascii') for key in bib_data.entries.keys(): print 'Key ' + key stream = StringIO.StringIO() writer.write_entry(key, bib_data.entries[key], stream) ref_obj = get_ref(key, stream.getvalue()) ref_obj.save() for field in bib_data.entries[key].fields: value = bib_data.entries[key].fields[field] col = get_column(field) if 'title' in field.lower(): ref_obj.title = value elif 'journal' in field.lower(): ref_obj.journal = value elif 'year' in field.lower(): ref_obj.year = int(value) attr = ReferenceAttribute(column=col, value=value) attr.save() ref_obj.referenceAttributes.add(attr) if bib_data.entries[key].persons: for person in bib_data.entries[key].persons['author']: first = person.get_part_as_text('first') middle = person.get_part_as_text('middle') prelast = person.get_part_as_text('prelast') last = person.get_part_as_text('last') lineage = person.get_part_as_text('lineage') auth = ReferenceAuthor(first_name=first, middle_name=middle, prelast_name=prelast, last_name=last, lineage=lineage) auth.save() ref_obj.authors.add(auth) ref_obj.save() print "Imported %i BibTeX references." % len(bib_data.entries)
def bibtex_import(filename, taxonomyItem): parser = bib_in.Parser() bib_data = parser.parse_file(filename) writer = bib_out.Writer(encoding='ascii') for key in bib_data.entries.keys(): stream = StringIO.StringIO() writer.write_entry(key, bib_data.entries[key], stream) title = getTitle(bib_data.entries[key].fields) ref_obj = get_ref(key, title, stream.getvalue()) try: ref_obj.save() for field in bib_data.entries[key].fields: value = bib_data.entries[key].fields[field] col = get_column(field) if 'title' in field.lower(): ref_obj.title = title elif 'journal' in field.lower(): ref_obj.journal = bibtex_purify(value) elif 'year' in field.lower(): ref_obj.year = int(value) elif 'url' in field.lower(): ref_obj.url = value elif 'abstract' in field.lower(): ref_obj.abstract = value elif 'doi' in field.lower(): doiPrepender = 'http://dx.doi.org/' if not value.startswith(doiPrepender): value = doiPrepender + value ref_obj.url = value attr = ReferenceAttribute(column=col, value=value) attr.save() ref_obj.referenceAttributes.add(attr) authorsAsText = "" count = 0 if bib_data.entries[key].persons: numberOfAuthors = len(bib_data.entries[key].persons['author']) for person in bib_data.entries[key].persons['author']: first = person.get_part_as_text('first') middle = person.get_part_as_text('middle') prelast = person.get_part_as_text('prelast') last = person.get_part_as_text('last') lineage = person.get_part_as_text('lineage') simpleAuthor = bibtex_purify(first + ' ' + last) if 'emph' in simpleAuthor: simpleAuthor = simpleAuthor.replace('emph', '') authorsAsText += simpleAuthor if numberOfAuthors > 1: if count != (numberOfAuthors -1): authorsAsText += ', ' if count == (numberOfAuthors - 2): authorsAsText += ' and ' count += 1 try: author, created = ReferenceAuthor.objects.get_or_create(first_name=first, last_name=last) except Exception, e: logger.debug( 'Author ' + first + ' ' + last + ' exists in multiple places. Error is ' + e.message) author = ReferenceAuthor.objects.filter(first_name=first).filter(last_name=last).__getitem__(0) created = False if created: author.middle_name = middle author.prelast_name = prelast author.lineage = lineage author.save() if not author in ref_obj.authors.all(): ref_obj.authors.add(author) ref_obj.authorsAsText = authorsAsText ref_obj.save() if not ref_obj in taxonomyItem.references.all(): taxonomyItem.references.add(ref_obj) except Exception, e: logger.log(e.message) connection._rollback()