Skip to content

Commit

Permalink
Merge pull request #1 from springload/feature/wagtail-1.3.1-update
Browse files Browse the repository at this point in the history
Templates and logic adapted to Wagtail 1.3.1 changes
  • Loading branch information
jordij committed Feb 15, 2016
2 parents 77a7d21 + 8b763ca commit f178b56
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name='wagtailembedder',
version='0.1.4',
version='0.1.5',
packages=['wagtailembedder'],
include_package_data=True,
license='BSD License',
Expand Down
8 changes: 7 additions & 1 deletion wagtailembedder/format.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from django.core.exceptions import ObjectDoesNotExist
from django.template.loader import render_to_string

from wagtail.wagtailsnippets.views.snippets import get_content_type_from_url_params
# from wagtail.wagtailembedder.embeds import get_embed


def embed_to_frontend_html(id, content_type_app_name, content_type_model_name):
"""
Provides the Snippet representation through the appropiate template
"""
try:
content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name)
model = content_type.model_class()
Expand All @@ -22,6 +25,9 @@ def embed_to_frontend_html(id, content_type_app_name, content_type_model_name):


def embed_to_editor_html(id, content_type_app_name, content_type_model_name):
"""
Provides the Snippet representation to display in the RichTextEditor
"""
try:
content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name)
model = content_type.model_class()
Expand Down
1 change: 1 addition & 0 deletions wagtailembedder/helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from wagtail.wagtailcore.rich_text import EMBED_HANDLERS

from wagtailembedder.format import embed_to_editor_html, embed_to_frontend_html


Expand Down
17 changes: 17 additions & 0 deletions wagtailembedder/static/wagtailembedder/js/hallo-embedder.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
function placeCaretAtEnd(el) {
el.focus();
if (typeof window.getSelection != "undefined" && typeof document.createRange != "undefined") {
var range = document.createRange();
range.selectNodeContents(el);
range.collapse(false);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (typeof document.body.createTextRange != "undefined") {
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.collapse(false);
textRange.select();
}
}

(function() {
(function($) {
return $.widget("IKS.halloembedder", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

<div class="nice-padding">
<ul class="listing">
{% for name, description, content_type in snippet_types %}
{% for app_label, name, description in snippet_types %}
<li>
<div class="row row-flush title snippet-list">
<h2>
<a href="{% url 'wagtailembedder_class_chosen' content_type.app_label content_type.model %}" class="snippet-embedder col6">
<a href="{% url 'wagtailembedder_class_chosen' app_label name %}" class="snippet-embedder col6">
{{ name|capfirst }}
</a>
</h2>
Expand Down
35 changes: 24 additions & 11 deletions wagtailembedder/views/chooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@

from wagtail.wagtailsnippets.models import get_snippet_content_types
from wagtail.wagtailsnippets.permissions import user_can_edit_snippet_type
from wagtail.wagtailsnippets.views.snippets import get_snippet_type_description, get_snippet_type_name
from wagtail.wagtailsnippets.views.snippets import get_content_type_from_url_params
from wagtail.wagtailsnippets.models import get_snippet_models

from wagtailembedder.format import embed_to_editor_html


@permission_required('wagtailadmin.access_admin')
def index(request):
snippet_types = [(
get_snippet_type_name(content_type)[1],
get_snippet_type_description(content_type),
content_type)
for content_type in get_snippet_content_types()
"""
Fetches all human-readabe names of all snippet classes and presents them
in a list.
"""
snippet_types = [
(
content_type._meta.app_label,
content_type._meta.model.__name__,
content_type._meta.description,
)
for content_type in get_snippet_models()
if user_can_edit_snippet_type(request.user, content_type)
]
return render_modal_workflow(
Expand All @@ -32,13 +39,16 @@ def index(request):


def index_objects(request, content_type_app_name, content_type_model_name):
"""
Fetch objects of related model of the given ContentType and call the template
to properly display them in a list.
"""
snippet_types = get_snippet_content_types()
for content_type in snippet_types:
name = get_snippet_type_name(content_type)[0]
if name.lower().replace(" ", "") == content_type_model_name:
model = content_type.model_class()
items = model.objects.all()
snippet_type_name, snippet_type_name_plural = get_snippet_type_name(content_type)
if content_type.model == content_type_model_name.lower():
items = content_type.model_class().objects.all()
snippet_type_name = content_type.model_class()._meta.verbose_name
snippet_type_name_plural = content_type.model_class()._meta.verbose_name_plural

return render_modal_workflow(
request,
Expand All @@ -56,6 +66,9 @@ def index_objects(request, content_type_app_name, content_type_model_name):


def choose_snippet(request, id, content_type_app_name, content_type_model_name):
"""
Choose snippet and display its representation in the Hallo.js richtext field.
"""
content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name)
if not user_can_edit_snippet_type(request.user, content_type):
raise PermissionDenied
Expand Down
2 changes: 2 additions & 0 deletions wagtailembedder/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from django.conf import settings
from django.utils.html import format_html, format_html_join
from django.core.urlresolvers import reverse

from wagtail.wagtailcore import hooks

from wagtailembedder import urls
from wagtailembedder.helper import add_embed_handler

Expand Down

0 comments on commit f178b56

Please sign in to comment.