Beispiel #1
0
def choose(request, content_type_app_name, content_type_model_name):
    content_type = get_content_type_from_url_params(content_type_app_name,
                                                    content_type_model_name)
    model = content_type.model_class()
    snippet_type_name = get_snippet_type_name(content_type)[0]

    items = model.objects.all()

    p = request.GET.get("p", 1)
    paginator = Paginator(items, 25)

    try:
        paginated_items = paginator.page(p)
    except PageNotAnInteger:
        paginated_items = paginator.page(1)
    except EmptyPage:
        paginated_items = paginator.page(paginator.num_pages)

    return render_modal_workflow(
        request, 'wagtailsnippets/chooser/choose.html',
        'wagtailsnippets/chooser/choose.js', {
            'content_type': content_type,
            'snippet_type_name': snippet_type_name,
            'items': paginated_items,
        })
Beispiel #2
0
def choose(request, content_type_app_name, content_type_model_name):
    content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name)
    model = content_type.model_class()
    snippet_type_name = get_snippet_type_name(content_type)[0]

    items = model.objects.all()

    p = request.GET.get("p", 1)
    paginator = Paginator(items, 25)

    try:
        paginated_items = paginator.page(p)
    except PageNotAnInteger:
        paginated_items = paginator.page(1)
    except EmptyPage:
        paginated_items = paginator.page(paginator.num_pages)

    return render_modal_workflow(
        request,
        'wagtailsnippets/chooser/choose.html', 'wagtailsnippets/chooser/choose.js',
        {
            'content_type': content_type,
            'snippet_type_name': snippet_type_name,
            'items': paginated_items,
        }
    )
Beispiel #3
0
def choose(request, content_type_app_name, content_type_model_name):
    content_type = get_content_type_from_url_params(content_type_app_name,
                                                    content_type_model_name)
    model = content_type.model_class()
    snippet_type_name, snippet_type_name_plural = get_snippet_type_name(
        content_type)

    items = model.objects.all()

    # Search
    is_searchable = class_is_indexed(model)
    is_searching = False
    search_query = None
    if is_searchable and 'q' in request.GET:
        search_form = SearchForm(
            request.GET,
            placeholder=_("Search %(snippet_type_name)s") %
            {'snippet_type_name': snippet_type_name_plural})

        if search_form.is_valid():
            search_query = search_form.cleaned_data['q']

            search_backend = get_search_backend()
            items = search_backend.search(search_query, items)
            is_searching = True

    else:
        search_form = SearchForm(
            placeholder=_("Search %(snippet_type_name)s") %
            {'snippet_type_name': snippet_type_name_plural})

    # Pagination
    paginator, paginated_items = paginate(request, items, per_page=25)

    # If paginating or searching, render "results.html"
    if request.GET.get('results', None) == 'true':
        return render(
            request, "wagtailsnippets/chooser/results.html", {
                'content_type': content_type,
                'snippet_type_name': snippet_type_name,
                'items': paginated_items,
                'query_string': search_query,
                'is_searching': is_searching,
            })

    return render_modal_workflow(
        request, 'wagtailsnippets/chooser/choose.html',
        'wagtailsnippets/chooser/choose.js', {
            'content_type': content_type,
            'snippet_type_name': snippet_type_name,
            'items': paginated_items,
            'is_searchable': is_searchable,
            'search_form': search_form,
            'query_string': search_query,
            'is_searching': is_searching,
        })
Beispiel #4
0
def choose(request, content_type_app_name, content_type_model_name):
    content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name)
    model = content_type.model_class()
    snippet_type_name, snippet_type_name_plural = get_snippet_type_name(content_type)

    items = model.objects.all()

    # Search
    is_searchable = class_is_indexed(model)
    is_searching = False
    search_query = None
    if is_searchable and 'q' in request.GET:
        search_form = SearchForm(request.GET, placeholder=_("Search %(snippet_type_name)s") % {
            'snippet_type_name': snippet_type_name_plural
        })

        if search_form.is_valid():
            search_query = search_form.cleaned_data['q']

            search_backend = get_search_backend()
            items = search_backend.search(search_query, items)
            is_searching = True

    else:
        search_form = SearchForm(placeholder=_("Search %(snippet_type_name)s") % {
            'snippet_type_name': snippet_type_name_plural
        })

    # Pagination
    paginator, paginated_items = paginate(request, items, per_page=25)

    # If paginating or searching, render "results.html"
    if request.GET.get('results', None) == 'true':
        return render(request, "wagtailsnippets/chooser/results.html", {
            'content_type': content_type,
            'snippet_type_name': snippet_type_name,
            'items': paginated_items,
            'query_string': search_query,
            'is_searching': is_searching,
        })

    return render_modal_workflow(
        request,
        'wagtailsnippets/chooser/choose.html', 'wagtailsnippets/chooser/choose.js',
        {
            'content_type': content_type,
            'snippet_type_name': snippet_type_name,
            'items': paginated_items,
            'is_searchable': is_searchable,
            'search_form': search_form,
            'query_string': search_query,
            'is_searching': is_searching,
        }
    )
Beispiel #5
0
    def setUp(self):
        content_type = get_content_type_from_url_params("tests", "advert")

        test_snippet = Advert()
        test_snippet.text = "test_advert"
        test_snippet.url = "http://www.example.com/"
        test_snippet.save()

        edit_handler_class = get_snippet_edit_handler(Advert)
        form_class = edit_handler_class.get_form_class(Advert)
        form = form_class(instance=test_snippet)

        self.snippet_chooser_panel_class = SnippetChooserPanel("text", content_type)
        self.snippet_chooser_panel = self.snippet_chooser_panel_class(instance=test_snippet, form=form)
Beispiel #6
0
def embed_to_frontend_html(id, content_type_app_name, content_type_model_name):
    try:
        content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name)
        model = content_type.model_class()
        instance = model.objects.get(id=id)
    except ObjectDoesNotExist:
        return ''

    if instance is not None:
        # Render template
        return render_to_string(content_type_app_name + '/snippets/' + content_type_model_name + '.html', {
            'snippet': instance,
        })
    else:
        return ''
Beispiel #7
0
def chosen(request, content_type_app_name, content_type_model_name, id):
    content_type = get_content_type_from_url_params(content_type_app_name,
                                                    content_type_model_name)
    model = content_type.model_class()
    item = get_object_or_404(model, id=id)

    snippet_json = json.dumps({
        'id': item.id,
        'string': text_type(item),
    })

    return render_modal_workflow(request, None,
                                 'wagtailsnippets/chooser/chosen.js', {
                                     'snippet_json': snippet_json,
                                 })
Beispiel #8
0
def choose(request, content_type_app_name, content_type_model_name):
    content_type = get_content_type_from_url_params(content_type_app_name,
                                                    content_type_model_name)
    model = content_type.model_class()
    snippet_type_name = get_snippet_type_name(content_type)[0]

    items = model.objects.all()

    return render_modal_workflow(
        request, 'wagtailsnippets/chooser/choose.html',
        'wagtailsnippets/chooser/choose.js', {
            'content_type': content_type,
            'snippet_type_name': snippet_type_name,
            'items': items,
        })
Beispiel #9
0
def wagtail_search_snippet(request, content_type_app_name, content_type_model_name):
    content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name)
    model = content_type.model_class()
    snippet_type_name = get_snippet_type_name(content_type)[0]
    desired_class = content_type.model_class()
    is_searching = False
    items = model.objects.all()

    if 'q' in request.GET:
        search_form = SearchForm(request.GET)
        if search_form.is_valid() and search_form.cleaned_data['q']:
            items = desired_class.objects.all().filter(content__icontains=search_form.cleaned_data['q'])
            total_items = items.count()
            is_searching = True

    p = request.GET.get("p", 1)
    paginator = Paginator(items, 25)

    try:
        paginated_items = paginator.page(p)
    except PageNotAnInteger:
        paginated_items = paginator.page(1)
    except EmptyPage:
        paginated_items = paginator.page(paginator.num_pages)

    if not is_searching:
        search_form = SearchForm()

    if is_searching:
        return render(request, 'wagtail_extras/snippet_search/_search_results.html', {
            'querystring': get_querystring(request),
            'searchform': search_form,
            'snippets': paginated_items,
            'snippet_type_name': snippet_type_name,
            'snippet_type': desired_class,
            'app_name': content_type_app_name,
            'model_name': content_type_model_name,
            'total_results': total_items
        })
    return render_modal_workflow(request, 'wagtail_extras/snippet_search/browse.html', 'wagtail_extras/snippet_search/browse.js', {
        'querystring': get_querystring(request),
        'search_form': search_form,
        'snippets': paginated_items,
        'snippet_type_name': snippet_type_name,
        'snippet_type': desired_class,
        'app_name': content_type_app_name,
        'model_name': content_type_model_name
    })
Beispiel #10
0
def choose(request, content_type_app_name, content_type_model_name):
    content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name)
    model = content_type.model_class()
    snippet_type_name = get_snippet_type_name(content_type)[0]

    items = model.objects.all()

    return render_modal_workflow(
        request,
        'wagtailsnippets/chooser/choose.html', 'wagtailsnippets/chooser/choose.js',
        {
            'content_type': content_type,
            'snippet_type_name': snippet_type_name,
            'items': items,
        }
    )
Beispiel #11
0
    def setUp(self):
        content_type = get_content_type_from_url_params('tests', 'advert')

        test_snippet = Advert()
        test_snippet.text = 'test_advert'
        test_snippet.url = 'http://www.example.com/'
        test_snippet.save()

        edit_handler_class = get_snippet_edit_handler(Advert)
        form_class = edit_handler_class.get_form_class(Advert)
        form = form_class(instance=test_snippet)

        self.snippet_chooser_panel_class = SnippetChooserPanel(
            'text', content_type)
        self.snippet_chooser_panel = self.snippet_chooser_panel_class(
            instance=test_snippet, form=form)
Beispiel #12
0
def chosen(request, content_type_app_name, content_type_model_name, id):
    content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name)
    model = content_type.model_class()
    item = get_object_or_404(model, id=id)

    snippet_json = json.dumps({
        'id': item.id,
        'string': unicode(item),
    })

    return render_modal_workflow(
        request,
        None, 'wagtailsnippets/chooser/chosen.js',
        {
            'snippet_json': snippet_json,
        }
    )
Beispiel #13
0
def embed_to_editor_html(id, content_type_app_name, content_type_model_name):
    try:
        content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name)
        model = content_type.model_class()
        instance = model.objects.get(id=id)
    except ObjectDoesNotExist:
        return ''

    if instance is not None:
        # Render template
        return render_to_string('wagtailembedder/editor/embed_editor.html', {
            'embed': instance,
            'content_type_app_name': content_type_app_name,
            'content_type_model_name': content_type_model_name,
        })
    else:
        return ''
Beispiel #14
0
def embed_to_frontend_html(id, content_type_app_name, content_type_model_name):
    try:
        content_type = get_content_type_from_url_params(
            content_type_app_name, content_type_model_name)
        model = content_type.model_class()
        instance = model.objects.get(id=id)
    except ObjectDoesNotExist:
        return ''

    if instance is not None:
        # Render template
        return render_to_string(
            content_type_app_name + '/snippets/' + content_type_model_name +
            '.html', {
                'snippet': instance,
            })
    else:
        return ''
Beispiel #15
0
def embed_to_editor_html(id, content_type_app_name, content_type_model_name):
    try:
        content_type = get_content_type_from_url_params(
            content_type_app_name, content_type_model_name)
        model = content_type.model_class()
        instance = model.objects.get(id=id)
    except ObjectDoesNotExist:
        return ''

    if instance is not None:
        # Render template
        return render_to_string(
            'wagtailembedder/editor/embed_editor.html', {
                'embed': instance,
                'content_type_app_name': content_type_app_name,
                'content_type_model_name': content_type_model_name,
            })
    else:
        return ''
Beispiel #16
0
def choose_snippet(request, id, content_type_app_name,
                   content_type_model_name):
    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
    model = content_type.model_class()
    try:
        model.objects.get(id=id)
    except ObjectDoesNotExist:
        return render_modal_workflow(
            request, None, None, {
                'error':
                'Sorry, an error occurred. Contact support or try again later.'
            })
    embed_html = embed_to_editor_html(id, content_type_app_name,
                                      content_type_model_name)

    return render_modal_workflow(request, None,
                                 'wagtailembedder/editor/snippet_chosen.js',
                                 {'embed_html': embed_html})
Beispiel #17
0
def choose_snippet(request, id, content_type_app_name, content_type_model_name):
    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
    model = content_type.model_class()
    try:
        model.objects.get(id=id)
    except ObjectDoesNotExist:
        return render_modal_workflow(
            request,
            None,
            None,
            {'error': 'Sorry, an error occurred. Contact support or try again later.'}
        )
    embed_html = embed_to_editor_html(id, content_type_app_name, content_type_model_name)

    return render_modal_workflow(
        request,
        None,
        'wagtailembedder/editor/snippet_chosen.js',
        {'embed_html': embed_html}
    )
Beispiel #18
0
    def get_snippet_model_from_url_params(app_name, model_name):
        content_type = get_content_type_from_url_params(app_name, model_name.lower())
        model = content_type.model_class()

        return model
Beispiel #19
0
    def get_snippet_model_from_url_params(app_name, model_name):
        content_type = get_content_type_from_url_params(
            app_name, model_name.lower())
        model = content_type.model_class()

        return model