def get_title_from_context(context): title = None if IAlchemistContent.providedBy(context): if IDCDescriptiveProperties.providedBy(context): title = context.title else: props = IDCDescriptiveProperties(context, None) if props is not None: title = props.title else: title = context.short_name elif IAlchemistContainer.providedBy(context): domain_model = context._class try: descriptor = queryModelDescriptor(domain_model) except: descriptor = None name = "" if descriptor: name = getattr(descriptor, 'container_name', None) if name is None: name = getattr(descriptor, 'display_name', None) if not name: name = getattr(context, '__name__', None) title = name elif ILocation.providedBy(context) and \ IDCDescriptiveProperties.providedBy(context): title = context.title return title
def _searchresults(self): section = get_section_name() subqueries = [] type_filter = "" if IAlchemistContainer.providedBy(self.context): for t in ALLOWED_TYPES["business"]: iface = resolve.resolve("bungeni.models.interfaces.I%sContainer"%t) if iface.providedBy(self.context): type_filter = t break if type_filter: type_query = self.searcher.query_field('object_type', type_filter) else: # Filter items allowed in current section for tq in ALLOWED_TYPES[section]: subqueries.append(self.searcher.query_field('object_type', tq)) type_query = self.searcher.query_composite(self.searcher.OP_OR, subqueries) self.query = self.searcher.query_composite(self.searcher.OP_AND, (self.query, type_query,)) try: results = self.searcher.search(self.query, 0, self.searcher.get_doccount()) except: results = [] results = filter(self.authorized, results) return results
def __call__(self, context): if IAlchemistContent.providedBy(context): ctx = context elif IAlchemistContainer.providedBy(context): domain_model = removeSecurityProxy( context.domain_model ) ctx = domain_model() wf = interfaces.IWorkflow(ctx) items=[] for state in wf.workflow.states.keys(): items.append(SimpleTerm(wf.workflow.states[state].id, wf.workflow.states[state].id, _(wf.workflow.states[state].title))) return SimpleVocabulary(items)
def __call__(self, context): if IAlchemistContent.providedBy(context): ctx = context elif IAlchemistContainer.providedBy(context): domain_model = removeSecurityProxy(context.domain_model) ctx = domain_model() wf = interfaces.IWorkflow(ctx) items = [] for state in wf.workflow.states.keys(): items.append(SimpleTerm(wf.workflow.states[state].id, wf.workflow.states[state].id, _(wf.workflow.states[state].title))) return SimpleVocabulary(items)
def form_name(self): parent = self.context.__parent__ if IAlchemistContainer.providedBy(parent): descriptor = queryModelDescriptor(parent.domain_model) elif IAlchemistContent.providedBy(self.context): descriptor = queryModelDescriptor(self.context.__class__) else: raise RuntimeError("Unsupported object: %s." % repr(self.context)) if descriptor: name = getattr(descriptor, 'display_name', None) if name is None: name = self.context.__class__.__name__ return name
def form_name( self ): parent = self.context.__parent__ if IAlchemistContainer.providedBy(parent): descriptor = queryModelDescriptor(parent.domain_model) elif IAlchemistContent.providedBy(self.context): descriptor = queryModelDescriptor(self.context.__class__) else: raise RuntimeError("Unsupported object: %s." % repr(self.context)) if descriptor: name = getattr(descriptor, 'display_name', None) if name is None: name = self.context.__class__.__name__ return name
def __new__(cls, context, request, view, manager): # we have both primary and secondary navigation, so we won't # show the navigation tree unless we're at a depth > 2 chain = get_context_chain(context)[:-2] if not chain: return # we require the tree to begin with a container object if not IReadContainer.providedBy(chain[-1]): return subcontext = chain[-1] if (len(chain) > 1 or IReadContainer.providedBy(subcontext) and not IAlchemistContainer.providedBy(subcontext) and len(subcontext)): inst = object.__new__(cls, context, request, view, manager) inst.chain = chain return inst
def expand_containers(self, items, containers, _url, chain=(), context=None): #seen_context = False current = False for key, container in containers: if IAlchemistContainer.providedBy(container): descriptor = queryModelDescriptor( proxy.removeSecurityProxy(container).domain_model) if descriptor: name = getattr(descriptor, 'container_name', None) if name is None: name = getattr(descriptor, 'display_name', None) if not name: name = container.domain_model.__name__ else: assert IDCDescriptiveProperties.providedBy(container) container = proxy.removeSecurityProxy(container) name = container.title if context is not None: current = container.__name__ == context.__name__ selected = len(chain) == 0 and current if current: #seen_context = True nodes = self.expand(chain) else: nodes = () items.append({ 'title': name, 'url': "%s/%s" % (_url.rstrip('/'), key), 'current': current, 'selected': selected, 'kind': 'container', 'nodes': nodes, })
def _searchresults(self): section = get_section_name() subqueries = [] type_filter = "" if IAlchemistContainer.providedBy(self.context): for t in ALLOWED_TYPES["business"]: iface = resolve.resolve( "bungeni.models.interfaces.I%sContainer" % t) if iface.providedBy(self.context): type_filter = t break if type_filter: type_query = self.searcher.query_field('object_type', type_filter) else: # Filter items allowed in current section for tq in ALLOWED_TYPES[section]: subqueries.append(self.searcher.query_field('object_type', tq)) type_query = self.searcher.query_composite( self.searcher.OP_OR, subqueries) self.query = self.searcher.query_composite(self.searcher.OP_AND, ( self.query, type_query, )) try: results = self.searcher.search(self.query, 0, self.searcher.get_doccount()) except: results = [] results = filter(self.authorized, results) return results
def filterFields(context, form_fields): omit_names = [] if IAlchemistContent.providedBy(context): md = queryModelDescriptor(context.__class__) for field in form_fields: try: can_write = security.canWrite(context, field.__name__) can_read = security.canAccess(context, field.__name__) except AttributeError: can_write = can_read = False if can_write: continue if can_read: field.for_display = True field.custom_widget = md.get(field.__name__).view_widget else: omit_names.append(field.__name__) elif not IAlchemistContainer.providedBy(context): ctx = getattr(context, 'context', None) if ctx: filterFields(ctx, form_fields) else: raise NotImplementedError return form_fields.omit(*omit_names)
def filterFields(context, form_fields): omit_names = [] if IAlchemistContent.providedBy(context): md = queryModelDescriptor(context.__class__) for field in form_fields: try: can_write = security.canWrite( context, field.__name__) can_read = security.canAccess( context, field.__name__) except AttributeError: can_write = can_read = False if can_write: continue if can_read: field.for_display = True field.custom_widget = md.get(field.__name__).view_widget else: omit_names.append(field.__name__) elif not IAlchemistContainer.providedBy(context): ctx=getattr(context, 'context', None) if ctx: filterFields(ctx, form_fields) else: raise NotImplementedError return form_fields.omit(*omit_names)
def expand(self, chain, include_siblings=True): if len(chain) == 0: return () context = chain.pop() items = [] if IApplication.providedBy(context): items.extend(self.expand(chain)) elif IAlchemistContent.providedBy(context): _url = url.absoluteURL(context, self.request) if IDCDescriptiveProperties.providedBy(context): title = context.title else: props = IDCDescriptiveProperties(context, None) if props is not None: title = props.title else: title = context.short_name selected = len(chain) == 0 if chain: nodes = self.expand(chain) else: kls = context.__class__ containers = [(key, getattr(context, key)) for key, value in kls.__dict__.items() if isinstance(value, ManagedContainerDescriptor)] nodes = [] self.expand_containers(nodes, containers, _url, chain, None) items.append({ 'title': title, 'url': _url, 'current': True, 'selected': selected, 'kind': 'content', 'nodes': nodes, }) elif IAlchemistContainer.providedBy(context): # loop through all managed containers of the parent # object, and include the present container as the # 'current' node. parent = context.__parent__ assert parent is not None _url = url.absoluteURL(parent, self.request) # append managed containers as child nodes kls = type(proxy.removeSecurityProxy(parent)) if include_siblings is True: if IApplication.providedBy(parent): containers = [ (name, parent[name]) for name in location.model_to_container_name_mapping.values() if name in parent ] elif IReadContainer.providedBy(parent): containers = list(parent.items()) else: containers = [ (key, getattr(parent, key)) for key, value in kls.__dict__.items() if isinstance(value, ManagedContainerDescriptor) ] else: containers = [(context.__name__, context)] self.expand_containers(items, containers, _url, chain, context) elif ILocation.providedBy(context): _url = url.absoluteURL(context, self.request) #props = IDCDescriptiveProperties.providedBy(context) and \ # context or IDCDescriptiveProperties(context) if IDCDescriptiveProperties.providedBy(context): props = IDCDescriptiveProperties(context) else: props = context props = proxy.removeSecurityProxy(props) selected = len(chain) == 0 if selected and IReadContainer.providedBy(context): nodes = [] try: self.expand_containers(nodes, context.items(), _url, chain, context) except: pass else: nodes = self.expand(chain) i_id = getattr(props, 'id', 'N/A') items.append({ 'title': getattr(props, 'title', i_id), 'url': _url, 'current': True, 'selected': selected, 'kind': 'location', 'nodes': nodes, }) elif IReadContainer.providedBy(context): items.extend(self.expand(chain)) return items
def expand(self, chain, include_siblings=True): if len(chain) == 0: return () context = chain.pop() items = [] if IApplication.providedBy(context): items.extend(self.expand(chain)) elif IAlchemistContent.providedBy(context): _url = url.absoluteURL(context, self.request) if IDCDescriptiveProperties.providedBy(context): title = context.title else: props = IDCDescriptiveProperties(context, None) if props is not None: title = props.title else: title = context.short_name selected = len(chain) == 0 if chain: nodes = self.expand(chain) else: kls = context.__class__ containers = [ (key, getattr(context, key)) for key, value in kls.__dict__.items() if isinstance(value, ManagedContainerDescriptor)] nodes = [] self.expand_containers(nodes, containers, _url, chain, None) items.append( {'title': title, 'url': _url, 'current': True, 'selected': selected, 'kind': 'content', 'nodes': nodes, }) elif IAlchemistContainer.providedBy(context): # loop through all managed containers of the parent # object, and include the present container as the # 'current' node. parent = context.__parent__ assert parent is not None _url = url.absoluteURL(parent, self.request) # append managed containers as child nodes kls = type(proxy.removeSecurityProxy(parent)) if include_siblings is True: if IApplication.providedBy(parent): containers = [ (name, parent[name]) for name in location.model_to_container_name_mapping.values() if name in parent] elif IReadContainer.providedBy(parent): containers = list(parent.items()) else: containers = [ (key, getattr(parent, key)) for key, value in kls.__dict__.items() if isinstance(value, ManagedContainerDescriptor)] else: containers = [(context.__name__, context)] self.expand_containers(items, containers, _url, chain, context) elif ILocation.providedBy(context): _url = url.absoluteURL(context, self.request) #props = IDCDescriptiveProperties.providedBy(context) and \ # context or IDCDescriptiveProperties(context) if IDCDescriptiveProperties.providedBy(context): props = IDCDescriptiveProperties(context) else: props = context props = proxy.removeSecurityProxy(props) selected = len(chain) == 0 if selected and IReadContainer.providedBy(context): nodes = [] try: self.expand_containers(nodes, context.items(), _url, chain, context) except: pass else: nodes = self.expand(chain) i_id = getattr(props, 'id','N/A') items.append( {'title': getattr(props, 'title', i_id), 'url': _url, 'current': True, 'selected': selected, 'kind': 'location', 'nodes': nodes, }) elif IReadContainer.providedBy(context): items.extend(self.expand(chain)) return items