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


Python Session.toForm方法代码示例

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


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

示例1: _createSessionObject

# 需要导入模块: from models import Session [as 别名]
# 或者: from models.Session import toForm [as 别名]
    def _createSessionObject(self, sessionForm):
        """Create Session object, returning SessionForm."""
        # make sure user is authenticated
        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException('Authorization required')

        # get the conference
        conf = ndb.Key(urlsafe=sessionForm.websafeConferenceKey).get()
        if not conf:
            raise endpoints.NotFoundException('No conference found with key: %s' % sessionForm.conferenceKey)

        # check ownership
        if getUserId(user) != conf.organizerUserId:
            raise endpoints.ForbiddenException('Only the organizer of this conference can add sessions.')

        # copy SessionForm/ProtoRPC Message into dict
        data = formToDict(sessionForm, exclude=('websafeKey', 'websafeConferenceKey'))
        # check required fields
        for key in Session.required_fields_schema:
            if not data[key]:
                raise endpoints.BadRequestException("'%s' field is required to create a session." % key)

        # convert date string to a datetime object.
        try:
            data['date'] = datetime.strptime(data['date'][:10], "%Y-%m-%d").date()
        except (TypeError, ValueError):
            raise endpoints.BadRequestException("Invalid date format. Please use 'YYYY-MM-DD'")

        # convert date string to a time object. HH:MM
        try:
            data['startTime'] = datetime.strptime(data['startTime'][:5], "%H:%M").time()
        except (TypeError, ValueError):
            raise endpoints.BadRequestException("Invalid date format. Please use 'HH:MM'")

        if data['duration'] <= 0:
            raise endpoints.BadRequestException("Duration must be greater than zero")

        if data['date'] < conf.startDate or data['date'] > conf.endDate:
            raise endpoints.BadRequestException("Session must be within range of conference start and end date")

        data['speaker'] = Speaker(name=data['speaker'])
        # ask Datastore to allocate an ID.
        s_id = Session.allocate_ids(size=1, parent=conf.key)[0]
        # Datastore returns an integer ID that we can use to create a session key
        data['key'] = ndb.Key(Session, s_id, parent=conf.key)
        # Add session to datastore
        session = Session(**data)
        session.put()

        # Add a task to check and update new featured speaker
        taskqueue.add(
            params={'websafeConferenceKey': conf.key.urlsafe(), 'speaker': session.speaker.name},
            url='/tasks/set_featured_speaker'
        )

        return session.toForm()
开发者ID:LuizGsa21,项目名称:p4-conference-central,代码行数:59,代码来源:conference.py


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