示例#1
0
    def get_form_class(self):
        if not self.__class__.form_class:
            content_type_name = self.get_content_type_name() 
            if content_type_name:
                # content type specific form class hook
                self.form_class = query_component('FormClass', name=content_type_name)
            if not self.form_class:
                # generic form class 
                self.form_class = query_component('FormClass')
            if not self.form_class:
                if isinstance(self.content_object, Model):
                    self.form_class = modelform_factory(self.content_object.__class__)

        return super(ComponentUpdateView, self).get_form_class()
示例#2
0
 def get_form_class(self):
     if not self.__class__.form_class:
         content_type_name = self.get_content_type_name() 
         if content_type_name:
             # content type specific form class hook
             self.form_class = query_component('FormClass', name=content_type_name)
         if not self.form_class:
             # generic form class 
             self.form_class = query_component('FormClass')
         if not self.form_class:
             model_cls = self.content_type.model_class()
             if issubclass(model_cls, Model):
                 self.form_class = modelform_factory(model_cls)
     return super(ComponentCreateView, self).get_form_class()
示例#3
0
def create(request, tree_context_path, *args, **kwargs):
    content_type_name = request.GET.get('ct') or request.POST.get('ct')
    if not content_type_name:
        #XXX invalid request??
        logger.error('ct not set')
        raise Http404

    content_type = get_content_type(*content_type_name.split('.'))
    if not content_type:
        logger.error('invalid ct: "%s"' % content_type_name)
        raise Http404

    # when creating/adding new content, context is parent of object being added
    if request.tree_context.path == '/':
        parent_object = None
    else:
        parent_object = request.tree_context.node.content_object

    create_view_cls = query_component('CreateView', (parent_object, content_type), name=content_type_name)
    if not create_view_cls:
        # generic view     
        create_view_cls = GenericCreateView

    logger.debug("Create View class: %s" % create_view_cls)

    #hacky, needed for portlets (portlet context_processors.py)
    request.view_component = create_view_cls

    view_func = create_view_cls.as_view(parent_object=parent_object, content_type=content_type, content_type_name=content_type_name, **kwargs)
    return view_func(request)
示例#4
0
    def delete(self, request, *args, **kwargs):
        content_type_name = self.get_content_type_name()

        logger.debug('ct name: "%s"' % content_type_name)

        delete_factory = None
        if content_type_name:
            delete_factory = query_component('DeleteFactory', name=content_type_name)
        if not delete_factory:
            delete_factory = query_component('DeleteFactory')

        if delete_factory:
            logger.debug('Delete factory: %s' % str(delete_factory))
            delete_factory(request, self.content_object, **kwargs)
            return HttpResponseRedirect(self.get_success_url())

        logger.debug('No delete factory, defaulting to DeleteView delete')

        return super(ComponentDeleteView, self).delete(request, *args, **kwargs)
示例#5
0
    def render(self):

        logger.debug("..... rendering ChildrenListPortlet .....")

        page = self.request.GET.get('page') or 1
        page_size = _page_size(self.request.tree_context.path)

        children_nodes = []
        for node in tqm.filter_children(self.request.tree_context.path,
                                        tree_context=self.request.tree_context,
                                        page=page, page_size=page_size):
            children_nodes.append(node)

        # current node children hook here to get html snippet
        # if lookup fails, fallback to the default below
        if self.request.tree_context.node:
            content_object = self.request.tree_context.node.content_object
            html_snippet_view = query_component('NodeListHtmlSnippetView', (self.request, content_object))
            if html_snippet_view:
                logger.debug("Found html snippet component view: %s" % html_snippet_view)
                return html_snippet_view()

        logger.debug("Returning generic nodes list html snippet")

        # build default html to return
        return_html = u'<p>\n'

        #XXX messy - clean up
        prev_content_type = None
        for cn in children_nodes:
            if not prev_content_type:
                return_html += str(cn.content_type) + '\n'
                return_html += u'<ul>\n'
            elif prev_content_type != cn.content_type:
                return_html += u'</ul>\n'
                return_html += str(cn.content_type) + '\n'
                return_html += u'<ul>\n'

            return_html += u'<li><a href="' + cn.absolute_path + u'">' + (cn.name or cn.slug) + u'</a></li>\n'

            prev_content_type = cn.content_type

        return_html += u'</ul></p>\n'

        logger.debug("processed nodes")

        # Pagination
        query_meta = tqm.get_meta()
        if query_meta:
            pagination_meta = query_meta.get('pagination')
            if pagination_meta:
                return_html += _pagination(self.request.tree_context.path, pagination_meta)
      
        return mark_safe(return_html) 
示例#6
0
    def form_valid(self, form):
        content_type_name = self.get_content_type_name() 

        logger.debug('Form class: %s, ct name: "%s"' % (self.form_class, content_type_name))

        create_factory = None
        if content_type_name:
            # content type specific create factory hook 
            create_factory = query_component('CreateFactory', name=content_type_name)
        if not create_factory:
            # try generic create factory
            create_factory = query_component('CreateFactory')

        if create_factory:
            logger.debug('Create factory: %s' % str(create_factory))
            create_factory(self.request, self.content_type, **form.cleaned_data)
            return HttpResponseRedirect(self.get_success_url())

        logger.debug('No create factory, defaulting to CreateView form_valid')

        return super(ComponentCreateView, self).form_valid(form)
示例#7
0
    def form_valid(self, form):
        content_type_name = self.get_content_type_name() 

        logger.debug('Form class: %s, ct name: "%s"' % (self.form_class, content_type_name))

        update_factory = None
        if content_type_name:
            # content type specific update factory hook 
            update_factory = query_component('UpdateFactory', name=content_type_name)
        if not update_factory:
            # try generic update factory
            update_factory = query_component('UpdateFactory')

        if update_factory:
            logger.debug('Update factory: %s' % str(update_factory))
            update_factory(self.request, self.content_object, **form.cleaned_data)
            return HttpResponseRedirect(self.get_success_url())

        logger.debug('No update factory, defaulting to UpdateView form_valid')

        # default Django UpdateView update 
        return super(ComponentUpdateView, self).form_valid(form)
示例#8
0
def _json_list_view(request, parent_content_object=None):
    logger.debug("looking up Json List View for (request, parent %s)" % parent_content_object)

    list_view = None
    if parent_content_object:
        # hook for specific list view for parent_content_object
        list_view = query_component("JsonListView", (request, parent_content_object))

    # generic node list json view
    if not list_view:
        list_view = get_component("JsonListView", (request,))

    logger.debug("Json List View found: %s" % list_view)
    return list_view
示例#9
0
def get_serializer_cls(model_cls=None, content_type_name=None, fields=None, **kwargs):
    if not content_type_name:
        if issubclass(model_cls, models.Model):
            # app hook to provide custom serializer class for this content type
            content_type_name = model_cls._meta.app_label + '.' + model_cls._meta.object_name.lower()

    if content_type_name:
        if not model_cls:
            model_cls = get_model(*content_type_name.split('.'))

        serializer_cls = query_component('SerializerClass', name=content_type_name)
        if serializer_cls:
            return serializer_cls

    # generic serializer class
    return serializer_class_factory(model_cls, fields=fields, **kwargs)
示例#10
0
def _json_list_view(request, parent_content_object=None):
    logger.debug("looking up Json List View for (request, parent %s)" %
                 parent_content_object)

    list_view = None
    if parent_content_object:
        # hook for specific list view for parent_content_object
        list_view = query_component('JsonListView',
                                    (request, parent_content_object))

    # generic node list json view
    if not list_view:
        list_view = get_component('JsonListView', (request, ))

    logger.debug("Json List View found: %s" % list_view)
    return list_view
示例#11
0
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
示例#12
0
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