allows to paste an existing URL into CMS link box
+from urllib.parse import urlparse
from django.conf.urls import url
from django.contrib import admin
+from django.contrib.sites.shortcuts import get_current_site
from django.forms import Media, widgets
from django.db.models import Q
from django.http import JsonResponse, HttpResponseForbidden, HttpResponseNotFound
+from django.urls import reverse
from django.utils.translation import get_language_from_request
from cms.models.pagemodel import Page
from cms.extensions import PageExtensionAdmin
+from cms.utils.page import get_page_from_path
from cmsplugin_cascade.models import CascadePage, IconFont
from cmsplugin_cascade.link.forms import format_page_link
@@ -55,9 +59,23 @@ class CascadePageAdmin(PageExtensionAdmin):
"""
if not request.is_ajax():
return HttpResponseForbidden()
-
- query_term = request.GET.get('term','').strip('/')
+ data = {'results': []}
language = get_language_from_request(request)
+ query_term = request.GET.get('term')
+ if not query_term:
+ return JsonResponse(data)
+
+ # first, try to resolve by URL if it points to a local resource
+ parse_result = urlparse(query_term)
+ if parse_result.netloc.split(':')[0] == request.META['HTTP_HOST'].split(':')[0]:
+ site = get_current_site(request)
+ path = parse_result.path.lstrip(reverse('pages-root')).rstrip('/')
+ page = get_page_from_path(site, path)
+ if page:
+ data['results'].append(self.get_result_set(language, page))
+ return JsonResponse(data)
+
+ # otherwise resolve by search term
matching_published_pages = Page.objects.published().public().filter(
Q(title_set__title__icontains=query_term, title_set__language=language)
| Q(title_set__path__icontains=query_term, title_set__language=language)
@@ -65,17 +83,19 @@ class CascadePageAdmin(PageExtensionAdmin):
| Q(title_set__page_title__icontains=query_term, title_set__language=language)
).distinct().order_by('title_set__title').iterator()
- data = {'results': []}
for page in matching_published_pages:
+ data['results'].append(self.get_result_set(language, page))
+ if len(data['results']) > 15:
+ break
+ return JsonResponse(data)
+
+ def get_result_set(self, language, page):
title = page.get_title(language=language)
path = page.get_absolute_url(language=language)
- data['results'].append({
+ return {
'id': page.pk,
'text': format_page_link(title, path),
- })
- if len(data['results']) > 15:
- break
- return JsonResponse(data)
+ }
def fetch_fonticons(self, request, iconfont_id=None):
try:
