def isTransitiveMember(obj, group): """Is `obj` a member of `group` (either directly or indirectly)? Suppose we have four groups, named a, b, c and d. >>> from schooltool.group.group import Group >>> g = [] >>> for i in range(4): ... group = groups[str(i)] = Group() ... g.append(group) >>> a, b, c, d = g A is a member of b, which is a member of c. >>> Membership(member=a, group=b) >>> Membership(member=b, group=c) If a is a member of b, it is, obviously, a transitive member >>> isTransitiveMember(a, b) True If a is a member of b, which is a member of c, then a is a transitive member of c. >>> isTransitiveMember(a, c) True We say that a is a transitive member of a, for convenience. >>> isTransitiveMember(a, a) True In all other cases, we return False. >>> isTransitiveMember(b, a) False >>> isTransitiveMember(c, a) False >>> isTransitiveMember(a, d) False """ if obj is group: return True # A group usually has more members than a member has groups, so we will # find all transitive groups of `obj` and see whether `group` is one of # them. It does not matter if we use breadth-first or depth-first search. queue = [obj] seen = set([id(obj)]) while queue: cur_obj = queue.pop() # therefore DFS; change to pop(0) to get BFS for new_group in getRelatedObjects(cur_obj, URIGroup): if new_group is group: return True if id(new_group) not in seen: seen.add(id(new_group)) queue.append(new_group) return False
def getCalendars(self): """Get a list of calendars to display. Yields tuples (calendar, color1, color2). """ owner = self.context.__parent__ user = IPerson(self.request.principal, None) if (user is not None and sameProxiedObjects(user, owner)): return instructs = list( getRelatedObjects(owner, URISection, rel_type=URIInstruction)) member_of = list( getRelatedObjects(owner, URIGroup, rel_type=URIMembership)) for obj in instructs + member_of: if IHaveSchedule.providedBy(obj): cal = ISchoolToolCalendar(obj, None) if cal is not None: yield (ISchoolToolCalendar(obj), '#9db8d2', '#7590ae')
def getCalendars(self): """Get a list of calendars to display. Yields tuples (calendar, color1, color2). """ owner = self.context.__parent__ user = IPerson(self.request.principal, None) if (user is not None and sameProxiedObjects(user, owner)): return instructs = list(getRelatedObjects( owner, URISection, rel_type=URIInstruction)) member_of = list(getRelatedObjects( owner, URIGroup, rel_type=URIMembership)) for obj in instructs + member_of: if IHaveSchedule.providedBy(obj): cal = ISchoolToolCalendar(obj, None) if cal is not None: yield(ISchoolToolCalendar(obj), '#9db8d2', '#7590ae')