def import_bibtex(request):
    if request.method == 'POST':
        # try to parse BibTex
        bib = parse(request.POST['bibliography'])

        # container for error messages
        errors = {}

        # publication types
        types = Type.objects.all()

        # check for errors
        if not bib:
            if not request.POST['bibliography']:
                errors['bibliography'] = 'This field is required.'

        if not errors:
            publications = []

            # try adding publications
            for entry in bib:
                if entry.has_key('title') and \
                   entry.has_key('author') and \
                   entry.has_key('year'):
                    # parse authors
                    authors = split(entry['author'], ' and ')
                    for i in range(len(authors)):
                        author = split(authors[i], ',')
                        author = [author[-1]] + author[:-1]
                        authors[i] = join(author, ' ')
                    authors = join(authors, ', ')

                    # add missing keys
                    keys = [
                        'journal', 'booktitle', 'publisher', 'url', 'doi',
                        'keywords', 'note', 'abstract', 'month'
                    ]

                    for key in keys:
                        if not entry.has_key(key):
                            entry[key] = ''

                    # map integer fields to integers
                    entry['month'] = MONTHS.get(entry['month'].lower(), 0)
                    entry['volume'] = entry.get('volume', None)
                    entry['number'] = entry.get('number', None)

                    # determine type
                    type_id = None

                    for t in types:
                        if entry['type'] in t.bibtex_type_list:
                            type_id = t.id
                            break

                    if type_id is None:
                        errors['bibliography'] = 'Type "' + entry[
                            'type'] + '" unknown.'
                        break

                    # add publication
                    publications.append(
                        Publication(type_id=type_id,
                                    citekey=entry['key'],
                                    title=entry['title'],
                                    authors=authors,
                                    year=entry['year'],
                                    month=entry['month'],
                                    journal=entry['journal'],
                                    book_title=entry['booktitle'],
                                    publisher=entry['publisher'],
                                    volume=entry['volume'],
                                    number=entry['number'],
                                    note=entry['note'],
                                    url=entry['url'],
                                    doi=entry['doi'],
                                    abstract=entry['abstract'],
                                    keywords=entry['keywords']))
                else:
                    errors[
                        'bibliography'] = 'Make sure that the keys title, author and year are present.'
                    break

        if not errors and not publications:
            errors['bibliography'] = 'No valid BibTex entries found.'

        if errors:
            # some error occurred
            return render_to_response(
                'admin/publications/import_bibtex.html', {
                    'errors': errors,
                    'title': 'Import BibTex',
                    'types': Type.objects.all(),
                    'request': request
                }, RequestContext(request))
        else:
            try:
                # save publications
                for publication in publications:
                    publication.save()
            except:
                msg = 'Some error occured during saving of publications.'
            else:
                if len(publications) > 1:
                    msg = 'Successfully added ' + str(
                        len(publications)) + ' publications.'
                else:
                    msg = 'Successfully added ' + str(
                        len(publications)) + ' publication.'

            # show message
            messages.info(request, msg)

            # redirect to publication listing
            return HttpResponseRedirect('../')
    else:
        return render_to_response(
            'admin/publications/import_bibtex.html', {
                'title': 'Import BibTex',
                'types': Type.objects.all(),
                'request': request
            }, RequestContext(request))
def import_bibtex(request):
	if request.method == 'POST':
		# try to parse BibTex
		bib = parse(request.POST['bibliography'])

		# container for error messages
		errors = {}

		# publication types
		types = Type.objects.all()

		# check for errors
		if not bib:
			if not request.POST['bibliography']:
				errors['bibliography'] = 'This field is required.'

		if not errors:
			publications = []

			# try adding publications
			for entry in bib:
				if entry.has_key('title') and \
				   entry.has_key('author') and \
				   entry.has_key('year'):
					# parse authors
					authors = split(entry['author'], ' and ')
					for i in range(len(authors)):
						author = split(authors[i], ',')
						author = [author[-1]] + author[:-1]
						authors[i] = join(author, ' ')
					authors = join(authors, ', ')

					# add missing keys
					keys = [
						'journal',
						'booktitle',
						'publisher',
						'institution',
						'url',
						'doi',
						'keywords',
						'note',
						'abstract',
						'month']

					for key in keys:
						if not entry.has_key(key):
							entry[key] = ''

					# map integer fields to integers
					entry['month'] = MONTHS.get(entry['month'].lower(), 0)
					entry['volume'] = entry.get('volume', None)
					entry['number'] = entry.get('number', None)

					# determine type
					type_id = None

					for t in types:
						if entry['type'] in t.bibtex_type_list:
							type_id = t.id
							break

					if type_id is None:
						errors['bibliography'] = 'Type "' + entry['type'] + '" unknown.'
						break

					# add publication
					publications.append(Publication(
						type_id=type_id,
						citekey=entry['key'],
						title=entry['title'],
						authors=authors,
						year=entry['year'],
						month=entry['month'],
						journal=entry['journal'],
						book_title=entry['booktitle'],
						publisher=entry['publisher'],
						institution=entry['institution'],
						volume=entry['volume'],
						number=entry['number'],
						note=entry['note'],
						url=entry['url'],
						doi=entry['doi'],
						abstract=entry['abstract'],
						keywords=entry['keywords']))
				else:
					errors['bibliography'] = 'Make sure that the keys title, author and year are present.'
					break

		if not errors and not publications:
			errors['bibliography'] = 'No valid BibTex entries found.'

		if errors:
			# some error occurred
			return render_to_response(
				'admin/publications/import_bibtex.html', {
					'errors': errors,
					'title': 'Import BibTex',
					'types': Type.objects.all(),
					'request': request},
				RequestContext(request))
		else:
			try:
				# save publications
				for publication in publications:
					publication.save()
			except:
				msg = 'Some error occured during saving of publications.'
			else:
				if len(publications) > 1:
					msg = 'Successfully added ' + str(len(publications)) + ' publications.'
				else:
					msg = 'Successfully added ' + str(len(publications)) + ' publication.'

			# show message
			messages.info(request, msg)

			# redirect to publication listing
			return HttpResponseRedirect('../')
	else:
		return render_to_response(
			'admin/publications/import_bibtex.html', {
				'title': 'Import BibTex',
				'types': Type.objects.all(),
				'request': request},
			RequestContext(request))
def import_bibtex(request):
    if request.method == 'POST':
        # try to parse BibTex
        bib = parse(request.POST['bibliography'])

        # container for error messages
        errors = {}

        # publication types
        types = Type.objects.all()

        # check for errors
        if not bib:
            if not request.POST['bibliography']:
                errors['bibliography'] = 'This field is required.'

        if not errors:
            publications = []

            # try adding publications
            for entry in bib:
                if 'title' in entry and \
                   'author' in entry and \
                   'year' in entry:
                    # parse authors
                    authors = entry['author'].split(' and ')
                    for i in range(len(authors)):
                        author = authors[i].split(',')
                        author = [author[-1]] + author[:-1]
                        authors[i] = ' '.join(author)
                    authors = ', '.join(authors)

                    # add missing keys
                    keys = [
                        'journal', 'booktitle', 'publisher', 'institution',
                        'url', 'doi', 'isbn', 'keywords', 'pages', 'note',
                        'abstract', 'month'
                    ]

                    for key in keys:
                        if not key in entry:
                            entry[key] = ''

                    # map integer fields to integers
                    entry['month'] = MONTHS.get(entry['month'].lower(), 0)

                    entry['volume'] = entry.get('volume', None)
                    entry['number'] = entry.get('number', None)

                    if isinstance(entry['volume'], six.text_type):
                        entry['volume'] = int(
                            re.sub('[^0-9]', '', entry['volume']))
                    if isinstance(entry['number'], six.text_type):
                        entry['number'] = int(
                            re.sub('[^0-9]', '', entry['number']))

                    # remove whitespace characters (likely due to line breaks)
                    entry['url'] = re.sub(r'\s', '', entry['url'])

                    # determine type
                    type_id = None

                    for t in types:
                        if entry['type'] in t.bibtex_type_list:
                            type_id = t.id
                            break

                    if type_id is None:
                        errors['bibliography'] = 'Type "' + entry[
                            'type'] + '" unknown.'
                        break

                    # add publication
                    publications.append(
                        Publication(type_id=type_id,
                                    citekey=entry['key'],
                                    title=entry['title'],
                                    authors=authors,
                                    year=entry['year'],
                                    month=entry['month'],
                                    journal=entry['journal'],
                                    book_title=entry['booktitle'],
                                    publisher=entry['publisher'],
                                    institution=entry['institution'],
                                    volume=entry['volume'],
                                    number=entry['number'],
                                    pages=entry['pages'],
                                    note=entry['note'],
                                    url=entry['url'],
                                    doi=entry['doi'],
                                    isbn=entry['isbn'],
                                    external=False,
                                    abstract=entry['abstract'],
                                    keywords=entry['keywords']))
                else:
                    errors[
                        'bibliography'] = 'Make sure that the keys title, author and year are present.'
                    break

        if not errors and not publications:
            errors['bibliography'] = 'No valid BibTex entries found.'

        if errors:
            # some error occurred
            return render(
                request, 'admin/publications/import_bibtex.html', {
                    'errors': errors,
                    'title': 'Import BibTex',
                    'types': Type.objects.all(),
                    'request': request
                })
        else:
            try:
                # save publications
                for publication in publications:
                    publication.save()
            except:
                msg = 'Some error occured during saving of publications.'
            else:
                if len(publications) > 1:
                    msg = 'Successfully added ' + str(
                        len(publications)) + ' publications.'
                else:
                    msg = 'Successfully added ' + str(
                        len(publications)) + ' publication.'

            # show message
            messages.info(request, msg)

            # redirect to publication listing
            if len(publications) == 1:
                return HttpResponseRedirect('../%s/change/' %
                                            publications[0].id)
            else:
                return HttpResponseRedirect('../')
    else:
        return render(
            request, 'admin/publications/import_bibtex.html', {
                'title': 'Import BibTex',
                'types': Type.objects.all(),
                'request': request
            })
Exemple #4
0
def import_bibtex(request):
	if request.method == 'POST':
		# try to parse BibTex
		the_bibtex_file_content = ''
		
		creators = request.POST.getlist('creators')
		productions = request.POST.getlist('productions')
		work_records = request.POST.getlist('work_records')

		the_bibtex_file = request.FILES.get('bibtex_file', '')
		if the_bibtex_file:
			if the_bibtex_file.multiple_chunks():
				the_bibtex_file_content =  ''.join(chunk for chunk in the_bibtex_file.chunks())
			else:
				the_bibtex_file_content = the_bibtex_file.read()

		bib = parse(the_bibtex_file_content)

		if not bib:
			bib = parse(request.POST['bibliography'])
			
		# container for error messages
		errors = {}

		# publication types
		types = Type.objects.all()

		# check for errors
		if not bib:
			if not request.POST['bibliography']:
				errors['bibliography'] = 'Please populate Bibliography or click browse to upload a Bibtex format file.'

		if not errors:
			publications = []

			# try adding publications
			for entry in bib:
				if (entry.has_key('title') and 
				   entry.has_key('author') and 
				   entry.has_key('year')):

					# parse authors
					authors = split(entry['author'], ' and ')
					for i in range(len(authors)):
						author = split(authors[i], ',')
						author = [author[-1]] + author[:-1]
						authors[i] = join(author, ' ')
					authors = join(authors, ', ')

					# add missing keys
					keys = [
						'annote',
						'booktitle',
						'chapter',
						'edition',
						'section',
						'editor',
						'howpublished',
						'institution',
						'journal',
						'key',
						'month',
						'note',
						'number',
						'organization',
						'pages',
						'publisher',
						'address',
						'school',
						'series',
						'volume',
						'issue',
						'url',
						'isbn',
						'issn',
						'lccn',
						'abstract',
						'keywords',
						'price',
						'copyright',
						'language',
						'contents',
						'doi']

					for key in keys:
						if not entry.has_key(key):
							if key == 'price':
								entry[key] = 0
							else:
								entry[key] = ''

					# map integer fields to integers
					entry['month'] = MONTHS.get(entry['month'].lower(), 0)
					entry['volume'] = entry.get('volume', None)
					entry['number'] = entry.get('number', None)

					# determine type
					type_id = None

					for t in types:
						if entry['type'] in t.bibtex_type_list:
							type_id = t.id
							break

					if type_id is None:
						errors['bibliography'] = 'Type "' + entry['type'] + '" unknown.'
						break

					# add publication
					publications.append(Publication(
						type_id=type_id,
						annote=entry['annote'],
						authors=authors,
						book_title=entry['booktitle'],
						chapter=entry['chapter'],
						edition=entry['edition'],
						section=entry['section'],
						editor=entry['editor'],
						how_published=entry['howpublished'],
						institution=entry['institution'],
						journal=entry['journal'],
						citekey=entry['key'],
						year=entry['year'],
						month=entry['month'],
						note=entry['note'],
						organization=entry['organization'],
						pages=entry['pages'],
						publisher=entry['publisher'],
						address=entry['address'],
						university=entry['school'],
						series=entry['series'],
						title=entry['title'],
						volume=entry['volume'],
						number=entry['issue'],
						url=entry['url'],
						isbn=entry['isbn'],
						issn=entry['issn'],
						archive_location=entry['lccn'],
						abstract=entry['abstract'],
						keywords=entry['keywords'],
						price=entry['price'],
						rights=entry['copyright'],
						language=entry['language'],
						table_of_content=entry['contents'],
						doi=entry['doi']))
				else:
					errors['bibliography'] = 'Make sure that the keys title, author and year are present.'
					break

		if not errors and not publications:
			errors['bibliography'] = 'No valid BibTex entries found.'

		if errors:
			# some error occurred
			return render_to_response(
				'admin/publications/import_bibtex.html', {
					'errors': errors,
					'title': 'Import BibTex',
					'types': Type.objects.all(),
					'request': request},
				RequestContext(request))
		else:
			try:
				# save publications
				for publication in publications:
					publication.save()
					try:
						for creator_id in creators:
							creator = Creator.objects.get(pk=creator_id)
							creator.primary_publications.add(publication)
					except:
						pass
					try:
						for production_id in productions:
							production = Production.objects.get(pk=production_id)
							production.primary_publications.add(publication)
					except:
						pass
					try:
						for work_record_id in work_records:
							work_record = WorkRecord.objects.get(pk=work_record_id)
							work_record.primary_publications.add(publication)
					except:
						pass
			except:
				msg = 'Some error occured during saving of publications.'
			else:
				if len(publications) > 1:
					msg = 'Successfully added ' + str(len(publications)) + ' publications.'
				else:
					msg = 'Successfully added ' + str(len(publications)) + ' publication.'

			# show message
			messages.info(request, msg)

			# redirect to publication listing
			return HttpResponseRedirect('../')
	else:
		Creators_qs = Creator.objects.all()
		Productions_qs = Production.objects.all().order_by('title')
		WorkRecord_qs = WorkRecord.objects.all().order_by('title')
		
		CREATOR_CHOICES = [] #[("", "-- Select a Creator --")]
		CREATOR_CHOICES += [(e.id,
						 e.creator_name)
						 for e in Creators_qs]

		PRODUCTION_CHOICES = [] #[("", "-- Select a Production --")]
		PRODUCTION_CHOICES += [(e.id,
						 e.title)
						 for e in Productions_qs]
		
		WORKRECORD_CHOICES = [] #[("", "-- Select a Written Work --")]
		WORKRECORD_CHOICES += [(e.id,
						 e.title)
						 for e in WorkRecord_qs]
			
		return render_to_response(
			'admin/publications/import_bibtex.html', {
				'title': 'Import BibTex',
				'types': Type.objects.all(),
				'creators': CREATOR_CHOICES,
				'productions': PRODUCTION_CHOICES,
				'work_records': WORKRECORD_CHOICES,
				'request': request},
			RequestContext(request))
Exemple #5
0
def import_bibtex(request):
    if request.method == 'POST':
        # try to parse BibTex
        the_bibtex_file_content = ''

        creators = request.POST.getlist('creators')
        productions = request.POST.getlist('productions')
        work_records = request.POST.getlist('work_records')

        the_bibtex_file = request.FILES.get('bibtex_file', '')
        if the_bibtex_file:
            if the_bibtex_file.multiple_chunks():
                the_bibtex_file_content = ''.join(
                    chunk for chunk in the_bibtex_file.chunks())
            else:
                the_bibtex_file_content = the_bibtex_file.read()

        bib = parse(the_bibtex_file_content)

        if not bib:
            bib = parse(request.POST['bibliography'])

        # container for error messages
        errors = {}

        # publication types
        types = Type.objects.all()

        # check for errors
        if not bib:
            if not request.POST['bibliography']:
                errors[
                    'bibliography'] = 'Please populate Bibliography or click browse to upload a Bibtex format file.'

        if not errors:
            publications = []

            # try adding publications
            for entry in bib:
                if (entry.has_key('title') and entry.has_key('author')
                        and entry.has_key('year')):

                    # parse authors
                    authors = split(entry['author'], ' and ')
                    for i in range(len(authors)):
                        author = split(authors[i], ',')
                        author = [author[-1]] + author[:-1]
                        authors[i] = join(author, ' ')
                    authors = join(authors, ', ')

                    # add missing keys
                    keys = [
                        'annote', 'booktitle', 'chapter', 'edition', 'section',
                        'editor', 'howpublished', 'institution', 'journal',
                        'key', 'month', 'note', 'number', 'organization',
                        'pages', 'publisher', 'address', 'school', 'series',
                        'volume', 'issue', 'url', 'isbn', 'issn', 'lccn',
                        'abstract', 'keywords', 'price', 'copyright',
                        'language', 'contents', 'doi'
                    ]

                    for key in keys:
                        if not entry.has_key(key):
                            if key == 'price':
                                entry[key] = 0
                            else:
                                entry[key] = ''

                    # map integer fields to integers
                    entry['month'] = MONTHS.get(entry['month'].lower(), 0)
                    entry['volume'] = entry.get('volume', None)
                    entry['number'] = entry.get('number', None)

                    # determine type
                    type_id = None

                    for t in types:
                        if entry['type'] in t.bibtex_type_list:
                            type_id = t.id
                            break

                    if type_id is None:
                        errors['bibliography'] = 'Type "' + entry[
                            'type'] + '" unknown.'
                        break

                    # add publication
                    publications.append(
                        Publication(type_id=type_id,
                                    annote=entry['annote'],
                                    authors=authors,
                                    book_title=entry['booktitle'],
                                    chapter=entry['chapter'],
                                    edition=entry['edition'],
                                    section=entry['section'],
                                    editor=entry['editor'],
                                    how_published=entry['howpublished'],
                                    institution=entry['institution'],
                                    journal=entry['journal'],
                                    citekey=entry['key'],
                                    year=entry['year'],
                                    month=entry['month'],
                                    note=entry['note'],
                                    organization=entry['organization'],
                                    pages=entry['pages'],
                                    publisher=entry['publisher'],
                                    address=entry['address'],
                                    university=entry['school'],
                                    series=entry['series'],
                                    title=entry['title'],
                                    volume=entry['volume'],
                                    number=entry['issue'],
                                    url=entry['url'],
                                    isbn=entry['isbn'],
                                    issn=entry['issn'],
                                    archive_location=entry['lccn'],
                                    abstract=entry['abstract'],
                                    keywords=entry['keywords'],
                                    price=entry['price'],
                                    rights=entry['copyright'],
                                    language=entry['language'],
                                    table_of_content=entry['contents'],
                                    doi=entry['doi']))
                else:
                    errors[
                        'bibliography'] = 'Make sure that the keys title, author and year are present.'
                    break

        if not errors and not publications:
            errors['bibliography'] = 'No valid BibTex entries found.'

        if errors:
            # some error occurred
            return render_to_response(
                'admin/publications/import_bibtex.html', {
                    'errors': errors,
                    'title': 'Import BibTex',
                    'types': Type.objects.all(),
                    'request': request
                }, RequestContext(request))
        else:
            try:
                # save publications
                for publication in publications:
                    publication.save()
                    try:
                        for creator_id in creators:
                            creator = Creator.objects.get(pk=creator_id)
                            creator.primary_publications.add(publication)
                    except:
                        pass
                    try:
                        for production_id in productions:
                            production = Production.objects.get(
                                pk=production_id)
                            production.primary_publications.add(publication)
                    except:
                        pass
                    try:
                        for work_record_id in work_records:
                            work_record = WorkRecord.objects.get(
                                pk=work_record_id)
                            work_record.primary_publications.add(publication)
                    except:
                        pass
            except:
                msg = 'Some error occured during saving of publications.'
            else:
                if len(publications) > 1:
                    msg = 'Successfully added ' + str(
                        len(publications)) + ' publications.'
                else:
                    msg = 'Successfully added ' + str(
                        len(publications)) + ' publication.'

            # show message
            messages.info(request, msg)

            # redirect to publication listing
            return HttpResponseRedirect('../')
    else:
        Creators_qs = Creator.objects.all()
        Productions_qs = Production.objects.all().order_by('title')
        WorkRecord_qs = WorkRecord.objects.all().order_by('title')

        CREATOR_CHOICES = []  #[("", "-- Select a Creator --")]
        CREATOR_CHOICES += [(e.id, e.creator_name) for e in Creators_qs]

        PRODUCTION_CHOICES = []  #[("", "-- Select a Production --")]
        PRODUCTION_CHOICES += [(e.id, e.title) for e in Productions_qs]

        WORKRECORD_CHOICES = []  #[("", "-- Select a Written Work --")]
        WORKRECORD_CHOICES += [(e.id, e.title) for e in WorkRecord_qs]

        return render_to_response(
            'admin/publications/import_bibtex.html', {
                'title': 'Import BibTex',
                'types': Type.objects.all(),
                'creators': CREATOR_CHOICES,
                'productions': PRODUCTION_CHOICES,
                'work_records': WORKRECORD_CHOICES,
                'request': request
            }, RequestContext(request))
Exemple #6
0
	people.sort(key=lambda x:x['name'])
	for p in people:
		createPerson(p)

with open ('initialdata/projects.json', 'r') as f:
	projects = json.load(f)
	for p in projects:
		createProject(p)

with open ('initialdata/pubs.json', 'r') as f:
	Publication.objects.all().delete()
	pubs = json.load(f)
	for pub in pubs:
		bibtex = pub.get('bibtex')
		if '@' in bibtex:			
			bib = parse(bibtex)
			response = import_bibtex_data(bib)
			if response['errors']:
				print(response)
				print(bibtex)
				pass
	
			if len(response['publications']) != 1:
				print(response)
				print('Gribēju atbildē vienu publikācijas objektu..')
			publication = response['publications'][0]

			for author in publication.authors_list:
				person = findPerson(author.strip())
				if person:
					person.publications.add(publication)