def get_layout_view(self, request, id): """ Return the metadata about a layout :param request: Django request object. :param id: Id integer value (pk) for the layout referenced. :return: JsonResponse with layout information or error message. """ # Get the layout or if it does not exist return an error message. try: layout = models.PageLayout.objects.get(pk=id) except models.PageLayout.DoesNotExist: json = {'success': False, 'error': 'Layout not found'} status = 404 else: template = layout.get_template() placeholders = get_template_placeholder_data(template) status = 200 # Set useful information regarding the layout. json = { 'id': layout.id, 'key': layout.key, 'title': layout.title, 'placeholders': [p.as_dict() for p in placeholders], } return JsonResponse(json, status=status)
def get_placeholder_data(self, request, obj=None): """ Read the placeholder data to display in the template. This reads :attr:`placeholder_layout` and :attr:`placeholder_layout_template`. It can be overwritten to return the layout depending on the page or request. Tip: if the object is given, this could read ``obj.plugin.get_render_template(request, obj)`` too. """ # This information is "magically" picked up in the template to create the tabs. # Some details about the inner workings of django-fluent-contents: # - the PlaceholderEditorInline reads this to construct the formset. # - the formset data is exposed to the template. # - the tabs are created based on the formset data. # if self.placeholder_layout: # Assume a list of `PlaceholderData` objects. return self.placeholder_layout elif self.placeholder_layout_template: # Read the placeholder data from the template once. template = get_template(self.placeholder_layout_template) self.placeholder_layout = get_template_placeholder_data(template) return self.placeholder_layout else: # Similar to PlaceholderEditorAdmin.get_placeholder_data(), # raise an exception to indicate the class is not properly used. raise ImproperlyConfigured( "The '{0}' subclass should define a static 'placeholder_layout', " "a 'placeholder_layout_template', " "or overwrite get_placeholder_data().".format(self.__class__.__name__) )
def get_all_allowed_plugins(self): """ By default, all plugins are allowed, unless a placeholder puts a limit on this. The page will load much faster if the plugin types are limited globally here. """ if self.all_allowed_plugins: # Limit the globally available plugins, # using the statically defined list. try: return plugin_pool.get_plugins_by_name(*self.all_allowed_plugins) except PluginNotFound as e: raise PluginNotFound(str(e) + " Update the plugin list of the {0}.all_allowed_plugins setting.".format(self.__class__.__name__)) elif self.placeholder_layout_template: # This page is edited with a fixed template. # Extract all slot names from the template, and base the list of plugins on that. # Note that this actually parses the template, but it will be cached for production environments. template = get_template(self.placeholder_layout_template) slots = [placeholder.slot for placeholder in get_template_placeholder_data(template)] # Resolve all plugins. plugins = [] for slot in slots: plugins.extend(plugin_pool.get_allowed_plugins(slot)) plugins = list(set(plugins)) # The list of names can be stored statically because it won't change anyway. # Otherwise it would be read at least 3 times in a request, # from the admin get_inline_instances(), get_formset() and if not settings.DEBUG: self.all_allowed_plugins = [plugin.name for plugin in plugins] return plugins else: # Accepts all plugins by default return super(FluentContentsPageAdmin, self).get_all_allowed_plugins()
def get_layout_view(self, request): """ Return the metadata about a layout """ template_name = request.GET['name'] # Check if template is allowed, avoid parsing random templates templates = dict(appconfig.SIMPLECMS_TEMPLATE_CHOICES) if not templates.has_key(template_name): jsondata = {'success': False, 'error': 'Template not found'} status = 404 else: # Extract placeholders from the template, and pass to the client. template = get_template(template_name) placeholders = get_template_placeholder_data(template) jsondata = { 'placeholders': [p.as_dict() for p in placeholders], } status = 200 jsonstr = json.dumps(jsondata) return HttpResponse(jsonstr, content_type='application/json', status=status)
def get_placeholder_data(self, request, obj=None): """ Provides a list of :class:`fluent_contents.models.PlaceholderData` classes, that describe the contents of the template. """ template = self.get_page_template(obj) if not template: return [] else: return get_template_placeholder_data(template)
def test_page_placeholder_metadata(self): """ The ``page_placeholder`` tag should expose metadata, which ``fluent_contents.analyzer`` can read. """ template = Template( """{% load fluent_contents_tags %}{% page_placeholder page "slot1" title="SlotTest1" role="s" %}""" ) # Test raw Placeholder extraction raw_placeholders = get_node_instances(template, PagePlaceholderNode) self.assertEqual(len(raw_placeholders), 1) self.assertEqual(raw_placeholders[0].get_slot(), "slot1") self.assertEqual(raw_placeholders[0].get_title(), "SlotTest1") self.assertEqual(raw_placeholders[0].get_role(), "s") # Now test the public API, that returns PlaceholderData objects. data = get_template_placeholder_data(template) self.assertEqual(len(data), 1) self.assertEqual(data[0].slot, "slot1") self.assertEqual(data[0].title, "SlotTest1") self.assertEqual(data[0].role, "s") # Test2: fallback code template = Template( """{% load fluent_contents_tags %}{% page_placeholder page "slot_test2" %}""" ) # Test raw Placeholder extraction raw_placeholders = get_node_instances(template, PagePlaceholderNode) self.assertEqual(len(raw_placeholders), 1) self.assertEqual(raw_placeholders[0].get_slot(), "slot_test2") self.assertEqual(raw_placeholders[0].get_title(), "Slot Test2") self.assertEqual(raw_placeholders[0].get_role(), None) # Test the public API data = get_template_placeholder_data(template) self.assertEqual(len(data), 1) self.assertEqual(data[0].slot, "slot_test2") self.assertEqual(data[0].title, "Slot Test2") self.assertEqual(data[0].role, "m") # Defaults to "main"
def get_placeholder_data(self, request, obj=None): """ Provides a list of `fluent_contents.models.PlaceholderData` classes, that describe the contents of the template. :param request: Django request object. :param obj: Object to get place holder data from. :return: list of `~fluent_contents.models.PlaceholderData` """ template = self.get_page_template(obj) if not template: return [] # No template means no data! else: return get_template_placeholder_data(template)
def test_page_placeholder_metadata(self): """ The ``page_placeholder`` tag should expose metadata, which ``fluent_contents.analyzer`` can read. """ template = Template("""{% load placeholder_tags %}{% page_placeholder page "slot1" title="SlotTest1" role="s" %}""") # Test raw Placeholder extraction raw_placeholders = get_node_instances(template, PagePlaceholderNode) self.assertEqual(len(raw_placeholders), 1) self.assertEqual(raw_placeholders[0].get_slot(), 'slot1') self.assertEqual(raw_placeholders[0].get_title(), 'SlotTest1') self.assertEqual(raw_placeholders[0].get_role(), 's') # Now test the public API, that returns PlaceholderData objects. data = get_template_placeholder_data(template) self.assertEqual(len(data), 1) self.assertEqual(data[0].slot, 'slot1') self.assertEqual(data[0].title, 'SlotTest1') self.assertEqual(data[0].role, 's') # Test2: fallback code template = Template("""{% load placeholder_tags %}{% page_placeholder page "slot_test2" %}""") # Test raw Placeholder extraction raw_placeholders = get_node_instances(template, PagePlaceholderNode) self.assertEqual(len(raw_placeholders), 1) self.assertEqual(raw_placeholders[0].get_slot(), 'slot_test2') self.assertEqual(raw_placeholders[0].get_title(), 'Slot Test2') self.assertEqual(raw_placeholders[0].get_role(), None) # Test the public API data = get_template_placeholder_data(template) self.assertEqual(len(data), 1) self.assertEqual(data[0].slot, 'slot_test2') self.assertEqual(data[0].title, 'Slot Test2') self.assertEqual(data[0].role, 'm') # Defaults to "main"
def get_layout_view(self, request, id): """ Return the metadata about a layout """ try: layout = PageLayout.objects.get(pk=id) except PageLayout.DoesNotExist: json = {"success": False, "error": "Layout not found"} status = 404 else: template = layout.get_template() placeholders = get_template_placeholder_data(template) status = 200 json = { "id": layout.id, "key": layout.key, "title": layout.title, "placeholders": [p.as_dict() for p in placeholders], } return JsonResponse(json, status=status)
def get_layout_view(self, request, id): """ Return the metadata about a layout """ try: layout = PageLayout.objects.get(pk=id) except PageLayout.DoesNotExist: json = {'success': False, 'error': 'Layout not found'} status = 404 else: template = layout.get_template() placeholders = get_template_placeholder_data(template) status = 200 json = { 'id': layout.id, 'key': layout.key, 'title': layout.title, 'placeholders': [p.as_dict() for p in placeholders], } return JsonResponse(json, status=status)
def get_layout_view(self, request): """ Return the metadata about a layout """ template_name = request.GET["name"] # Check if template is allowed, avoid parsing random templates templates = dict(appconfig.SIMPLECMS_TEMPLATE_CHOICES) if template_name not in templates: jsondata = {"success": False, "error": "Template not found"} status = 404 else: # Extract placeholders from the template, and pass to the client. template = get_template(template_name) placeholders = get_template_placeholder_data(template) jsondata = {"placeholders": [p.as_dict() for p in placeholders]} status = 200 jsonstr = json.dumps(jsondata) return HttpResponse(jsonstr, content_type="application/json", status=status)
def get_placeholder_data(self): """ Return placeholder data for this layout's template. """ return get_template_placeholder_data(self.get_template())
def get_placeholder_data(self, request, obj): template = self.get_page_template(obj) return get_template_placeholder_data(template)