def test_create(self): """ BarePaper.create checks its arguments are non-empty """ names = [ BareName.create('Peter', 'Johnstone'), BareName.create('Xing', 'Li') ] pubdate = datetime.date(year=2014, month=9, day=4) # No title self.assertRaises(ValueError, BarePaper.create, '', names, pubdate) # No authors self.assertRaises(ValueError, BarePaper.create, 'Excellent title', [], pubdate) # No publication date self.assertRaises(ValueError, BarePaper.create, 'Excellent title', names, None) # Invalid visibility self.assertRaises(ValueError, BarePaper.create, 'Excellent title', names, pubdate, visible="something") # Not enough affiliations self.assertRaises(ValueError, BarePaper.create, 'Excellent title', names, pubdate, affiliations=['ENS'])
def test_paper_with_empty_slug(self): """ Papers may have titles with characters that are all ignored by slugify. """ p = Paper.get_or_create('!@#$%^*()', [BareName.create('Jean', 'Saisrien')], datetime.date(2016, 7, 2)) self.assertEqual(p.slug, '') self.checkPage('paper', args=[p.pk, p.slug])
def test_add_author(self): """ p.add_author adds the author at the right place """ names = [BareName.create('Peter', 'Johnstone'), BareName.create('Xing', 'Li'), BareName.create('John', 'Dubuc')] p = BarePaper.create('The title', [names[0]], datetime.date(year=2012, month=1, day=9)) p.add_author(BareAuthor(name=names[2])) self.assertEqual(len(p.authors), 2) p.add_author(BareAuthor(name=names[1]), position=1) self.assertListEqual(p.author_names(), names) self.assertRaises(ValueError, p.add_author, BareAuthor(name=BareName.create( 'Cantor', 'Bernstein')), position=8)
def test_paper_with_empty_slug(self, db, check_page): """ Papers may have titles with characters that are all ignored by slugify. """ p = Paper.get_or_create('!@#$%^*()', [BareName.create('Jean', 'Saisrien')], datetime.date(2016, 7, 2)) p.visible = True # Force paper to be visible even if it an orphan p.save() assert p.slug == '' check_page(200, 'paper', args=[p.pk, p.slug])
def test_paper_with_empty_slug(self): """ Papers may have titles with characters that are all ignored by slugify. """ p = Paper.get_or_create( '!@#$%^*()', [BareName.create('Jean', 'Saisrien')], datetime.date(2016, 7, 2)) p.visible = True # Force paper to be visible even if it an orphan p.save() self.assertEqual(p.slug, '') self.checkPage('paper', args=[p.pk, p.slug])
def test_create(self): """ BarePaper.create checks its arguments are non-empty """ names = [BareName.create('Peter', 'Johnstone'), BareName.create('Xing', 'Li')] pubdate = datetime.date(year=2014, month=9, day=4) # No title self.assertRaises(ValueError, BarePaper.create, '', names, pubdate) # No authors self.assertRaises(ValueError, BarePaper.create, 'Excellent title', [], pubdate) # No publication date self.assertRaises(ValueError, BarePaper.create, 'Excellent title', names, None) # Invalid visibility self.assertRaises(ValueError, BarePaper.create, 'Excellent title', names, pubdate, visible="something") # Not enough affiliations self.assertRaises(ValueError, BarePaper.create, 'Excellent title', names, pubdate, affiliations=['ENS'])
def test_add_author(self): """ p.add_author adds the author at the right place """ names = [ BareName.create('Peter', 'Johnstone'), BareName.create('Xing', 'Li'), BareName.create('John', 'Dubuc') ] p = BarePaper.create('The title', [names[0]], datetime.date(year=2012, month=1, day=9)) p.add_author(BareAuthor(name=names[2])) self.assertEqual(len(p.authors), 2) p.add_author(BareAuthor(name=names[1]), position=1) self.assertListEqual(p.author_names(), names) self.assertRaises( ValueError, p.add_author, BareAuthor(name=BareName.create('Cantor', 'Bernstein')), position=8)
def api_paper_query(request): try: fields = json.loads(request.body.decode('utf-8')) except (ValueError, UnicodeDecodeError): raise BadRequest('Invalid JSON payload') doi = fields.get('doi') if doi: p = None try: p = Paper.get_by_doi(doi) if not p: p = Paper.create_by_doi(doi) except MetadataSourceException: pass if p is None: raise BadRequest('Could not find a paper with this DOI') return {'status': 'ok', 'paper': p.json()} title = fields.get('title') if not isinstance(title, str) or not title or len(title) > 512: raise BadRequest( 'Invalid title, has to be a non-empty string shorter than 512 characters') date = fields.get('date') if not isinstance(date, str): raise BadRequest('A date is required') try: date = tolerant_datestamp_to_datetime(date) except ValueError as e: raise BadRequest(str(e)) authors = fields.get('authors') if not isinstance(authors, list): raise BadRequest('A list of authors is expected') parsed_authors = [] for a in authors: author = None if not isinstance(a, dict): raise BadRequest('Invalid author') if 'first' in a and 'last' in a: if not isinstance(a['first'], str) or not isinstance(a['last'], str) or not a['last']: raise BadRequest('Invalid (first,last) name provided') else: author = (a['first'], a['last']) elif 'plain' in a: if not isinstance(a['plain'], str) or not a['plain']: raise BadRequest('Invalid plain name provided') else: author = parse_comma_name(a['plain']) if author is None: raise BadRequest('Invalid author') parsed_authors.append(BareName.create(author[0], author[1])) if not authors: raise BadRequest('No authors provided') try: # Validate the metadata against our data model, # and compute the fingerprint to look up the paper in the DB. # This does NOT create a paper in the database - we do not want # to create papers for every search query we get! p = BarePaper.create(title, parsed_authors, date) except ValueError as e: raise BadRequest('Invalid paper: {}'.format(e)) try: model_paper = Paper.objects.get(fingerprint=p.fingerprint) return {'status': 'ok', 'paper': model_paper.json()} except Paper.DoesNotExist: return {'status': 'not found'}, 404
def api_paper_query(request): try: fields = json.loads(request.body.decode('utf-8')) except (ValueError, UnicodeDecodeError): raise BadRequest('Invalid JSON payload') doi = fields.get('doi') if doi: p = None try: p = Paper.get_by_doi(doi) if not p: p = Paper.create_by_doi(doi) except MetadataSourceException: pass if p is None: raise BadRequest('Could not find a paper with this DOI') return {'status': 'ok', 'paper': p.json()} title = fields.get('title') if not isinstance(title, str) or not title or len(title) > 512: raise BadRequest( 'Invalid title, has to be a non-empty string shorter than 512 characters' ) date = fields.get('date') if not isinstance(date, str): raise BadRequest('A date is required') try: date = tolerant_datestamp_to_datetime(date) except ValueError as e: raise BadRequest(str(e)) authors = fields.get('authors') if not isinstance(authors, list): raise BadRequest('A list of authors is expected') parsed_authors = [] for a in authors: author = None if not isinstance(a, dict): raise BadRequest('Invalid author') if 'first' in a and 'last' in a: if not isinstance(a['first'], str) or not isinstance( a['last'], str) or not a['last']: raise BadRequest('Invalid (first,last) name provided') else: author = (a['first'], a['last']) elif 'plain' in a: if not isinstance(a['plain'], str) or not a['plain']: raise BadRequest('Invalid plain name provided') else: author = parse_comma_name(a['plain']) if author is None: raise BadRequest('Invalid author') parsed_authors.append(BareName.create(author[0], author[1])) if not authors: raise BadRequest('No authors provided') try: # Validate the metadata against our data model, # and compute the fingerprint to look up the paper in the DB. # This does NOT create a paper in the database - we do not want # to create papers for every search query we get! p = BarePaper.create(title, parsed_authors, date) except ValueError as e: raise BadRequest('Invalid paper: {}'.format(e)) try: model_paper = Paper.objects.get(fingerprint=p.fingerprint) return {'status': 'ok', 'paper': model_paper.json()} except Paper.DoesNotExist: return {'status': 'not found'}, 404
def api_paper_query(request): try: fields = json.loads(request.body.decode('utf-8')) except (ValueError, UnicodeDecodeError): raise BadRequest('Invalid JSON payload') doi = fields.get('doi') if doi: p = None try: p = Paper.create_by_doi(doi, bare=True) except MetadataSourceException: pass if p is None: raise BadRequest('Could not find a paper with this DOI') return {'status': 'ok', 'paper': p.json()} title = fields.get('title') if not isinstance(title, unicode) or not title or len(title) > 512: raise BadRequest( 'Invalid title, has to be a non-empty string shorter than 512 characters' ) date = fields.get('date') if not isinstance(date, unicode): raise BadRequest('A date is required') try: date = tolerant_datestamp_to_datetime(date) except ValueError as e: raise BadRequest(unicode(e)) authors = fields.get('authors') if not isinstance(authors, list): raise BadRequest('A list of authors is expected') parsed_authors = [] for a in authors: author = None if not isinstance(a, dict): raise BadRequest('Invalid author') if 'first' in a and 'last' in a: if not isinstance(a['first'], unicode) or not isinstance( a['last'], unicode) or not a['last']: raise BadRequest('Invalid (first,last) name provided') else: author = (a['first'], a['last']) elif 'plain' in a: if not isinstance(a['plain'], unicode) or not a['plain']: raise BadRequest('Invalid plain name provided') else: author = parse_comma_name(a['plain']) if author is None: raise BadRequest('Invalid author') parsed_authors.append(BareName.create(author[0], author[1])) if not authors: raise BadRequest('No authors provided') try: p = BarePaper.create(title, parsed_authors, date) except ValueError: raise BadRequest('Invalid paper') return {'status': 'ok', 'paper': p.json()}
def setUp(self): self.ist = BarePaper.create('Groundbreaking Results', [BareName.create('Alfred', 'Kastler'), BareName.create('John', 'Dubuc')], datetime.date(year=2015, month=3, day=2))
def setUp(self): self.ist = BarePaper.create('Groundbreaking Results', [ BareName.create('Alfred', 'Kastler'), BareName.create('John', 'Dubuc') ], datetime.date(year=2015, month=3, day=2))
def api_paper_query(request): try: fields = json.loads(request.body.decode('utf-8')) except (ValueError, UnicodeDecodeError): raise BadRequest('Invalid JSON payload') doi = fields.get('doi') if doi: p = None try: p = Paper.get_by_doi(doi) if not p: p = Paper.create_by_doi(doi) except MetadataSourceException: pass if p is None: raise BadRequest('Could not find a paper with this DOI') return {'status': 'ok', 'paper': p.json()} title = fields.get('title') if not isinstance(title, str) or not title or len(title) > 512: raise BadRequest( 'Invalid title, has to be a non-empty string shorter than 512 characters') date = fields.get('date') if not isinstance(date, str): raise BadRequest('A date is required') try: date = tolerant_datestamp_to_datetime(date) except ValueError as e: raise BadRequest(str(e)) authors = fields.get('authors') if not isinstance(authors, list): raise BadRequest('A list of authors is expected') parsed_authors = [] for a in authors: author = None if not isinstance(a, dict): raise BadRequest('Invalid author') if 'first' in a and 'last' in a: if not isinstance(a['first'], str) or not isinstance(a['last'], str) or not a['last']: raise BadRequest('Invalid (first,last) name provided') else: author = (a['first'], a['last']) elif 'plain' in a: if not isinstance(a['plain'], str) or not a['plain']: raise BadRequest('Invalid plain name provided') else: author = parse_comma_name(a['plain']) if author is None: raise BadRequest('Invalid author') parsed_authors.append(BareName.create(author[0], author[1])) if not authors: raise BadRequest('No authors provided') try: p = BarePaper.create(title, parsed_authors, date) except ValueError: raise BadRequest('Invalid paper') return {'status': 'ok', 'paper': p.json()}