Ejemplo n.º 1
0
    def render_to_response_with_refresh(self, relation_id, app_obj, msg=None):
        
        id_items = check_object_html_id(relation_id,
                                        types=[settings.SLUG_PLUGIN,
                                               settings.SLUG_APP])
        # APP rendering
        if id_items[0] == settings.SLUG_APP:
            # Get placeholder slug in page
            placeholder_slug = app_obj.page.get().placeholder_slug
        # Plugin Rendering
        else:
            # Get placeholder_slug in relation
            placeholder_slug = app_obj.pages.get(pages=self.request.page).placeholder_slug

        placeholder_slug_items = check_placeholder_html_id(placeholder_slug)
        layout_section_slug = placeholder_slug_items[0]
        placeholder_id = placeholder_slug_items[2]
        rendering_context = RenderingContext(self.request)
        html = rendering_context.get_html_placeholder(layout_section_slug, placeholder_id,
                                                      context=RequestContext(self.request))
        datas = {'html': html, 'placeholder_slug': placeholder_slug}
        if msg:
            datas['msg'] = msg
        response = Response(status.HTTP_200_OK, datas)
        return self.render(response)
Ejemplo n.º 2
0
    def post(self, request, relation_html_id):
        """
        Update plugin modifications.

        If modifications are correct return confirmation message
        and the new render of the layout section;
        if not, return the plugin form with error messages

        Parameters :
          - relation_html_id : PluginRelation Id

        POST parameters :
          - form fields
          - csrf token
        """
        pk = check_object_html_id(relation_html_id)[1]
        try:
            plugin_relation = PluginRelation.objects.filter(
                pages__website__exact=request.website, 
                id__exact=pk)[0]
        except IndexError:
            raise Http404
        # Create the plugin form
        plugin = plugin_relation.content_object
        PluginFormClass = plugin.get_admin_form()
        form = PluginFormClass(request.POST, instance=plugin)

        if form.is_valid():
            plugin = form.save()
            placeholder_slug_items = check_placeholder_html_id(
                plugin_relation.placeholder_slug)
            layout_section_slug = placeholder_slug_items[0]
            rendering_context = RenderingContext(request)
            html_rendering = rendering_context.get_html_layout(layout_section_slug)

            response = Response(status.HTTP_200_OK,
                                {"msg": MESSAGES.get('item_edit_success',""),
                                 'html': html_rendering,
                                 'layout_section_slug': layout_section_slug})

            return self.render(response)
        
        else:            
            # Invalid form => 400 BAD REQUEST
            # with forms (and errors..)
            html = render_to_string('administration/plugin/plugin-edit.html',
                                    {'form': form,
                                     'plugin': plugin,
                                     'plugin_relation_html_id': relation_html_id},
                                    context_instance = RequestContext(request))
            
            raise ErrorResponse(status.HTTP_400_BAD_REQUEST,
                                {'msg': MESSAGES.get('invalid_data', ""),
                                 'html': html})
Ejemplo n.º 3
0
    def base_view(self, request, html_id_object, url_action):
        """
        Basic View of actions admin.

        This method gets the object related to the request
        and return the action asked.
        """
        # Get and check app/plugin object HTML ID
        # Types accepted : PluginRelation or App
        # If slug not valid => raise HTTP_400_BAD_REQUEST
        object_type, object_id = check_object_html_id(
            html_id_object, types=[settings.SLUG_PLUGIN, settings.SLUG_APP])

        # Case #1 - Object Type : PluginRelation
        if object_type == settings.SLUG_PLUGIN:
            # Get plugin relation
            try:
                obj_relation = PluginRelation.objects\
                    .get(id__exact=object_id)
            except PluginRelation.DoesNotExist:
                # If the plugin is not found => 404
                raise ErrorResponse(status.HTTP_404_NOT_FOUND,
                                    {'msg': MESSAGES.get('default_error', "")})
            # Get plugin object
            obj = obj_relation.content_object

        # Case #2 - Object Type : App
        # Necessarily : object_type == settings.SLUG_APP:
        else:
            # Get app object
            obj = request.page.app_page_object
            # We check that slug parameter is correct
            if obj.pk != int(object_id):
                raise ErrorResponse(status.HTTP_404_NOT_FOUND,
                                    {'msg': MESSAGES.get('default_error', "")})

        # Formatting url action
        # (add '/' at the begining and the ending)
        if url_action[0] != '/':
            url_action = '/' + url_action
        if url_action[-1] != '/':
            url_action = url_action + '/'

        # Dispatcher View
        try:
            match = resolve(url_action, urlconf=obj.get_actions_urlconf())
            return match.func(request, html_id_object, obj, **match.kwargs)
        except Http404:
            raise ErrorResponse(status.HTTP_404_NOT_FOUND,
                                {'msg': MESSAGES.get('action_not_found', "")})
Ejemplo n.º 4
0
    def base_view(self, request, html_id_object, url_action):
        """
        Basic View of actions admin.

        This method gets the object related to the request
        and return the action asked.
        """
        # Get and check app/plugin object HTML ID
        # Types accepted : PluginRelation or App
        # If slug not valid => raise HTTP_400_BAD_REQUEST
        object_type, object_id = check_object_html_id(
            html_id_object, types=[settings.SLUG_PLUGIN, settings.SLUG_APP])

        # Case #1 - Object Type : PluginRelation
        if object_type == settings.SLUG_PLUGIN:
            # Get plugin relation
            try:
                obj_relation = PluginRelation.objects\
                    .get(id__exact=object_id)
            except PluginRelation.DoesNotExist:
                # If the plugin is not found => 404
                raise ErrorResponse(status.HTTP_404_NOT_FOUND,
                                    {'msg': MESSAGES.get('default_error', "")})
            # Get plugin object
            obj = obj_relation.content_object

        # Case #2 - Object Type : App
        # Necessarily : object_type == settings.SLUG_APP:
        else:
            # Get app object
            obj = request.page.app_page_object
            # We check that slug parameter is correct
            if obj.pk != int(object_id):
                raise ErrorResponse(status.HTTP_404_NOT_FOUND,
                                    {'msg': MESSAGES.get('default_error', "")})

        # Formatting url action
        # (add '/' at the begining and the ending)
        if url_action[0] != '/': 
            url_action = '/' + url_action
        if url_action[-1] != '/': 
            url_action = url_action + '/'

        # Dispatcher View
        try:
            match = resolve(url_action, urlconf=obj.get_actions_urlconf())
            return match.func(request, html_id_object, obj, **match.kwargs)
        except Http404:
            raise ErrorResponse(status.HTTP_404_NOT_FOUND,
                                {'msg': MESSAGES.get('action_not_found', "")})
Ejemplo n.º 5
0
    def delete(self, request, relation_html_id):
        """
        Delete a plugin.

        Parameters :
          - pk : PluginRelation.id.
        """

        pk = check_object_html_id(relation_html_id)[1]

        try:
            obj = PluginRelation.objects.filter(pages__website__exact=request.website, 
                                                id__exact=pk)[0]
        except IndexError:
            raise Http404

        obj.delete()
        
        response = Response(status.HTTP_200_OK,
                            {"msg": MESSAGES.get('plugin_delete_success', '')})
        return self.render(response)    
Ejemplo n.º 6
0
    def get(self, request, relation_html_id=None):
        """
        Return plugin form to create or update a plugin.

        If `relation_html_id` is not None, we get the form of
        the existing plugin,else we get an empty form
        in order to create a new plugin.

        Parameters :
          - relation_html_id : Html ID of the plugin relation,
                               e.g. 'plugin-relation-2' where
                               '2' is the PluginRelation ID.

        GET parameters : (required if `pk` is None)
          - placeholder_id : Html ID of the placeholder,
                             e.g. 'content-placeholder-1'.
          - plugin_type    : Content Type ID of the new plugin.
        """
        # ----
        # Get a form of an existing plugin
        # ----
        if relation_html_id is not None:
            # Get the ID relation
            pk = check_object_html_id(relation_html_id)[1]
            try:
                obj = PluginRelation.objects.filter(
                                        pages__website__exact=request.website,
                                        id__exact=pk)[0]
            except IndexError:
                raise Http404
            # Create the plugin form
            plugin = obj.content_object
            PluginFormClass = plugin.get_admin_form()
            form = PluginFormClass(instance=plugin)
            # Get the html of the form
            content = render_to_string('administration/plugin/plugin-edit.html',
                                       {'form': form,
                                        'plugin': plugin,
                                        'plugin_relation_html_id': relation_html_id},
                                       context_instance=RequestContext(request))
            response = Response(status.HTTP_200_OK, {'html': content,})
            return self.render(response)
        # ----
        # Get an empty form to create a new plugin
        # ----
        placeholder_id = request.GET.get('placeholder_id', None)
        plugin_type    = request.GET.get('plugin_type', None)

        if placeholder_id and plugin_type:
            # Check if placeholder ID is valid
            check_placeholder_html_id(placeholder_id)
            try:
                # Get class of the plugin type
                plugin_ct = CTA().get_for_pk(plugin_type)
                PluginClass = plugin_ct.model_class()                
                # Create an empty admin form
                PluginFormClass = PluginClass().get_admin_form()
                plugin_form = PluginFormClass()
                # Get html code of the form
                content = render_to_string('administration/plugin/plugin-create.html',
                                           {'form': plugin_form,
                                            'placeholder_id': placeholder_id,
                                            'plugin_type': plugin_type,},
                                           context_instance=RequestContext(request))
                response = Response(status.HTTP_200_OK, {'html': content,})
                return self.render(response)

            except ContentType.DoesNotExist:
                raise ErrorResponse(status.HTTP_400_BAD_REQUEST,
                                    {'msg': MESSAGES.get('default_error', "")})
        # Bad parameters => 400
        else:
            raise ErrorResponse(status.HTTP_400_BAD_REQUEST,
                                {'msg': MESSAGES.get('default_error', "")})
Ejemplo n.º 7
0
    def post(self, request):
        """
        Update a PluginRelation.

        Parameters :
          - placeholder_id : HTML ID of the new placeholder, eg "content-placeholder-1"
          - plugins_order[] : Ordered list of plugins HTML IDs in the new placeholder
        """
        
        placeholder_html_id = request.POST.get('placeholder_id', None)
        plugins_order  = request.POST.getlist('plugins_order[]')

        if placeholder_html_id and plugins_order:

            # Check placeholder HTML ID
            check_placeholder_html_id(placeholder_html_id,
                                      extras_id=[settings.HTML_ID_PLACEHOLDER_CLIPBOARD,])

            i = 0
            for plugin_id in plugins_order:                
                # Check plugin HTML ID
                plugin_type, relation_id = check_object_html_id(plugin_id,
                                                           types=[settings.SLUG_PLUGIN,
                                                                  settings.SLUG_APP])

                # Be careful, can be a Page object or a relation object
                # In case you are moving the app and not a plugin
                if plugin_type == settings.SLUG_APP:
                    if placeholder_html_id == settings.HTML_ID_PLACEHOLDER_CLIPBOARD:
                        raise ErrorResponse(status.HTTP_400_BAD_REQUEST,
                                            {'msg': MESSAGES.get('default_error', "")})
                    # Get page object to manage app order
                    obj = request.page
                # Plugin object
                else:
                    try:
                        obj = PluginRelation.objects.filter(pages__website__exact=request.website, 
                                                            id__exact=relation_id)[0]

                        # 1. This new placeholder_html_id is website placeholder
                        #      - Add all pages
                        #      - Activate auto creation on new pages
                        if not (is_page_placeholder_html_id(placeholder_html_id) or 
                                placeholder_html_id == settings.HTML_ID_PLACEHOLDER_CLIPBOARD):
                            obj.pages.add(*request.website.pages.all())
                            if not obj.display_on_new_pages:
                                obj.display_on_new_pages = True
                                obj.save()

                        else:
                        # 2. This new placeholder_html_id is page placeholder
                        #      - Delete all pages
                        #      - Add current pages
                        #      - Deactivate auto creation in new page
                            obj.pages.clear()
                            obj.pages.add(request.page)
                            if obj.display_on_new_pages:
                                obj.display_on_new_pages = False
                                obj.save()
                    except IndexError:
                        raise ErrorResponse(status.HTTP_400_BAD_REQUEST,
                                            {'msg': MESSAGES.get('default_error', "")})
                # Update order
                obj.plugin_order = i
                if plugin_type == settings.SLUG_APP:
                    if i > 5:
                        obj.plugin_order -= 5
                else:
                    i = i + 10
                # Update placeholder slug
                obj.placeholder_slug = placeholder_html_id
                obj.save()
            # Send a 200 Response
            response = Response(status.HTTP_200_OK,
                                {"msg": MESSAGES.get('items_move_success', '')})
            return self.render(response)

        # Bad parameters => 400 BAR REQUEST
        else:
            raise ErrorResponse(status.HTTP_400_BAD_REQUEST,
                                {'msg': MESSAGES.get('default_error', "")})