예제 #1
0
    def test_doesnt_insert_unsaved_object(self, backend):
        obj = models.SearchTest(title="Test")
        backend().reset_mock()

        index.insert_or_update_object(obj)

        self.assertFalse(backend().add.mock_calls)
예제 #2
0
def add(request):
    Document = get_document_model()
    DocumentForm = get_document_form(Document)

    if request.method == 'POST':
        doc = Document(uploaded_by_user=request.user)
        form = DocumentForm(request.POST,
                            request.FILES,
                            instance=doc,
                            user=request.user)
        if form.is_valid():
            form.save()

            # Reindex the document to make sure all tags are indexed
            search_index.insert_or_update_object(doc)

            messages.success(request,
                             _("Document '{0}' added.").format(doc.title),
                             buttons=[
                                 messages.button(
                                     reverse('tuiuiudocs:edit',
                                             args=(doc.id, )), _('Edit'))
                             ])
            return redirect('tuiuiudocs:index')
        else:
            messages.error(request,
                           _("The document could not be saved due to errors."))
    else:
        form = DocumentForm(user=request.user)

    return render(request, "tuiuiudocs/documents/add.html", {
        'form': form,
    })
예제 #3
0
    def test_inserts_object(self, backend):
        obj = models.SearchTest.objects.create(title="Test")
        backend().reset_mock()

        index.insert_or_update_object(obj)

        backend().add.assert_called_with(obj)
예제 #4
0
def chooser_upload(request):
    Document = get_document_model()
    DocumentForm = get_document_form(Document)

    if request.method == 'POST':
        document = Document(uploaded_by_user=request.user)
        form = DocumentForm(request.POST,
                            request.FILES,
                            instance=document,
                            user=request.user)

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

            # Reindex the document to make sure all tags are indexed
            search_index.insert_or_update_object(document)

            return render_modal_workflow(
                request, None, 'tuiuiudocs/chooser/document_chosen.js',
                {'document_json': get_document_json(document)})
    else:
        form = DocumentForm(user=request.user)

    documents = Document.objects.order_by('title')

    return render_modal_workflow(request, 'tuiuiudocs/chooser/chooser.html',
                                 'tuiuiudocs/chooser/chooser.js', {
                                     'documents': documents,
                                     'uploadform': form
                                 })
예제 #5
0
def post_save_signal_handler(instance, update_fields=None, **kwargs):
    if update_fields is not None:
        # fetch a fresh copy of instance from the database to ensure
        # that we're not indexing any of the unsaved data contained in
        # the fields that were not passed in update_fields
        instance = type(instance).objects.get(pk=instance.pk)

    index.insert_or_update_object(instance)
예제 #6
0
    def test_catches_index_error(self, backend):
        obj = models.SearchTest.objects.create(title="Test")

        backend().add.side_effect = ValueError("Test")
        backend().reset_mock()

        with self.assertLogs('tuiuiu.search.index', level='ERROR') as cm:
            index.insert_or_update_object(obj)

        self.assertEqual(len(cm.output), 1)
        self.assertIn("Exception raised while adding <SearchTest: Test> into the 'default' search backend", cm.output[0])
        self.assertIn("Traceback (most recent call last):", cm.output[0])
        self.assertIn("ValueError: Test", cm.output[0])
예제 #7
0
    def test_converts_to_specific_page(self, backend):
        root_page = Page.objects.get(id=1)
        page = root_page.add_child(instance=SimplePage(title="test", slug="test", content="test"))

        # Convert page into a generic "Page" object and add it into the index
        unspecific_page = page.page_ptr

        backend().reset_mock()

        index.insert_or_update_object(unspecific_page)

        # It should be automatically converted back to the specific version
        backend().add.assert_called_with(page)
예제 #8
0
def chooser_upload(request):
    Image = get_image_model()
    ImageForm = get_image_form(Image)

    searchform = SearchForm()

    if request.method == 'POST':
        image = Image(uploaded_by_user=request.user)
        form = ImageForm(request.POST,
                         request.FILES,
                         instance=image,
                         user=request.user)

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

            # Reindex the image to make sure all tags are indexed
            search_index.insert_or_update_object(image)

            if request.GET.get('select_format'):
                form = ImageInsertionForm(
                    initial={'alt_text': image.default_alt_text})
                return render_modal_workflow(
                    request, 'tuiuiuimages/chooser/select_format.html',
                    'tuiuiuimages/chooser/select_format.js', {
                        'image': image,
                        'form': form
                    })
            else:
                # not specifying a format; return the image details now
                return render_modal_workflow(
                    request, None, 'tuiuiuimages/chooser/image_chosen.js',
                    {'image_json': get_image_json(image)})
    else:
        form = ImageForm(user=request.user)

    images = Image.objects.order_by('-created_at')
    paginator, images = paginate(request, images, per_page=12)

    return render_modal_workflow(request, 'tuiuiuimages/chooser/chooser.html',
                                 'tuiuiuimages/chooser/chooser.js', {
                                     'images': images,
                                     'uploadform': form,
                                     'searchform': searchform
                                 })
예제 #9
0
def add(request):
    ImageModel = get_image_model()
    ImageForm = get_image_form(ImageModel)

    if request.method == 'POST':
        image = ImageModel(uploaded_by_user=request.user)
        form = ImageForm(request.POST,
                         request.FILES,
                         instance=image,
                         user=request.user)
        if form.is_valid():
            # Set image file size
            image.file_size = image.file.size

            form.save()

            # Reindex the image to make sure all tags are indexed
            search_index.insert_or_update_object(image)

            messages.success(request,
                             _("Image '{0}' added.").format(image.title),
                             buttons=[
                                 messages.button(
                                     reverse('tuiuiuimages:edit',
                                             args=(image.id, )), _('Edit'))
                             ])
            return redirect('tuiuiuimages:index')
        else:
            messages.error(request,
                           _("The image could not be created due to errors."))
    else:
        form = ImageForm(user=request.user)

    return render(request, "tuiuiuimages/images/add.html", {
        'form': form,
    })
예제 #10
0
def edit(request, image_id):
    Image = get_image_model()
    ImageForm = get_image_form(Image)

    image = get_object_or_404(Image, id=image_id)

    if not permission_policy.user_has_permission_for_instance(
            request.user, 'change', image):
        return permission_denied(request)

    if request.method == 'POST':
        original_file = image.file
        form = ImageForm(request.POST,
                         request.FILES,
                         instance=image,
                         user=request.user)
        if form.is_valid():
            if 'file' in form.changed_data:
                # if providing a new image file, delete the old one and all renditions.
                # NB Doing this via original_file.delete() clears the file field,
                # which definitely isn't what we want...
                original_file.storage.delete(original_file.name)
                image.renditions.all().delete()

                # Set new image file size
                image.file_size = image.file.size

            form.save()

            # Reindex the image to make sure all tags are indexed
            search_index.insert_or_update_object(image)

            messages.success(request,
                             _("Image '{0}' updated.").format(image.title),
                             buttons=[
                                 messages.button(
                                     reverse('tuiuiuimages:edit',
                                             args=(image.id, )),
                                     _('Edit again'))
                             ])
            return redirect('tuiuiuimages:index')
        else:
            messages.error(request,
                           _("The image could not be saved due to errors."))
    else:
        form = ImageForm(instance=image, user=request.user)

    # Check if we should enable the frontend url generator
    # :) foo
    try:
        reverse('tuiuiuimages_serve', args=('foo', '1', 'bar'))
        url_generator_enabled = True
    except NoReverseMatch:
        url_generator_enabled = False

    if image.is_stored_locally():
        # Give error if image file doesn't exist
        if not os.path.isfile(image.file.path):
            messages.error(
                request,
                _("The source image file could not be found. Please change the source or delete the image."
                  ).format(image.title),
                buttons=[
                    messages.button(
                        reverse('tuiuiuimages:delete', args=(image.id, )),
                        _('Delete'))
                ])

    return render(
        request, "tuiuiuimages/images/edit.html", {
            'image':
            image,
            'form':
            form,
            'url_generator_enabled':
            url_generator_enabled,
            'filesize':
            image.get_file_size(),
            'user_can_delete':
            permission_policy.user_has_permission_for_instance(
                request.user, 'delete', image),
        })
예제 #11
0
def edit(request, document_id):
    Document = get_document_model()
    DocumentForm = get_document_form(Document)

    doc = get_object_or_404(Document, id=document_id)

    if not permission_policy.user_has_permission_for_instance(
            request.user, 'change', doc):
        return permission_denied(request)

    if request.method == 'POST':
        original_file = doc.file
        form = DocumentForm(request.POST,
                            request.FILES,
                            instance=doc,
                            user=request.user)
        if form.is_valid():
            if 'file' in form.changed_data:
                # if providing a new document file, delete the old one.
                # NB Doing this via original_file.delete() clears the file field,
                # which definitely isn't what we want...
                original_file.storage.delete(original_file.name)
            doc = form.save()

            # Reindex the document to make sure all tags are indexed
            search_index.insert_or_update_object(doc)

            messages.success(request,
                             _("Document '{0}' updated").format(doc.title),
                             buttons=[
                                 messages.button(
                                     reverse('tuiuiudocs:edit',
                                             args=(doc.id, )), _('Edit'))
                             ])
            return redirect('tuiuiudocs:index')
        else:
            messages.error(request,
                           _("The document could not be saved due to errors."))
    else:
        form = DocumentForm(instance=doc, user=request.user)

    filesize = None

    # Get file size when there is a file associated with the Document object
    if doc.file:
        try:
            filesize = doc.file.size
        except OSError:
            # File doesn't exist
            pass

    if not filesize:
        messages.error(
            request,
            _("The file could not be found. Please change the source or delete the document"
              ),
            buttons=[
                messages.button(reverse('tuiuiudocs:delete', args=(doc.id, )),
                                _('Delete'))
            ])

    return render(
        request, "tuiuiudocs/documents/edit.html", {
            'document':
            doc,
            'filesize':
            filesize,
            'form':
            form,
            'user_can_delete':
            permission_policy.user_has_permission_for_instance(
                request.user, 'delete', doc),
        })