#6445 [FIX]: Document chooser’s "Search" tab broken after a failed upload

Open samgans
Coverage Reach
wagtail/admin/views/pages/edit.py wagtail/admin/views/pages/create.py wagtail/admin/views/pages/revisions.py wagtail/admin/views/pages/workflow.py wagtail/admin/views/pages/preview.py wagtail/admin/views/pages/search.py wagtail/admin/views/pages/history.py wagtail/admin/views/pages/moderation.py wagtail/admin/views/pages/move.py wagtail/admin/views/pages/copy.py wagtail/admin/views/pages/listing.py wagtail/admin/views/pages/lock.py wagtail/admin/views/pages/convert_alias.py wagtail/admin/views/pages/unpublish.py wagtail/admin/views/pages/delete.py wagtail/admin/views/pages/ordering.py wagtail/admin/views/pages/usage.py wagtail/admin/views/pages/utils.py wagtail/admin/views/workflows.py wagtail/admin/views/generic.py wagtail/admin/views/account.py wagtail/admin/views/mixins.py wagtail/admin/views/chooser.py wagtail/admin/views/reports.py wagtail/admin/views/collections.py wagtail/admin/views/home.py wagtail/admin/views/page_privacy.py wagtail/admin/views/collection_privacy.py wagtail/admin/views/userbar.py wagtail/admin/views/tags.py wagtail/admin/rich_text/converters/html_to_contentstate.py wagtail/admin/rich_text/converters/editor_html.py wagtail/admin/rich_text/converters/html_ruleset.py wagtail/admin/rich_text/converters/contentstate_models.py wagtail/admin/rich_text/converters/contentstate.py wagtail/admin/rich_text/editors/hallo.py wagtail/admin/rich_text/editors/draftail/__init__.py wagtail/admin/rich_text/editors/draftail/features.py wagtail/admin/rich_text/__init__.py wagtail/admin/edit_handlers.py wagtail/admin/forms/collections.py wagtail/admin/forms/workflows.py wagtail/admin/forms/pages.py wagtail/admin/forms/models.py wagtail/admin/forms/choosers.py wagtail/admin/forms/view_restrictions.py wagtail/admin/forms/tags.py wagtail/admin/forms/auth.py wagtail/admin/forms/search.py wagtail/admin/forms/__init__.py wagtail/admin/compare.py wagtail/admin/widgets/chooser.py wagtail/admin/widgets/datetime.py wagtail/admin/widgets/button.py wagtail/admin/widgets/filtered_select.py wagtail/admin/widgets/tags.py wagtail/admin/widgets/workflows.py wagtail/admin/widgets/button_select.py wagtail/admin/widgets/auto_height_text.py wagtail/admin/widgets/__init__.py wagtail/admin/wagtail_hooks.py wagtail/admin/templatetags/wagtailadmin_tags.py wagtail/admin/templatetags/wagtailuserbar.py wagtail/admin/static_src/wagtailadmin/js/page-editor.js wagtail/admin/static_src/wagtailadmin/js/vendor/urlify.js wagtail/admin/static_src/wagtailadmin/js/vendor/jquery-3.5.1.min.js wagtail/admin/static_src/wagtailadmin/app/wagtailadmin.entry.js wagtail/admin/static_src/wagtailadmin/app/draftail.entry.js wagtail/admin/action_menu.py wagtail/admin/mail.py wagtail/admin/api/serializers.py wagtail/admin/api/views.py wagtail/admin/api/filters.py wagtail/admin/api/urls.py wagtail/admin/filters.py wagtail/admin/auth.py wagtail/admin/menu.py wagtail/admin/userbar.py wagtail/admin/viewsets/model.py wagtail/admin/viewsets/__init__.py wagtail/admin/viewsets/base.py wagtail/admin/checks.py wagtail/admin/search.py wagtail/admin/urls/__init__.py wagtail/admin/urls/pages.py wagtail/admin/urls/collections.py wagtail/admin/urls/workflows.py wagtail/admin/urls/reports.py wagtail/admin/urls/password_reset.py wagtail/admin/site_summary.py wagtail/admin/messages.py wagtail/admin/log_action_registry.py wagtail/admin/staticfiles.py wagtail/admin/navigation.py wagtail/admin/models.py wagtail/admin/signal_handlers.py wagtail/admin/localization.py wagtail/admin/modal_workflow.py wagtail/admin/apps.py wagtail/admin/jinja2tags.py wagtail/admin/datetimepicker.py wagtail/admin/blocks.py wagtail/admin/signals.py wagtail/admin/__init__.py wagtail/contrib/modeladmin/views.py wagtail/contrib/modeladmin/options.py wagtail/contrib/modeladmin/helpers/button.py wagtail/contrib/modeladmin/helpers/permission.py wagtail/contrib/modeladmin/helpers/url.py wagtail/contrib/modeladmin/helpers/search.py wagtail/contrib/modeladmin/helpers/__init__.py wagtail/contrib/modeladmin/templatetags/modeladmin_tags.py wagtail/contrib/modeladmin/menus.py wagtail/contrib/modeladmin/mixins.py wagtail/contrib/modeladmin/forms.py wagtail/contrib/modeladmin/apps.py wagtail/contrib/modeladmin/__init__.py wagtail/contrib/redirects/views.py wagtail/contrib/redirects/base_formats.py wagtail/contrib/redirects/management/commands/import_redirects.py wagtail/contrib/redirects/models.py wagtail/contrib/redirects/forms.py wagtail/contrib/redirects/tmp_storages.py wagtail/contrib/redirects/middleware.py wagtail/contrib/redirects/utils.py wagtail/contrib/redirects/wagtail_hooks.py wagtail/contrib/redirects/apps.py wagtail/contrib/redirects/urls.py wagtail/contrib/redirects/permissions.py wagtail/contrib/redirects/__init__.py wagtail/contrib/postgres_search/backend.py wagtail/contrib/postgres_search/query.py wagtail/contrib/postgres_search/utils.py wagtail/contrib/postgres_search/models.py wagtail/contrib/postgres_search/apps.py wagtail/contrib/postgres_search/__init__.py wagtail/contrib/forms/views.py wagtail/contrib/forms/models.py wagtail/contrib/forms/forms.py wagtail/contrib/forms/utils.py wagtail/contrib/forms/edit_handlers.py wagtail/contrib/forms/wagtail_hooks.py wagtail/contrib/forms/apps.py wagtail/contrib/forms/urls.py wagtail/contrib/forms/__init__.py wagtail/contrib/frontend_cache/tests.py wagtail/contrib/frontend_cache/backends.py wagtail/contrib/frontend_cache/utils.py wagtail/contrib/frontend_cache/signal_handlers.py wagtail/contrib/frontend_cache/apps.py wagtail/contrib/frontend_cache/__init__.py wagtail/contrib/search_promotions/tests.py wagtail/contrib/search_promotions/views.py wagtail/contrib/search_promotions/forms.py wagtail/contrib/search_promotions/wagtail_hooks.py wagtail/contrib/search_promotions/models.py wagtail/contrib/search_promotions/templatetags/wagtailsearchpromotions_tags.py wagtail/contrib/search_promotions/apps.py wagtail/contrib/search_promotions/admin_urls.py wagtail/contrib/search_promotions/__init__.py wagtail/contrib/routable_page/tests.py wagtail/contrib/routable_page/models.py wagtail/contrib/routable_page/templatetags/wagtailroutablepage_tags.py wagtail/contrib/routable_page/apps.py wagtail/contrib/routable_page/__init__.py wagtail/contrib/settings/views.py wagtail/contrib/settings/models.py wagtail/contrib/settings/jinja2tags.py wagtail/contrib/settings/registry.py wagtail/contrib/settings/context_processors.py wagtail/contrib/settings/forms.py wagtail/contrib/settings/templatetags/wagtailsettings_tags.py wagtail/contrib/settings/wagtail_hooks.py wagtail/contrib/settings/apps.py wagtail/contrib/settings/urls.py wagtail/contrib/settings/permissions.py wagtail/contrib/settings/__init__.py wagtail/contrib/table_block/tests.py wagtail/contrib/table_block/blocks.py wagtail/contrib/table_block/templatetags/table_block_tags.py wagtail/contrib/table_block/apps.py wagtail/contrib/table_block/__init__.py wagtail/contrib/sitemaps/tests.py wagtail/contrib/sitemaps/sitemap_generator.py wagtail/contrib/sitemaps/views.py wagtail/contrib/sitemaps/apps.py wagtail/contrib/sitemaps/__init__.py wagtail/contrib/styleguide/views.py wagtail/contrib/styleguide/wagtail_hooks.py wagtail/contrib/styleguide/tests.py wagtail/contrib/styleguide/apps.py wagtail/contrib/styleguide/__init__.py wagtail/core/models.py wagtail/core/blocks/field_block.py wagtail/core/blocks/stream_block.py wagtail/core/blocks/base.py wagtail/core/blocks/struct_block.py wagtail/core/blocks/list_block.py wagtail/core/blocks/static_block.py wagtail/core/blocks/__init__.py wagtail/core/blocks/utils.py wagtail/core/management/commands/fixtree.py wagtail/core/management/commands/publish_scheduled_pages.py wagtail/core/management/commands/create_log_entries_from_revisions.py wagtail/core/management/commands/replace_text.py wagtail/core/management/commands/purge_revisions.py wagtail/core/management/commands/move_pages.py wagtail/core/management/commands/set_url_paths.py wagtail/core/permission_policies/collections.py wagtail/core/permission_policies/base.py wagtail/core/permission_policies/__init__.py wagtail/core/query.py wagtail/core/rich_text/rewriters.py wagtail/core/rich_text/feature_registry.py wagtail/core/rich_text/__init__.py wagtail/core/rich_text/pages.py wagtail/core/utils.py wagtail/core/templatetags/wagtailcore_tags.py wagtail/core/fields.py wagtail/core/whitelist.py wagtail/core/treebeard.py wagtail/core/wagtail_hooks.py wagtail/core/hooks.py wagtail/core/jinja2tags.py wagtail/core/views.py wagtail/core/signal_handlers.py wagtail/core/forms.py wagtail/core/workflows.py wagtail/core/sites.py wagtail/core/signals.py wagtail/core/urls.py wagtail/core/apps.py wagtail/core/url_routing.py wagtail/core/compat.py wagtail/core/permissions.py wagtail/core/__init__.py wagtail/core/exceptions.py wagtail/images/views/images.py wagtail/images/views/multiple.py wagtail/images/views/chooser.py wagtail/images/views/serve.py wagtail/images/models.py wagtail/images/image_operations.py wagtail/images/rect.py wagtail/images/templatetags/wagtailimages_tags.py wagtail/images/wagtail_hooks.py wagtail/images/fields.py wagtail/images/rich_text/contentstate.py wagtail/images/rich_text/editor_html.py wagtail/images/rich_text/__init__.py wagtail/images/formats.py wagtail/images/api/v2/views.py wagtail/images/api/v2/serializers.py wagtail/images/api/fields.py wagtail/images/api/admin/views.py wagtail/images/api/admin/serializers.py wagtail/images/utils.py wagtail/images/forms.py wagtail/images/checks.py wagtail/images/blocks.py wagtail/images/widgets.py wagtail/images/jinja2tags.py wagtail/images/signal_handlers.py wagtail/images/edit_handlers.py wagtail/images/__init__.py wagtail/images/apps.py wagtail/images/shortcuts.py wagtail/images/admin.py wagtail/images/admin_urls.py wagtail/images/permissions.py wagtail/images/urls.py wagtail/images/exceptions.py wagtail/search/backends/elasticsearch2.py wagtail/search/backends/base.py wagtail/search/backends/db.py wagtail/search/backends/elasticsearch7.py wagtail/search/backends/__init__.py wagtail/search/backends/elasticsearch5.py wagtail/search/backends/elasticsearch6.py wagtail/search/index.py wagtail/search/management/commands/update_index.py wagtail/search/management/commands/search_garbage_collect.py wagtail/search/management/commands/wagtail_update_index.py wagtail/search/utils.py wagtail/search/models.py wagtail/search/query.py wagtail/search/views/queries.py wagtail/search/signal_handlers.py wagtail/search/apps.py wagtail/search/queryset.py wagtail/search/wagtail_hooks.py wagtail/search/urls/admin.py wagtail/search/forms.py wagtail/search/__init__.py wagtail/snippets/tests.py wagtail/snippets/views/snippets.py wagtail/snippets/views/chooser.py wagtail/snippets/action_menu.py wagtail/snippets/wagtail_hooks.py wagtail/snippets/widgets.py wagtail/snippets/models.py wagtail/snippets/blocks.py wagtail/snippets/permissions.py wagtail/snippets/templatetags/wagtailsnippets_admin_tags.py wagtail/snippets/edit_handlers.py wagtail/snippets/apps.py wagtail/snippets/urls.py wagtail/snippets/__init__.py wagtail/users/tests.py wagtail/users/forms.py wagtail/users/views/users.py wagtail/users/views/groups.py wagtail/users/wagtail_hooks.py wagtail/users/templatetags/wagtailusers_tags.py wagtail/users/models.py wagtail/users/utils.py wagtail/users/apps.py wagtail/users/widgets.py wagtail/users/urls/users.py wagtail/users/__init__.py wagtail/api/v2/views.py wagtail/api/v2/serializers.py wagtail/api/v2/filters.py wagtail/api/v2/utils.py wagtail/api/v2/router.py wagtail/api/v2/signal_handlers.py wagtail/api/v2/pagination.py wagtail/api/v2/apps.py wagtail/api/v2/__init__.py wagtail/api/conf.py wagtail/api/__init__.py wagtail/documents/views/documents.py wagtail/documents/views/chooser.py wagtail/documents/views/serve.py wagtail/documents/views/multiple.py wagtail/documents/models.py wagtail/documents/wagtail_hooks.py wagtail/documents/rich_text/editor_html.py wagtail/documents/rich_text/contentstate.py wagtail/documents/rich_text/__init__.py wagtail/documents/api/v2/views.py wagtail/documents/api/v2/serializers.py wagtail/documents/api/admin/views.py wagtail/documents/forms.py wagtail/documents/widgets.py wagtail/documents/blocks.py wagtail/documents/__init__.py wagtail/documents/apps.py wagtail/documents/signal_handlers.py wagtail/documents/admin.py wagtail/documents/edit_handlers.py wagtail/documents/admin_urls.py wagtail/documents/permissions.py wagtail/documents/urls.py wagtail/embeds/finders/oembed.py wagtail/embeds/finders/embedly.py wagtail/embeds/finders/__init__.py wagtail/embeds/finders/base.py wagtail/embeds/oembed_providers.py wagtail/embeds/rich_text/contentstate.py wagtail/embeds/rich_text/__init__.py wagtail/embeds/rich_text/editor_html.py wagtail/embeds/blocks.py wagtail/embeds/models.py wagtail/embeds/views/chooser.py wagtail/embeds/embeds.py wagtail/embeds/wagtail_hooks.py wagtail/embeds/templatetags/wagtailembeds_tags.py wagtail/embeds/format.py wagtail/embeds/apps.py wagtail/embeds/exceptions.py wagtail/embeds/forms.py wagtail/embeds/urls.py wagtail/embeds/__init__.py wagtail/utils/sendfile.py wagtail/utils/setup.py wagtail/utils/sendfile_streaming_backend.py wagtail/utils/deprecation.py wagtail/utils/decorators.py wagtail/utils/version.py wagtail/utils/widgets.py wagtail/utils/apps.py wagtail/utils/loading.py wagtail/utils/urlpatterns.py wagtail/utils/utils.py wagtail/sites/tests.py wagtail/sites/views.py wagtail/sites/wagtail_hooks.py wagtail/sites/forms.py wagtail/sites/apps.py wagtail/sites/__init__.py wagtail/locales/tests.py wagtail/locales/views.py wagtail/locales/wagtail_hooks.py wagtail/locales/forms.py wagtail/locales/utils.py wagtail/locales/apps.py wagtail/locales/__init__.py wagtail/bin/wagtail.py wagtail/__init__.py client/src/components/Draftail/sources/ModalWorkflowSource.js client/src/components/Draftail/decorators/TooltipEntity.js client/src/components/Draftail/decorators/Link.js client/src/components/Draftail/decorators/Document.js client/src/components/Draftail/blocks/MediaBlock.js client/src/components/Draftail/blocks/ImageBlock.js client/src/components/Draftail/blocks/EmbedBlock.js client/src/components/Draftail/EditorFallback/EditorFallback.js client/src/components/Draftail/index.js client/src/components/Draftail/DraftUtils.js client/src/components/Draftail/Tooltip/Tooltip.js client/src/components/Explorer/ExplorerPanel.js client/src/components/Explorer/actions.js client/src/components/Explorer/reducers/nodes.js client/src/components/Explorer/reducers/explorer.js client/src/components/Explorer/Explorer.js client/src/components/Explorer/ExplorerItem.js client/src/components/Explorer/index.js client/src/components/Explorer/ExplorerHeader.js client/src/components/Explorer/ExplorerToggle.js client/src/components/Explorer/PageCount.js client/src/components/Portal/Portal.js client/src/components/Button/Button.js client/src/components/UpgradeNotification/index.js client/src/components/Transition/Transition.js client/src/components/Icon/Icon.js client/src/components/PublicationStatus/PublicationStatus.js client/src/components/LoadingSpinner/LoadingSpinner.js client/src/includes/initSubmenus.js client/src/includes/initSkipLink.js client/src/utils/focus.js client/src/utils/version.js client/src/utils/actions.js client/src/api/client.js client/src/api/admin.js client/src/config/wagtailConfig.js

Flags

Flags have been temporarily removed from this view while the flagging feature is refactored for better performance and user experience.

You can still use flags when viewing individual files. Flag-level thresholds will also remain on pull and merge requests in your repository provider.

More information can be found in our documentation.


@@ -17,7 +17,7 @@
Loading
17 17
permission_checker = PermissionPolicyChecker(permission_policy)
18 18
19 19
20 -
def get_chooser_context():
20 +
def get_chooser_json_data():
21 21
    """construct context variables needed by the chooser JS"""
22 22
    return {
23 23
        'step': 'chooser',
@@ -42,6 +42,47 @@
Loading
42 42
    }
43 43
44 44
45 +
def get_chooser_context(request, documents, uploadform, **kwargs):
46 +
    """Helper function to return common template context variables for the main chooser view"""
47 +
    context = {
48 +
        'uploadform': uploadform,
49 +
        'is_searching': False,
50 +
    }
51 +
52 +
    for hook in hooks.get_hooks('construct_document_chooser_queryset'):
53 +
        documents = hook(documents, request)
54 +
55 +
    collection_id = request.GET.get('collection_id')
56 +
    if collection_id:
57 +
        documents = documents.filter(collection=collection_id)
58 +
        context['collection_id'] = collection_id
59 +
    else:
60 +
        collections = Collection.objects.all()
61 +
        if len(collections) > 2:
62 +
            context['collections'] = collections
63 +
        else:
64 +
            context['collections'] = None
65 +
66 +
    context['documents_exist'] = documents.exists()
67 +
68 +
    searchform = SearchForm(request.GET)
69 +
    if searchform.is_valid():
70 +
        q = searchform.cleaned_data['q']
71 +
        documents = documents.search(q)
72 +
73 +
        context['query_string'] = q
74 +
        context['is_searching'] = True
75 +
    else:
76 +
        context['searchform'] = SearchForm
77 +
78 +
    paginator = Paginator(documents, per_page=10)
79 +
    documents = paginator.get_page(request.GET.get('p'))
80 +
81 +
    context['documents'] = documents
82 +
    context.update(**kwargs)
83 +
    return context
84 +
85 +
45 86
def chooser(request):
46 87
    Document = get_document_model()
47 88
@@ -51,62 +92,16 @@
Loading
51 92
    else:
52 93
        uploadform = None
53 94
54 -
    documents = Document.objects.all()
55 -
56 -
    # allow hooks to modify the queryset
57 -
    for hook in hooks.get_hooks('construct_document_chooser_queryset'):
58 -
        documents = hook(documents, request)
95 +
    documents = Document.objects.order_by('-created_at')
96 +
    context = get_chooser_context(request, documents, uploadform)
59 97
60 -
    q = None
61 98
    if 'q' in request.GET or 'p' in request.GET or 'collection_id' in request.GET:
62 -
63 -
        collection_id = request.GET.get('collection_id')
64 -
        if collection_id:
65 -
            documents = documents.filter(collection=collection_id)
66 -
        documents_exist = documents.exists()
67 -
68 -
        searchform = SearchForm(request.GET)
69 -
        if searchform.is_valid():
70 -
            q = searchform.cleaned_data['q']
71 -
72 -
            documents = documents.search(q)
73 -
            is_searching = True
74 -
        else:
75 -
            documents = documents.order_by('-created_at')
76 -
            is_searching = False
77 -
78 -
        # Pagination
79 -
        paginator = Paginator(documents, per_page=10)
80 -
        documents = paginator.get_page(request.GET.get('p'))
81 -
82 -
        return TemplateResponse(request, "wagtaildocs/chooser/results.html", {
83 -
            'documents': documents,
84 -
            'documents_exist': documents_exist,
85 -
            'uploadform': uploadform,
86 -
            'query_string': q,
87 -
            'is_searching': is_searching,
88 -
            'collection_id': collection_id,
89 -
        })
99 +
        return TemplateResponse(request, "wagtaildocs/chooser/results.html", context)
90 100
    else:
91 -
        searchform = SearchForm()
92 -
93 -
        collections = Collection.objects.all()
94 -
        if len(collections) < 2:
95 -
            collections = None
96 -
97 -
        documents = documents.order_by('-created_at')
98 -
        documents_exist = documents.exists()
99 -
        paginator = Paginator(documents, per_page=10)
100 -
        documents = paginator.get_page(request.GET.get('p'))
101 -
102 -
        return render_modal_workflow(request, 'wagtaildocs/chooser/chooser.html', None, {
103 -
            'documents': documents,
104 -
            'documents_exist': documents_exist,
105 -
            'uploadform': uploadform,
106 -
            'searchform': searchform,
107 -
            'collections': collections,
108 -
            'is_searching': False,
109 -
        }, json_data=get_chooser_context())
101 +
        return render_modal_workflow(
102 +
            request, 'wagtaildocs/chooser/chooser.html', None,
103 +
            context, json_data=get_chooser_json_data()
104 +
        )
110 105
111 106
112 107
def document_chosen(request, document_id):
@@ -125,19 +120,19 @@
Loading
125 120
126 121
    if request.method == 'POST':
127 122
        document = Document(uploaded_by_user=request.user)
128 -
        form = DocumentForm(
123 +
        uploadform = DocumentForm(
129 124
            request.POST, request.FILES, instance=document, user=request.user, prefix='document-chooser-upload'
130 125
        )
131 126
132 -
        if form.is_valid():
127 +
        if uploadform.is_valid():
133 128
            document.file_size = document.file.size
134 129
135 130
            # Set new document file hash
136 131
            document.file.seek(0)
137 132
            document._set_file_hash(document.file.read())
138 133
            document.file.seek(0)
139 134
140 -
            form.save()
135 +
            uploadform.save()
141 136
142 137
            # Reindex the document to make sure all tags are indexed
143 138
            search_index.insert_or_update_object(document)
@@ -147,12 +142,12 @@
Loading
147 142
                None, json_data={'step': 'document_chosen', 'result': get_document_result_data(document)}
148 143
            )
149 144
    else:
150 -
        form = DocumentForm(user=request.user, prefix='document-chooser-upload')
145 +
        uploadform = DocumentForm(user=request.user, prefix='document-chooser-upload')
151 146
152 -
    documents = Document.objects.order_by('title')
147 +
    documents = Document.objects.order_by('-created_at')
153 148
149 +
    context = get_chooser_context(request, documents, uploadform)
154 150
    return render_modal_workflow(
155 151
        request, 'wagtaildocs/chooser/chooser.html', None,
156 -
        {'documents': documents, 'uploadform': form},
157 -
        json_data=get_chooser_context()
152 +
        context, json_data=get_chooser_json_data()
158 153
    )

@@ -22,7 +22,7 @@
Loading
22 22
CHOOSER_PAGE_SIZE = getattr(settings, 'WAGTAILIMAGES_CHOOSER_PAGE_SIZE', 12)
23 23
24 24
25 -
def get_chooser_js_data():
25 +
def get_chooser_json_data():
26 26
    """construct context variables needed by the chooser JS"""
27 27
    return {
28 28
        'step': 'chooser',
@@ -51,22 +51,50 @@
Loading
51 51
    }
52 52
53 53
54 -
def get_chooser_context(request):
54 +
def get_chooser_context(request, images, uploadform, **kwargs):
55 55
    """Helper function to return common template context variables for the main chooser view"""
56 -
57 -
    collections = Collection.objects.all()
58 -
    if len(collections) < 2:
59 -
        collections = None
60 -
61 -
    return {
62 -
        'searchform': SearchForm(),
63 -
        'is_searching': False,
64 -
        'query_string': None,
56 +
    context = {
57 +
        'uploadform': uploadform,
65 58
        'will_select_format': request.GET.get('select_format'),
66 59
        'popular_tags': popular_tags_for_model(get_image_model()),
67 -
        'collections': collections,
60 +
        'is_searching': False,
68 61
    }
69 62
63 +
    for hook in hooks.get_hooks('construct_image_chooser_queryset'):
64 +
        images = hook(images, request)
65 +
66 +
    collection_id = request.GET.get('collection_id')
67 +
    if collection_id:
68 +
        images = images.filter(collection=collection_id)
69 +
        context['collection_id'] = collection_id
70 +
    else:
71 +
        collections = Collection.objects.all()
72 +
        if len(collections) > 2:
73 +
            context['collections'] = collections
74 +
        else:
75 +
            context['collections'] = None
76 +
77 +
    searchform = SearchForm(request.GET)
78 +
    if searchform.is_valid():
79 +
        q = searchform.cleaned_data['q']
80 +
        images = images.search(q)
81 +
82 +
        context['query_string'] = q
83 +
        context['is_searching'] = True
84 +
    else:
85 +
        context['searchform'] = SearchForm
86 +
        tag_name = request.GET.get('tag')
87 +
88 +
        if tag_name:
89 +
            images = images.filter(tags__name=tag_name)
90 +
91 +
    paginator = Paginator(images, per_page=CHOOSER_PAGE_SIZE)
92 +
    images = paginator.get_page(request.GET.get('p'))
93 +
94 +
    context['images'] = images
95 +
    context.update(**kwargs)
96 +
    return context
97 +
70 98
71 99
def chooser(request):
72 100
    Image = get_image_model()
@@ -78,57 +106,16 @@
Loading
78 106
        uploadform = None
79 107
80 108
    images = Image.objects.order_by('-created_at')
81 -
82 -
    # allow hooks to modify the queryset
83 -
    for hook in hooks.get_hooks('construct_image_chooser_queryset'):
84 -
        images = hook(images, request)
85 -
109 +
    context = get_chooser_context(request, images, uploadform)
86 110
    if (
87 111
        'q' in request.GET or 'p' in request.GET or 'tag' in request.GET
88 112
        or 'collection_id' in request.GET
89 113
    ):
90 -
        # this request is triggered from search, pagination or 'popular tags';
91 -
        # we will just render the results.html fragment
92 -
        collection_id = request.GET.get('collection_id')
93 -
        if collection_id:
94 -
            images = images.filter(collection=collection_id)
95 -
96 -
        searchform = SearchForm(request.GET)
97 -
        if searchform.is_valid():
98 -
            q = searchform.cleaned_data['q']
99 -
100 -
            images = images.search(q)
101 -
            is_searching = True
102 -
        else:
103 -
            is_searching = False
104 -
            q = None
105 -
106 -
            tag_name = request.GET.get('tag')
107 -
            if tag_name:
108 -
                images = images.filter(tags__name=tag_name)
109 -
110 -
        # Pagination
111 -
        paginator = Paginator(images, per_page=CHOOSER_PAGE_SIZE)
112 -
        images = paginator.get_page(request.GET.get('p'))
113 -
114 -
        return TemplateResponse(request, "wagtailimages/chooser/results.html", {
115 -
            'images': images,
116 -
            'is_searching': is_searching,
117 -
            'query_string': q,
118 -
            'will_select_format': request.GET.get('select_format')
119 -
        })
114 +
        return TemplateResponse(request, "wagtailimages/chooser/results.html", context)
120 115
    else:
121 -
        paginator = Paginator(images, per_page=CHOOSER_PAGE_SIZE)
122 -
        images = paginator.get_page(request.GET.get('p'))
123 -
124 -
        context = get_chooser_context(request)
125 -
        context.update({
126 -
            'images': images,
127 -
            'uploadform': uploadform,
128 -
        })
129 116
        return render_modal_workflow(
130 117
            request, 'wagtailimages/chooser/chooser.html', None, context,
131 -
            json_data=get_chooser_js_data()
118 +
            json_data=get_chooser_json_data()
132 119
        )
133 120
134 121
@@ -148,11 +135,11 @@
Loading
148 135
149 136
    if request.method == 'POST':
150 137
        image = Image(uploaded_by_user=request.user)
151 -
        form = ImageForm(
138 +
        uploadform = ImageForm(
152 139
            request.POST, request.FILES, instance=image, user=request.user, prefix='image-chooser-upload'
153 140
        )
154 141
155 -
        if form.is_valid():
142 +
        if uploadform.is_valid():
156 143
            # Set image file size
157 144
            image.file_size = image.file.size
158 145
@@ -161,18 +148,18 @@
Loading
161 148
            image._set_file_hash(image.file.read())
162 149
            image.file.seek(0)
163 150
164 -
            form.save()
151 +
            uploadform.save()
165 152
166 153
            # Reindex the image to make sure all tags are indexed
167 154
            search_index.insert_or_update_object(image)
168 155
169 156
            if request.GET.get('select_format'):
170 -
                form = ImageInsertionForm(
157 +
                uploadform = ImageInsertionForm(
171 158
                    initial={'alt_text': image.default_alt_text}, prefix='image-chooser-insertion'
172 159
                )
173 160
                return render_modal_workflow(
174 161
                    request, 'wagtailimages/chooser/select_format.html', None,
175 -
                    {'image': image, 'form': form}, json_data={'step': 'select_format'}
162 +
                    {'image': image, 'form': uploadform}, json_data={'step': 'select_format'}
176 163
                )
177 164
            else:
178 165
                # not specifying a format; return the image details now
@@ -181,25 +168,14 @@
Loading
181 168
                    None, json_data={'step': 'image_chosen', 'result': get_image_result_data(image)}
182 169
                )
183 170
    else:
184 -
        form = ImageForm(user=request.user, prefix='image-chooser-upload')
171 +
        uploadform = ImageForm(user=request.user, prefix='image-chooser-upload')
185 172
186 173
    images = Image.objects.order_by('-created_at')
187 174
188 -
    # allow hooks to modify the queryset
189 -
    for hook in hooks.get_hooks('construct_image_chooser_queryset'):
190 -
        images = hook(images, request)
191 -
192 -
    paginator = Paginator(images, per_page=CHOOSER_PAGE_SIZE)
193 -
    images = paginator.get_page(request.GET.get('p'))
194 -
195 -
    context = get_chooser_context(request)
196 -
    context.update({
197 -
        'images': images,
198 -
        'uploadform': form,
199 -
    })
175 +
    context = get_chooser_context(request, images, uploadform)
200 176
    return render_modal_workflow(
201 177
        request, 'wagtailimages/chooser/chooser.html', None, context,
202 -
        json_data=get_chooser_js_data()
178 +
        json_data=get_chooser_json_data()
203 179
    )
204 180
205 181

Everything is accounted for!

No changes detected that need to be reviewed.
What changes does Codecov check for?
Lines, not adjusted in diff, that have changed coverage data.
Files that introduced coverage data that had none before.
Files that have missing coverage data that once were tracked.
Files Coverage
client/src 92.85%
wagtail +<.01% 89.99%
Project Totals (436 files) 90.08%
Loading