Ejemplo n.º 1
0
def list(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()

    permissions = [
        get_permission_name(action, model)
        for action in ['add', 'change', 'delete']
    ]
    if not any([request.user.has_perm(perm) for perm in permissions]):
        return permission_denied(request)

    snippet_type_name, snippet_type_name_plural = get_snippet_type_name(content_type)

    items = model.objects.all()

    # Pagination
    p = request.GET.get('p', 1)
    paginator = Paginator(items, 20)

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

    return render(request, 'wagtailsnippets/snippets/type_index.html', {
        'content_type': content_type,
        'snippet_type_name': snippet_type_name,
        'snippet_type_name_plural': snippet_type_name_plural,
        'items': paginated_items,
        'can_add_snippet': request.user.has_perm(get_permission_name('add', model)),
    })
Ejemplo n.º 2
0
def list(request, app_label, model_name):
    model = get_snippet_model_from_url_params(app_label, model_name)

    permissions = [
        get_permission_name(action, model)
        for action in ['add', 'change', 'delete']
    ]
    if not any([request.user.has_perm(perm) for perm in permissions]):
        return permission_denied(request)

    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': model._meta.verbose_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': model._meta.verbose_name_plural})

    paginator, paginated_items = paginate(request, items)

    # Template
    if request.is_ajax():
        template = 'wagtailsnippets/snippets/results.html'
    else:
        template = 'wagtailsnippets/snippets/type_index.html'

    return render(
        request, template, {
            'model_opts':
            model._meta,
            'items':
            paginated_items,
            'can_add_snippet':
            request.user.has_perm(get_permission_name('add', model)),
            'is_searchable':
            is_searchable,
            'search_form':
            search_form,
            'is_searching':
            is_searching,
            'query_string':
            search_query,
        })
Ejemplo n.º 3
0
def list(request, app_label, model_name):
    model = get_snippet_model_from_url_params(app_label, model_name)

    permissions = [
        get_permission_name(action, model)
        for action in ['add', 'change', 'delete']
    ]
    if not any([request.user.has_perm(perm) for perm in permissions]):
        return permission_denied(request)

    items = model.objects.all()

    # Preserve the snippet's model-level ordering if specified, but fall back on PK if not
    # (to ensure pagination is consistent)
    if not items.ordered:
        items = items.order_by('pk')

    # 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': model._meta.verbose_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': model._meta.verbose_name_plural
        })

    paginator, paginated_items = paginate(request, items)

    # Template
    if request.is_ajax():
        template = 'wagtailsnippets/snippets/results.html'
    else:
        template = 'wagtailsnippets/snippets/type_index.html'

    return render(request, template, {
        'model_opts': model._meta,
        'items': paginated_items,
        'can_add_snippet': request.user.has_perm(get_permission_name('add', model)),
        'is_searchable': is_searchable,
        'search_form': search_form,
        'is_searching': is_searching,
        'query_string': search_query,
    })
Ejemplo n.º 4
0
def list(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()

    permissions = [
        get_permission_name(action, model)
        for action in ['add', 'change', 'delete']
    ]
    if not any([request.user.has_perm(perm) for perm in permissions]):
        return permission_denied(request)

    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
        })

    paginator, paginated_items = paginate(request, items)

    # Template
    if request.is_ajax():
        template = 'wagtailsnippets/snippets/results.html'
    else:
        template = 'wagtailsnippets/snippets/type_index.html'

    return render(request, template, {
        'content_type': content_type,
        'snippet_type_name': snippet_type_name,
        'snippet_type_name_plural': snippet_type_name_plural,
        'items': paginated_items,
        'can_add_snippet': request.user.has_perm(get_permission_name('add', model)),
        'is_searchable': is_searchable,
        'search_form': search_form,
        'is_searching': is_searching,
        'query_string': search_query,
    })
Ejemplo n.º 5
0
def list(request, app_label, model_name):
    model = get_snippet_model_from_url_params(app_label, model_name)

    permissions = [get_permission_name(action, model) for action in ["add", "change", "delete"]]
    if not any([request.user.has_perm(perm) for perm in permissions]):
        return permission_denied(request)

    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": model._meta.verbose_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": model._meta.verbose_name_plural}
        )

    paginator, paginated_items = paginate(request, items)

    # Template
    if request.is_ajax():
        template = "wagtailsnippets/snippets/results.html"
    else:
        template = "wagtailsnippets/snippets/type_index.html"

    return render(
        request,
        template,
        {
            "model_opts": model._meta,
            "items": paginated_items,
            "can_add_snippet": request.user.has_perm(get_permission_name("add", model)),
            "is_searchable": is_searchable,
            "search_form": search_form,
            "is_searching": is_searching,
            "query_string": search_query,
        },
    )
Ejemplo n.º 6
0
def delete(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()

    permission = get_permission_name('delete', model)
    if not request.user.has_perm(permission):
        return permission_denied(request)

    snippet_type_name = get_snippet_type_name(content_type)[0]

    instance = get_object_or_404(model, id=id)

    if request.POST:
        instance.delete()
        messages.success(
            request,
            _("{snippet_type} '{instance}' deleted.").format(
                snippet_type=capfirst(snippet_type_name), instance=instance))
        return redirect('wagtailsnippets:list', content_type.app_label,
                        content_type.model)

    return render(
        request, 'wagtailsnippets/snippets/confirm_delete.html', {
            'content_type': content_type,
            'snippet_type_name': snippet_type_name,
            'instance': instance,
        })
Ejemplo n.º 7
0
def delete(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()

    permission = get_permission_name("delete", model)
    if not request.user.has_perm(permission):
        return permission_denied(request)

    snippet_type_name = get_snippet_type_name(content_type)[0]

    instance = get_object_or_404(model, id=id)

    if request.POST:
        instance.delete()
        messages.success(
            request,
            _("{snippet_type} '{instance}' deleted.").format(
                snippet_type=capfirst(snippet_type_name), instance=instance
            ),
        )
        return redirect("wagtailsnippets:list", content_type.app_label, content_type.model)

    return render(
        request,
        "wagtailsnippets/snippets/confirm_delete.html",
        {"content_type": content_type, "snippet_type_name": snippet_type_name, "instance": instance},
    )
Ejemplo n.º 8
0
def edit(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()

    permission = get_permission_name("change", model)
    if not request.user.has_perm(permission):
        return permission_denied(request)

    snippet_type_name = get_snippet_type_name(content_type)[0]

    instance = get_object_or_404(model, id=id)
    edit_handler_class = get_snippet_edit_handler(model)
    form_class = edit_handler_class.get_form_class(model)

    if request.POST:
        form = form_class(request.POST, request.FILES, instance=instance)

        if form.is_valid():
            form.save()

            messages.success(
                request,
                _("{snippet_type} '{instance}' updated.").format(
                    snippet_type=capfirst(snippet_type_name), instance=instance
                ),
                buttons=[
                    messages.button(
                        reverse(
                            "wagtailsnippets:edit", args=(content_type_app_name, content_type_model_name, instance.id)
                        ),
                        _("Edit"),
                    )
                ],
            )
            return redirect("wagtailsnippets:list", content_type.app_label, content_type.model)
        else:
            messages.error(request, _("The snippet could not be saved due to errors."))
            edit_handler = edit_handler_class(instance=instance, form=form)
    else:
        form = form_class(instance=instance)
        edit_handler = edit_handler_class(instance=instance, form=form)

    return render(
        request,
        "wagtailsnippets/snippets/edit.html",
        {
            "content_type": content_type,
            "snippet_type_name": snippet_type_name,
            "instance": instance,
            "edit_handler": edit_handler,
        },
    )
Ejemplo n.º 9
0
def edit(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()

    permission = get_permission_name('change', model)
    if not request.user.has_perm(permission):
        return permission_denied(request)

    snippet_type_name = get_snippet_type_name(content_type)[0]

    instance = get_object_or_404(model, id=id)
    edit_handler_class = get_snippet_edit_handler(model)
    form_class = edit_handler_class.get_form_class(model)

    if request.POST:
        form = form_class(request.POST, request.FILES, instance=instance)

        if form.is_valid():
            form.save()

            messages.success(request,
                             _("{snippet_type} '{instance}' updated.").format(
                                 snippet_type=capfirst(snippet_type_name),
                                 instance=instance),
                             buttons=[
                                 messages.button(
                                     reverse('wagtailsnippets:edit',
                                             args=(content_type_app_name,
                                                   content_type_model_name,
                                                   instance.id)), _('Edit'))
                             ])
            return redirect('wagtailsnippets:list', content_type.app_label,
                            content_type.model)
        else:
            messages.error(request,
                           _("The snippet could not be saved due to errors."))
            edit_handler = edit_handler_class(instance=instance, form=form)
    else:
        form = form_class(instance=instance)
        edit_handler = edit_handler_class(instance=instance, form=form)

    return render(
        request, 'wagtailsnippets/snippets/edit.html', {
            'content_type': content_type,
            'snippet_type_name': snippet_type_name,
            'instance': instance,
            'edit_handler': edit_handler
        })
Ejemplo n.º 10
0
def edit(request, app_label, model_name, id):
    model = get_snippet_model_from_url_params(app_label, model_name)

    permission = get_permission_name('change', model)
    if not request.user.has_perm(permission):
        return permission_denied(request)

    instance = get_object_or_404(model, id=id)
    edit_handler_class = get_snippet_edit_handler(model)
    form_class = edit_handler_class.get_form_class(model)

    if request.method == 'POST':
        form = form_class(request.POST, request.FILES, instance=instance)

        if form.is_valid():
            form.save()

            messages.success(
                request,
                _("{snippet_type} '{instance}' updated.").format(
                    snippet_type=capfirst(model._meta.verbose_name_plural),
                    instance=instance),
                buttons=[
                    messages.button(
                        reverse('wagtailsnippets:edit',
                                args=(app_label, model_name, instance.id)),
                        _('Edit'))
                ])
            return redirect('wagtailsnippets:list', app_label, model_name)
        else:
            messages.error(request,
                           _("The snippet could not be saved due to errors."))
            edit_handler = edit_handler_class(instance=instance, form=form)
    else:
        form = form_class(instance=instance)
        edit_handler = edit_handler_class(instance=instance, form=form)

    return render(
        request, 'wagtailsnippets/snippets/edit.html', {
            'model_opts': model._meta,
            'instance': instance,
            'edit_handler': edit_handler,
            'form': form,
        })
Ejemplo n.º 11
0
def edit(request, app_label, model_name, id):
    model = get_snippet_model_from_url_params(app_label, model_name)

    permission = get_permission_name('change', model)
    if not request.user.has_perm(permission):
        return permission_denied(request)

    instance = get_object_or_404(model, id=id)
    edit_handler_class = get_snippet_edit_handler(model)
    form_class = edit_handler_class.get_form_class(model)

    if request.method == 'POST':
        form = form_class(request.POST, request.FILES, instance=instance)

        if form.is_valid():
            form.save()

            messages.success(
                request,
                _("{snippet_type} '{instance}' updated.").format(
                    snippet_type=capfirst(model._meta.verbose_name_plural),
                    instance=instance
                ),
                buttons=[
                    messages.button(reverse(
                        'wagtailsnippets:edit', args=(app_label, model_name, instance.id)
                    ), _('Edit'))
                ]
            )
            return redirect('wagtailsnippets:list', app_label, model_name)
        else:
            messages.error(request, _("The snippet could not be saved due to errors."))
            edit_handler = edit_handler_class(instance=instance, form=form)
    else:
        form = form_class(instance=instance)
        edit_handler = edit_handler_class(instance=instance, form=form)

    return render(request, 'wagtailsnippets/snippets/edit.html', {
        'model_opts': model._meta,
        'instance': instance,
        'edit_handler': edit_handler,
        'form': form,
    })
Ejemplo n.º 12
0
def create(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()

    permission = get_permission_name('add', model)
    if not request.user.has_perm(permission):
        return permission_denied(request)

    snippet_type_name = get_snippet_type_name(content_type)[0]

    instance = model()
    edit_handler_class = get_snippet_edit_handler(model)
    form_class = edit_handler_class.get_form_class(model)

    if request.POST:
        form = form_class(request.POST, request.FILES, instance=instance)

        if form.is_valid():
            form.save()

            messages.success(
                request,
                _("{snippet_type} '{instance}' created.").format(
                    snippet_type=capfirst(get_snippet_type_name(content_type)[0]),
                    instance=instance
                ),
                buttons=[
                    messages.button(reverse('wagtailsnippets:edit', args=(content_type_app_name, content_type_model_name, instance.id)), _('Edit'))
                ]
            )
            return redirect('wagtailsnippets:list', content_type.app_label, content_type.model)
        else:
            messages.error(request, _("The snippet could not be created due to errors."))
            edit_handler = edit_handler_class(instance=instance, form=form)
    else:
        form = form_class(instance=instance)
        edit_handler = edit_handler_class(instance=instance, form=form)

    return render(request, 'wagtailsnippets/snippets/create.html', {
        'content_type': content_type,
        'snippet_type_name': snippet_type_name,
        'edit_handler': edit_handler,
    })
Ejemplo n.º 13
0
def create(request, app_label, model_name):
    model = get_snippet_model_from_url_params(app_label, model_name)

    permission = get_permission_name("add", model)
    if not request.user.has_perm(permission):
        return permission_denied(request)

    instance = model()
    edit_handler_class = get_snippet_edit_handler(model)
    form_class = edit_handler_class.get_form_class(model)

    if request.method == "POST":
        form = form_class(request.POST, request.FILES, instance=instance)

        if form.is_valid():
            form.save()

            messages.success(
                request,
                _("{snippet_type} '{instance}' created.").format(
                    snippet_type=capfirst(model._meta.verbose_name), instance=instance
                ),
                buttons=[
                    messages.button(
                        reverse("wagtailsnippets:edit", args=(app_label, model_name, instance.id)), _("Edit")
                    )
                ],
            )
            return redirect("wagtailsnippets:list", app_label, model_name)
        else:
            messages.error(request, _("The snippet could not be created due to errors."))
            edit_handler = edit_handler_class(instance=instance, form=form)
    else:
        form = form_class(instance=instance)
        edit_handler = edit_handler_class(instance=instance, form=form)

    return render(
        request,
        "wagtailsnippets/snippets/create.html",
        {"model_opts": model._meta, "edit_handler": edit_handler, "form": form},
    )
Ejemplo n.º 14
0
def delete(request, app_label, model_name, id):
    model = get_snippet_model_from_url_params(app_label, model_name)

    permission = get_permission_name("delete", model)
    if not request.user.has_perm(permission):
        return permission_denied(request)

    instance = get_object_or_404(model, id=id)

    if request.POST:
        instance.delete()
        messages.success(
            request,
            _("{snippet_type} '{instance}' deleted.").format(
                snippet_type=capfirst(model._meta.verbose_name_plural), instance=instance
            ),
        )
        return redirect("wagtailsnippets:list", app_label, model_name)

    return render(
        request, "wagtailsnippets/snippets/confirm_delete.html", {"model_opts": model._meta, "instance": instance}
    )
Ejemplo n.º 15
0
def delete(request, app_label, model_name, id):
    model = get_snippet_model_from_url_params(app_label, model_name)

    permission = get_permission_name('delete', model)
    if not request.user.has_perm(permission):
        return permission_denied(request)

    instance = get_object_or_404(model, id=id)

    if request.POST:
        instance.delete()
        messages.success(
            request,
            _("{snippet_type} '{instance}' deleted.").format(
                snippet_type=capfirst(model._meta.verbose_name_plural),
                instance=instance))
        return redirect('wagtailsnippets:list', app_label, model_name)

    return render(request, 'wagtailsnippets/snippets/confirm_delete.html', {
        'model_opts': model._meta,
        'instance': instance,
    })
Ejemplo n.º 16
0
def delete(request, app_label, model_name, id):
    model = get_snippet_model_from_url_params(app_label, model_name)

    permission = get_permission_name('delete', model)
    if not request.user.has_perm(permission):
        return permission_denied(request)

    instance = get_object_or_404(model, id=id)

    if request.method == 'POST':
        instance.delete()
        messages.success(
            request,
            _("{snippet_type} '{instance}' deleted.").format(
                snippet_type=capfirst(model._meta.verbose_name_plural),
                instance=instance
            )
        )
        return redirect('wagtailsnippets:list', app_label, model_name)

    return render(request, 'wagtailsnippets/snippets/confirm_delete.html', {
        'model_opts': model._meta,
        'instance': instance,
    })
Ejemplo n.º 17
0
def list(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()

    permissions = [get_permission_name(action, model) for action in ["add", "change", "delete"]]
    if not any([request.user.has_perm(perm) for perm in permissions]):
        return permission_denied(request)

    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
    p = request.GET.get("p", 1)
    paginator = Paginator(items, 20)

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

    # Template
    if request.is_ajax():
        template = "wagtailsnippets/snippets/results.html"
    else:
        template = "wagtailsnippets/snippets/type_index.html"

    return render(
        request,
        template,
        {
            "content_type": content_type,
            "snippet_type_name": snippet_type_name,
            "snippet_type_name_plural": snippet_type_name_plural,
            "items": paginated_items,
            "can_add_snippet": request.user.has_perm(get_permission_name("add", model)),
            "is_searchable": is_searchable,
            "search_form": search_form,
            "is_searching": is_searching,
            "query_string": search_query,
        },
    )
Ejemplo n.º 18
0
def list(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()

    permissions = [
        get_permission_name(action, model)
        for action in ['add', 'change', 'delete']
    ]
    if not any([request.user.has_perm(perm) for perm in permissions]):
        return permission_denied(request)

    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
    p = request.GET.get('p', 1)
    paginator = Paginator(items, 20)

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

    # Template
    if request.is_ajax():
        template = 'wagtailsnippets/snippets/results.html'
    else:
        template = 'wagtailsnippets/snippets/type_index.html'

    return render(
        request, template, {
            'content_type':
            content_type,
            'snippet_type_name':
            snippet_type_name,
            'snippet_type_name_plural':
            snippet_type_name_plural,
            'items':
            paginated_items,
            'can_add_snippet':
            request.user.has_perm(get_permission_name('add', model)),
            'is_searchable':
            is_searchable,
            'search_form':
            search_form,
            'is_searching':
            is_searching,
            'query_string':
            search_query,
        })