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
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
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)
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
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 = []
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)
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)
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(), })
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)
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
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()
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()))
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)
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)
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)
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
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()
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
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)
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
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 = []
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())
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
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)
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)
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, }, ], }