当前位置: 首页>>代码示例>>Python>>正文


Python Contribution.allocate_friendly_ids方法代码示例

本文整理汇总了Python中indico.modules.events.contributions.models.contributions.Contribution.allocate_friendly_ids方法的典型用法代码示例。如果您正苦于以下问题:Python Contribution.allocate_friendly_ids方法的具体用法?Python Contribution.allocate_friendly_ids怎么用?Python Contribution.allocate_friendly_ids使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在indico.modules.events.contributions.models.contributions.Contribution的用法示例。


在下文中一共展示了Contribution.allocate_friendly_ids方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_contrib_friendly_id

# 需要导入模块: from indico.modules.events.contributions.models.contributions import Contribution [as 别名]
# 或者: from indico.modules.events.contributions.models.contributions.Contribution import allocate_friendly_ids [as 别名]
def test_contrib_friendly_id(monkeypatch, dummy_event, create_contribution):
    counter = Incrementer()
    monkeypatch.setattr(contrib_module, 'increment_and_get', counter)

    contrib_1 = create_contribution(dummy_event, 'Contribution 1', timedelta(minutes=60))
    assert contrib_1.friendly_id == 1

    contrib_2 = create_contribution(dummy_event, 'Contribution 2', timedelta(minutes=60))
    assert contrib_2.friendly_id == 2

    assert counter == 2

    # pre-allocate 8 friendly ids
    Contribution.allocate_friendly_ids(dummy_event, 8)
    assert g.friendly_ids[Contribution][dummy_event.id] == range(3, 11)
    assert counter == 10

    for fid in g.friendly_ids[Contribution][dummy_event.id][:]:
        contrib = create_contribution(dummy_event, 'Contribution {}'.format(fid), timedelta(minutes=30))
        assert contrib.friendly_id == fid

    # increment_and_get doesn't get called because the ids
    # have been pre-allocated
    assert counter == 10
开发者ID:bkolobara,项目名称:indico,代码行数:26,代码来源:contributions_test.py

示例2: import_contributions_from_csv

# 需要导入模块: from indico.modules.events.contributions.models.contributions import Contribution [as 别名]
# 或者: from indico.modules.events.contributions.models.contributions.Contribution import allocate_friendly_ids [as 别名]
def import_contributions_from_csv(event, f):
    """Import timetable contributions from a CSV file into an event."""
    reader = csv.reader(f.read().splitlines())
    contrib_data = []

    for num_row, row in enumerate(reader, 1):
        try:
            start_dt, duration, title, first_name, last_name, affiliation, email = \
                [to_unicode(value).strip() for value in row]
            email = email.lower()
        except ValueError:
            raise UserValueError(_('Row {}: malformed CSV data - please check that the number of columns is correct')
                                 .format(num_row))
        try:
            parsed_start_dt = event.tzinfo.localize(dateutil.parser.parse(start_dt)) if start_dt else None
        except ValueError:
            raise UserValueError(_("Row {row}: can't parse date: \"{date}\"").format(row=num_row, date=start_dt))

        try:
            parsed_duration = timedelta(minutes=int(duration)) if duration else None
        except ValueError:
            raise UserValueError(_("Row {row}: can't parse duration: {duration}").format(row=num_row,
                                                                                         duration=duration))

        if not title:
            raise UserValueError(_("Row {}: contribution title is required").format(num_row))

        if email and not validate_email(email):
            raise UserValueError(_("Row {row}: invalid email address: {email}").format(row=num_row, email=email))

        contrib_data.append({
            'start_dt': parsed_start_dt,
            'duration': parsed_duration or timedelta(minutes=20),
            'title': title,
            'speaker': {
                'first_name': first_name,
                'last_name': last_name,
                'affiliation': affiliation,
                'email': email
            }
        })

    # now that we're sure the data is OK, let's pre-allocate the friendly ids
    # for the contributions in question
    Contribution.allocate_friendly_ids(event, len(contrib_data))
    contributions = []
    all_changes = defaultdict(list)

    for contrib_fields in contrib_data:
        speaker_data = contrib_fields.pop('speaker')

        with track_time_changes() as changes:
            contribution = create_contribution(event, contrib_fields, extend_parent=True)

        contributions.append(contribution)
        for key, val in changes[event].viewitems():
            all_changes[key].append(val)

        email = speaker_data['email']
        if not email:
            continue

        # set the information of the speaker
        person = get_event_person(event, {
            'firstName': speaker_data['first_name'],
            'familyName': speaker_data['last_name'],
            'affiliation': speaker_data['affiliation'],
            'email': email
        })
        link = ContributionPersonLink(person=person, is_speaker=True)
        link.populate_from_dict({
            'first_name': speaker_data['first_name'],
            'last_name': speaker_data['last_name'],
            'affiliation': speaker_data['affiliation']
        })
        contribution.person_links.append(link)

    return contributions, all_changes
开发者ID:indico,项目名称:indico,代码行数:80,代码来源:util.py


注:本文中的indico.modules.events.contributions.models.contributions.Contribution.allocate_friendly_ids方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。