Example #1
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', "")})
Example #2
0
    def put(self, request):
        """
        Create a new plugin.

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

        PUT parameters :
          - placeholder_id : Html ID of the placeholder, e.g. 'content-placeholder-1'.
          - plugin_type    : Content type ID of the new plugin.
          - form fields
          - csrf token
        """
        # Get PUT parameters
        request.PUT = self.DATA.copy() 

        placeholder_html_id = request.PUT.get('placeholder_id', None)
        plugin_type    = request.PUT.get('plugin_type', None)

        if placeholder_html_id and plugin_type:
            # Check if placeholder ID is valid
            placeholder_slug_items = check_placeholder_html_id(placeholder_html_id)
            layout_section_slug = placeholder_slug_items[0]
            # Get form of the plugin type
            try:
                content_type = CTA().get_for_pk(plugin_type)
            except ContentType.DoesNotExist:
                raise ErrorResponse(status.HTTP_400_BAD_REQUEST,
                                    {'msg': MESSAGES.get('default_error', "")})

            PluginClass     = content_type.model_class()
            plugin          = PluginClass()
            PluginFormClass = plugin.get_admin_form()
            form            = PluginFormClass(request.PUT, instance=plugin)

            if form.is_valid():
                # Creation of the new plugin
                new_plugin = form.save()
                # Creation of the relation
                display_on_new_pages = (not is_page_placeholder_html_id(placeholder_html_id))
                relation = PluginRelation.objects.create(content_object=new_plugin,
                                                         placeholder_slug= placeholder_html_id,
                                                         display_on_new_pages=display_on_new_pages)
                relation.pages.add(request.page)

                # At the moment, we displayed it on everypage
                if display_on_new_pages:
                    for page in request.website.pages.all():
                        relation.pages.add(page)
                
                # Set right order
                try:
                    last_relation = PluginRelation.objects.filter(
                        pages=request.page,
                        placeholder_slug=placeholder_html_id).order_by('-plugin_order')[0]
                    plugin_order = last_relation.plugin_order + 10
                except IndexError:
                    plugin_order = 0
                relation.plugin_order = plugin_order
                # Saving modifications
                relation.save()

                rendering_context = RenderingContext(request)
                plugin_html_medias = rendering_context\
                    .get_html_medias_for_plugin_relation(relation)
                html_rendering = rendering_context.get_html_layout(layout_section_slug)
                
                # Sending response
                response = Response(status.HTTP_200_OK,
                                    {'msg': MESSAGES.get('item_edit_success', ''),
                                     'html': html_rendering,
                                     'layout_section_slug': layout_section_slug,
                                     'medias': plugin_html_medias})
                return self.render(response)
            
            # Invalid Form => 400 BAD REQUEST
            else:
                html = render_to_string('administration/plugin/plugin-create.html',
                                        {'form': form,
                                         'placeholder_id': placeholder_html_id,
                                         'plugin_type': plugin_type},
                                       context_instance=RequestContext(request))
                raise ErrorResponse(status.HTTP_400_BAD_REQUEST,
                                    {'msg': MESSAGES.get('invalid_data', ""),
                                     'html': html})
        # Bad parameters => 400 BAD REQUEST
        else:
            raise ErrorResponse(status.HTTP_400_BAD_REQUEST,
                                {'msg': MESSAGES.get('default_error', "")})