def resolveHierarchicalId(objId): """ Gets an object from its Id (unless it doesn't exist, in which case it returns None """ from MaKaC.conference import ConferenceHolder m = re.match(r'(\w+)(?:\.(s?)(\w+))?(?:\.(\w+))?', objId) # If the expression doesn't match at all, return if not m or not m.groups()[0]: return None try: m = m.groups() conference = ConferenceHolder().getById(m[0]) if m[1]: # session id specified - session or slot session = conference.getSessionById(m[2]) if m[3]: # session slot: 1234.s12.1 return session.getSlotById(m[3]) else: # session: 1234.s12 return session else: if m[2]: # second token is not a session id # (either contribution or subcontribution) contribution = conference.getContributionById(m[2]) if m[3]: # subcontribution: 1234.12.1 return contribution.getSubContributionById(m[3]) else: # contribution: 1234.12 return contribution else: # there's not second token # it's definitely a conference return conference except errors.MaKaCError: return None
def linked_object(self): """Returns the linked object.""" from MaKaC.conference import CategoryManager, ConferenceHolder if self.link_type == LinkType.category: return CategoryManager().getById(self.category_id, True) event = ConferenceHolder().getById(self.event_id, True) if event is None: return None if self.link_type == LinkType.event: return event elif self.link_type == LinkType.session: return event.getSessionById(self.session_id) elif self.link_type == LinkType.contribution: return event.getContributionById(self.contribution_id) elif self.link_type == LinkType.subcontribution: contribution = event.getContributionById(self.contribution_id) if contribution is None: return None return contribution.getSubContributionById(self.subcontribution_id)
def get_object_from_args(args=None): """Retrieves an event object from request arguments. This utility is meant to be used in cases where the same controller can deal with objects attached to various parts of an event which use different URLs to indicate which object to use. :param args: The request arguments. If unspecified, ``request.view_args`` is used. :return: An ``(object_type, event, object)`` tuple. The event is always the :class:`Conference` associated with the object. The object may be a `Conference`, `Session`, `Contribution` or `SubContribution`. If the object does not exist, ``(object_type, None, None)`` is returned. """ from MaKaC.conference import ConferenceHolder if args is None: args = request.view_args object_type = args['object_type'] event = ConferenceHolder().getById(args['confId'], True) obj = None if event is None: obj = None elif object_type == 'event': obj = event elif object_type == 'session': obj = event.getSessionById(args['sessionId']) elif object_type == 'contribution': obj = event.getContributionById(args['contribId']) elif object_type == 'subcontribution': contrib = event.getContributionById(args['contribId']) if contrib is not None: obj = contrib.getSubContributionById(args['subContId']) else: raise ValueError('Unexpected object type: {}'.format(object_type)) if obj is not None: return object_type, event, obj else: return object_type, None, None