Example #1
0
    def create_file_attachment(self,
                               review_request,
                               orig_filename='filename.png',
                               caption='My Caption',
                               draft=False,
                               **kwargs):
        """Creates a FileAttachment for testing.

        The FileAttachment is tied to the given ReviewRequest. It's populated
        with default data that can be overridden by the caller.
        """
        file_attachment = FileAttachment(caption=caption,
                                         orig_filename=orig_filename,
                                         mimetype='image/png',
                                         **kwargs)

        filename = os.path.join(settings.STATIC_ROOT, 'rb', 'images',
                                'trophy.png')

        with open(filename, 'r') as f:
            file_attachment.file.save(filename, File(f), save=True)

        if draft:
            review_request_draft = ReviewRequestDraft.create(review_request)
            review_request_draft.file_attachments.add(file_attachment)
        else:
            review_request.file_attachments.add(file_attachment)

        return file_attachment
Example #2
0
    def create(self, file, review_request, filediff=None):
        caption = self.cleaned_data['caption'] or file.name
        mimetype = file.content_type or self._guess_mimetype(file)
        filename = '%s__%s' % (uuid4(), file.name)

        attachment_kwargs = {
            'caption': '',
            'draft_caption': caption,
            'orig_filename': os.path.basename(file.name),
            'mimetype': mimetype,
        }

        if filediff:
            file_attachment = FileAttachment.objects.create_from_filediff(
                filediff, save=False, **attachment_kwargs)
        else:
            file_attachment = FileAttachment(**attachment_kwargs)

        file_attachment.file.save(filename, file, save=True)

        draft = ReviewRequestDraft.create(review_request)
        draft.file_attachments.add(file_attachment)
        draft.save()

        return file_attachment
Example #3
0
    def create(self, file, review_request):
        caption = self.cleaned_data['caption']

        file_attachment = FileAttachment(caption='',
                                         draft_caption=caption,
                                         mimetype=file.content_type)
        file_attachment.file.save(file.name, file, save=True)

        draft = ReviewRequestDraft.create(review_request)
        draft.file_attachments.add(file_attachment)
        draft.save()

        return file_attachment
Example #4
0
    def create(self, file, review_request):
        caption = self.cleaned_data['caption'] or file.name
        mimetype = file.content_type or self._guess_mimetype(file)
        filename = '%s__%s' % (uuid4(), file.name)

        file_attachment = FileAttachment(
            caption='',
            draft_caption=caption,
            orig_filename=os.path.basename(file.name),
            mimetype=mimetype)
        file_attachment.file.save(filename, file, save=True)

        draft = ReviewRequestDraft.create(review_request)
        draft.file_attachments.add(file_attachment)
        draft.save()

        return file_attachment
Example #5
0
    def create(self, user, local_site=None):
        """Create a FileAttachment based on this form.

        Args:
            user (django.contrib.auth.models.User):
                The user who owns this file attachment.

            local_site (reviewboard.site.models.LocalSite, optional):
                The optional local site.

        Returns:
            reviewboard.attachments.models.FileAttachment:
            The new file attachment model.
        """
        file_obj = self.files.get('path')

        attachment_kwargs = {
            'uuid': uuid4(),
            'user': user,
            'local_site': local_site,
        }

        if file_obj:
            mimetype = get_uploaded_file_mimetype(file_obj)
            filename = get_unique_filename(file_obj.name)

            attachment_kwargs.update({
                'caption':
                self.cleaned_data['caption'] or file_obj.name,
                'orig_filename':
                os.path.basename(file_obj.name),
                'mimetype':
                mimetype,
            })

            file_attachment = FileAttachment(**attachment_kwargs)
            file_attachment.file.save(filename, file_obj, save=True)
        else:
            attachment_kwargs['caption'] = self.cleaned_data['caption'] or ''

            file_attachment = FileAttachment.objects.create(
                **attachment_kwargs)

        return file_attachment
Example #6
0
    def create(self, file, review_request, filediff=None):
        caption = self.cleaned_data['caption'] or file.name

        # There are several things that can go wrong with browser-provided
        # mimetypes. In one case (bug 3427), Firefox on Linux Mint was
        # providing a mimetype that looked like 'text/text/application/pdf',
        # which is unparseable. IE also has a habit of setting any unknown file
        # type to 'application/octet-stream', rather than just choosing not to
        # provide a mimetype. In the case where what we get from the browser
        # is obviously wrong, try to guess.
        if (file.content_type and
            len(file.content_type.split('/')) == 2 and
            file.content_type != 'application/octet-stream'):
            mimetype = file.content_type
        else:
            mimetype = self._guess_mimetype(file)

        filename = '%s__%s' % (uuid4(), file.name)

        attachment_kwargs = {
            'caption': '',
            'draft_caption': caption,
            'orig_filename': os.path.basename(file.name),
            'mimetype': mimetype,
        }

        if filediff:
            file_attachment = FileAttachment.objects.create_from_filediff(
                filediff,
                save=False,
                **attachment_kwargs)
        else:
            file_attachment = FileAttachment(**attachment_kwargs)

        file_attachment.file.save(filename, file, save=True)

        draft = ReviewRequestDraft.create(review_request)
        draft.file_attachments.add(file_attachment)
        draft.save()

        return file_attachment
Example #7
0
    def _create_base_file_attachment(self,
                                     caption='My Caption',
                                     orig_filename='filename.png',
                                     has_file=True,
                                     user=None,
                                     with_local_site=False,
                                     local_site_name=None,
                                     local_site=None,
                                     **kwargs):
        """Create a FileAttachment object with the given parameters.

        When creating a
        :py:class:`reviewboard.attachments.models.FileAttachment` that will be
        associated to a review request, a user and local_site should not be
        specified.

        Args:
            caption (unicode, optional):
                The caption for the file attachment.

            orig_filename (unicode, optional):
                The original name of the file to set in the model.

            has_file (bool, optional):
                ``True`` if an actual file object should be included in the
                model.

            user (django.contrib.auth.models.User, optonal):
                The user who owns the file attachment.

            with_local_site (bool, optional):
                ``True`` if the file attachment should be associated with a
                local site. If this is set, one of ``local_site_name`` or
                ``local_site`` should be provided as well.

            local_site_name (unicode, optional):
                The name of the local site to associate this attachment with.

            local_site (reviewboard.site.models.LocalSite, optional):
                The local site to associate this attachment with.

            kwargs (dict):
                Additional keyword arguments to pass into the FileAttachment
                constructor.

        Returns:
            reviewboard.attachments.models.FileAttachment:
            The new file attachment instance.
        """
        if with_local_site:
            local_site = self.get_local_site(name=local_site_name)

        file_attachment = FileAttachment(caption=caption,
                                         user=user,
                                         uuid='test-uuid',
                                         local_site=local_site,
                                         **kwargs)

        if has_file:
            filename = os.path.join(settings.STATIC_ROOT, 'rb', 'images',
                                    'logo.png')

            file_attachment.orig_filename = orig_filename
            file_attachment.mimetype = 'image/png'

            with open(filename, 'r') as f:
                file_attachment.file.save(filename, File(f), save=True)

        file_attachment.save()

        return file_attachment
Example #8
0
    def _create_base_file_attachment(self,
                                     caption='My Caption',
                                     orig_filename='filename.png',
                                     has_file=True,
                                     user=None,
                                     with_local_site=False,
                                     local_site_name=None,
                                     local_site=None,
                                     **kwargs):
        """Create a FileAttachment object with the given parameters.

        When creating a
        :py:class:`reviewboard.attachments.models.FileAttachment` that will be
        associated to a review request, a user and local_site should not be
        specified.

        Args:
            caption (unicode, optional):
                The caption for the file attachment.

            orig_filename (unicode, optional):
                The original name of the file to set in the model.

            has_file (bool, optional):
                ``True`` if an actual file object should be included in the
                model.

            user (django.contrib.auth.models.User, optonal):
                The user who owns the file attachment.

            with_local_site (bool, optional):
                ``True`` if the file attachment should be associated with a
                local site. If this is set, one of ``local_site_name`` or
                ``local_site`` should be provided as well.

            local_site_name (unicode, optional):
                The name of the local site to associate this attachment with.

            local_site (reviewboard.site.models.LocalSite, optional):
                The local site to associate this attachment with.

            kwargs (dict):
                Additional keyword arguments to pass into the FileAttachment
                constructor.

        Returns:
            reviewboard.attachments.models.FileAttachment:
            The new file attachment instance.
        """
        if with_local_site:
            local_site = self.get_local_site(name=local_site_name)

        file_attachment = FileAttachment(
            caption=caption,
            user=user,
            uuid='test-uuid',
            local_site=local_site,
            **kwargs)

        if has_file:
            filename = os.path.join(settings.STATIC_ROOT, 'rb', 'images',
                                    'logo.png')

            file_attachment.orig_filename = orig_filename
            file_attachment.mimetype = 'image/png'

            with open(filename, 'r') as f:
                file_attachment.file.save(filename, File(f), save=True)

        file_attachment.save()

        return file_attachment
Example #9
0
    def test_is_from_diff_with_filediff(self):
        """Testing FileAttachment.is_from_diff with filediff association"""
        filediff = self.make_filediff()
        file_attachment = FileAttachment(added_in_filediff=filediff)

        self.assertTrue(file_attachment.is_from_diff)
Example #10
0
    def test_is_from_diff_with_repository(self):
        """Testing FileAttachment.is_from_diff with repository association"""
        repository = self.create_repository()
        file_attachment = FileAttachment(repository=repository)

        self.assertTrue(file_attachment.is_from_diff)
Example #11
0
    def test_is_from_diff_with_no_association(self):
        """Testing FileAttachment.is_from_diff with standard attachment"""
        file_attachment = FileAttachment()

        self.assertFalse(file_attachment.is_from_diff)
Example #12
0
    def create(self, filediff=None):
        """Create a FileAttachment based on this form.

        Args:
            filediff (reviewboard.diffviewer.models.filediff.FileDiff, optional):
                The optional diff to attach this file to (for use when this
                file represents a binary file within the diff).

        Returns:
            reviewboard.attachments.models.FileAttachment:
            The new file attachment model.
        """
        file_obj = self.files['path']
        caption = self.cleaned_data['caption'] or file_obj.name

        mimetype = get_uploaded_file_mimetype(file_obj)
        filename = get_unique_filename(file_obj.name)

        if self.cleaned_data['attachment_history'] is None:
            # This is a new file: create a new FileAttachmentHistory for it
            attachment_history = FileAttachmentHistory()
            attachment_revision = 1

            attachment_history.display_position = \
                FileAttachmentHistory.compute_next_display_position(
                    self.review_request)
            attachment_history.save()
            self.review_request.file_attachment_histories.add(
                attachment_history)
        else:
            attachment_history = self.cleaned_data['attachment_history']

            try:
                latest = attachment_history.file_attachments.latest()
            except FileAttachment.DoesNotExist:
                latest = None

            if latest is None:
                # This should theoretically never happen, but who knows.
                attachment_revision = 1
            elif latest.review_request.exists():
                # This is a new update in the draft.
                attachment_revision = latest.attachment_revision + 1
            else:
                # The most recent revision is part of the same draft. Delete it
                # and replace with the newly uploaded file.
                attachment_revision = latest.attachment_revision
                latest.delete()

        attachment_kwargs = {
            'attachment_history': attachment_history,
            'attachment_revision': attachment_revision,
            'caption': '',
            'draft_caption': caption,
            'orig_filename': os.path.basename(file_obj.name),
            'mimetype': mimetype,
        }

        if filediff:
            file_attachment = FileAttachment.objects.create_from_filediff(
                filediff, save=False, **attachment_kwargs)
        else:
            file_attachment = FileAttachment(**attachment_kwargs)

        file_attachment.file.save(filename, file_obj, save=True)

        draft = ReviewRequestDraft.create(self.review_request)
        draft.file_attachments.add(file_attachment)
        draft.save()

        return file_attachment
Example #13
0
    def create(self, filediff=None):
        file = self.files['path']
        caption = self.cleaned_data['caption'] or file.name

        # There are several things that can go wrong with browser-provided
        # mimetypes. In one case (bug 3427), Firefox on Linux Mint was
        # providing a mimetype that looked like 'text/text/application/pdf',
        # which is unparseable. IE also has a habit of setting any unknown file
        # type to 'application/octet-stream', rather than just choosing not to
        # provide a mimetype. In the case where what we get from the browser
        # is obviously wrong, try to guess.
        if (file.content_type and len(file.content_type.split('/')) == 2
                and file.content_type != 'application/octet-stream'):
            mimetype = file.content_type
        else:
            mimetype = self._guess_mimetype(file)

        filename = '%s__%s' % (uuid4(), file.name)

        if self.cleaned_data['attachment_history'] is None:
            # This is a new file: create a new FileAttachmentHistory for it
            attachment_history = FileAttachmentHistory()
            attachment_revision = 1

            attachment_history.display_position = \
                FileAttachmentHistory.compute_next_display_position(
                    self.review_request)
            attachment_history.save()
            self.review_request.file_attachment_histories.add(
                attachment_history)
        else:
            attachment_history = self.cleaned_data['attachment_history']

            try:
                latest = attachment_history.file_attachments.latest()
            except FileAttachment.DoesNotExist:
                latest = None

            if latest is None:
                # This should theoretically never happen, but who knows.
                attachment_revision = 1
            elif latest.review_request.exists():
                # This is a new update in the draft.
                attachment_revision = latest.attachment_revision + 1
            else:
                # The most recent revision is part of the same draft. Delete it
                # and replace with the newly uploaded file.
                attachment_revision = latest.attachment_revision
                latest.delete()

        attachment_kwargs = {
            'attachment_history': attachment_history,
            'attachment_revision': attachment_revision,
            'caption': '',
            'draft_caption': caption,
            'orig_filename': os.path.basename(file.name),
            'mimetype': mimetype,
        }

        if filediff:
            file_attachment = FileAttachment.objects.create_from_filediff(
                filediff, save=False, **attachment_kwargs)
        else:
            file_attachment = FileAttachment(**attachment_kwargs)

        file_attachment.file.save(filename, file, save=True)

        draft = ReviewRequestDraft.create(self.review_request)
        draft.file_attachments.add(file_attachment)
        draft.save()

        return file_attachment
Example #14
0
    def test_is_from_diff_with_repository(self):
        """Testing FileAttachment.is_from_diff with repository association"""
        repository = Repository.objects.get(pk=1)
        file_attachment = FileAttachment(repository=repository)

        self.assertTrue(file_attachment.is_from_diff)