def ComponentRegistrationAddSubscriber(componentRegistration, event): """Receive notification of add event.""" component = componentRegistration.component try: dependents = IDependable(component) except TypeError: return objectpath = zapi.getPath(componentRegistration) dependents.addDependent(objectpath)
def hasDependents(self, item): # We cannot adapt security-proxied objects to IDependable. Unwrapping # is safe since we do not modify anything, and the information whether # an object can be deleted or not is not classified. unwrapped_context = removeSecurityProxy(item) dependable = IDependable(unwrapped_context, None) if dependable is None: return False else: return bool(dependable.dependents())
def CheckDependency(event): object = event.object dependency = IDependable(object, None) if dependency is not None: dependents = dependency.dependents() if dependents: mapping = { "object": zapi.getPath(object), "dependents": ", ".join(dependents) } raise DependencyError(Message(exception_msg, mapping=mapping))
def getBoundContact(context): person = IBasicPerson(removeSecurityProxy(context)) annotations = IAnnotations(person) contact = annotations.get(PERSON_CONTACT_KEY, None) if contact is None: contact = BoundContact() annotations[PERSON_CONTACT_KEY] = contact contact.__name__ = 'contact' contact.__parent__ = person dependable = IDependable(contact) dependable.addDependent("") notify(ObjectAddedEvent(contact, contact.__parent__, contact.__name__)) return contact
def demographics(self): pos = 0 result = [] for demo in self.context.values(): dependable = IDependable(removeSecurityProxy(demo), None) removable = dependable is None or not dependable.dependents() pos += 1 result.append({ 'name': demo.__name__, 'title': demo.title, 'pos': pos, 'removable': removable, }) return result
def importDefaultGroups(self, activeSchoolyear): oldGroups = IGroupContainer(activeSchoolyear) newGroups = IGroupContainer(self.object) for groupId in defaultGroups: if groupId in oldGroups: oldGroup = oldGroups[groupId] newGroup = Group(oldGroup.title, oldGroup.description) newGroups[groupId] = newGroup IDependable(newGroup).addDependent('')
def has_dependents(self): """Check whether an object has dependents (via IDependable). Objects that have dependents cannot be removed from the system. Sample usage in a page template: <input type="checkbox" name="delete" tal:attributes="disabled obj/schooltool:has_dependents" /> """ # We cannot adapt security-proxied objects to IDependable. Unwrapping # is safe since we do not modify anything, and the information whether # an object can be deleted or not is not classified. unwrapped_context = removeSecurityProxy(self.context) dependable = IDependable(unwrapped_context, None) if dependable is None: return False else: return bool(dependable.dependents())
def initializeGroupContainer(self): groups = IGroupContainer(self.object) for id, title in defaultGroups.items(): group = groups[id] = Group(title) IDependable(group).addDependent('') persons = ISchoolToolApplication(None)['persons'] manager = persons.super_user if manager is None: return for id in defaultManagerGroups: if manager not in groups[id].members: groups[id].members.add(manager)
def restoreManagerUser(self, app, password): """Ensure there is a manager user Create a user if needed, set password to default, grant manager permissions """ # set the site so that catalog utilities and person factory # utilities and subscribers were available setSite(app) if MANAGER_USERNAME not in app['persons']: factory = getUtility(IPersonFactory) manager = factory.createManagerUser(MANAGER_USERNAME) app['persons'][MANAGER_USERNAME] = manager IDependable(manager).addDependent('') manager = app['persons'][MANAGER_USERNAME] manager.setPassword(password) app['persons'].super_user = manager setSite(None)
def setUpLeaveSchoolDemographics(app): dfs = app.get('schooltool.basicperson.demographics_fields') if dfs is None: return dfs['leave_date'] = DateFieldDescription('leave_date', _('Date of un-enrollment'), limit_keys=['students']) dfs['leave_reason'] = EnumFieldDescription('leave_reason', _('Reason for un-enrollment'), limit_keys=['students']) dfs['leave_reason'].items = [_('Transferred'), _('Dropped out')] dfs['leave_destination'] = EnumFieldDescription('leave_destination', _('Destination school'), limit_keys=['students']) dfs['leave_destination'].items = [ _('Example School A'), _('Example School B') ] for name in LEAVE_SCHOOL_FIELDS: if name in dfs: IDependable(dfs[name]).addDependent('')
def __call__(self): contact = IContact(self.object) dependable = IDependable(contact) dependable.removeDependent("") notify( ObjectRemovedEvent(contact, contact.__parent__, contact.__name__))
def render(self, *args, **kw): unwrapped = removeSecurityProxy(self.context) dependable = IDependable(unwrapped, None) if dependable is None or not bool(dependable.dependents()): return super(GroupDeleteLink, self).render(*args, **kw)
def __call__(self): contact = IContact(self.object) dependable = IDependable(contact) dependable.removeDependent("") notify(ObjectRemovedEvent(contact, contact.__parent__, contact.__name__))
def render(self, *args, **kw): dep = IDependable(removeSecurityProxy(self.context), None) if (dep is not None and dep.dependents()): return '' return flourish.page.ModalFormLinkViewlet.render(self, *args, **kw)
def __call__(self): group_container = IGroupContainer(self.object) for group_id, group in list(group_container.items()): IDependable(group).removeDependent('') del group_container[group_id] del group_container.__parent__[group_container.__name__]