def get_create_content_types(tree_context): if not tree_context.authenticated_user: # anonymous user return [] if tree_context.node: context_content_type_name = tree_context.node.content_type.app_label + '.' + tree_context.node.content_type.model else: # at site root context node is None context_content_type_name = '<root>' allowed_children_types = get_children_constraints(context_content_type_name) logger.debug('"%s" children constraint types: %s' % (context_content_type_name, str(allowed_children_types))) content_types = [] for child_constraint in allowed_children_types: content_type_name = child_constraint['content_type_name'] quantifier = child_constraint.get('quantifier') or -1 if quantifier >= 0: obj_count = count_children(tree_context.path, ct=content_type_name) if not obj_count < quantifier: # reached max num of objects, cannot create more logger.warning('max num (%s) of "%s" objects reached' % (obj_count, content_type_name)) continue # Model constraints specify valid children types, but this could vary further # depending on tree context. Provide app hook to provide valid types per context. context_types_util = query_component('ContextTypesUtil', name=content_type_name) if context_types_util: content_types_names = context_types_util(tree_context.path) else: content_types_names = [] content_model_class = get_model(*content_type_name.split('.')) if issubclass(content_model_class, TreeContent): content_types_names.append(content_type_name) else: # could be a generic parent class with concrete TreeContent subclasses for sc in content_model_class.__subclasses__(): if issubclass(sc, TreeContent): sc_type_name = sc._meta.app_label + '.' + sc._meta.object_name.lower() content_types_names.append(sc_type_name) for ct_name in content_types_names: logger.debug('processing create link for type: "%s"' % (ct_name)) (ct_app_label, ct_model_name) = ct_name.split('.') if not tree_context.authenticated_user.is_superuser and not 'tree.add_content' in tree_context.user_permission_names: # not a superuser nor does user have the generic `add_content` permission # (user with `add_content` perm can create content of any type) # check for specific permission required_create_permission = ct_app_label + '.add_' + ct_model_name if not required_create_permission in tree_context.user_permission_names: # user missing create permission for this content type # skip creating create link for this type logger.debug('user not allowed to create content of type: "%s"' % ct_name) continue content_types.append(ct_name) logger.debug('create types for user "%s", context_path "%s": %s' % (tree_context.authenticated_user.username, tree_context.path, content_types)) return content_types
def count(parent_path, **kwargs): logger.debug("parent_path: %s, kwargs: %s" % (parent_path, kwargs)) from ztree.query.traverse import count_children return (count_children(parent_path, **kwargs), None)
def get_create_content_types(tree_context): if not tree_context.authenticated_user: # anonymous user return [] if tree_context.node: context_content_type_name = tree_context.node.content_type.app_label + '.' + tree_context.node.content_type.model else: # at site root context node is None context_content_type_name = '<root>' allowed_children_types = get_children_constraints( context_content_type_name) logger.debug('"%s" children constraint types: %s' % (context_content_type_name, str(allowed_children_types))) content_types = [] for child_constraint in allowed_children_types: content_type_name = child_constraint['content_type_name'] quantifier = child_constraint.get('quantifier') or -1 if quantifier >= 0: obj_count = count_children(tree_context.path, ct=content_type_name) if not obj_count < quantifier: # reached max num of objects, cannot create more logger.warning('max num (%s) of "%s" objects reached' % (obj_count, content_type_name)) continue # Model constraints specify valid children types, but this could vary further # depending on tree context. Provide app hook to provide valid types per context. context_types_util = query_component('ContextTypesUtil', name=content_type_name) if context_types_util: content_types_names = context_types_util(tree_context.path) else: content_types_names = [] content_model_class = get_model(*content_type_name.split('.')) if issubclass(content_model_class, TreeContent): content_types_names.append(content_type_name) else: # could be a generic parent class with concrete TreeContent subclasses for sc in content_model_class.__subclasses__(): if issubclass(sc, TreeContent): sc_type_name = sc._meta.app_label + '.' + sc._meta.object_name.lower( ) content_types_names.append(sc_type_name) for ct_name in content_types_names: logger.debug('processing create link for type: "%s"' % (ct_name)) (ct_app_label, ct_model_name) = ct_name.split('.') if not tree_context.authenticated_user.is_superuser and not 'tree.add_content' in tree_context.user_permission_names: # not a superuser nor does user have the generic `add_content` permission # (user with `add_content` perm can create content of any type) # check for specific permission required_create_permission = ct_app_label + '.add_' + ct_model_name if not required_create_permission in tree_context.user_permission_names: # user missing create permission for this content type # skip creating create link for this type logger.debug( 'user not allowed to create content of type: "%s"' % ct_name) continue content_types.append(ct_name) logger.debug('create types for user "%s", context_path "%s": %s' % (tree_context.authenticated_user.username, tree_context.path, content_types)) return content_types