django summernote create custom buttons - django

I'd like to add a custom button similar to this one:
$('#makeSnote').click(function(event) {
var highlight = window.getSelection(),
spn = document.createElement('span'),
range = highlight.getRangeAt(0)
spn.innerHTML = highlight;
spn.className = 'snote';
spn.style.color = 'blue';
range.deleteContents();
range.insertNode(spn);
});
JS Fiddle
http://jsfiddle.net/ypweuw1L/
I'm not sure how would I go about adding this into my Django app? I don't see anything in the django-summernote docs.

I don't know about django-summernote well. But you can create custom button and use it on toolbar with options.
for more details...
http://summernote.org/deep-dive/#custom-button

Related

Modifying the Wagtail publish dropdown (per app)

I'd like to reconfigure the default "Publish" menu. The default configuration is this:
I'd like to make Publish the default action, and move it to the top. I'd also like to remove Submit for Moderation, as our site has no current need for that feature.
Ideally, I'd love to be able to override the menu config on a per-app basis - we will likely have other sections of our site in the future where we want a different config.
Is this possible?
This isn't currently possible I'm afraid - the menu items are fixed in wagtailadmin/pages/create.html and edit.html.
This is possible as of Wagtail 2.4 using the register_page_action_menu_item hook, as per Yannic Hamann's answer. Additionally, Wagtail 2.7 (not released at time of writing) provides a construct_page_listing_buttons hook for modifying existing options.
You can add a new item to the action menu by registering a custom menu item with the help of wagtail hooks.
To do so create a file named wagtail_hooks.py within any of your existing Django apps.
from wagtail.core import hooks
from wagtail.admin.action_menu import ActionMenuItem
class GuacamoleMenuItem(ActionMenuItem):
label = "Guacamole"
def get_url(self, request, context):
return "https://www.youtube.com/watch?v=dNJdJIwCF_Y"
#hooks.register('register_page_action_menu_item')
def register_guacamole_menu_item():
return GuacamoleMenuItem(order=10)
Source
If you want to remove an existing menu item:
#hooks.register('construct_page_action_menu')
def remove_submit_to_moderator_option(menu_items, request, context):
menu_items[:] = [item for item in menu_items if item.name != 'action-submit']
The default button SAVE DRAFT is still hardcoded and therefore cannot be configured so easily. See here.
It seems it can't be done on server side without some monkey-patching.
However if you want it for yourself (or have access to computers of those who will publish) you can modify your browser instead.
Install Tampermonkey browser addon
Create new script with a content below
Change http://127.0.0.1:8000/admin/* to your wagtail admin panel url pattern
Save script and check admin panel
Result should look:
// ==UserScript==
// #name Wagtail: replace "Save draft" with "Publish"
// #match *://127.0.0.1:8000/admin/*
// #require https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// ==/UserScript==
let $ = window.jQuery;
function modify() {
let draft = $("button.button-longrunning:contains('Save draft')");
let publish = $("button.button-longrunning:contains('Publish')");
if (draft.length && publish.length) {
swap(publish, draft);
}
};
function swap(a, b) {
a = $(a); b = $(b);
var tmp = $('<span>').hide();
a.before(tmp);
b.before(a);
tmp.replaceWith(b);
};
$(document).ready(function() {
setTimeout(function() {
try {
modify();
}
catch (e) {
console.error(e, e.stack);
}
}, 100);
});
Modifying the code above, these selectors works for every admin language:
let draft = $("button.button-longrunning.action-save");
let publish = $("button.button-longrunning[name='action-publish']");

Sitecore, render Item in code with personalization in mvc

My terminology might be slightly off as I am new to Sitecore mvc. I am trying to hardcode a view rendering with a hard coded datasource (I have to do it this way for other requirements) This view rendering has placeholders that are dynamically set and then personalized.
How can I trigger the ViewRendering on an item?
I've tried ItemRendering, but it doesn't seem to be picking up the stuff set up in the PageEditor.
* To be clear, the helpful posts below have the rendering working, but we do not seem to be getting the personalized datasources. *
I believe this is what you're after:
Item item = /* think you already have this */;
// Setup
var rendering = new Sitecore.Mvc.Presentation.Rendering {
RenderingType = "Item",
Renderer = new Sitecore.Mvc.Presentation.ItemRenderer.ItemRenderer {
Item = item
}
};
rendering["DataSource"] = item.ID.ToString();
// Execution
var writer = new System.IO.StringWriter();
var args = new Sitecore.Mvc.Pipelines.Response.RenderRendering(rendering, writer);
Sitecore.Mvc.Pipelines.PipelineService.Get().RunPipeline("mvc.renderRendering", args);
// Your result:
var html = writer.ToString();
You can statically bind a View Rendering using the Rendering() method of the Sitecore HTML Helper. This also works for Controller Renderings using the same method.
#Html.Sitecore().Rendering("<rendering item id>")
This method accepts an anonymous object for passing in parameters, such as the Datasource and caching options.
#Html.Sitecore().Rendering("<rendering item id>", new { DataSource = "<datasource item id>", Cacheable = true, Cache_VaryByData = true })
In your view rendering, I am assuming you have a placeholder defined along with the appropriate placeholder settings in Sitecore (If not, feel free to comment with more detail and I will update my answer).
#Html.Sitecore().Placeholder("my-placeholder")
In this case, "my-placeholder" will be handled like any other placeholder, so you will be able to add and personalize components within it from the Page Editor.

Overriding Admin shapes in custom Orchard CMS theme

I'm doing a custom theme for Orchard CMS.
As part of client project one of the requirements is to have some extra functionality in blog admin page.
This is pretty easy doing some simple changes in Parts.Blogs.BlogPost.ListAdmin.cshtml
I do not want to change the Blog source code, I would like to override that wiew in theme just like I'm doing with all the others on front end.
Following some guidelines found on orchard forum I have tried the following paths:
~/Themes/MyTheme/Views/Parts.Blogs.BlogPost.ListAdmin.cshtml
~/Themes/MyTheme/Views/Orchard.Blogs/Parts.Blogs.BlogPost.ListAdmin.cshtml
~/Themes/MyTheme/Views/Dashboard/Admin/Parts.Blogs.BlogPost.ListAdmin.cshtml
but the view is not picked up.
So, How may I override a view in my theme that will be picked up by admin dashboard instead of the default one?
Thanks
You need to create a theme with a project file, then add a .cs file with something like this in it:
public class AdminOverride : IThemeSelector
{
public ThemeSelectorResult GetTheme(RequestContext context)
{
if (AdminFilter.IsApplied(context))
{
return new ThemeSelectorResult { Priority = 111, ThemeName = "NewAdminTheme" };
}
return null;
}
}
Don't set this theme as current though, just enable it from the backend. You will also need to set your TheAdmin as the base theme in the Theme.txt like this:
BaseTheme: TheAdmin

How to add a new template to Typo3 "Layouts" drop-down list

I have a Typo3 installation with 3 existing layout options. These are all declared in the page.ts file like so:
#normal layout:
[globalVar=TSFE:page|layout=0]
page.10.template.file = fileadmin/template/classic-page.html
page.includeCSS.screen = fileadmin/template/css/style.css
page.includeCSS.screen.media = screen
[global]
And they are all in this list further down the page.ts file, like so:
TCEFORM.pages {
layout.altLabels.0 = Normal
layout.altLabels.1 = Startpage
layout.altLabels.2 = Landing page
}
All of these layout options are displayed in the CMS on the "Edit Page (X)" > Appearance page, in a drop-down list of possible layout options. Handy!
Now I have a shiny new template that I want to add as an option. I can apply it to a specific page id (say, page id #563) by adding this code to the page.ts:
[globalVar = TSFE:id=563]
page.10.template.file = fileadmin/template/shinynewtemplate.html
[GLOBAL]
But I can't seem to add it as a new layout option in the drop-down menu. I have tried this:
#shiny new layout:
[globalVar=TSFE:page|layout=3]
page.10.template.file = fileadmin/template/shinynewtemplate.html
page.includeCSS.screen = fileadmin/template/css/style.css
page.includeCSS.screen.media = screen
[global]
TCEFORM.pages {
layout.altLabels.0 = Normal
layout.altLabels.1 = Startpage
layout.altLabels.2 = Landing page
layout.altLabels.3 = Shiny new page
}
But no banana. It doesn't show up in the Appearance layout list, so I can't apply it to a page.
What am I missing? Is there somewhere else that I need to declare this template file so that it will show up as an option in the drop-down list?
An alternative label does not help, if there is no item which will use your label.
You need to add the new item (Page TS Config!):
TCEFORM.pages {
layout.addItems.3 = Shiny new page
}
See TSconfig

Django admin Many2Many widget customization

I need to customize how the m2m widget for Django Admin gets displayed but I am kind of stumped where to start. I have tried subclassing couple of widgets from django.forms and django.contrib.admin.wigets but nothing seems to be working.
Here's a depiction of what I am looking for http://i.stack.imgur.com/81AY3.png.
Any help appreciated.
That looks like the kind of thing that could be achieved with JavaScript alone. For adding your own JavaScript to the Django admin, see the documentation for ModelAdmin media definitions.
This is what I came up with. It does most of the work. However, the list does not get updated when a new item is added and changing an item does not redirect back to the original page.
/your_app/forms.py
class ProductForm(forms.ModelForm):
class Media:
js = ('js/custom_m2m.js',)
class Meta:
model = Product
/your_media/js/custom_m2m.js
django.jQuery(function() {
var $ = django.jQuery;
// Add a new place holder div to hold the m2m list
$('div.options div').append('<div class="newdiv"></div>');
// Assign some variables
var target = "options"; // <-- Target Field
var newdiv = $('div.newdiv');
var next_page = window.location.pathname;
var add_link = $('div.'+target+' div a#add_id_'+target);
var edit_img = "/static/media_admin/img/admin/icon_changelink.gif";
var add_img = "/static/media_admin/img/admin/icon_addlink.gif";
// Make the placeholder div bit nicer
newdiv.attr("style", "line-height:20px; margin-left:105px;");
// Iterate through select options and append them to 'newdiv'
$('select#id_'+target+' option[selected="selected"]').each(function() {
newdiv.append(''+$(this).text()+' <img src="'+edit_img+'" /><br />');
});
// Add a 'Add new' link after the option list
add_link.html('<strong>Add new</strong> ' + add_link.html());
add_link.appendTo(newdiv);
// Show the 'newdiv' and hide the original dropdown
$('select#id_'+target).after(newdiv);
$('select#id_'+target).css("display", "none");
$('div.'+target+' p[class="help"]').css("display", "none");
});
As you can see, the above script uses some hardcoded paths. Any improvement would be helpful.