def add_publication_to_db(publication, session):
    to_add = session.query(
        models.Publication).filter_by(scholar_id=scholar_id).first()
    if to_add is None:
        to_add = models.Publication()

    to_add.scholar_id = publication.scholar_id
    to_add.title = publication.title
    to_add.type = publication.type
    to_add.year_of_publication = publication.year_of_publication
    to_add.total_citations = publication.total_citations
    to_add.author_names = publication.author_names
    to_add.retrieved_at = publication.retrieved_at
    # New additions
    to_add.venue = publication.venue
    to_add.volume = publication.volume
    to_add.issue = publication.issue
    to_add.publisher = publication.publisher
    to_add.pages = publication.pages

    publication_citations_per_year = []
    for cpy in publication.citations_per_year:
        citations_per_year = session.query(
            models.PublicationCitationsPerYear).filter_by(
                publication_id=to_add.id, year=cpy.year).first()
        if citations_per_year is None:
            citations_per_year = models.PublicationCitationsPerYear()
        citations_per_year.year = cpy.year
        citations_per_year.citations = cpy.citations
        publication_citations_per_year.append(citations_per_year)

    if publication_citations_per_year:
        to_add.citations_per_year = publication_citations_per_year

    session.add(to_add)
    session.commit()
def publication_has_pub_id_test():
    import models
    publication = models.Publication()
    if not hasattr(publication, 'pub_id'):
        raise TypeError('publication has no attribute pub_id')
def publication_has_year_test():
    import models
    publication = models.Publication()
    if not hasattr(publication, 'year'):
        raise TypeError('publication has no attribute year')
def publication_has_journal_test():
    import models
    publication = models.Publication()
    if not hasattr(publication, 'journal'):
        raise TypeError('publication has no attribute journal')
def publication_has_title_test():
    import models
    publication = models.Publication()
    if not hasattr(publication, 'title'):
        raise TypeError('publication has no attribute title')
def publication_has_authors_test():
    import models
    publication = models.Publication()
    if not hasattr(publication, 'authors'):
        raise TypeError('publication has no attribute authors')
def add_author_to_db(author, session, org=None):
    to_add = get_author_from_db(author.scholar_id, session)

    to_add.scholar_id = author.scholar_id
    to_add.name = author.name
    to_add.email_domain = author.email_domain
    to_add.total_citations = author.total_citations
    to_add.h_index = author.h_index
    to_add.i10_index = author.i10_index

    # Try to assign organization by a tag in the html/Google Org_id
    if not to_add.organization and org:
        candidate_org = session.query(models.Organization).filter_by(
            scholar_org_id = org).first()
        if candidate_org:
            to_add.organization = candidate_org
            to_add.auto_org_assignment = True

    # Try to assign organization by email domain
    if not to_add.organization and author.email_domain:
        clg = session.query(models.Author).filter_by(
            email_domain = author.email_domain).first()
        candidate_org = clg.organization if clg else None
        if candidate_org and candidate_org.parent:
            to_add.organization = candidate_org.ancestors()[-1]
            to_add.auto_org_assignment = True
        elif candidate_org:
            to_add.organization = candidate_org
            to_add.auto_org_assignment = True


    # # LIST:
    # citations_per_year
    author_citations_per_year = []
    for cpy in author.citations_per_year:
        citations_per_year = session.query(models.AuthorCitationsPerYear).filter_by(author_id = to_add.id, year = cpy.year).first()
        if citations_per_year is None:
            citations_per_year = models.AuthorCitationsPerYear()
        citations_per_year.year = cpy.year
        citations_per_year.citations = cpy.citations
        author_citations_per_year.append(citations_per_year)


    # # LIST:
    # coauthors
    author_coauthors = []
    for co in author.coauthors:
        coauthor = session.query(models.Author).filter_by(scholar_id = co.scholar_id).first()
        if coauthor is None:
            coauthor = models.Author()
        coauthor.scholar_id = co.scholar_id
        author_coauthors.append(coauthor)
    to_add.coauthors = author_coauthors


    # # LIST:
    # publications
    author_publications = []
    for pub in author.publications:
        publication = session.query(models.Publication).filter_by(scholar_id = pub.scholar_id).first()
        if publication is None:
            publication = models.Publication()
        publication.scholar_id = pub.scholar_id
        publication.title = pub.title
        publication.year_of_publication = pub.year_of_publication
        publication.total_citations = pub.total_citations
        author_publications.append(publication)
    to_add.publications = author_publications

    to_add.retrieved_at = datetime.datetime.now()

    session.add(to_add)
    session.commit()