def layout_regions_data(): """ Builds a json object to embed on the admin's change_form of Layout. This object contains all the available views and regions for all templates. """ theme_name = SiteSettings.objects.get().theme theme_settings = get_theme(theme_name) out_dict = {} layout_templates = {} for name, dic_ in theme_settings.layout_templates.iteritems(): regions = dic_['regions'] regions_data = [{'region_name': '', 'verbose_name': '------'}] items = sorted(regions.items(), key = lambda region: region[1]['weight']) for region_name, data in items: if region_name != 'content': regions_data.extend([{'region_name': region_name,'verbose_name': data['name']}]) layout_templates[name] = regions_data out_dict["layout_templates"] = layout_templates views_for_models = {} models = frontend.site.get_all_registry_models() for model in models: views = [{'view_name': '', 'verbose_name': '------'}] views.extend([{'view_name': view.name, 'verbose_name': view.verbose_name} for view in frontend.site.get_views(model) if view.is_region_view]) ctype = ContentType.objects.get_for_model(model) views_for_models[ctype.pk] = views out_dict["views_for_models"] = views_for_models json_data = json.dumps(out_dict, cls=LazyJSONEncoder) return json_data
def change_view(self, request, object_id, form_url='', extra_context=None): theme_settings = get_theme(SiteSettings.objects.get().theme) layout_template = Layout.objects.get(pk=object_id).template layout_regions = theme_settings.layout_templates[layout_template]['regions'] layout_regions = sorted(layout_regions.items(), key=lambda x: x[1]['weight']) extra_context = {'layout_regions': layout_regions} return super(LayoutAdmin, self).change_view(request, object_id, form_url, extra_context)
def render(self, context): theme = get_theme(context["CYCLOPE_CURRENT_THEME"]) # if region_name is none, it is the main content if not context.has_key("region_name") or not context["region_name"]: region = "content" else: region = context["region_name"] content_classes = "" if hasattr(theme, "inline_content_classes"): # if content_classes are defined return them otherwise return empty content_classes = theme.inline_content_classes.get(region, "") return content_classes
def __init__(self, *args, **kwargs): super(LayoutAdminForm, self).__init__(*args, **kwargs) try: theme_name = SiteSettings.objects.get().theme except: return theme = get_theme(theme_name) tpl_choices = [(tpl, tpl_settings['verbose_name']) for tpl, tpl_settings in theme.layout_templates.items()] self.fields['template'].choices = tpl_choices
def layout_regions_json(self, request): """View to dynamically update template regions select in the admin.""" template_filename = request.GET['q'] theme_name = SiteSettings.objects.get().theme theme_settings = get_theme(theme_name) regions = theme_settings.layout_templates[template_filename]['regions'] regions_data = [{'region_name': '', 'verbose_name': '------'}] regions_data.extend([{ 'region_name': region_name, 'verbose_name': verbose_name } for region_name, verbose_name in sorted(regions.items(), key=lambda r: r[1]) if region_name != 'content']) json_data = simplejson.dumps(regions_data, cls=LazyJSONEncoder) return HttpResponse(json_data, mimetype='application/json')
def region(context, region_name): """Defines a region where views can be inserted in a template. The views that will actualy be inserted are defined in a Layout. Usage:: {% region 'region_name' %} The region name must be one of the regions available to the template according to the theme configuration (as defined in the theme's __init__ file) """ theme = get_theme(SiteSettings.objects.get().theme) region_ids = "" if hasattr(theme, "region_layout_ids"): # if region_ids are defined return them otherwise return empty region_ids = theme.region_layout_ids.get(region_name, "") region_classes = "" if hasattr(theme, "region_layout_classes"): # if content_classes are defined return them otherwise return empty region_classes = theme.region_layout_classes.get(region_name, "") # if we have a layout in the context use that one, otherwise guess it from the request layout = context.get('layout', layout_for_request(context['request'])) region_vars = {'layout_name': layout.slug, 'region_name': region_name, 'region_layout_ids': region_ids, 'region_layout_classes': region_classes} regionviews = layout.regionview_set.filter( region=region_name).order_by('weight') views = [] for regionview in regionviews: view_vars={} view = frontend.site.get_view( regionview.content_type.model_class(), regionview.content_view, ) # instance views need instance data -> slug if view.is_instance_view: if regionview.content_object is None: raise template.TemplateSyntaxError slug = regionview.content_object.slug view_vars['output'] = view(context['request'], region_name=region_name, content_object=regionview.content_object, view_options=regionview.view_options) view_vars['slug'] = slug else: view_vars['output'] = view(context['request'], region_name=region_name, view_options=regionview.view_options) view_vars['name'] = regionview.content_view view_vars['model'] = regionview.content_type.model views.append(view_vars) region_vars['views'] = views return region_vars
def test_api(self): all_themes = themes.get_all_themes() theme = all_themes[DEFAULT_THEME] self.assertTrue( "layout_two_columns_left.html" in theme.layout_templates) self.assertTrue(theme is themes.get_theme(DEFAULT_THEME))