Exemplo n.º 1
0
def save_uploaded_image(image_data, name_prefix, thumb_size=None):
    try:
        image = Image.open(image_data)
        image.verify()
    except IOError:
        raise ValidationError('Invalid image')

    try:
        hash = hashlib.md5(image_data.read()).hexdigest()
        name = "%s-%s.%s" % (name_prefix, hash, image.format.lower())

        image_file = File(image_data)
        image_file.name = name
        thumb_file = None

        if thumb_size is not None:
            # http://www.pythonware.com/library/pil/handbook/image.htm
            # ...if you need to load the image after using this method,
            # you must reopen the image file.
            image_data.seek(0)
            image = Image.open(image_data)
            image.thumbnail(thumb_size, Image.ANTIALIAS)
            temp = StringIO()
            image.save(temp, format=image.format)
            temp.seek(0)
            thumb_file = SimpleUploadedFile(
                'thumb-' + name, temp.read(),
                'image/%s' % image.format.lower())

        # Reset image position
        image_data.seek(0)

        return image_file, thumb_file
    except:
        raise ValidationError('Could not upload image')
Exemplo n.º 2
0
def test_admin_upload_studies_keep_study_fields(admin_client):
    filename = 'studies_sample_alt.csv'
    file_path = os.path.dirname(os.path.abspath(__file__))
    sample_csv = os.path.join(file_path, filename)

    with open(sample_csv, 'rb') as f:
        tmp_file = File(f)
        response = admin_client.post('/admin/studies/study/import_studies/',
                                     {'studies_file': tmp_file, 'study_id_field': 'STUDYID'})
    assert response.status_code == 302
    assert response.url == reverse('admin:studies_studyfield_changelist')

    filename = 'studies_sample.csv'
    file_path = os.path.dirname(os.path.abspath(__file__))
    sample_csv = os.path.join(file_path, filename)

    with open(sample_csv, 'rb') as f:
        tmp_file = File(f)
        response = admin_client.post('/admin/studies/study/import_studies/',
                                     {'studies_file': tmp_file, 'study_id_field': 'STUDYID', 'keep_fields': True})
    assert response.status_code == 302
    assert response.url == reverse('admin:studies_studyfield_changelist')

    assert len(Study.objects.all()) == 9
    assert StudyField.objects.count() == 48
    assert StudyVariable.objects.count() == 114
Exemplo n.º 3
0
    def save(self, *args, **kwargs):

        # This is not having the intended effect
        if 'file' not in self._meta.fields:
            self._meta.fields.append('file')

        if self.cleaned_data['unzip_archive']:
            new_attachments = []
            try:
                for zipinfo in self.zipfile.filelist:
                    f = tempfile.NamedTemporaryFile(mode='r+w')
                    f.write(self.zipfile.read(zipinfo.filename))
                    f = File(f, name=zipinfo.filename)
                    try:
                        attachment = models.Attachment()
                        attachment.article = self.article
                        attachment.original_filename = zipinfo.filename
                        attachment.save()
                        attachment.articles.add(self.article)
                        attachment_revision = models.AttachmentRevision()
                        attachment_revision.file = f
                        attachment_revision.description = self.cleaned_data[
                            'description']
                        attachment_revision.attachment = attachment
                        attachment_revision.set_from_request(self.request)
                        attachment_revision.save()
                        f.close()
                    except models.IllegalFileExtension:
                        raise
                    new_attachments.append(attachment_revision)
            except zipfile.BadZipfile:
                raise
            return new_attachments
        else:
            return super(AttachmentArchiveForm, self).save(*args, **kwargs)
Exemplo n.º 4
0
    def save(self, *args, **kwargs):

        # This is not having the intended effect
        if not 'file' in self._meta.fields:
            self._meta.fields.append('file')

        if self.cleaned_data['unzip_archive']:
            new_attachments = []
            try:
                for zipinfo in self.zipfile.filelist:
                    f = tempfile.NamedTemporaryFile(mode='r+w')
                    f.write(self.zipfile.read(zipinfo.filename))
                    f = File(f, name=zipinfo.filename)
                    try:
                        attachment = models.Attachment()
                        attachment.article = self.article
                        attachment.original_filename = zipinfo.filename
                        attachment.save()
                        attachment.articles.add(self.article)
                        attachment_revision = models.AttachmentRevision()
                        attachment_revision.file = f
                        attachment_revision.description = self.cleaned_data['description']
                        attachment_revision.attachment = attachment
                        attachment_revision.set_from_request(self.request)
                        attachment_revision.save()
                        f.close()
                    except models.IllegalFileExtension:
                        raise
                    new_attachments.append(attachment_revision)
            except zipfile.BadZipfile:
                raise
            return new_attachments
        else:
            return super(AttachmentArcihveForm, self).save(*args, **kwargs)
Exemplo n.º 5
0
def create_thumbnail(_file, size, path):
    image = Image.open(_file)
    image.thumbnail(size, Image.ANTIALIAS)
    thumb = File(io.BytesIO(), name=path)
    image.save(thumb, image.format)
    thumb.size = len(thumb.file.getvalue())  # to use default_storage.save file must have size
    default_storage.save(path, thumb)
    return image
Exemplo n.º 6
0
def create_thumbnail(_file, size, path):
    image = Image.open(_file)
    image.thumbnail(size, Image.ANTIALIAS)
    thumb = File(io.BytesIO(), name=path)
    image.save(thumb, image.format)
    thumb.size = len(thumb.file.getvalue()
                     )  # to use default_storage.save file must have size
    default_storage.save(path, thumb)
    return image
Exemplo n.º 7
0
    def process(self, category: FileCategoryDirectories, filename: str):
        msgs = MessageGroup()
        try:
            with TemporaryDirectory() as d:
                try:
                    msg = self.extractall(unpack_destination=d, filename=filename)
                except zipfile.BadZipFile as e:
                    msg = create_fs_message(e, StagingDirectories.sip, MessageLevels.error)
                except tarfile.TarError as e:
                    msg = create_fs_message(e, StagingDirectories.sip, MessageLevels.error)
                except Exception as e:
                    logger.exception("Error unpacking archive")
                    msg = create_fs_message(e, StagingDirectories.sip, MessageLevels.error)

                if msg is not None:
                    return msg

                rootdir = self.find_root_directory(d)
                for unpacked_file in Path(rootdir).rglob('*'):
                    if unpacked_file.is_file():
                        with File(unpacked_file.open('rb')) as unpacked_fileobj:
                            relpath = Path(category.name, unpacked_file.relative_to(rootdir))
                            msgs.append(self.sip_storage.log_save(name=str(relpath), content=unpacked_fileobj))
                msgs.downgrade()
        except Exception as e:
            msgs.append(create_fs_message(str(e), StagingDirectories.sip, MessageLevels.error))
        return msgs
Exemplo n.º 8
0
 def test_comment_json_reply_comment_as_issue(self):
     thread = self.review.revision.commentthread_set.latest()
     response = self.client.post(
         self.api_url, {
             'comment':
             "test_comment_json_reply_comment",
             '0-attachment':
             File(BytesIO(b'test_file_1'), name='test_file_1.png'),
             '0-description':
             'Test Description',
             '0-sort_order':
             1,
             'thread':
             thread.pk,
             'is_issue':
             True
         })
     self.assertStatusCode(response, 200)
     comment = models.Comment.objects.latest('created')
     self.assertIsNotNone(comment.issue)
     self.assertTrue(
         comment.events.filter(
             event_type__code=models.EventType.COMMENT_ADDED).exists())
     self.assertEqual(json.loads(response.content.decode('utf8')), {
         'status': 'success',
         'errors': '',
         'comment': comment.to_json()
     })
     self.assertEqual(comment.comment, 'test_comment_json_reply_comment')
     self.assertEqual(comment.thread, thread)
     self.assertEqual(comment.attachments.count(), 1)
Exemplo n.º 9
0
 def setUp(self):
     super(TestCommentAPIViews, self).setUp()
     assert self.client.login(username=self.user.username,
                              password='******')
     # Sample review
     self.review = models.Review.create_review(**self.default_review_kwargs)
     attachments = [{
         'attachment':
         File(BytesIO(b'test_file_1'), name='test_file_1.png'),
         'description':
         'Test Description',
     }]
     self.comment = models.Comment.create_comment(
         commenter=self.user,
         review=self.review.revision,
         comment='Test Comment',
         attachments=attachments,
     )
     self.api_url = reverse('comments-api',
                            kwargs={
                                'review_pk': self.review.pk,
                                'proj_slug': self.review.project.slug,
                                'rev_num': self.review.revision.number,
                            })
     # Reset out mail queue
     mail.outbox = []
Exemplo n.º 10
0
def test_admin_upload_idx_zip(admin_client):
    filename = 'IDX_SAMPLE.csv'
    file_path = os.path.dirname(os.path.abspath(__file__))
    sample_csv = os.path.join(file_path, filename)

    Domain.objects.create(code='RELTIVE', is_qualifier=True)
    Domain.objects.create(code='SAMPLE')

    with tempfile.NamedTemporaryFile(suffix='zip') as tmp:
        with zipfile.ZipFile(tmp, 'w') as archive:
            with open(sample_csv, 'rb') as f:
                archive.writestr(filename, f.read())
        tmp.seek(0)
        tmp_file = File(tmp)
        response = admin_client.post('/admin/studies/count/import_idx/',
                                     {'idx_file': tmp_file,
                                      'study_id_field': "STUDYID",
                                      'count_subj_field': "COUNT_SUBJ",
                                      'count_obs_field': "COUNT_OBS"})
    assert response.status_code == 302
    assert response.url == reverse('admin:studies_count_changelist')

    assert len(Study.objects.all()) == 1
    assert len(Variable.objects.all()) == 20
    assert len(Count.objects.all()) == 18
Exemplo n.º 11
0
 def test_comment_to_json(self):
     review = models.Review.create_review(**self.default_review_kwargs)
     thread = models.CommentThread.create_comment_thread(review.revision)
     attachments = [{
         'attachment':
         File(BytesIO(b'test_file_1'), name='test_file_1.png'),
         'sort_order':
         1,
         'description':
         'Test Description'
     }]
     comment = models.Comment.create_comment(
         commenter=self.user,
         review=review.revision,
         comment="test comment",
         thread=thread,
         attachments=attachments,
     )
     self.assertEqual(
         comment.to_json(), {
             'id': comment.pk,
             'thread': comment.thread.pk,
             'name': comment.commenter.userprofile.name,
             'comment': comment.comment,
             'attachment_count': comment.attachments.count(),
             'attachments': [comment.attachments.get().to_json()],
             'url': comment.get_absolute_url(),
             'thread_url': comment.get_absolute_thread_url(),
             'issue': {},
             'created': comment.created.isoformat(),
             'modified': comment.modified.isoformat(),
         })
Exemplo n.º 12
0
 def test_comment_json_create_comment_thread(self):
     response = self.client.post(
         self.api_url, {
             'comment':
             "test_comment_json_create_comment_thread",
             '0-attachment':
             File(BytesIO(b'test_file_1'), name='test_file_1.png'),
             '0-description':
             'Test Description',
         })
     self.assertStatusCode(response, 200)
     comment = models.Comment.objects.get(
         comment='test_comment_json_create_comment_thread')
     self.assertTrue(
         comment.events.filter(
             event_type__code=models.EventType.COMMENT_ADDED).exists())
     self.assertEqual(json.loads(response.content.decode('utf8')), {
         'status': 'success',
         'errors': '',
         'comment': comment.to_json()
     })
     self.assertIsNone(comment.issue)
     comment = self.review.revision.commentthread_set.latest(
     ).comment_set.get()
     self.assertTrue(
         comment.events.filter(
             event_type__code=models.EventType.COMMENT_ADDED).exists())
     self.assertEqual(comment.comment,
                      'test_comment_json_create_comment_thread')
     self.assertEqual(comment.attachments.count(), 1)
Exemplo n.º 13
0
    def test_comment_with_multiple_attachments(self):
        response = self.client.post(
            self.api_url, {
                'comment':
                'Test comment with multiple attachments',
                '0-attachment':
                File(BytesIO(b'test_file_1'), name='test_file_1.png'),
                '0-description':
                'Image Attachment',
                '1-attachment':
                File(BytesIO(b'test_file_2'), name='test_file_2.mov'),
                '1-description':
                'Movie Attachment',
                '2-attachment':
                File(BytesIO(b'test_file_3'), name='test_file_3.wav'),
                '2-description':
                'Audio Attachment',
            })
        self.assertStatusCode(response, 200)
        comment = models.Comment.objects.latest('created')
        self.assertTrue(
            comment.events.filter(
                event_type__code=models.EventType.COMMENT_ADDED).exists())
        self.assertEqual(comment.comment,
                         'Test comment with multiple attachments')
        self.assertEqual(comment.attachments.count(), 3)
        attachment_one, attachment_two, attachment_three = comment.attachments.all(
        ).order_by('sort_order')
        # One
        self.assertEqual(attachment_one.attachment.file.read(), b'test_file_1')
        self.assertEqual(attachment_one.attachment_type, 'image')
        self.assertEqual(attachment_one.description, 'Image Attachment')
        self.assertEqual(attachment_one.sort_order, 0)

        # Two
        self.assertEqual(attachment_two.attachment.file.read(), b'test_file_2')
        self.assertEqual(attachment_two.attachment_type, 'movie')
        self.assertEqual(attachment_two.description, 'Movie Attachment')
        self.assertEqual(attachment_two.sort_order, 1)

        # Three
        self.assertEqual(attachment_three.attachment.file.read(),
                         b'test_file_3')
        self.assertEqual(attachment_three.attachment_type, 'audio')
        self.assertEqual(attachment_three.description, 'Audio Attachment')
        self.assertEqual(attachment_three.sort_order, 2)
Exemplo n.º 14
0
def save_uploaded_image(image_data, name_prefix, thumb_size=None):
    image_data.seek(0, os.SEEK_END)
    file_size = image_data.tell()

    if file_size > settings.MAXIMUM_IMAGE_SIZE:
        raise ValidationError(trans('The uploaded image is too large'))

    image_data.seek(0)

    try:
        image = Image.open(image_data)
        image.verify()
    except IOError:
        raise ValidationError(trans('Invalid image'))

    try:
        hash = hashlib.md5(image_data.read()).hexdigest()
        name = "%s-%s.%s" % (name_prefix, hash, image.format.lower())

        image_file = File(image_data)
        image_file.name = name
        thumb_file = None

        if thumb_size is not None:
            # http://effbot.org/imagingbook/image.htm
            # ...if you need to load the image after using this method,
            # you must reopen the image file.
            image_data.seek(0)
            image = Image.open(image_data)
            image.thumbnail(thumb_size, Image.ANTIALIAS)
            temp = StringIO()
            image.save(temp, format=image.format)
            temp.seek(0)
            thumb_file = SimpleUploadedFile(
                'thumb-' + name, temp.read(),
                'image/%s' % image.format.lower())

        # Reset image position
        image_data.seek(0)

        return image_file, thumb_file
    except:
        raise ValidationError(trans('Image upload issue'))
Exemplo n.º 15
0
def test_importstudiesform_valid():
    filename = 'studies_sample.csv'
    file_path = os.path.dirname(os.path.abspath(__file__))
    sample_csv = os.path.join(file_path, filename)
    with open(sample_csv, 'rb') as f:
        tmp_file = File(f)
        files = dict(studies_file=tmp_file)
        data = dict(study_id_field="Study_ID")
        form = ImportStudiesForm(files=files, data=data)
        assert form.is_valid()
Exemplo n.º 16
0
 def setUpClass(cls):
     super().setUpClass()
     target_identifier = 't{}'.format(datetime.now().timestamp())
     cls.target = Target.objects.create(name=target_identifier)
     cls.prods = [
         DataProduct.objects.create(product_id=f'test_{i}',
                                    target=cls.target) for i in range(4)
     ]
     for prod in cls.prods:
         fn = f'{prod.product_id}_file'
         prod.data.save(fn, File(BytesIO()))
Exemplo n.º 17
0
 def test_create_attachment(self):
     attachment = models.Attachment.create_attachment(
         attachment=File(BytesIO(b'test_file_1'), name='test_file_1.mov'),
         description='Create Attachment',
         sort_order=5,
         content_object=self.review,
     )
     self.assertEqual(attachment.description, 'Create Attachment')
     self.assertEqual(attachment.attachment_type, constants.MOVIE)
     self.assertEqual(attachment.sort_order, 5)
     self.assertEqual(attachment.content_object, self.review)
Exemplo n.º 18
0
    def test_form_add(self):
        base64_picture_mime = b64.encodestring(PICTURE)
        Form = modelform_factory(WebcamTestModel)

        form = Form({'data_photo': base64_picture_mime})
        assert form.is_valid(), form._errors
        obj = form.save()

        self.assertTrue(os.path.isfile(temp_storage.path(obj.photo.name)),
                        '`{0.photo.name}` is not a file'.format(obj))
        self.assertEqual(len(obj.photo), File(file(PICTURE_PATH)).size)
Exemplo n.º 19
0
 def test_import_add_jobs(self):
     project = ProjectFactory(with_job=True)
     job = project.jobs.get()
     path = get_test_data_path("25144280.x83")
     form = JobImportForm(project=project,
                          data={},
                          files={"file": File(path.open())})
     self.assertTrue(form.is_valid())
     self.assertEqual(project.jobs.count(), 1)
     form.save()
     self.assertEqual(project.jobs.count(), 2)
     new_job = project.jobs.exclude(pk=job.pk).get()
     self.assertEqual("Dachdecker- und Klempnerarbeiten", new_job.name)
     self.assertTrue(new_job.account)
Exemplo n.º 20
0
def test_admin_upload_studies_study_id_field_not_in_files(admin_client):
    filename = 'studies_sample_alt.csv'
    file_path = os.path.dirname(os.path.abspath(__file__))
    sample_csv = os.path.join(file_path, filename)
    study_info_field = 'FOOBAR'

    with open(sample_csv, 'rb') as f:
        tmp_file = File(f)
        msg = ('Please ensure that FOOBAR is a column header in '
               'uploaded file')
        with pytest.raises(CommandError) as excinfo:
            admin_client.post('/admin/studies/study/import_studies/',
                              {'studies_file': tmp_file,
                               'study_id_field': study_info_field})
    assert str(excinfo.value) == msg
Exemplo n.º 21
0
def test_importidxform_valid():
    filename = 'IDX_SAMPLE.csv'
    file_path = os.path.dirname(os.path.abspath(__file__))
    sample_csv = os.path.join(file_path, filename)
    with tempfile.NamedTemporaryFile(suffix='zip') as tmp:
        with zipfile.ZipFile(tmp, 'w') as archive:
            with open(sample_csv, 'rb') as f:
                archive.writestr(filename, f.read())
        tmp.seek(0)
        tmp_file = File(tmp)
        data = dict(study_id_field="Study_ID",
                    count_subj_field="COUNT_SUBJ",
                    count_obs_field="COUNT_OBS")
        form = ImportIDXForm(files=dict(idx_file=tmp_file), data=data)
        assert form.is_valid()
Exemplo n.º 22
0
    def build_sip(self, originals_dir: Optional[str] = None):
        logger.info("building sip")
        if originals_dir is None:
            originals_dir = self.originals_dir
        originals_storage = self.get_originals_storage(originals_dir)
        sip_storage = self.get_sip_storage()
        sip_storage.clear()

        msgs = self._create_msg_group()
        for name in originals_storage.list():
            path = self.originals_dir.joinpath(name)
            logger.debug("adding file: {}".format(path.relative_to(self.originals_dir)))
            category = get_category(Path(name).parts[0])
            with File(path.open('rb')) as f:
                msgs.append(self._add_to_sip(name=str(name), content=f, category=category))

        return msgs
Exemplo n.º 23
0
 def test_comment_creation_without_comment(self):
     response = self.client.post(
         self.api_url, {
             'comment':
             "",
             '0-attachment':
             File(BytesIO(b'test_file_1'), name='test_file_1.png'),
             '0-description':
             'Test Description',
         })
     self.assertStatusCode(response, 200)
     comment = models.Comment.objects.latest('created')
     self.assertTrue(
         comment.events.filter(
             event_type__code=models.EventType.COMMENT_ADDED).exists())
     self.assertIsNone(comment.issue)
     self.assertEqual(comment.comment, '')
     self.assertEqual(comment.attachments.count(), 1)
Exemplo n.º 24
0
def test_admin_upload_studies_clear_all(admin_client):
    filename = 'studies_sample.csv'
    file_path = os.path.dirname(os.path.abspath(__file__))
    sample_csv = os.path.join(file_path, filename)

    with open(sample_csv, 'rb') as f:
        tmp_file = File(f)
        response = admin_client.post('/admin/studies/study/import_studies/',
                                     {'studies_file': tmp_file, 'study_id_field': 'STUDYID'})
    assert response.status_code == 302
    assert response.url == reverse('admin:studies_studyfield_changelist')

    assert len(Study.objects.all()) == 9
    assert StudyField.objects.count() == 47
    assert StudyVariable.objects.count() == 114
    counts = (StudyVariable.objects
                           .filter(study_field__field_name='Subject_Count')
                           .values_list('value', flat=True))
    assert sum([float(c) for c in counts if c is not None]) == 708147
Exemplo n.º 25
0
 def setUp(self):
     super(TestCommentViews, self).setUp()
     assert self.client.login(username=self.user.username,
                              password='******')
     # Sample review
     self.review = models.Review.create_review(**self.default_review_kwargs)
     attachments = [{
         'attachment':
         File(BytesIO(b'test_file_1'), name='test_file_1.png'),
         'description':
         'Test Description',
     }]
     self.comment = models.Comment.create_comment(
         commenter=self.user,
         review=self.review.revision,
         comment='Test Comment',
         attachments=attachments,
     )
     # Reset out mail queue
     mail.outbox = []
Exemplo n.º 26
0
 def test_create_comment_on_draft(self):
     self.review.state = constants.DRAFT
     self.review.save(update_fields=['state'])
     models.UserReviewStatus.objects.filter(review=self.review).update(
         read=True)
     mail.outbox = []
     comment = models.Comment.create_comment(
         commenter=self.user,
         review=self.review.revision,
         comment='Test Comment',
         attachments=[
             {
                 'attachment':
                 File(BytesIO(b'test_file_1'), name='test_file_1.png'),
                 'description':
                 'Image 1',
             },
         ])
     self.assertEqual(
         comment.get_absolute_url(), '{}#{}'.format(
             reverse('review-rev-detail',
                     kwargs={
                         'proj_slug': self.review.project.slug,
                         'pk': self.review.pk,
                         'rev_num': self.review.revision.number,
                     }), comment.thread.pk))
     self.assertEqual(comment.thread.review_revision, self.review.revision)
     self.assertEqual(comment.attachments.count(), 1)
     attachment = comment.attachments.get()
     self.assertEqual(attachment.description, 'Image 1')
     self.assertEqual(attachment.attachment_type, 'image')
     self.assertEqual(attachment.sort_order, 0)
     self.assertEqual(comment.commenter, self.user)
     self.assertEqual(comment.comment, 'Test Comment')
     self.assertEqual(len(mail.outbox), 0)
     self.assertEqual(
         comment.__str__(),
         'Comment by {} on Review: {}'.format(comment.commenter.username,
                                              self.review.title))
     self.assertFalse(self.task_patch.delay.called)
     self.assertFalse(comment.events.exists())
Exemplo n.º 27
0
 def save(self, commit=True):
     adding = self.instance._state.adding
     company = super().save(commit)
     if adding:
         company.activate()
         Worker.objects.create(company=company,
                               user=self.user,
                               is_owner=True)
         Project.objects.create(name="Template Project", is_template=True)
         create_chart_of_accounts()
         demo_letterhead = os.path.join(settings.MEDIA_ROOT,
                                        "demo_letterhead.pdf")
         letterhead = clean_letterhead_pdf(File(open(demo_letterhead,
                                                     "rb")),
                                           save=True)
         DocumentSettings.objects.create(
             proposal_letterhead=letterhead,
             invoice_letterhead=letterhead,
             evidence_letterhead=letterhead,
             itemized_letterhead=letterhead,
             timesheet_letterhead=letterhead,
         )
     return company
Exemplo n.º 28
0
 def test_create_comment_with_thread(self):
     review = models.Review.create_review(**self.default_review_kwargs)
     thread = models.CommentThread.create_comment_thread(review.revision)
     models.UserReviewStatus.objects.filter(review=review).update(read=True)
     attachments = [{
         'attachment':
         File(BytesIO(b'test_file_1'), name='test_file_1.png'),
         'sort_order':
         1,
         'description':
         'Test Description'
     }]
     mail.outbox = []
     comment = models.Comment.create_comment(
         commenter=self.user,
         review=review.revision,
         comment='Test Comment',
         attachments=attachments,
         thread=thread,
     )
     self.assertEqual(comment.thread, thread)
     self.assertEqual(comment.thread.review_revision, review.revision)
     self.assertEqual(comment.attachments.count(), 1)
     attachment = comment.attachments.get()
     self.assertEqual(attachment.description, 'Test Description')
     self.assertEqual(attachment.attachment_type, 'image')
     self.assertEqual(comment.commenter, self.user)
     self.assertEqual(comment.comment, 'Test Comment')
     event = comment.events.get()
     self.assertEqual(event.event_type.code, event.event_type.COMMENT_ADDED)
     self.assertEqual(event.related_object, comment)
     self.assertEqual(event.user, comment.commenter)
     self.assertEqual(len(mail.outbox), 5)
     statuses = models.UserReviewStatus.objects.filter(review=review)
     self.assertEqual(statuses.count(), 6)
     self.assertEqual(statuses.filter(read=True).count(), 1)
     self.assertEqual(statuses.filter(read=False).count(), 5)
Exemplo n.º 29
0
 def test_create_comment(self):
     dropped_reviewer = self.review.reviewer_set.all()[0]
     dropped_follower = self.review.follower_set.all()[0]
     dropped_reviewer.drop_reviewer(
         self.review.creator_set.active().get().user)
     dropped_follower.drop_follower(
         self.review.creator_set.active().get().user)
     mail.outbox = []
     models.UserReviewStatus.objects.filter(review=self.review).update(
         read=True)
     attachments = [
         {
             'attachment': File(BytesIO(b'test_file_1'),
                                name='test_file_1.png'),
             'description': 'Image 1',
         },
         {
             'attachment': File(BytesIO(b'test_file_2'),
                                name='test_file_2.jpg'),
             'description': 'Image 2',
         },
     ]
     comment = models.Comment.create_comment(
         commenter=self.user,
         review=self.review.revision,
         comment='Test Comment',
         attachments=attachments,
     )
     self.assertEqual(
         comment.get_absolute_url(), '{}#{}'.format(
             reverse('review-rev-detail',
                     kwargs={
                         'proj_slug': self.review.project.slug,
                         'pk': self.review.pk,
                         'rev_num': self.review.revision.number,
                     }), comment.pk))
     self.assertIsNone(comment.issue)
     self.assertEqual(comment.thread.review_revision, self.review.revision)
     self.assertEqual(comment.attachments.count(), 2)
     attachment_one, attachment_two = comment.attachments.all()
     self.assertEqual(attachment_one.description, 'Image 1')
     self.assertEqual(attachment_one.attachment_type, 'image')
     self.assertEqual(attachment_one.sort_order, 0)
     self.assertEqual(attachment_two.description, 'Image 2')
     self.assertEqual(attachment_two.attachment_type, 'image')
     self.assertEqual(attachment_two.sort_order, 1)
     self.assertEqual(comment.commenter, self.user)
     self.assertEqual(comment.comment, 'Test Comment')
     self.assertEqual(len(mail.outbox), 3)
     statuses = models.UserReviewStatus.objects.filter(review=self.review)
     self.assertEqual(statuses.count(), 6)
     # Dropped people aren't updated
     self.assertEqual(statuses.filter(read=True).count(), 3)
     self.assertEqual(statuses.filter(read=False).count(), 3)
     self.assertEqual(
         comment.__str__(),
         'Comment by {} on Review: {}'.format(comment.commenter.username,
                                              self.review.title))
     self.task_patch.delay.assert_called_with(
         self.review.pk,
         self.hook.pk,
         {'comment': comment.to_json()},
     )
     event = comment.events.get()
     self.assertEqual(event.event_type.code, event.event_type.COMMENT_ADDED)
     self.assertEqual(event.related_object, comment)
     self.assertEqual(event.user, comment.commenter)
Exemplo n.º 30
0
    def setUp(self):
        self.user = User.objects.create_user(username='******')
        self.user.set_password('testing')
        self.user.email = '*****@*****.**'
        self.user.save()
        self.co_owner = User.objects.create_user(username='******')
        self.co_owner.set_password('testing')
        self.co_owner.email = '*****@*****.**'
        self.co_owner.save()
        self.system_user = User.objects.get(username='******')
        self.group = models.Group.objects.get(slug='default-group')
        self.project = models.Project.objects.get(slug='default-project')
        models.GroupMember.objects.create(user=self.user, group=self.group)
        models.GroupMember.objects.create(user=self.co_owner, group=self.group)
        for count in range(0, 3):
            u = User.objects.create_user(username='******'.format(count))
            u.set_password('testing')
            u.email = '{}@example.com'.format(u.username)
            u.save()
            models.GroupMember.objects.create(user=u, group=self.group)

        for count in range(0, 2):
            u = User.objects.create_user(username='******'.format(count))
            u.set_password('testing')
            u.email = '{}@example.com'.format(u.username)
            u.save()
            models.GroupMember.objects.create(user=u, group=self.group)

        self.test_users = User.objects.filter(
            username__startswith="test_user_")
        self.followers = User.objects.filter(username__startswith='follower_')
        self.default_review_kwargs = {
            'creators': [self.user],
            'title':
            'Test Title',
            'description':
            'Test Description',
            'case_link':
            'http://example.org/',
            'reviewers':
            self.test_users,
            'followers':
            self.followers,
            'project':
            self.project,
            'state':
            constants.OPEN,
            'attachments': [
                {
                    'attachment':
                    File(BytesIO(b'test_file_1'), name='test_file_1.jpeg'),
                    'description':
                    'Testing',
                    'sort_order':
                    1,
                },
                {
                    'attachment':
                    File(BytesIO(b'test_file_2'), name='test_file_2.png'),
                    'description':
                    'Testing',
                    'sort_order':
                    2,
                },
            ],
        }