def test_is_page_placeholder_html_id(self): layout_slug_content = settings.SLUG_CONTENT self.assertTrue(is_page_placeholder_html_id(layout_slug_content)) layout_slug_content_prefix = '%s_%s' % (settings.SLUG_CONTENT, 'foobar') self.assertTrue(is_page_placeholder_html_id(layout_slug_content_prefix)) placeholder_html_id = '%s%s' % (layout_slug_content_prefix, settings.HTML_ID_PLACEHOLDER) self.assertTrue(is_page_placeholder_html_id(placeholder_html_id)) wrong_placeholder_html_id = '%s%s' % ('foobar', settings.HTML_ID_PLACEHOLDER) self.assertFalse(is_page_placeholder_html_id(wrong_placeholder_html_id))
def test_is_page_placeholder_html_id(self): layout_slug_content = settings.SLUG_CONTENT self.assertTrue(is_page_placeholder_html_id(layout_slug_content)) layout_slug_content_prefix = '%s_%s' % (settings.SLUG_CONTENT, 'foobar') self.assertTrue( is_page_placeholder_html_id(layout_slug_content_prefix)) placeholder_html_id = '%s%s' % (layout_slug_content_prefix, settings.HTML_ID_PLACEHOLDER) self.assertTrue(is_page_placeholder_html_id(placeholder_html_id)) wrong_placeholder_html_id = '%s%s' % ('foobar', settings.HTML_ID_PLACEHOLDER) self.assertFalse( is_page_placeholder_html_id(wrong_placeholder_html_id))
def render_layout(parser, token): try: tag_name, layout_slug = token.split_contents() except ValueError: raise template.TemplateSyntaxError("%r tag requires a single argument" % token.contents.split()[0]) if not (layout_slug[0] == layout_slug[-1] and layout_slug[0] in ('"', "'")): raise template.TemplateSyntaxError("%r tag's argument should be in quotes" % tag_name) if is_page_placeholder_html_id(layout_slug[1:-1]): raise template.TemplateSyntaxError("%r tag's argument shouldn't start with the value : '%s'" % ( tag_name, settings.SLUG_CONTENT)) return RenderLayoutNode(layout_slug[1:-1])
def set_layout_template_file(self, layout_section_slug, layout_template_slug): # We get existing layout object layout_object = self._get_layout_object(layout_section_slug) if not layout_object: # We create the layout object layout_object = Layout(slug=layout_section_slug) if is_page_placeholder_html_id(layout_section_slug): # Specific case of the layout content section : # we bind the layout for this page layout_object.related_object_type = CTA().page layout_object.related_object_id = self.page.pk else: layout_object.related_object_type = CTA().website layout_object.related_object_id = self.website.pk # Global modification layout_object.template = layout_template_slug layout_object.save()
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', "")})
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', "")})