Example #1
0
def search(request):
    Image = get_image_model()
    images = []
    q = None
    is_searching = False

    if 'q' in request.GET:
        form = SearchForm(request.GET)
        if form.is_valid():
            q = form.cleaned_data['q']

            # page number
            p = request.GET.get("p", 1)
            is_searching = True
            images = Image.search(q, results_per_page=20, page=p)
    else:
        form = SearchForm()

    if request.is_ajax():
        return render(request, "wagtailimages/images/results.html", {
            'images': images,
            'is_searching': is_searching,
            'search_query': q,
        })
    else:
        return render(
            request, "wagtailimages/images/index.html", {
                'form': form,
                'images': images,
                'is_searching': is_searching,
                'popular_tags': Image.popular_tags(),
                'search_query': q,
            })
Example #2
0
def search(request, parent_page_id=None):
    # A missing or empty page_type parameter indicates 'all page types' (i.e. descendants of wagtailcore.page)
    page_type_string = request.GET.get('page_type') or 'wagtailcore.page'

    try:
        desired_classes = page_models_from_string(page_type_string)
    except (ValueError, LookupError):
        raise Http404

    search_form = SearchForm(request.GET)
    if search_form.is_valid() and search_form.cleaned_data['q']:
        pages = Page.objects.exclude(
            depth=1  # never include root
        )
        pages = filter_page_type(pages, desired_classes)
        pages = pages.search(search_form.cleaned_data['q'], fields=['title'])
    else:
        pages = Page.objects.none()

    paginator, pages = paginate(request, pages, per_page=25)

    for page in pages:
        page.can_choose = True

    return render(
        request, 'wagtailadmin/chooser/_search_results.html',
        shared_context(request, {
            'searchform': search_form,
            'pages': pages,
            'page_type_string': page_type_string,
        })
    )
Example #3
0
def index(request):
    q = None
    is_searching = False

    model_fields = [f.name for f in User._meta.get_fields()]

    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search users"))
        if form.is_valid():
            q = form.cleaned_data['q']
            is_searching = True
            conditions = Q()

            if 'username' in model_fields:
                conditions |= Q(username__icontains=q)

            if 'first_name' in model_fields:
                conditions |= Q(first_name__icontains=q)

            if 'last_name' in model_fields:
                conditions |= Q(last_name__icontains=q)

            if 'email' in model_fields:
                conditions |= Q(email__icontains=q)

            users = User.objects.filter(conditions)
    else:
        form = SearchForm(placeholder=_("Search users"))

    if not is_searching:
        users = User.objects.all()

    if 'last_name' in model_fields and 'first_name' in model_fields:
        users = users.order_by('last_name', 'first_name')

    if 'ordering' in request.GET:
        ordering = request.GET['ordering']

        if ordering == 'username':
            users = users.order_by(User.USERNAME_FIELD)
    else:
        ordering = 'name'

    paginator, users = paginate(request, users)

    if request.is_ajax():
        return render(request, "wagtailusers/users/results.html", {
            'users': users,
            'is_searching': is_searching,
            'query_string': q,
            'ordering': ordering,
        })
    else:
        return render(request, "wagtailusers/users/index.html", {
            'search_form': form,
            'users': users,
            'is_searching': is_searching,
            'ordering': ordering,
            'query_string': q,
        })
Example #4
0
def index(request):
    submissions = Submission.objects.all()

    # Ordering
    if request.GET.get('ordering') in ['title', '-submitted_at']:
        ordering = request.GET['ordering']
    else:
        ordering = '-submitted_at'
    submissions = submissions.order_by(ordering)

    # Search
    query_string = None
    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search submissions"))
        if form.is_valid():
            query_string = form.cleaned_data['q']
            submissions = submissions.search(query_string)
    else:
        form = SearchForm(placeholder=_("Search submissions"))

    # Pagination
    paginator, submissions = paginate(request, submissions)

    return render(
        request, 'submissions/admin/index.html', {
            'submissions': submissions,
            'ordering': ordering,
            'query_string': query_string,
            'is_searching': bool(query_string),
            'search_form': form,
        })
Example #5
0
def search(request):
    pages = []
    q = None

    if "q" in request.GET:
        form = SearchForm(request.GET)
        if form.is_valid():
            q = form.cleaned_data["q"]

            pages = Page.objects.all().prefetch_related("content_type").search(q)
            paginator, pages = paginate(request, pages)
    else:
        form = SearchForm()

    if request.is_ajax():
        return render(
            request,
            "wagtailadmin/pages/search_results.html",
            {"pages": pages, "query_string": q, "pagination_query_params": ("q=%s" % q) if q else ""},
        )
    else:
        return render(
            request,
            "wagtailadmin/pages/search.html",
            {
                "search_form": form,
                "pages": pages,
                "query_string": q,
                "pagination_query_params": ("q=%s" % q) if q else "",
            },
        )
Example #6
0
def search(request, parent_page_id=None):
    page_type = request.GET.get('page_type') or 'wagtailcore.page'
    content_type_app_name, content_type_model_name = page_type.split('.')

    try:
        content_type = ContentType.objects.get_by_natural_key(content_type_app_name, content_type_model_name)
    except ContentType.DoesNotExist:
        raise Http404
    desired_class = content_type.model_class()

    search_form = SearchForm(request.GET)
    if search_form.is_valid() and search_form.cleaned_data['q']:
        pages = desired_class.objects.exclude(
            depth=1  # never include root
        ).filter(title__icontains=search_form.cleaned_data['q'])[:10]
    else:
        pages = desired_class.objects.none()

    shown_pages = []
    for page in pages:
        page.can_choose = True
        shown_pages.append(page)

    return render(request, 'wagtailadmin/chooser/_search_results.html', {
        'querystring': get_querystring(request),
        'searchform': search_form,
        'pages': shown_pages,
    })
Example #7
0
def chooser(request, get_results=False):
    # Get most popular queries
    queries = models.Query.get_most_popular()

    # If searching, filter results by query string
    query_string = None
    if "q" in request.GET:
        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            query_string = searchform.cleaned_data["q"]
            queries = queries.filter(query_string__icontains=normalise_query_string(query_string))
    else:
        searchform = SearchForm()

    paginator, queries = paginate(request, queries, per_page=10)

    # Render
    if get_results:
        return render(request, "wagtailsearch/queries/chooser/results.html", {"queries": queries})
    else:
        return render_modal_workflow(
            request,
            "wagtailsearch/queries/chooser/chooser.html",
            "wagtailsearch/queries/chooser/chooser.js",
            {"queries": queries, "searchform": searchform},
        )
Example #8
0
def search(request):
    documents = []
    q = None
    is_searching = False

    if 'q' in request.GET:
        form = SearchForm(request.GET)
        if form.is_valid():
            q = form.cleaned_data['q']

            is_searching = True
            documents = Document.search(q, results_per_page=20, prefetch_tags=True)
    else:
        form = SearchForm()

    if request.is_ajax():
        return render(request, "wagtaildocs/documents/results.html", {
            'documents': documents,
            'is_searching': is_searching,
            'search_query': q
        })
    else:
        return render(request, "wagtaildocs/documents/index.html", {
            'form': form,
            'documents': documents,
            'is_searching': True,
            'search_query': q,
            'popular_tags': Document.popular_tags(),
        })
Example #9
0
def chooser(request):
    Document = get_document_model()

    if permission_policy.user_has_permission(request.user, "add"):
        DocumentForm = get_document_form(Document)
        uploadform = DocumentForm()
    else:
        uploadform = None

    documents = []

    q = None
    is_searching = False
    if "q" in request.GET or "p" in request.GET or "collection_id" in request.GET:
        documents = Document.objects.all()

        collection_id = request.GET.get("collection_id")
        if collection_id:
            documents = documents.filter(collection=collection_id)

        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            q = searchform.cleaned_data["q"]

            documents = documents.search(q)
            is_searching = True
        else:
            documents = documents.order_by("-created_at")
            is_searching = False

        # Pagination
        paginator, documents = paginate(request, documents, per_page=10)

        return render(
            request,
            "wagtaildocs/chooser/results.html",
            {"documents": documents, "query_string": q, "is_searching": is_searching},
        )
    else:
        searchform = SearchForm()

        collections = Collection.objects.all()
        if len(collections) < 2:
            collections = None

        documents = Document.objects.order_by("-created_at")
        paginator, documents = paginate(request, documents, per_page=10)

    return render_modal_workflow(
        request,
        "wagtaildocs/chooser/chooser.html",
        "wagtaildocs/chooser/chooser.js",
        {
            "documents": documents,
            "uploadform": uploadform,
            "searchform": searchform,
            "collections": collections,
            "is_searching": False,
        },
    )
Example #10
0
def index(request):
    Report = get_report_model()

    # Get reports (filtered by user permission)
    reports = permission_policy.instances_user_has_any_permission_for(
        request.user, ['change', 'delete'])

    # Ordering
    if 'ordering' in request.GET and request.GET['ordering'] in [
            'title', '-created_at'
    ]:
        ordering = request.GET['ordering']
    else:
        ordering = '-created_at'
    reports = reports.order_by(ordering)

    # Search
    query_string = None
    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search reports"))
        if form.is_valid():
            query_string = form.cleaned_data['q']
            reports = reports.search(query_string)
    else:
        form = SearchForm(placeholder=_("Search reports"))

    # Pagination
    paginator, reports = paginate(request, reports)

    # Create response
    if request.is_ajax():
        return render(
            request, 'wagtailreports/reports/results.html', {
                'ordering':
                ordering,
                'reports':
                reports,
                'query_string':
                query_string,
                'is_searching':
                bool(query_string),
                'user_can_add':
                permission_policy.user_has_permission(request.user, 'add'),
            })
    else:
        return render(
            request, 'wagtailreports/reports/index.html', {
                'ordering':
                ordering,
                'reports':
                reports,
                'query_string':
                query_string,
                'is_searching':
                bool(query_string),
                'search_form':
                form,
                'user_can_add':
                permission_policy.user_has_permission(request.user, 'add'),
            })
Example #11
0
def search(request):
    pages = []
    q = None

    if 'q' in request.GET:
        form = SearchForm(request.GET)
        if form.is_valid():
            q = form.cleaned_data['q']

            pages = Page.objects.all().prefetch_related('content_type').search(q)
            paginator, pages = paginate(request, pages)
    else:
        form = SearchForm()

    if request.is_ajax():
        return render(request, "wagtailadmin/pages/search_results.html", {
            'pages': pages,
            'query_string': q,
            'pagination_query_params': ('q=%s' % q) if q else ''
        })
    else:
        return render(request, "wagtailadmin/pages/search.html", {
            'search_form': form,
            'pages': pages,
            'query_string': q,
            'pagination_query_params': ('q=%s' % q) if q else ''
        })
Example #12
0
def search(request):
    Image = get_image_model()
    images = []
    q = None
    is_searching = False

    if 'q' in request.GET:
        form = SearchForm(request.GET)
        if form.is_valid():
            q = form.cleaned_data['q']

            # page number
            p = request.GET.get("p", 1)
            is_searching = True
            images = Image.search(q, results_per_page=20, page=p)
    else:
        form = SearchForm()

    if request.is_ajax():
        return render(request, "wagtailimages/images/results.html", {
            'images': images,
            'is_searching': is_searching,
            'search_query': q,
        })
    else:
        return render(request, "wagtailimages/images/index.html", {
            'form': form,
            'images': images,
            'is_searching': is_searching,
            'popular_tags': Image.popular_tags(),
            'search_query': q,
        })
Example #13
0
def search(request, parent_page_id=None):
    page_type = request.GET.get('page_type') or 'wagtailcore.page'
    content_type_app_name, content_type_model_name = page_type.split('.')

    try:
        content_type = ContentType.objects.get_by_natural_key(
            content_type_app_name, content_type_model_name)
    except ContentType.DoesNotExist:
        raise Http404
    desired_class = content_type.model_class()

    search_form = SearchForm(request.GET)
    if search_form.is_valid() and search_form.cleaned_data['q']:
        pages = desired_class.objects.exclude(
            depth=1  # never include root
        ).filter(title__icontains=search_form.cleaned_data['q'])[:10]
    else:
        pages = desired_class.objects.none()

    shown_pages = []
    for page in pages:
        page.can_choose = True
        shown_pages.append(page)

    return render(
        request, 'wagtailadmin/chooser/_search_results.html',
        shared_context(request, {
            'searchform': search_form,
            'pages': shown_pages,
        }))
Example #14
0
def search(request, parent_page_id=None):
    # A missing or empty page_type parameter indicates 'all page types' (i.e. descendants of wagtailcore.page)
    page_type_string = request.GET.get('page_type') or 'wagtailcore.page'

    try:
        desired_class = page_model_from_string(page_type_string)
    except (ValueError, LookupError):
        raise Http404

    search_form = SearchForm(request.GET)
    if search_form.is_valid() and search_form.cleaned_data['q']:
        pages = desired_class.objects.exclude(
            depth=1  # never include root
        ).filter(title__icontains=search_form.cleaned_data['q'])[:10]
    else:
        pages = desired_class.objects.none()

    shown_pages = []
    for page in pages:
        page.can_choose = True
        shown_pages.append(page)

    return render(
        request, 'wagtailadmin/chooser/_search_results.html',
        shared_context(request, {
            'searchform': search_form,
            'pages': shown_pages,
            'page_type_string': page_type_string,
        })
    )
Example #15
0
def index(request):
    q = None
    is_searching = False

    model_fields = [f.name for f in User._meta.get_fields()]

    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search users"))
        if form.is_valid():
            q = form.cleaned_data['q']
            is_searching = True
            conditions = Q()

            if 'username' in model_fields:
                conditions |= Q(username__icontains=q)

            if 'first_name' in model_fields:
                conditions |= Q(first_name__icontains=q)

            if 'last_name' in model_fields:
                conditions |= Q(last_name__icontains=q)

            if 'email' in model_fields:
                conditions |= Q(email__icontains=q)

            users = User.objects.filter(conditions)
    else:
        form = SearchForm(placeholder=_("Search users"))

    if not is_searching:
        users = User.objects.all()

    if 'last_name' in model_fields and 'first_name' in model_fields:
        users = users.order_by('last_name', 'first_name')

    if 'ordering' in request.GET:
        ordering = request.GET['ordering']

        if ordering == 'username':
            users = users.order_by(User.USERNAME_FIELD)
    else:
        ordering = 'name'

    paginator, users = paginate(request, users)

    if request.is_ajax():
        return render(request, "wagtailusers/users/results.html", {
            'users': users,
            'is_searching': is_searching,
            'query_string': q,
            'ordering': ordering,
        })
    else:
        return render(request, "wagtailusers/users/index.html", {
            'search_form': form,
            'users': users,
            'is_searching': is_searching,
            'ordering': ordering,
            'query_string': q,
        })
Example #16
0
def chooser(request):
    if request.user.has_perm('wagtaildocs.add_document'):
        uploadform = DocumentForm()
    else:
        uploadform = None

    documents = []

    q = None
    is_searching = False
    if 'q' in request.GET or 'p' in request.GET:
        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            q = searchform.cleaned_data['q']

            documents = Document.objects.search(q)
            is_searching = True
        else:
            documents = Document.objects.order_by('-created_at')
            is_searching = False

        # Pagination
        p = request.GET.get("p", 1)
        paginator = Paginator(documents, 10)

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

        return render(
            request, "wagtaildocs/chooser/results.html", {
                'documents': documents,
                'query_string': q,
                'is_searching': is_searching,
            })
    else:
        searchform = SearchForm()

        documents = Document.objects.order_by('-created_at')
        p = request.GET.get("p", 1)
        paginator = Paginator(documents, 10)

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

    return render_modal_workflow(
        request, 'wagtaildocs/chooser/chooser.html',
        'wagtaildocs/chooser/chooser.js', {
            'documents': documents,
            'uploadform': uploadform,
            'searchform': searchform,
            'is_searching': False,
        })
Example #17
0
def search(request):
    pages = []
    q = None

    if 'q' in request.GET:
        form = SearchForm(request.GET)
        if form.is_valid():
            q = form.cleaned_data['q']

            pages = Page.objects.all().prefetch_related('content_type').search(
                q)
            paginator, pages = paginate(request, pages)
    else:
        form = SearchForm()

    if request.is_ajax():
        return render(
            request, "wagtailadmin/pages/search_results.html", {
                'pages': pages,
                'query_string': q,
                'pagination_query_params': ('q=%s' % q) if q else ''
            })
    else:
        return render(
            request, "wagtailadmin/pages/search.html", {
                'search_form': form,
                'pages': pages,
                'query_string': q,
                'pagination_query_params': ('q=%s' % q) if q else ''
            })
def chooser(request):
    Document = get_document_model()

    if permission_policy.user_has_permission(request.user, 'add'):
        DocumentForm = get_document_form(Document)
        uploadform = DocumentForm(user=request.user)
    else:
        uploadform = None

    documents = Document.objects.all()

    # allow hooks to modify the queryset
    for hook in hooks.get_hooks('construct_document_chooser_queryset'):
        documents = hook(documents, request)

    q = None
    if 'q' in request.GET or 'p' in request.GET or 'collection_id' in request.GET:

        collection_id = request.GET.get('collection_id')
        if collection_id:
            documents = documents.filter(collection=collection_id)

        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            q = searchform.cleaned_data['q']

            documents = documents.search(q)
            is_searching = True
        else:
            documents = documents.order_by('-created_at')
            is_searching = False

        # Pagination
        paginator, documents = paginate(request, documents, per_page=10)

        return render(
            request, "wagtaildocs/chooser/results.html", {
                'documents': documents,
                'query_string': q,
                'is_searching': is_searching,
            })
    else:
        searchform = SearchForm()

        collections = Collection.objects.all()
        if len(collections) < 2:
            collections = None

        documents = documents.order_by('-created_at')
        paginator, documents = paginate(request, documents, per_page=10)

        return render_modal_workflow(
            request, 'wagtaildocs/chooser/chooser.html',
            'wagtaildocs/chooser/chooser.js', {
                'documents': documents,
                'uploadform': uploadform,
                'searchform': searchform,
                'collections': collections,
                'is_searching': False,
            })
Example #19
0
def index(request):
    submissions = Submission.objects.all()

    # Ordering
    if request.GET.get('ordering') in ['title', '-submitted_at']:
        ordering = request.GET['ordering']
    else:
        ordering = '-submitted_at'
    submissions = submissions.order_by(ordering)

    # Search
    query_string = None
    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search submissions"))
        if form.is_valid():
            query_string = form.cleaned_data['q']
            submissions = submissions.search(query_string)
    else:
        form = SearchForm(placeholder=_("Search submissions"))

    # Pagination
    paginator, submissions = paginate(request, submissions)

    return render(request, 'submissions/admin/index.html', {
        'submissions': submissions,
        'ordering': ordering,
        'query_string': query_string,
        'is_searching': bool(query_string),
        'search_form': form,
    })
def chooser(request, get_results=False):
    # Get most popular queries
    queries = models.Query.get_most_popular()

    # If searching, filter results by query string
    query_string = None
    if 'q' in request.GET:
        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            query_string = searchform.cleaned_data['q']
            queries = queries.filter(query_string__icontains=normalise_query_string(query_string))
    else:
        searchform = SearchForm()

    paginator, queries = paginate(request, queries, per_page=10)

    # Render
    if get_results:
        return render(request, "wagtailsearch/queries/chooser/results.html", {
            'queries': queries,
        })
    else:
        return render_modal_workflow(
            request, 'wagtailsearch/queries/chooser/chooser.html', 'wagtailsearch/queries/chooser/chooser.js', {
                'queries': queries,
                'searchform': searchform,
            }
        )
Example #21
0
def search(request, parent_page_id=None):
    # A missing or empty page_type parameter indicates 'all page types' (i.e. descendants of wagtailcore.page)
    page_type_string = request.GET.get('page_type') or 'wagtailcore.page'

    try:
        desired_classes = page_models_from_string(page_type_string)
    except (ValueError, LookupError):
        raise Http404

    search_form = SearchForm(request.GET)
    if search_form.is_valid() and search_form.cleaned_data['q']:
        pages = Page.objects.exclude(
            depth=1  # never include root
        )
        pages = filter_page_type(pages, desired_classes)
        pages = pages.search(search_form.cleaned_data['q'], fields=['title'])
    else:
        pages = Page.objects.none()

    paginator, pages = paginate(request, pages, per_page=25)

    for page in pages:
        page.can_choose = True

    return render(
        request, 'wagtailadmin/chooser/_search_results.html',
        shared_context(request, {
            'searchform': search_form,
            'pages': pages,
            'page_type_string': page_type_string,
        })
    )
Example #22
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,
        })
Example #23
0
def browse(request, parent_page_id=None):
    page_type = request.GET.get('page_type') or 'wagtailcore.page'
    content_type_app_name, content_type_model_name = page_type.split('.')

    is_searching = False

    try:
        content_type = ContentType.objects.get_by_natural_key(content_type_app_name, content_type_model_name)
    except ContentType.DoesNotExist:
        raise Http404
    desired_class = content_type.model_class()

    if 'q' in request.GET:
        search_form = SearchForm(request.GET)
        if search_form.is_valid() and search_form.cleaned_data['q']:
            pages = desired_class.objects.exclude(
                depth=1  # never include root
            ).filter(title__icontains=search_form.cleaned_data['q'])[:10]
            is_searching = True

    if not is_searching:
        if parent_page_id:
            parent_page = get_object_or_404(Page, id=parent_page_id)
        else:
            parent_page = Page.get_first_root_node()

        parent_page.can_choose = issubclass(parent_page.specific_class, desired_class)
        search_form = SearchForm()
        pages = parent_page.get_children()

    # restrict the page listing to just those pages that:
    # - are of the given content type (taking into account class inheritance)
    # - or can be navigated into (i.e. have children)

    shown_pages = []
    for page in pages:
        page.can_choose = issubclass(page.specific_class, desired_class)
        page.can_descend = page.get_children_count()

        if page.can_choose or page.can_descend:
            shown_pages.append(page)

    if is_searching:
        return render(request, 'wagtailadmin/chooser/_search_results.html', {
            'querystring': get_querystring(request),
            'searchform': search_form,
            'pages': pages,
            'is_searching': is_searching
        })

    return render_modal_workflow(request, 'wagtailadmin/chooser/browse.html', 'wagtailadmin/chooser/browse.js', {
        'allow_external_link': request.GET.get('allow_external_link'),
        'allow_email_link': request.GET.get('allow_email_link'),
        'querystring': get_querystring(request),
        'parent_page': parent_page,
        'pages': shown_pages,
        'search_form': search_form,
        'is_searching': False
    })
Example #24
0
def chooser(request):
    Image = get_image_model()

    if permission_policy.user_has_permission(request.user, 'add'):
        ImageForm = get_image_form(Image)
        uploadform = ImageForm(user=request.user)
    else:
        uploadform = None

    images = Image.objects.order_by('-created_at')

    # allow hooks to modify the queryset
    for hook in hooks.get_hooks('construct_image_chooser_queryset'):
        images = hook(images, request)

    if ('q' in request.GET or 'p' in request.GET or 'tag' in request.GET
            or 'collection_id' in request.GET):
        # this request is triggered from search, pagination or 'popular tags';
        # we will just render the results.html fragment
        collection_id = request.GET.get('collection_id')
        if collection_id:
            images = images.filter(collection=collection_id)

        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            q = searchform.cleaned_data['q']

            images = images.search(q)
            is_searching = True
        else:
            is_searching = False
            q = None

            tag_name = request.GET.get('tag')
            if tag_name:
                images = images.filter(tags__name=tag_name)

        # Pagination
        paginator, images = paginate(request, images, per_page=12)

        return render(
            request, "wagtailimages/chooser/results.html", {
                'images': images,
                'is_searching': is_searching,
                'query_string': q,
                'will_select_format': request.GET.get('select_format')
            })
    else:
        paginator, images = paginate(request, images, per_page=12)

        context = get_chooser_context(request)
        context.update({
            'images': images,
            'uploadform': uploadform,
        })
        return render_modal_workflow(request,
                                     'wagtailimages/chooser/chooser.html',
                                     'wagtailimages/chooser/chooser.js',
                                     context)
Example #25
0
def index(request):
    # Get documents
    documents = Document.objects.all()

    # Ordering
    if 'ordering' in request.GET and request.GET['ordering'] in ['title', '-created_at']:
        ordering = request.GET['ordering']
    else:
        ordering = '-created_at'
    documents = documents.order_by(ordering)

    # Permissions
    if not request.user.has_perm('wagtaildocs.change_document'):
        # restrict to the user's own documents
        documents = documents.filter(uploaded_by_user=request.user)

    # Search
    query_string = None
    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search documents"))
        if form.is_valid():
            query_string = form.cleaned_data['q']
            if not request.user.has_perm('wagtaildocs.change_document'):
                # restrict to the user's own documents
                documents = Document.search(query_string, filters={'uploaded_by_user_id': request.user.id})
            else:
                documents = Document.search(query_string)
    else:
        form = SearchForm(placeholder=_("Search documents"))

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

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

    # Create response
    if request.is_ajax():
        return render(request, 'wagtaildocs/documents/results.html', {
            'ordering': ordering,
            'documents': documents,
            'query_string': query_string,
            'is_searching': bool(query_string),
        })
    else:
        return render(request, 'wagtaildocs/documents/index.html', {
            'ordering': ordering,
            'documents': documents,
            'query_string': query_string,
            'is_searching': bool(query_string),

            'search_form': form,
            'popular_tags': Document.popular_tags(),
        })
Example #26
0
def index(request):
    # Get documents
    documents = Document.objects.all()

    # Ordering
    if 'ordering' in request.GET and request.GET['ordering'] in ['title', '-created_at']:
        ordering = request.GET['ordering']
    else:
        ordering = '-created_at'
    documents = documents.order_by(ordering)

    # Permissions
    if not request.user.has_perm('wagtaildocs.change_document'):
        # restrict to the user's own documents
        documents = documents.filter(uploaded_by_user=request.user)

    # Search
    query_string = None
    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder_suffix='documents')
        if form.is_valid():
            query_string = form.cleaned_data['q']
            if not request.user.has_perm('wagtaildocs.change_document'):
                # restrict to the user's own documents
                documents = Document.search(query_string, filters={'uploaded_by_user_id': request.user.id})
            else:
                documents = Document.search(query_string)
    else:
        form = SearchForm(placeholder_suffix='documents')

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

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

    # Create response
    if request.is_ajax():
        return render(request, 'wagtaildocs/documents/results.html', {
            'ordering': ordering,
            'documents': documents,
            'query_string': query_string,
            'is_searching': bool(query_string),
        })
    else:
        return render(request, 'wagtaildocs/documents/index.html', {
            'ordering': ordering,
            'documents': documents,
            'query_string': query_string,
            'is_searching': bool(query_string),

            'search_form': form,
            'popular_tags': Document.popular_tags(),
        })
Example #27
0
def index(request):
    Image = get_image_model()

    q = None
    p = request.GET.get("p", 1)
    is_searching = False

    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder_suffix="images")
        if form.is_valid():
            q = form.cleaned_data['q']

            is_searching = True
            if not request.user.has_perm('wagtailimages.change_image'):
                # restrict to the user's own images
                images = Image.search(
                    q,
                    results_per_page=20,
                    page=p,
                    filters={'uploaded_by_user_id': request.user.id})
            else:
                images = Image.search(q, results_per_page=20, page=p)
        else:
            images = Image.objects.order_by('-created_at')
            if not request.user.has_perm('wagtailimages.change_image'):
                # restrict to the user's own images
                images = images.filter(uploaded_by_user=request.user)
    else:
        images = Image.objects.order_by('-created_at')
        if not request.user.has_perm('wagtailimages.change_image'):
            # restrict to the user's own images
            images = images.filter(uploaded_by_user=request.user)
        form = SearchForm(placeholder_suffix="images")

    if not is_searching:
        paginator = Paginator(images, 20)

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

    if request.is_ajax():
        return render(request, "wagtailimages/images/results.html", {
            'images': images,
            'is_searching': is_searching,
            'search_query': q,
        })
    else:
        return render(
            request, "wagtailimages/images/index.html", {
                'search_form': form,
                'images': images,
                'is_searching': is_searching,
                'popular_tags': Image.popular_tags(),
                'search_query': q,
            })
Example #28
0
def index(request):
    p = request.GET.get("p", 1)
    q = None
    is_searching = False

    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search redirects"))
        if form.is_valid():
            q = form.cleaned_data['q']
            is_searching = True

            redirects = models.Redirect.get_for_site(
                site=request.site).prefetch_related('redirect_page').filter(
                    old_path__icontains=q)

    if not is_searching:
        # Get redirects
        redirects = models.Redirect.get_for_site(
            site=request.site).prefetch_related('redirect_page')
        form = SearchForm(placeholder=_("Search redirects"))

    if 'ordering' in request.GET:
        ordering = request.GET['ordering']

        if ordering in [
                'old_path',
        ]:
            if ordering != 'old_path':
                redirects = redirects.order_by(ordering)
    else:
        ordering = 'old_path'

    paginator = Paginator(redirects, 20)

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

    # Render template
    if request.is_ajax():
        return render(
            request, "wagtailredirects/results.html", {
                'ordering': ordering,
                'redirects': redirects,
                'is_searching': is_searching,
                'search_query': q,
            })
    else:
        return render(
            request, "wagtailredirects/index.html", {
                'ordering': ordering,
                'search_form': form,
                'redirects': redirects,
                'is_searching': is_searching,
            })
def index(request):
    EmbedVideo = get_embed_video_model()

    # Get embed videos
    embed_videos = EmbedVideo.objects.order_by('-created_at')

    # Permissions
    if not request.user.has_perm('wagtail_embed_videos.change_embedvideo'):
        # restrict to the user's own embed videos
        embed_videos = embed_videos.filter(uploaded_by_user=request.user)

    # Search
    query_string = None
    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search videos"))
        if form.is_valid():
            query_string = form.cleaned_data['q']

            if not request.user.has_perm(
                    'wagtail_embed_videos.change_embedvideo'):
                # restrict to the user's own embed videos
                embed_videos = EmbedVideo.objects.search(
                    query_string,
                    filters={'uploaded_by_user_id': request.user.id})
            else:
                embed_videos = EmbedVideo.objects.search(query_string)
    else:
        form = SearchForm(placeholder=_("Search videos"))

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

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

    # Create response
    if request.is_ajax():
        return render(
            request, 'wagtail_embed_videos/embed_videos/results.html', {
                'embed_videos': embed_videos,
                'query_string': query_string,
                'is_searching': bool(query_string),
            })
    else:
        return render(
            request, 'wagtail_embed_videos/embed_videos/index.html', {
                'embed_videos': embed_videos,
                'query_string': query_string,
                'is_searching': bool(query_string),
                'search_form': form,
                'popular_tags': popular_tags_for_model(EmbedVideo),
            })
Example #30
0
def index(request):
    Image = get_image_model()

    # Get images (filtered by user permission)
    images = permission_policy.instances_user_has_any_permission_for(request.user, ["change", "delete"]).order_by(
        "-created_at"
    )

    # Search
    query_string = None
    if "q" in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search images"))
        if form.is_valid():
            query_string = form.cleaned_data["q"]

            images = images.search(query_string)
    else:
        form = SearchForm(placeholder=_("Search images"))

    # Filter by collection
    current_collection = None
    collection_id = request.GET.get("collection_id")
    if collection_id:
        try:
            current_collection = Collection.objects.get(id=collection_id)
            images = images.filter(collection=current_collection)
        except (ValueError, Collection.DoesNotExist):
            pass

    paginator, images = paginate(request, images)

    collections = permission_policy.collections_user_has_any_permission_for(request.user, ["add", "change"])
    if len(collections) < 2:
        collections = None

    # Create response
    if request.is_ajax():
        return render(
            request,
            "wagtailimages/images/results.html",
            {"images": images, "query_string": query_string, "is_searching": bool(query_string)},
        )
    else:
        return render(
            request,
            "wagtailimages/images/index.html",
            {
                "images": images,
                "query_string": query_string,
                "is_searching": bool(query_string),
                "search_form": form,
                "popular_tags": Image.popular_tags(),
                "collections": collections,
                "current_collection": current_collection,
                "user_can_add": permission_policy.user_has_permission(request.user, "add"),
            },
        )
Example #31
0
def index(request):
    q = None
    p = request.GET.get("p", 1)
    is_searching = False

    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search users"))
        if form.is_valid():
            q = form.cleaned_data['q']

            is_searching = True
            users = User.objects.filter(
                Q(username__icontains=q) | Q(first_name__icontains=q)
                | Q(last_name__icontains=q) | Q(email__icontains=q))
    else:
        form = SearchForm(placeholder=_("Search users"))

    if not is_searching:
        users = User.objects

    users = users.order_by('last_name', 'first_name')

    if 'ordering' in request.GET:
        ordering = request.GET['ordering']

        if ordering in ['name', 'username']:
            if ordering != 'name':
                users = users.order_by(ordering)
    else:
        ordering = 'name'

    paginator = Paginator(users, 20)

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

    if request.is_ajax():
        return render(
            request, "wagtailusers/users/results.html", {
                'users': users,
                'is_searching': is_searching,
                'query_string': q,
                'ordering': ordering,
            })
    else:
        return render(
            request, "wagtailusers/users/index.html", {
                'search_form': form,
                'users': users,
                'is_searching': is_searching,
                'ordering': ordering,
                'query_string': q,
            })
Example #32
0
def index(request):
    q = None
    is_searching = False

    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search users"))
        if form.is_valid():
            q = form.cleaned_data['q']

            is_searching = True

            if User.USERNAME_FIELD == 'username':
                users = User.objects.filter(
                    Q(username__icontains=q) |
                    Q(first_name__icontains=q) |
                    Q(last_name__icontains=q) |
                    Q(email__icontains=q)
                )
            else:
                users = User.objects.filter(
                    Q(first_name__icontains=q) |
                    Q(last_name__icontains=q) |
                    Q(email__icontains=q)
                )
    else:
        form = SearchForm(placeholder=_("Search users"))

    if not is_searching:
        users = User.objects

    users = users.order_by('last_name', 'first_name')

    if 'ordering' in request.GET:
        ordering = request.GET['ordering']

        if ordering == 'username':
            users = users.order_by(ordering)
    else:
        ordering = 'name'

    paginator, users = paginate(request, users)

    if request.is_ajax():
        return render(request, "wagtailusers/users/results.html", {
            'users': users,
            'is_searching': is_searching,
            'query_string': q,
            'ordering': ordering,
        })
    else:
        return render(request, "wagtailusers/users/index.html", {
            'search_form': form,
            'users': users,
            'is_searching': is_searching,
            'ordering': ordering,
            'query_string': q,
        })
Example #33
0
def chooser(request):
    Document = get_document_model()

    if permission_policy.user_has_permission(request.user, 'add'):
        DocumentForm = get_document_form(Document)
        uploadform = DocumentForm(user=request.user)
    else:
        uploadform = None

    documents = Document.objects.all()

    # allow hooks to modify the queryset
    for hook in hooks.get_hooks('construct_document_chooser_queryset'):
        documents = hook(documents, request)

    q = None
    if 'q' in request.GET or 'p' in request.GET or 'collection_id' in request.GET:

        collection_id = request.GET.get('collection_id')
        if collection_id:
            documents = documents.filter(collection=collection_id)

        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            q = searchform.cleaned_data['q']

            documents = documents.search(q)
            is_searching = True
        else:
            documents = documents.order_by('-created_at')
            is_searching = False

        # Pagination
        paginator, documents = paginate(request, documents, per_page=10)

        return render(request, "wagtaildocs/chooser/results.html", {
            'documents': documents,
            'query_string': q,
            'is_searching': is_searching,
        })
    else:
        searchform = SearchForm()

        collections = Collection.objects.all()
        if len(collections) < 2:
            collections = None

        documents = documents.order_by('-created_at')
        paginator, documents = paginate(request, documents, per_page=10)

        return render_modal_workflow(request, 'wagtaildocs/chooser/chooser.html', 'wagtaildocs/chooser/chooser.js', {
            'documents': documents,
            'uploadform': uploadform,
            'searchform': searchform,
            'collections': collections,
            'is_searching': False,
        })
Example #34
0
def chooser(request):
    if request.user.has_perm('wagtaildocs.add_document'):
        uploadform = DocumentForm()
    else:
        uploadform = None

    documents = []

    q = None
    is_searching = False
    if 'q' in request.GET or 'p' in request.GET:
        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            q = searchform.cleaned_data['q']

            documents = Document.objects.search(q)
            is_searching = True
        else:
            documents = Document.objects.order_by('-created_at')
            is_searching = False

        # Pagination
        p = request.GET.get("p", 1)
        paginator = Paginator(documents, 10)

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

        return render(request, "wagtaildocs/chooser/results.html", {
            'documents': documents,
            'query_string': q,
            'is_searching': is_searching,
        })
    else:
        searchform = SearchForm()

        documents = Document.objects.order_by('-created_at')
        p = request.GET.get("p", 1)
        paginator = Paginator(documents, 10)

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

    return render_modal_workflow(request, 'wagtaildocs/chooser/chooser.html', 'wagtaildocs/chooser/chooser.js', {
        'documents': documents,
        'uploadform': uploadform,
        'searchform': searchform,
        'is_searching': False,
    })
Example #35
0
def index(request):
    Image = get_image_model()

    # Get images
    images = Image.objects.order_by('-created_at')

    # Permissions
    if not request.user.has_perm('wagtailimages.change_image'):
        # restrict to the user's own images
        images = images.filter(uploaded_by_user=request.user)

    # Search
    query_string = None
    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search images"))
        if form.is_valid():
            query_string = form.cleaned_data['q']

            is_searching = True
            if not request.user.has_perm('wagtailimages.change_image'):
                # restrict to the user's own images
                images = Image.search(
                    query_string,
                    filters={'uploaded_by_user_id': request.user.id})
            else:
                images = Image.search(query_string)
    else:
        form = SearchForm(placeholder=_("Search images"))

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

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

    # Create response
    if request.is_ajax():
        return render(
            request, 'wagtailimages/images/results.html', {
                'images': images,
                'query_string': query_string,
                'is_searching': bool(query_string),
            })
    else:
        return render(
            request, 'wagtailimages/images/index.html', {
                'images': images,
                'query_string': query_string,
                'is_searching': bool(query_string),
                'search_form': form,
                'popular_tags': Image.popular_tags(),
            })
Example #36
0
def index(request):
    q = None
    p = request.GET.get("p", 1)
    is_searching = False

    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search users"))
        if form.is_valid():
            q = form.cleaned_data['q']

            is_searching = True

            if User.USERNAME_FIELD == 'username':
                users = User.objects.filter(Q(username__icontains=q) | Q(first_name__icontains=q) | Q(last_name__icontains=q) | Q(email__icontains=q))
            else:
                users = User.objects.filter(Q(first_name__icontains=q) | Q(last_name__icontains=q) | Q(email__icontains=q))
    else:
        form = SearchForm(placeholder=_("Search users"))

    if not is_searching:
        users = User.objects

    users = users.order_by('last_name', 'first_name')

    if 'ordering' in request.GET:
        ordering = request.GET['ordering']

        if ordering in ['name', 'username']:
            if ordering != 'name':
                users = users.order_by(ordering)
    else:
        ordering = 'name'

    paginator = Paginator(users, 20)

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

    if request.is_ajax():
        return render(request, "wagtailusers/users/results.html", {
            'users': users,
            'is_searching': is_searching,
            'query_string': q,
            'ordering': ordering,
        })
    else:
        return render(request, "wagtailusers/users/index.html", {
            'search_form': form,
            'users': users,
            'is_searching': is_searching,
            'ordering': ordering,
            'query_string': q,
        })
Example #37
0
def index(request):
    Image = get_image_model()

    # Get images
    images = Image.objects.order_by("-created_at")

    # Permissions
    if not request.user.has_perm("wagtailimages.change_image"):
        # restrict to the user's own images
        images = images.filter(uploaded_by_user=request.user)

    # Search
    query_string = None
    if "q" in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search images"))
        if form.is_valid():
            query_string = form.cleaned_data["q"]

            is_searching = True
            if not request.user.has_perm("wagtailimages.change_image"):
                # restrict to the user's own images
                images = Image.search(query_string, filters={"uploaded_by_user_id": request.user.id})
            else:
                images = Image.search(query_string)
    else:
        form = SearchForm(placeholder=_("Search images"))

    # Pagination
    p = request.GET.get("p", 1)
    paginator = Paginator(images, 20)

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

    # Create response
    if request.is_ajax():
        return render(
            request,
            "wagtailimages/images/results.html",
            {"images": images, "query_string": query_string, "is_searching": bool(query_string)},
        )
    else:
        return render(
            request,
            "wagtailimages/images/index.html",
            {
                "images": images,
                "query_string": query_string,
                "is_searching": bool(query_string),
                "search_form": form,
                "popular_tags": Image.popular_tags(),
            },
        )
Example #38
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,
        })
Example #39
0
def index(request):
    Image = get_image_model()

    # Get images (filtered by user permission)
    images = permission_policy.instances_user_has_any_permission_for(
        request.user, ['change', 'delete']
    ).order_by('-created_at')

    # Search
    query_string = None
    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search images"))
        if form.is_valid():
            query_string = form.cleaned_data['q']

            images = images.search(query_string)
    else:
        form = SearchForm(placeholder=_("Search images"))

    # Filter by collection
    current_collection = None
    collection_id = request.GET.get('collection_id')
    if collection_id:
        try:
            current_collection = Collection.objects.get(id=collection_id)
            images = images.filter(collection=current_collection)
        except (ValueError, Collection.DoesNotExist):
            pass

    paginator, images = paginate(request, images)

    collections = permission_policy.collections_user_has_any_permission_for(
        request.user, ['add', 'change']
    )
    if len(collections) < 2:
        collections = None

    # Create response
    if request.is_ajax():
        return render(request, 'wagtailimages/images/results.html', {
            'images': images,
            'query_string': query_string,
            'is_searching': bool(query_string),
        })
    else:
        return render(request, 'wagtailimages/images/index.html', {
            'images': images,
            'query_string': query_string,
            'is_searching': bool(query_string),

            'search_form': form,
            'popular_tags': Image.popular_tags(),
            'collections': collections,
            'current_collection': current_collection,
            'user_can_add': permission_policy.user_has_permission(request.user, 'add'),
        })
Example #40
0
def chooser(request):
    VideoForm = get_video_form(Video)
    uploadform = VideoForm()

    videos = Video.objects.order_by('-created_at')

    q = None
    if ('q' in request.GET or 'p' in request.GET or 'tag' in request.GET
            or 'collection_id' in request.GET):
        # this request is triggered from search, pagination or 'popular tags';
        # we will just render the results.html fragment
        collection_id = request.GET.get('collection_id')
        if collection_id:
            videos = videos.filter(collection=collection_id)

        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            q = searchform.cleaned_data['q']

            videos = videos.search(q)
            is_searching = True
        else:
            is_searching = False

            tag_name = request.GET.get('tag')
            if tag_name:
                videos = videos.filter(tags__name=tag_name)

        # Pagination
        paginator, videos = paginate(request, videos, per_page=12)

        return render(request, "wagtailvideos/chooser/results.html", {
            'videos': videos,
            'is_searching': is_searching,
            'query_string': q,
        })
    else:
        searchform = SearchForm()

        collections = Collection.objects.all()
        if len(collections) < 2:
            collections = None

        paginator, videos = paginate(request, videos, per_page=12)

    return render_modal_workflow(
        request, 'wagtailvideos/chooser/chooser.html',
        'wagtailvideos/chooser/chooser.js', {
            'videos': videos,
            'uploadform': uploadform,
            'searchform': searchform,
            'is_searching': False,
            'query_string': q,
            'popular_tags': popular_tags_for_model(Video),
            'collections': collections,
        })
Example #41
0
def choose(request, app_label, model_name):
    model = get_snippet_model_from_url_params(app_label, model_name)

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

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

    # 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",
            {
                "model_opts": model._meta,
                "items": paginated_items,
                "query_string": search_query,
                "is_searching": is_searching,
            },
        )

    return render_modal_workflow(
        request,
        "wagtailsnippets/chooser/choose.html",
        "wagtailsnippets/chooser/choose.js",
        {
            "model_opts": model._meta,
            "items": paginated_items,
            "is_searchable": is_searchable,
            "search_form": search_form,
            "query_string": search_query,
            "is_searching": is_searching,
        },
    )
Example #42
0
    def get_context_data(self, **kwargs):
        """
        Returns context data dictionary.

        :rtype: dict.
        """
        # Initialize common variables.
        createform          = None
        queryset            = kwargs.pop('object_list', self.object_list)
        context_object_name = self.get_context_object_name(queryset)

        # Are we searching?
        is_searching    = False
        query_string    = None
        plural          = unicode(self.model._meta.verbose_name_plural)
        placeholder     = _(u'Search {0}'.format(plural))
        if 'q' in self.request.GET:
            searchform = SearchForm(self.request.GET, placeholder=placeholder)
            if searchform.is_valid():
                query_string    = searchform.cleaned_data['q']
                is_searching    = True
                queryset        = self.model.search(
                    query_string,
                    filters=self.get_search_filters()
                )
        else:
            searchform  = SearchForm(placeholder=placeholder)

            # Add create form if user has "add" permission.
            permission_string = get_model_permission('add', self.model)
            if self.request.user.has_perm(permission_string):
                createform = self.get_form_class()

        # Paginate the results.
        page_size   = self.get_paginate_by(queryset)
        page        = None
        if page_size:
            # Get paginated results.
            paginator, page, queryset, is_paginated = self.paginate_queryset(
                queryset,
                page_size
            )

        kwargs.update({
            'object_list':  queryset,
            'createform':   createform,
            'searchform':   searchform,
            'is_searching': is_searching,
            'page':         page,
        })

        if context_object_name is not None:
            kwargs.update({context_object_name: queryset})

        return kwargs
Example #43
0
def chooser(request):
    Image = get_image_model()

    if permission_policy.user_has_permission(request.user, 'add'):
        ImageForm = get_image_form(Image)
        uploadform = ImageForm()
    else:
        uploadform = None

    images = Image.objects.order_by('-created_at')

    q = None
    if 'q' in request.GET or 'p' in request.GET or 'tag' in request.GET:
        # this request is triggered from search, pagination or 'popular tags';
        # we will just render the results.html fragment

        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            q = searchform.cleaned_data['q']

            images = images.search(q)
            is_searching = True
        else:
            is_searching = False

            tag_name = request.GET.get('tag')
            if tag_name:
                images = images.filter(tags__name=tag_name)

        # Pagination
        paginator, images = paginate(request, images, per_page=12)

        return render(
            request, "wagtailimages/chooser/results.html", {
                'images': images,
                'is_searching': is_searching,
                'query_string': q,
                'will_select_format': request.GET.get('select_format')
            })
    else:
        searchform = SearchForm()

        paginator, images = paginate(request, images, per_page=12)

    return render_modal_workflow(
        request, 'wagtailimages/chooser/chooser.html',
        'wagtailimages/chooser/chooser.js', {
            'images': images,
            'uploadform': uploadform,
            'searchform': searchform,
            'is_searching': False,
            'query_string': q,
            'will_select_format': request.GET.get('select_format'),
            'popular_tags': Image.popular_tags(),
        })
def choose(request, app_label, model_name):
    model = get_snippet_model_from_url_params(app_label, model_name)

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

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

    # 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", {
                'model_opts': model._meta,
                'items': paginated_items,
                'query_string': search_query,
                'is_searching': is_searching,
            })

    return render_modal_workflow(
        request, 'wagtailsnippets/chooser/choose.html',
        'wagtailsnippets/chooser/choose.js', {
            'model_opts': model._meta,
            'items': paginated_items,
            'is_searchable': is_searchable,
            'search_form': search_form,
            'query_string': search_query,
            'is_searching': is_searching,
        })
Example #45
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,
    })
Example #46
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,
    })
Example #47
0
def index(request):
    q = None
    is_searching = False

    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search users"))
        if form.is_valid():
            q = form.cleaned_data['q']

            is_searching = True

            if User.USERNAME_FIELD == 'username':
                users = User.objects.filter(
                    Q(username__icontains=q) | Q(first_name__icontains=q)
                    | Q(last_name__icontains=q) | Q(email__icontains=q))
            else:
                users = User.objects.filter(
                    Q(first_name__icontains=q) | Q(last_name__icontains=q)
                    | Q(email__icontains=q))
    else:
        form = SearchForm(placeholder=_("Search users"))

    if not is_searching:
        users = User.objects

    users = users.order_by('last_name', 'first_name')

    if 'ordering' in request.GET:
        ordering = request.GET['ordering']

        if ordering == 'username':
            users = users.order_by(ordering)
    else:
        ordering = 'name'

    paginator, users = paginate(request, users)

    if request.is_ajax():
        return render(
            request, "wagtailusers/users/results.html", {
                'users': users,
                'is_searching': is_searching,
                'query_string': q,
                'ordering': ordering,
            })
    else:
        return render(
            request, "wagtailusers/users/index.html", {
                'search_form': form,
                'users': users,
                'is_searching': is_searching,
                'ordering': ordering,
                'query_string': q,
            })
Example #48
0
def index(request):
    Image = get_image_model()

    q = None
    p = request.GET.get("p", 1)
    is_searching = False

    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder_suffix="images")
        if form.is_valid():
            q = form.cleaned_data['q']

            is_searching = True
            if not request.user.has_perm('wagtailimages.change_image'):
                # restrict to the user's own images
                images = Image.search(q, results_per_page=20, page=p, filters={'uploaded_by_user_id': request.user.id})
            else:
                images = Image.search(q, results_per_page=20, page=p)
        else:
            images = Image.objects.order_by('-created_at')
            if not request.user.has_perm('wagtailimages.change_image'):
                # restrict to the user's own images
                images = images.filter(uploaded_by_user=request.user)
    else:
        images = Image.objects.order_by('-created_at')
        if not request.user.has_perm('wagtailimages.change_image'):
            # restrict to the user's own images
            images = images.filter(uploaded_by_user=request.user)
        form = SearchForm(placeholder_suffix="images")

    if not is_searching:
        paginator = Paginator(images, 20)

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

    if request.is_ajax():
        return render(request, "wagtailimages/images/results.html", {
            'images': images,
            'is_searching': is_searching,
            'search_query': q,
        })
    else:
        return render(request, "wagtailimages/images/index.html", {
            'search_form': form,
            'images': images,
            'is_searching': is_searching,
            'popular_tags': Image.popular_tags(),
            'search_query': q,
        })
Example #49
0
def index(request):
    Image = get_image_model()

    # Get images
    images = Image.objects.order_by('-created_at')

    # Permissions
    if not request.user.has_perm('wagtailimages.change_image'):
        # restrict to the user's own images
        images = images.filter(uploaded_by_user=request.user)

    # Search
    query_string = None
    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder_suffix="images")
        if form.is_valid():
            query_string = form.cleaned_data['q']

            is_searching = True
            if not request.user.has_perm('wagtailimages.change_image'):
                # restrict to the user's own images
                images = Image.search(query_string, filters={'uploaded_by_user_id': request.user.id})
            else:
                images = Image.search(query_string)
    else:
        form = SearchForm(placeholder_suffix="images")

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

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

    # Create response
    if request.is_ajax():
        return render(request, 'wagtailimages/images/results.html', {
            'images': images,
            'query_string': query_string,
            'is_searching': bool(query_string),
        })
    else:
        return render(request, 'wagtailimages/images/index.html', {
            'images': images,
            'query_string': query_string,
            'is_searching': bool(query_string),

            'search_form': form,
            'popular_tags': Image.popular_tags(),
        })
Example #50
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,
        }
    )
Example #51
0
def index(request):
    q = None
    p = request.GET.get("p", 1)
    is_searching = False

    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search groups"))
        if form.is_valid():
            q = form.cleaned_data['q']

            is_searching = True
            groups = Group.objects.filter(name__icontains=q)
    else:
        form = SearchForm(placeholder=_("Search groups"))

    if not is_searching:
        groups = Group.objects

    groups = groups.order_by('name')

    if 'ordering' in request.GET:
        ordering = request.GET['ordering']

        if ordering in ['name', 'username']:
            if ordering != 'name':
                groups = groups.order_by(ordering)
    else:
        ordering = 'name'

    paginator = Paginator(groups, 20)

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

    if request.is_ajax():
        return render(request, "wagtailusers/groups/results.html", {
            'groups': groups,
            'is_searching': is_searching,
            'query_string': q,
            'ordering': ordering,
        })
    else:
        return render(request, "wagtailusers/groups/index.html", {
            'search_form': form,
            'groups': groups,
            'is_searching': is_searching,
            'ordering': ordering,
            'query_string': q,
        })
Example #52
0
def index(request):
    q = None
    is_searching = False

    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder='Search groups')
        if form.is_valid():
            q = form.cleaned_data['q']

            is_searching = True
            groups = Group.objects.filter(name__icontains=q)
    else:
        form = SearchForm(placeholder='Search groups')

    if not is_searching:
        groups = Group.objects.all()

    groups = groups.order_by('name')

    if 'ordering' in request.GET:
        ordering = request.GET['ordering']

        if ordering == 'name':
            groups = groups.order_by('name')
        elif ordering == '-name':
            groups = groups.order_by('-name')
    else:
        ordering = 'name'

    if not request.user.is_superuser:
        # Non-superusers should see only the Groups associated with the current Site.
        groups = groups.filter(name__startswith=request.site.hostname)

    unused, groups = paginate(request, groups)

    if request.is_ajax():
        return TemplateResponse(
            request, "wagtail_patches/groups/results.tpl", {
                'groups': groups,
                'is_searching': is_searching,
                'query_string': q,
                'ordering': ordering,
            })
    else:
        return TemplateResponse(
            request, "wagtail_patches/groups/index.html", {
                'search_form': form,
                'groups': groups,
                'is_searching': is_searching,
                'ordering': ordering,
                'query_string': q,
            })
Example #53
0
def index(request):
    Document = get_document_model()

    # Get documents
    documents = Document.objects.all()

    # Ordering
    if 'ordering' in request.GET and request.GET['ordering'] in [
            'title', '-created_at'
    ]:
        ordering = request.GET['ordering']
    else:
        ordering = '-created_at'
    documents = documents.order_by(ordering)

    # Permissions
    if not request.user.has_perm('wagtaildocs.change_document'):
        # restrict to the user's own documents
        documents = documents.filter(uploaded_by_user=request.user)

    # Search
    query_string = None
    if 'q' in request.GET:
        form = SearchForm(request.GET, placeholder=_("Search documents"))
        if form.is_valid():
            query_string = form.cleaned_data['q']
            documents = documents.search(query_string)
    else:
        form = SearchForm(placeholder=_("Search documents"))

    # Pagination
    paginator, documents = paginate(request, documents)

    # Create response
    if request.is_ajax():
        return render(
            request, 'wagtaildocs/documents/results.html', {
                'ordering': ordering,
                'documents': documents,
                'query_string': query_string,
                'is_searching': bool(query_string),
            })
    else:
        return render(
            request, 'wagtaildocs/documents/index.html', {
                'ordering': ordering,
                'documents': documents,
                'query_string': query_string,
                'is_searching': bool(query_string),
                'search_form': form,
                'popular_tags': Document.popular_tags(),
            })
Example #54
0
def chooser(request):
    Image = get_image_model()

    if request.user.has_perm('wagtailimages.add_image'):
        ImageForm = get_image_form(Image)
        uploadform = ImageForm()
    else:
        uploadform = None

    images = Image.objects.order_by('-created_at')

    q = None
    if 'q' in request.GET or 'p' in request.GET or 'tag' in request.GET:
        # this request is triggered from search, pagination or 'popular tags';
        # we will just render the results.html fragment

        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            q = searchform.cleaned_data['q']

            images = images.search(q)
            is_searching = True
        else:
            is_searching = False

            tag_name = request.GET.get('tag')
            if tag_name:
                images = images.filter(tags__name=tag_name)

        # Pagination
        paginator, images = paginate(request, images, per_page=12)

        return render(request, "wagtailimages/chooser/results.html", {
            'images': images,
            'is_searching': is_searching,
            'query_string': q,
            'will_select_format': request.GET.get('select_format')
        })
    else:
        searchform = SearchForm()

        paginator, images = paginate(request, images, per_page=12)

    return render_modal_workflow(request, 'wagtailimages/chooser/chooser.html', 'wagtailimages/chooser/chooser.js', {
        'images': images,
        'uploadform': uploadform,
        'searchform': searchform,
        'is_searching': False,
        'query_string': q,
        'will_select_format': request.GET.get('select_format'),
        'popular_tags': Image.popular_tags(),
    })
Example #55
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,
        },
    )
Example #56
0
    def get_context_data(self, **kwargs):
        """
        Returns context data dictionary.

        :rtype: dict.
        """
        # Initialize common variables.
        createform = None
        queryset = kwargs.pop('object_list', self.object_list)
        context_object_name = self.get_context_object_name(queryset)

        # Are we searching?
        is_searching = False
        query_string = None
        plural = unicode(self.model._meta.verbose_name_plural)
        placeholder = _(u'Search {0}'.format(plural))
        if 'q' in self.request.GET:
            searchform = SearchForm(self.request.GET, placeholder=placeholder)
            if searchform.is_valid():
                query_string = searchform.cleaned_data['q']
                is_searching = True
                queryset = self.model.search(query_string,
                                             filters=self.get_search_filters())
        else:
            searchform = SearchForm(placeholder=placeholder)

            # Add create form if user has "add" permission.
            permission_string = get_model_permission('add', self.model)
            if self.request.user.has_perm(permission_string):
                createform = self.get_form_class()

        # Paginate the results.
        page_size = self.get_paginate_by(queryset)
        page = None
        if page_size:
            # Get paginated results.
            paginator, page, queryset, is_paginated = self.paginate_queryset(
                queryset, page_size)

        kwargs.update({
            'object_list': queryset,
            'createform': createform,
            'searchform': searchform,
            'is_searching': is_searching,
            'page': page,
        })

        if context_object_name is not None:
            kwargs.update({context_object_name: queryset})

        return kwargs
Example #57
0
def chooser(request):
    Media = get_media_model()

    media_files = []

    q = None
    is_searching = False
    if 'q' in request.GET or 'p' in request.GET or 'collection_id' in request.GET:
        media_files = Media.objects.all()

        collection_id = request.GET.get('collection_id')
        if collection_id:
            media_files = media_files.filter(collection=collection_id)

        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            q = searchform.cleaned_data['q']

            media_files = media_files.search(q)
            is_searching = True
        else:
            media_files = media_files.order_by('-created_at')
            is_searching = False

        # Pagination
        paginator, media_files = paginate(request, media_files, per_page=10)

        return render(
            request, "wagtailmedia/chooser/results.html", {
                'media_files': media_files,
                'query_string': q,
                'is_searching': is_searching,
            })
    else:
        searchform = SearchForm()

        collections = Collection.objects.all()
        if len(collections) < 2:
            collections = None

        media_files = Media.objects.order_by('-created_at')
        paginator, media_files = paginate(request, media_files, per_page=10)

    return render_modal_workflow(
        request, 'wagtailmedia/chooser/chooser.html',
        'wagtailmedia/chooser/chooser.js', {
            'media_files': media_files,
            'searchform': searchform,
            'collections': collections,
            'is_searching': False,
        })
Example #58
0
def choose(request):
    # TODO: Ideally this would return the endnotes for the current article.
    items = EndNote.objects.all()

    # Search
    is_searchable = class_is_indexed(EndNote)
    is_searching = False
    search_query = None
    if is_searchable and 'q' in request.GET:
        search_form = SearchForm(request.GET,
                                 placeholder=_("Search End Notes"))

        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 End Notes"))

    # Pagination
    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 paginating or searching, render "results.html"
    if request.GET.get('results', None) == 'true':
        return render(
            request, "content_notes/chooser/results.html", {
                'items': paginated_items,
                'query_string': search_query,
                'is_searching': is_searching,
            })

    return render_modal_workflow(
        request, 'content_notes/chooser/choose.html',
        'content_notes/chooser/choose.js', {
            'items': paginated_items,
            'is_searchable': is_searchable,
            'search_form': search_form,
            'query_string': search_query,
            'is_searching': is_searching,
        })
Example #59
0
def chooser(request):
    Report = get_report_model()

    if permission_policy.user_has_permission(request.user, 'add'):
        ReportForm = get_report_form(Report)
        uploadform = ReportForm(user=request.user)
    else:
        uploadform = None

    reports = Report.objects.all()

    # allow hooks to modify the queryset
    for hook in hooks.get_hooks('construct_report_chooser_queryset'):
        reports = hook(reports, request)

    q = None
    if 'q' in request.GET or 'p' in request.GET:

        searchform = SearchForm(request.GET)
        if searchform.is_valid():
            q = searchform.cleaned_data['q']

            reports = reports.search(q)
            is_searching = True
        else:
            reports = reports.order_by('-created_at')
            is_searching = False

        # Pagination
        paginator, reports = paginate(request, reports, per_page=10)

        return render(request, "wagtailreports/chooser/results.html", {
            'reports': reports,
            'query_string': q,
            'is_searching': is_searching,
        })
    else:
        searchform = SearchForm()

        reports = reports.order_by('-created_at')
        paginator, reports = paginate(request, reports, per_page=10)

        return render_modal_workflow(
            request, 'wagtailreports/chooser/chooser.html',
            'wagtailreports/chooser/chooser.js', {
                'reports': reports,
                'uploadform': uploadform,
                'searchform': searchform,
                'is_searching': False,
            })