def load_community(data, logos_dir):
    """Load community from data dump.

    :param data: Dictionary containing community data.
    :type data: dict
    :param logos_dir: Path to a local directory with community logos.
    :type logos_dir: str
    """
    from invenio_communities.models import Community
    from invenio_communities.utils import save_and_validate_logo
    logo_ext_washed = logo_ext_wash(data['logo_ext'])
    c = Community(
        id=data['id'],
        id_user=data['id_user'],
        title=data['title'],
        description=data['description'],
        page=data['page'],
        curation_policy=data['curation_policy'],
        last_record_accepted=iso2dt_or_none(data['last_record_accepted']),
        logo_ext=logo_ext_washed,
        ranking=data['ranking'],
        fixed_points=data['fixed_points'],
        created=iso2dt(data['created']),
        updated=iso2dt(data['last_modified']),
    )
    logo_path = join(logos_dir, "{0}.{1}".format(c.id, logo_ext_washed))
    db.session.add(c)
    if isfile(logo_path):
        with open(logo_path, 'rb') as fp:
            save_and_validate_logo(fp, logo_path, c.id)
    db.session.commit()
示例#2
0
def load_community(data, logos_dir):
    """Load community from data dump.

    :param data: Dictionary containing community data.
    :type data: dict
    :param logos_dir: Path to a local directory with community logos.
    :type logos_dir: str
    """
    from invenio_communities.models import Community
    from invenio_communities.utils import save_and_validate_logo
    logo_ext_washed = logo_ext_wash(data['logo_ext'])
    c = Community(
        id=data['id'],
        id_user=data['id_user'],
        title=data['title'],
        description=data['description'],
        page=data['page'],
        curation_policy=data['curation_policy'],
        last_record_accepted=iso2dt_or_none(data['last_record_accepted']),
        logo_ext=logo_ext_washed,
        ranking=data['ranking'],
        fixed_points=data['fixed_points'],
        created=iso2dt(data['created']),
        updated=iso2dt(data['last_modified']),
    )
    logo_path = join(logos_dir, "{0}.{1}".format(c.id, logo_ext_washed))
    db.session.add(c)
    if isfile(logo_path):
        with open(logo_path, 'rb') as fp:
            save_and_validate_logo(fp, logo_path, c.id)
    db.session.commit()
def test_oaiset_add_remove_record(app):
    """Test the API method for manual record adding."""
    with app.app_context():
        oaiset1 = OAISet(spec='abc')
        rec1 = Record.create({'title': 'Test1'})
        oaiset1.add_record(rec1)
        assert 'abc' in rec1['_oai']['sets']
        assert 'updated' in rec1['_oai']
        dt1 = iso2dt(rec1['_oai']['updated'])
        assert dt1.year == datetime.utcnow().year  # Test if parsed OK

        oaiset1.remove_record(rec1)
        assert 'abc' not in rec1['_oai']['sets']
        dt2 = iso2dt(rec1['_oai']['updated'])
        assert dt2 >= dt1
示例#4
0
 def _get_month(self):
     """Return the month in which the work was published."""
     months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun',
               'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
     if "publication_date" in self.record:
         return months[iso2dt(self.record["publication_date"]).month - 1]
     else:
         return ""
示例#5
0
def iso2dt_or_none(iso_dt):
    """Turn ISO-formatted date into datetime (None if 'iso_dt' is empty).

    :param iso_dt: ISO-formatted date
    :type iso_dt: str
    :returns: datetime object or None
    :rtype: datetime
    """
    return iso2dt(iso_dt) if iso_dt else None
示例#6
0
def iso2dt_or_none(iso_dt):
    """Turn ISO-formatted date into datetime (None if 'iso_dt' is empty).

    :param iso_dt: ISO-formatted date
    :type iso_dt: str
    :returns: datetime object or None
    :rtype: datetime
    """
    return iso2dt(iso_dt) if iso_dt else None
示例#7
0
def test_oaiset_add_remove_record(app):
    """Test the API method for manual record adding."""
    with app.app_context():
        oaiset1 = OAISet(spec='abc')
        rec1 = Record.create({'title_statement': {'title': 'Test1'}})
        assert not oaiset1.has_record(rec1)
        oaiset1.add_record(rec1)
        assert 'abc' in rec1['_oai']['sets']
        assert 'updated' in rec1['_oai']
        assert oaiset1.has_record(rec1)
        dt1 = iso2dt(rec1['_oai']['updated'])
        assert dt1.year == datetime.utcnow().year  # Test if parsed OK

        oaiset1.remove_record(rec1)
        assert 'abc' not in rec1['_oai']['sets']
        assert not oaiset1.has_record(rec1)
        dt2 = iso2dt(rec1['_oai']['updated'])
        assert dt2 >= dt1
示例#8
0
def load_featured(data):
    """Load community featuring from data dump.

    :param data: Dictionary containing community featuring data.
    :type data: dict
    """
    obj = FeaturedCommunity(id=data['id'],
                            id_community=data['id_community'],
                            start_date=iso2dt(data['start_date']))
    db.session.add(obj)
    db.session.commit()
示例#9
0
    def _get_citation_key(self):
        """Return citation key."""
        if "recid" in self.record:
            authors = self.record.get("creators", None)
            if authors:
                first_author = authors[0]
                name = first_author.get("familyname", first_author.get("name"))
                pubdate = self.record.get('publication_date', None)
                if pubdate:
                    year = "{}_{}".format(
                        iso2dt(pubdate).year, self.record['recid'])
                else:
                    year = self.record['recid']

                return "{0}_{1}".format(
                    slugify(name, separator="_", max_length=40), year)
            else:
                return six.text_type(self.record['recid'])
        else:
            raise MissingRequiredFieldError("citation key")
示例#10
0
文件: bibtex.py 项目: lnielsen/zenodo
    def _get_citation_key(self):
        """Return citation key."""
        if "recid" in self.record:
            authors = self.record.get("creators", None)
            if authors:
                first_author = authors[0]
                name = first_author.get(
                    "familyname",
                    first_author.get("name")
                )
                pubdate = self.record.get('publication_date', None)
                if pubdate:
                    year = "{}_{}".format(iso2dt(pubdate).year,
                                          self.record['recid'])
                else:
                    year = self.record['recid']

                return "{0}_{1}".format(slugify(name, separator="_",
                                                max_length=40),
                                        year)
            else:
                return six.text_type(self.record['recid'])
        else:
            raise MissingRequiredFieldError("citation key")
示例#11
0
 def _get_year(self):
     """Return the year of publication."""
     if "publication_date" in self.record:
         return six.text_type(iso2dt(self.record["publication_date"]).year)
     else:
         return ""