예제 #1
0
    def test_utf16_thumbnail(self):
        """Testing file attachment thumbnail generation for UTF-16 files"""
        filename = os.path.join(os.path.dirname(__file__), "testdata", "utf-16.txt")
        with open(filename) as f:
            review_request = self.create_review_request(publish=True)

            file = SimpleUploadedFile(f.name, f.read(), content_type="text/plain;charset=utf-16le")
            form = UploadFileForm(review_request, files={"path": file})
            form.is_valid()

            file_attachment = form.create()

            self.assertEqual(
                file_attachment.thumbnail,
                '<div class="file-thumbnail"> <div class="file-thumbnail-clipp'
                'ed"><pre>UTF-16le encoded sample plain-text file</pre><pre>'
                "\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e"
                "\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e"
                "\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e"
                "\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e</pre>"
                "<pre></pre><pre>Markus Kuhn [\u02c8ma\u02b3k\u028as ku\u02d0"
                "n] &lt;http://www.cl.cam.ac.uk/~mgk25/&gt; \u2014 2002-07-25"
                "</pre><pre></pre><pre></pre><pre>The ASCII compatible UTF-8 "
                "encoding used in this plain-text file</pre><pre>is defined "
                "in Unicode, ISO 10646-1, and RFC 2279.</pre><pre></pre><pre>"
                "</pre><pre>Using Unicode/UTF-8, you can write in emails and "
                "source code things such as</pre><pre></pre><pre>Mathematics "
                "and sciences:</pre><pre></pre><pre>  \u222e E\u22c5da = Q,  "
                "n \u2192 \u221e, \u2211 f(i) = \u220f g(i),      \u23a7\u23a1"
                "\u239b\u250c\u2500\u2500\u2500\u2500\u2500\u2510\u239e\u23a4"
                "\u23ab</pre><pre>                                           "
                " \u23aa\u23a2\u239c\u2502a\xb2+b\xb3 \u239f\u23a5\u23aa"
                "</pre><pre>  \u2200x\u2208</pre></div></div>",
            )
예제 #2
0
    def setUp(self):
        uploaded_file = SimpleUploadedFile("test.txt", b"<p>This is a test</p>", content_type="text/plain")

        review_request = self.create_review_request(publish=True)

        form = UploadFileForm(review_request, files={"path": uploaded_file})
        self.assertTrue(form.is_valid())
예제 #3
0
    def test_utf16_thumbnail(self):
        """Testing file attachment thumbnail generation for UTF-16 files"""
        filename = os.path.join(os.path.dirname(__file__), 'testdata',
                                'utf-16.txt')
        with open(filename) as f:
            review_request = self.create_review_request(publish=True)

            file = SimpleUploadedFile(
                f.name, f.read(), content_type='text/plain;charset=utf-16le')
            form = UploadFileForm(review_request, files={'path': file})
            form.is_valid()

            file_attachment = form.create()

            self.assertEqual(
                file_attachment.thumbnail,
                '<div class="file-thumbnail-clipped"><pre>'
                'UTF-16le encoded sample plain-text file</pre>'
                '<pre>\u203e\u203e\u203e\u203e\u203e\u203e'
                '\u203e\u203e\u203e\u203e\u203e\u203e\u203e'
                '\u203e\u203e\u203e\u203e\u203e\u203e\u203e'
                '\u203e\u203e\u203e\u203e\u203e\u203e\u203e'
                '\u203e\u203e\u203e\u203e\u203e\u203e\u203e'
                '\u203e\u203e\u203e\u203e\u203e</pre><pre>'
                '</pre><pre>Markus Kuhn [\u02c8ma\u02b3k\u028as'
                ' ku\u02d0n] &lt;http://www.cl.cam.ac.uk/~mgk25/'
                '&gt; \u2014 2002-07-25</pre><pre></pre><pre>'
                '</pre><pre>The ASCII compatible UTF-8 encoding '
                'used in this plain-text file</pre><pre>is '
                'defined in Unicode, ISO 10646-1, and RFC 2279.'
                '</pre></div>')
예제 #4
0
    def test_utf16_thumbnail(self):
        """Testing file attachment thumbnail generation for UTF-16 files"""
        filename = os.path.join(os.path.dirname(__file__), 'testdata',
                                'utf-16.txt')
        with open(filename) as f:
            review_request = self.create_review_request(publish=True)

            file = SimpleUploadedFile(
                f.name, f.read(), content_type='text/plain;charset=utf-16le')
            form = UploadFileForm(review_request, files={'path': file})
            form.is_valid()

            file_attachment = form.create()

            self.assertEqual(
                file_attachment.thumbnail,
                '<div class="file-thumbnail"> <div class="file-thumbnail-clipp'
                'ed"><pre>UTF-16le encoded sample plain-text file</pre><pre>'
                '\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e'
                '\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e'
                '\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e'
                '\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e\u203e</pre>'
                '<pre></pre><pre>Markus Kuhn [\u02c8ma\u02b3k\u028as ku\u02d0'
                'n] &lt;http://www.cl.cam.ac.uk/~mgk25/&gt; \u2014 2002-07-25'
                '</pre><pre></pre><pre></pre><pre>The ASCII compatible UTF-8 '
                'encoding used in this plain-text file</pre><pre>is defined '
                'in Unicode, ISO 10646-1, and RFC 2279.</pre><pre></pre><pre>'
                '</pre><pre>Using Unicode/UTF-8, you can write in emails and '
                'source code things such as</pre><pre></pre><pre>Mathematics '
                'and sciences:</pre><pre></pre><pre>  \u222e E\u22c5da = Q,  '
                'n \u2192 \u221e, \u2211 f(i) = \u220f g(i),      \u23a7\u23a1'
                '\u239b\u250c\u2500\u2500\u2500\u2500\u2500\u2510\u239e\u23a4'
                '\u23ab</pre><pre>                                           '
                ' \u23aa\u23a2\u239c\u2502a\xb2+b\xb3 \u239f\u23a5\u23aa'
                '</pre><pre>  \u2200x\u2208</pre></div></div>')
예제 #5
0
    def test_utf16_thumbnail(self):
        """Testing file attachment thumbnail generation for UTF-16 files"""
        filename = os.path.join(os.path.dirname(__file__),
                                'testdata', 'utf-16.txt')
        with open(filename) as f:
            review_request = self.create_review_request(publish=True)

            file = SimpleUploadedFile(f.name, f.read(),
                                      content_type='text/plain;charset=utf-16le')
            form = UploadFileForm(review_request, files={'path': file})
            form.is_valid()

            file_attachment = form.create()

            self.assertEqual(file_attachment.thumbnail,
                             '<div class="file-thumbnail-clipped"><pre>'
                             'UTF-16le encoded sample plain-text file</pre>'
                             '<pre>\u203e\u203e\u203e\u203e\u203e\u203e'
                             '\u203e\u203e\u203e\u203e\u203e\u203e\u203e'
                             '\u203e\u203e\u203e\u203e\u203e\u203e\u203e'
                             '\u203e\u203e\u203e\u203e\u203e\u203e\u203e'
                             '\u203e\u203e\u203e\u203e\u203e\u203e\u203e'
                             '\u203e\u203e\u203e\u203e\u203e</pre><pre>'
                             '</pre><pre>Markus Kuhn [\u02c8ma\u02b3k\u028as'
                             ' ku\u02d0n] &lt;http://www.cl.cam.ac.uk/~mgk25/'
                             '&gt; \u2014 2002-07-25</pre><pre></pre><pre>'
                             '</pre><pre>The ASCII compatible UTF-8 encoding '
                             'used in this plain-text file</pre><pre>is '
                             'defined in Unicode, ISO 10646-1, and RFC 2279.'
                             '</pre></div>')
예제 #6
0
    def test_upload_file(self):
        """Testing uploading a file attachment"""
        review_request = self.create_review_request(publish=True)

        file = self.make_uploaded_file()
        form = UploadFileForm(review_request, files={"path": file})
        self.assertTrue(form.is_valid())

        file_attachment = form.create()
        self.assertTrue(os.path.basename(file_attachment.file.name).endswith("__trophy.png"))
        self.assertEqual(file_attachment.mimetype, "image/png")
예제 #7
0
    def update(self, request, *args, **kwargs):
        try:
            filediff = self.get_object(request, *args, **kwargs)
            review_request_draft = filediff.diffset.review_request_draft.get()
        except ObjectDoesNotExist:
            return DOES_NOT_EXIST

        if not review_request_draft.is_mutable_by(request.user):
            return self._no_access_error(request.user)

        if 'dest_attachment_file' in request.FILES:
            if not filediff.binary:
                return INVALID_FORM_DATA, {
                    'fields': {
                        'dest_attachment_file': [
                            'Cannot upload a file attachment to a '
                            'non-binary file in a diff.',
                        ]
                    }
                }

            try:
                # Check if there's already an attachment. If so, bail.
                attachment = FileAttachment.objects.get_for_filediff(filediff)

                return INVALID_FORM_DATA, {
                    'fields': {
                        'dest_attachment_file': [
                            'There is already a file attachment associated '
                            'with this binary file.',
                        ]
                    }
                }
            except ObjectDoesNotExist:
                pass

            dest_attachment_file = request.FILES.get('dest_attachment_file')

            form = UploadFileForm({}, {
                'path': dest_attachment_file,
            })

            if not form.is_valid():
                return INVALID_FORM_DATA, {
                    'fields': self._get_form_errors(form),
                }

            form.create(dest_attachment_file,
                        review_request_draft.review_request,
                        filediff)

        return 200, {
            self.item_result_key: filediff,
        }
예제 #8
0
    def update(self, request, *args, **kwargs):
        try:
            filediff = self.get_object(request, *args, **kwargs)
            review_request_draft = filediff.diffset.review_request_draft.get()
        except ObjectDoesNotExist:
            return DOES_NOT_EXIST

        if not review_request_draft.is_mutable_by(request.user):
            return self._no_access_error(request.user)

        if 'dest_attachment_file' in request.FILES:
            if not filediff.binary:
                return INVALID_FORM_DATA, {
                    'fields': {
                        'dest_attachment_file': [
                            'Cannot upload a file attachment to a '
                            'non-binary file in a diff.',
                        ]
                    }
                }

            try:
                # Check if there's already an attachment. If so, bail.
                attachment = FileAttachment.objects.get_for_filediff(filediff)

                return INVALID_FORM_DATA, {
                    'fields': {
                        'dest_attachment_file': [
                            'There is already a file attachment associated '
                            'with this binary file.',
                        ]
                    }
                }
            except ObjectDoesNotExist:
                pass

            dest_attachment_file = request.FILES.get('dest_attachment_file')

            form = UploadFileForm({}, {
                'path': dest_attachment_file,
            })

            if not form.is_valid():
                return INVALID_FORM_DATA, {
                    'fields': self._get_form_errors(form),
                }

            form.create(dest_attachment_file,
                        review_request_draft.review_request, filediff)

        return 200, {
            self.item_result_key: filediff,
        }
예제 #9
0
    def test_upload_file_with_history_mismatch(self):
        """Testing uploading a file attachment to an existing
        FileAttachmentHistory with a mismatched review request
        """
        review_request_1 = self.create_review_request(publish=True)
        review_request_2 = self.create_review_request(publish=True)

        history = FileAttachmentHistory.objects.create(display_position=0)
        review_request_1.file_attachment_histories.add(history)

        form = UploadFileForm(review_request_2, data={"attachment_history": history.pk}, files={"path": file})
        self.assertFalse(form.is_valid())
예제 #10
0
    def test_upload_file_with_history(self):
        """Testing uploading a file attachment to an existing
        FileAttachmentHistory
        """
        review_request_1 = self.create_review_request(publish=True)
        history = FileAttachmentHistory.objects.create(display_position=0)
        review_request_1.file_attachment_histories.add(history)

        file = self.make_uploaded_file()
        form = UploadFileForm(review_request_1, data={"attachment_history": history.pk}, files={"path": file})
        self.assertTrue(form.is_valid())
        form.create()
예제 #11
0
    def test_upload_file(self):
        """Testing uploading a file attachment"""
        file = self.make_uploaded_file()
        form = UploadFileForm(files={
            'path': file,
        })
        self.assertTrue(form.is_valid())

        review_request = self.create_review_request(publish=True)
        file_attachment = form.create(file, review_request)
        self.assertTrue(os.path.basename(file_attachment.file.name).endswith(
            '__trophy.png'))
        self.assertEqual(file_attachment.mimetype, 'image/png')
예제 #12
0
    def setUp(self):
        uploaded_file = SimpleUploadedFile('test.txt',
                                           b'<p>This is a test</p>',
                                           content_type='text/plain')

        review_request = self.create_review_request(publish=True)

        form = UploadFileForm(review_request, files={
            'path': uploaded_file,
        })
        self.assertTrue(form.is_valid())

        self.file_attachment = form.create()
예제 #13
0
    def test_upload_file(self):
        """Testing uploading a file attachment"""
        file = self.make_uploaded_file()
        form = UploadFileForm(files={
            'path': file,
        })
        self.assertTrue(form.is_valid())

        review_request = ReviewRequest.objects.get(pk=1)
        file_attachment = form.create(file, review_request)
        self.assertTrue(os.path.basename(file_attachment.file.name).endswith(
            '__trophy.png'))
        self.assertEqual(file_attachment.mimetype, 'image/png')
예제 #14
0
    def test_upload_file_with_history(self):
        """Testing uploading a file attachment to an existing
        FileAttachmentHistory
        """
        review_request_1 = self.create_review_request(publish=True)
        history = FileAttachmentHistory.objects.create(display_position=0)
        review_request_1.file_attachment_histories.add(history)

        file = self.make_uploaded_file()
        form = UploadFileForm(review_request_1,
                              data={'attachment_history': history.pk},
                              files={'path': file})
        self.assertTrue(form.is_valid())
        form.create()
예제 #15
0
    def update(self, request, *args, **kwargs):
        try:
            filediff = self.get_object(request, *args, **kwargs)
            review_request_draft = filediff.diffset.review_request_draft.get()
        except ObjectDoesNotExist:
            return DOES_NOT_EXIST

        if not review_request_draft.is_mutable_by(request.user):
            return self._no_access_error(request.user)

        if "dest_attachment_file" in request.FILES:
            if not filediff.binary:
                return (
                    INVALID_FORM_DATA,
                    {
                        "fields": {
                            "dest_attachment_file": [
                                "Cannot upload a file attachment to a " "non-binary file in a diff."
                            ]
                        }
                    },
                )

            try:
                # Check if there's already an attachment. If so, bail.
                attachment = FileAttachment.objects.get_for_filediff(filediff)

                return (
                    INVALID_FORM_DATA,
                    {
                        "fields": {
                            "dest_attachment_file": [
                                "There is already a file attachment associated " "with this binary file."
                            ]
                        }
                    },
                )
            except ObjectDoesNotExist:
                pass

            dest_attachment_file = request.FILES.get("dest_attachment_file")

            form = UploadFileForm({}, {"path": dest_attachment_file})

            if not form.is_valid():
                return INVALID_FORM_DATA, {"fields": self._get_form_errors(form)}

            form.create(dest_attachment_file, review_request_draft.review_request, filediff)

        return 200, {self.item_result_key: filediff}
예제 #16
0
    def test_upload_file_with_history_mismatch(self):
        """Testing uploading a file attachment to an existing
        FileAttachmentHistory with a mismatched review request
        """
        review_request_1 = self.create_review_request(publish=True)
        review_request_2 = self.create_review_request(publish=True)

        history = FileAttachmentHistory.objects.create(display_position=0)
        review_request_1.file_attachment_histories.add(history)

        form = UploadFileForm(review_request_2,
                              data={'attachment_history': history.pk},
                              files={'path': file})
        self.assertFalse(form.is_valid())
예제 #17
0
    def setUp(self):
        uploaded_file = SimpleUploadedFile(
            'test.txt',
            b'<p>This is a test</p>',
            content_type='text/plain')

        review_request = self.create_review_request(publish=True)

        form = UploadFileForm(review_request, files={
            'path': uploaded_file,
        })
        self.assertTrue(form.is_valid())

        self.file_attachment = form.create()
예제 #18
0
파일: views.py 프로젝트: isakb/reviewboard
def _make_review_request_context(review_request, extra_context):
    """Returns a dictionary for template contexts used for review requests.

    The dictionary will contain the common data that is used for all
    review request-related pages (the review request detail page, the diff
    viewer, and the screenshot pages).

    For convenience, extra data can be passed to this dictionary.
    """
    if review_request.repository:
        upload_diff_form = UploadDiffForm(review_request)
        scmtool = review_request.repository.get_scmtool()
    else:
        upload_diff_form = None
        scmtool = None

    return dict(
        {
            'review_request': review_request,
            'upload_diff_form': upload_diff_form,
            'upload_screenshot_form': UploadScreenshotForm(),
            'file_attachment_form': UploadFileForm(),
            'comment_file_form': CommentFileForm(),
            'scmtool': scmtool,
        }, **extra_context)
예제 #19
0
    def test_upload_file(self):
        """Testing uploading a file attachment."""
        filename = os.path.join(settings.STATIC_ROOT, "rb", "images", "trophy.png")
        f = open(filename, "r")
        file = SimpleUploadedFile(f.name, f.read(), content_type="image/png")
        f.close()

        form = UploadFileForm(files={"path": file})
        form.is_valid()
        print form.errors
        self.assertTrue(form.is_valid())

        review_request = ReviewRequest.objects.get(pk=1)
        file_attachment = form.create(file, review_request)
        self.assertEqual(os.path.basename(file_attachment.file.name), "trophy.png")
        self.assertEqual(file_attachment.mimetype, "image/png")
예제 #20
0
    def create(self, request, *args, **kwargs):
        """Creates a new file from a file attachment.

        This accepts any file type and associates it with a draft of a
        review request.

        It is expected that the client will send the data as part of a
        :mimetype:`multipart/form-data` mimetype. The file's name
        and content should be stored in the ``path`` field. A typical request
        may look like::

            -- SoMe BoUnDaRy
            Content-Disposition: form-data; name=path; filename="foo.zip"

            <Content here>
            -- SoMe BoUnDaRy --
        """
        try:
            review_request = \
                resources.review_request.get_object(request, *args, **kwargs)
        except ObjectDoesNotExist:
            return DOES_NOT_EXIST

        if not review_request.is_mutable_by(request.user):
            return self.get_no_access_error(request)

        form_data = request.POST.copy()
        form = UploadFileForm(review_request, form_data, request.FILES)

        if not form.is_valid():
            return INVALID_FORM_DATA, {
                'fields': self._get_form_errors(form),
            }

        try:
            file = form.create()
        except ValueError as e:
            return INVALID_FORM_DATA, {
                'fields': {
                    'path': [six.text_type(e)],
                },
            }

        return 201, {
            self.item_result_key:
            self.serialize_object(file, request=request, *args, **kwargs),
        }
    def create(self, request, *args, **kwargs):
        """Creates a new file from a file attachment.

        This accepts any file type and associates it with a draft of a
        review request.

        It is expected that the client will send the data as part of a
        :mimetype:`multipart/form-data` mimetype. The file's name
        and content should be stored in the ``path`` field. A typical request
        may look like::

            -- SoMe BoUnDaRy
            Content-Disposition: form-data; name=path; filename="foo.zip"

            <Content here>
            -- SoMe BoUnDaRy --
        """
        try:
            review_request = \
                resources.review_request.get_object(request, *args, **kwargs)
        except ObjectDoesNotExist:
            return DOES_NOT_EXIST

        if not review_request.is_mutable_by(request.user):
            return self.get_no_access_error(request)

        form_data = request.POST.copy()
        form = UploadFileForm(review_request, form_data, request.FILES)

        if not form.is_valid():
            return INVALID_FORM_DATA, {
                'fields': self._get_form_errors(form),
            }

        try:
            file = form.create()
        except ValueError as e:
            return INVALID_FORM_DATA, {
                'fields': {
                    'path': [six.text_type(e)],
                },
            }

        return 201, {
            self.item_result_key: self.serialize_object(
                file, request=request, *args, **kwargs),
        }
예제 #22
0
def make_review_request_context(request, review_request, extra_context={}):
    """Returns a dictionary for template contexts used for review requests.

    The dictionary will contain the common data that is used for all
    review request-related pages (the review request detail page, the diff
    viewer, and the screenshot pages).

    For convenience, extra data can be passed to this dictionary.
    """
    if review_request.repository:
        upload_diff_form = UploadDiffForm(review_request, request=request)
        scmtool = review_request.repository.get_scmtool()
    else:
        upload_diff_form = None
        scmtool = None

    if 'blocks' not in extra_context:
        extra_context['blocks'] = list(review_request.blocks.all())

    return dict(
        {
            'mutable_by_user':
            review_request.is_mutable_by(request.user),
            'status_mutable_by_user':
            review_request.is_status_mutable_by(request.user),
            'review_request':
            review_request,
            'upload_diff_form':
            upload_diff_form,
            'upload_screenshot_form':
            UploadScreenshotForm(),
            'file_attachment_form':
            UploadFileForm(),
            'comment_file_form':
            CommentFileForm(),
            'scmtool':
            scmtool,
        }, **extra_context)
예제 #23
0
    def test_upload_file(self):
        """Testing uploading a file attachment."""
        filename = os.path.join(settings.STATIC_ROOT, 'rb', 'images',
                                'trophy.png')
        f = open(filename, 'r')
        file = SimpleUploadedFile(f.name, f.read(), content_type='image/png')
        f.close()

        form = UploadFileForm(files={
            'path': file,
        })
        form.is_valid()
        print form.errors
        self.assertTrue(form.is_valid())

        review_request = ReviewRequest.objects.get(pk=1)
        file_attachment = form.create(file, review_request)
        self.assertEqual(os.path.basename(file_attachment.file.name),
                         'trophy.png')
        self.assertEqual(file_attachment.mimetype, 'image/png')
예제 #24
0
    def update(self, request, extra_fields={}, *args, **kwargs):
        """Updates a per-file diff.

        If this represents a binary file, then the contents of the binary
        file can be uploaded before the review request is published.

        Extra data can be stored for later lookup by passing
        ``extra_data.key_name=value``. The ``key_name`` and ``value`` can be
        any valid strings. Passing a blank ``value`` will remove the key. The
        ``extra_data.`` prefix is required.
        """
        try:
            filediff = self.get_object(request, *args, **kwargs)
            review_request_draft = filediff.diffset.review_request_draft.get()
        except ObjectDoesNotExist:
            return DOES_NOT_EXIST

        if not review_request_draft.is_mutable_by(request.user):
            return self._no_access_error(request.user)

        if 'dest_attachment_file' in request.FILES:
            if not filediff.binary:
                return INVALID_FORM_DATA, {
                    'fields': {
                        'dest_attachment_file': [
                            'Cannot upload a file attachment to a '
                            'non-binary file in a diff.',
                        ]
                    }
                }

            try:
                # Check if there's already an attachment. If so, bail.
                FileAttachment.objects.get_for_filediff(filediff)

                return INVALID_FORM_DATA, {
                    'fields': {
                        'dest_attachment_file': [
                            'There is already a file attachment associated '
                            'with this binary file.',
                        ]
                    }
                }
            except ObjectDoesNotExist:
                pass

            dest_attachment_file = request.FILES.get('dest_attachment_file')

            form = UploadFileForm(review_request_draft.review_request, {}, {
                'path': dest_attachment_file,
            })

            if not form.is_valid():
                return INVALID_FORM_DATA, {
                    'fields': self._get_form_errors(form),
                }

            form.create(filediff)

        if extra_fields:
            self.import_extra_data(filediff, filediff.extra_data, extra_fields)
            filediff.save(update_fields=['extra_data'])

        return 200, {
            self.item_result_key: filediff,
        }
예제 #25
0
    def update(self, request, extra_fields={}, *args, **kwargs):
        """Updates a per-file diff.

        If this represents a binary file, then the contents of the binary
        file can be uploaded before the review request is published.

        Extra data can be stored later lookup. See
        :ref:`webapi2.0-extra-data` for more information.
        """
        try:
            filediff = self.get_object(request, *args, **kwargs)
            review_request_draft = filediff.diffset.review_request_draft.get()
        except ObjectDoesNotExist:
            return DOES_NOT_EXIST

        if not review_request_draft.is_mutable_by(request.user):
            return self.get_no_access_error(request)

        if 'dest_attachment_file' in request.FILES:
            if not filediff.binary:
                return INVALID_FORM_DATA, {
                    'fields': {
                        'dest_attachment_file': [
                            'Cannot upload a file attachment to a '
                            'non-binary file in a diff.',
                        ]
                    }
                }

            try:
                # Check if there's already an attachment. If so, bail.
                FileAttachment.objects.get_for_filediff(filediff)

                return INVALID_FORM_DATA, {
                    'fields': {
                        'dest_attachment_file': [
                            'There is already a file attachment associated '
                            'with this binary file.',
                        ]
                    }
                }
            except ObjectDoesNotExist:
                pass

            dest_attachment_file = request.FILES.get('dest_attachment_file')

            form = UploadFileForm(review_request_draft.review_request, {}, {
                'path': dest_attachment_file,
            })

            if not form.is_valid():
                return INVALID_FORM_DATA, {
                    'fields': self._get_form_errors(form),
                }

            form.create(filediff)

        if extra_fields:
            try:
                self.import_extra_data(filediff, filediff.extra_data,
                                       extra_fields)
            except ImportExtraDataError as e:
                return e.error_payload

            filediff.save(update_fields=['extra_data'])

        return 200, {
            self.item_result_key: filediff,
        }
예제 #26
0
    def test_upload_file_revisions(self):
        """Testing uploading multiple revisions of a file"""
        review_request = self.create_review_request(publish=True)
        history = FileAttachmentHistory.objects.create(display_position=0)
        review_request.file_attachment_histories.add(history)
        file = self.make_uploaded_file()

        # Add a file with the given history
        form = UploadFileForm(review_request,
                              data={'attachment_history': history.pk},
                              files={'path': file})
        self.assertTrue(form.is_valid())
        file_attachment = form.create()
        history = FileAttachmentHistory.objects.get(pk=history.pk)
        self.assertEqual(file_attachment.attachment_revision, 1)
        self.assertEqual(history.latest_revision, 1)
        self.assertEqual(history.display_position, 0)

        review_request.get_draft().publish()

        # Post an update
        form = UploadFileForm(review_request,
                              data={'attachment_history': history.pk},
                              files={'path': file})
        self.assertTrue(form.is_valid())
        file_attachment = form.create()
        history = FileAttachmentHistory.objects.get(pk=history.pk)
        self.assertEqual(file_attachment.attachment_revision, 2)
        self.assertEqual(history.latest_revision, 2)
        self.assertEqual(history.display_position, 0)

        review_request.get_draft().publish()

        # Post two updates without publishing the draft in between
        form = UploadFileForm(review_request,
                              data={'attachment_history': history.pk},
                              files={'path': file})
        self.assertTrue(form.is_valid())
        file_attachment = form.create()
        history = FileAttachmentHistory.objects.get(pk=history.pk)
        self.assertEqual(file_attachment.attachment_revision, 3)
        self.assertEqual(history.latest_revision, 3)
        self.assertEqual(history.display_position, 0)

        form = UploadFileForm(review_request,
                              data={'attachment_history': history.pk},
                              files={'path': file})
        self.assertTrue(form.is_valid())
        file_attachment = form.create()
        history = FileAttachmentHistory.objects.get(pk=history.pk)
        self.assertEqual(file_attachment.attachment_revision, 3)
        self.assertEqual(history.latest_revision, 3)
        self.assertEqual(history.display_position, 0)

        # Add another (unrelated) file to check display position
        form = UploadFileForm(review_request, files={'path': file})
        self.assertTrue(form.is_valid())
        file_attachment = form.create()
        self.assertEqual(file_attachment.attachment_revision, 1)
        self.assertEqual(file_attachment.attachment_history.latest_revision, 1)
        self.assertEqual(file_attachment.attachment_history.display_position,
                         1)