def test_large_upload(self): tdir = tempfile.gettempdir() file1 = tempfile.NamedTemporaryFile(suffix=".file1", dir=tdir) file1.write('a' * (2 ** 21)) file1.seek(0) file2 = tempfile.NamedTemporaryFile(suffix=".file2", dir=tdir) file2.write('a' * (10 * 2 ** 20)) file2.seek(0) post_data = { 'name': 'Ringo', 'file_field1': file1, 'file_field2': file2, } for key in post_data.keys(): try: post_data[key + '_hash'] = sha_constructor(post_data[key].read()).hexdigest() post_data[key].seek(0) except AttributeError: post_data[key + '_hash'] = sha_constructor(post_data[key]).hexdigest() response = self.client.post('/file_uploads/verify/', post_data) self.assertEqual(response.status_code, 200)
def test_file_content(self): tdir = tempfile.gettempdir() file = tempfile.NamedTemporaryFile with file(suffix=".ctype_extra", dir=tdir) as no_content_type, \ file(suffix=".ctype_extra", dir=tdir) as simple_file: no_content_type.write(b'no content') no_content_type.seek(0) simple_file.write(b'text content') simple_file.seek(0) simple_file.content_type = 'text/plain' string_io = StringIO('string content') bytes_io = BytesIO(b'binary content') response = self.client.post('/echo_content/', { 'no_content_type': no_content_type, 'simple_file': simple_file, 'string': string_io, 'binary': bytes_io, }) received = json.loads(response.content.decode('utf-8')) self.assertEqual(received['no_content_type'], 'no content') self.assertEqual(received['simple_file'], 'text content') self.assertEqual(received['string'], 'string content') self.assertEqual(received['binary'], 'binary content')
def test_large_upload(self): tdir = tempfile.gettempdir() file = tempfile.NamedTemporaryFile with file(suffix=".file1", dir=tdir) as file1, file(suffix=".file2", dir=tdir) as file2: file1.write(b'a' * (2 ** 21)) file1.seek(0) file2.write(b'a' * (10 * 2 ** 20)) file2.seek(0) post_data = { 'name': 'Ringo', 'file_field1': file1, 'file_field2': file2, } for key in list(post_data): try: post_data[key + '_hash'] = hashlib.sha1(post_data[key].read()).hexdigest() post_data[key].seek(0) except AttributeError: post_data[key + '_hash'] = hashlib.sha1(force_bytes(post_data[key])).hexdigest() response = self.client.post('/verify/', post_data) self.assertEqual(response.status_code, 200)
def make_test_file(content=None): if content == None: content = 'I am a test file for upload.' # Shamelessly stolen from Django's own file-upload tests. tdir = tempfile.gettempdir() file_for_upload = tempfile.NamedTemporaryFile(suffix=".txt", dir=tdir) file_for_upload.write(content) file_for_upload.seek(0) return file_for_upload
def test_should_not_accept_exe_source_file(self): tdir = temp.gettempdir() tmp_file = temp.NamedTemporaryFile with tmp_file(suffix=".exe", dir=tdir) as source_file: source_file.write('a' * (2 ** 21)) source_file.seek(0) data = self.formset(source=source_file) response = self.client.post(self.url, data) assert response.status_code == 200 assert not Version.objects.get(pk=self.version.pk).source
def test_should_not_accept_exe_source_file(self): with temp.NamedTemporaryFile( suffix='.exe', dir=temp.gettempdir()) as source_file: with zipfile.ZipFile(source_file, 'w') as zip_file: zip_file.writestr('foo', 'a' * (2 ** 21)) source_file.seek(0) data = self.formset(source=source_file) response = self.client.post(self.url, data) assert response.status_code == 200 assert not Version.objects.get(pk=self.version.pk).source
def test_edit_attachment(self): file_for_upload = make_test_file( content='I am a test file for editing.') post_data = { 'title': 'Test editing file', 'description': 'A test file for editing.', 'comment': 'Initial upload', 'file': file_for_upload, } resp = self.client.post(reverse('attachments.new_attachment'), data=post_data) tdir = tempfile.gettempdir() edited_file_for_upload = tempfile.NamedTemporaryFile(suffix=".txt", dir=tdir) edited_file_for_upload.write( 'I am a new version of the test file for editing.') edited_file_for_upload.seek(0) post_data = { 'title': 'Test editing file', 'description': 'A test file for editing.', 'comment': 'Second revision.', 'file': edited_file_for_upload, } attachment = Attachment.objects.get(title='Test editing file') resp = self.client.post(reverse('attachments.edit_attachment', kwargs={ 'attachment_id': attachment.id, }), data=post_data) eq_(302, resp.status_code) # Re-fetch because it's been updated. attachment = Attachment.objects.get(title='Test editing file') eq_(resp['Location'], 'http://testserver%s' % attachment.get_absolute_url()) eq_(2, attachment.revisions.count()) rev = attachment.current_revision eq_('admin', rev.creator.username) eq_('Second revision.', rev.comment) ok_(rev.is_approved) url = attachment.get_file_url() resp = self.client.get(url, HTTP_HOST=settings.ATTACHMENT_HOST) eq_('text/plain', rev.mime_type) ok_('I am a new version of the test file for editing.' in resp.streaming_content)
def course_syllabus(request, slug): course = Course.objects.get(slug=slug) syllabus = Syllabus.objects.filter(course=course,user=request.user) SyllabusFormset = modelformset_factory(Syllabus, form=SyllabusForm, extra=1, max_num=1) formset_syl = SyllabusFormset(request.POST or None, request.FILES or None, queryset=syllabus) BASE_DIR = os.path.dirname(os.path.dirname(__file__)) print tempfile.gettempdir() if request.method == 'POST': if formset_syl.is_valid(): for form in formset_syl: form1 = form.save(commit=False) form1.user = request.user form1.path = request.get_full_path() form1.course = course if '.docx' in str(form1.syllabus): form1.save() else: data = {} data['response'] = False new_data = json.dumps(data) return HttpResponse(new_data, content_type='application/json') AWS_KEY = #key AWS_SECRET = #secret aws_connection = S3Connection(AWS_KEY, AWS_SECRET) obj = Syllabus.objects.get(course=course,user=request.user) bucket_name = #bucket key = aws_connection.get_bucket(bucket_name).get_key('media/' + str(obj.syllabus)) res = key.get_contents_to_filename(BASE_DIR +'/'+key.name) html = convert(BASE_DIR +'/'+key.name) obj.html = html obj.save() response_data = {} response_data['response'] = True new_data = json.dumps(response_data) return HttpResponse(new_data, content_type='application/json')
def html2pdf(json_data): file_name = "report.pdf" pdf_absolute_path = tempfile.gettempdir() + "/" + file_name HTML(string=json_data).write_pdf(pdf_absolute_path) try: pdf = open(pdf_absolute_path, "rb") response = FileResponse(pdf, content_type='application/pdf') except IOError: return HttpResponseNotFound() response['Content-Disposition'] = 'attachment; filename=' + file_name return response
def make_test_file(content=None, suffix='.txt'): """ Create a fake file for testing purposes. """ if content is None: content = 'I am a test file for upload.' # Shamelessly stolen from Django's own file-upload tests. tdir = tempfile.gettempdir() file_for_upload = tempfile.NamedTemporaryFile(suffix=suffix, dir=tdir) file_for_upload.write(content.encode('utf-8')) file_for_upload.seek(0) return file_for_upload
def test_edit_attachment(self): file_for_upload = make_test_file( content='I am a test file for editing.') post_data = { 'title': 'Test editing file', 'description': 'A test file for editing.', 'comment': 'Initial upload', 'file': file_for_upload, } resp = self.client.post(reverse('attachments.new_attachment'), data=post_data) tdir = tempfile.gettempdir() edited_file_for_upload = tempfile.NamedTemporaryFile(suffix=".txt", dir=tdir) edited_file_for_upload.write( 'I am a new version of the test file for editing.') edited_file_for_upload.seek(0) post_data = { 'title': 'Test editing file', 'description': 'A test file for editing.', 'comment': 'Second revision.', 'file': edited_file_for_upload, } attachment = Attachment.objects.get(title='Test editing file') resp = self.client.post(reverse('attachments.edit_attachment', kwargs={ 'attachment_id': attachment.id, }), data=post_data) eq_(302, resp.status_code) # Re-fetch because it's been updated. attachment = Attachment.objects.get(title='Test editing file') eq_(resp['Location'], 'http://testserver%s' % attachment.get_absolute_url()) eq_(2, attachment.revisions.count()) rev = attachment.current_revision eq_('admin', rev.creator.username) eq_('Second revision.', rev.comment) ok_(rev.is_approved) url = attachment.get_file_url() resp = self.client.get(url, HTTP_HOST=settings.ATTACHMENT_HOST) eq_('text/plain', rev.mime_type) ok_('I am a new version of the test file for editing.' in resp.content)
def test_should_accept_zip_source_file(self): tdir = temp.gettempdir() tmp_file = temp.NamedTemporaryFile with tmp_file(suffix=".zip", dir=tdir) as source_file: source_file.write('a' * (2**21)) source_file.seek(0) data = self.formset(source=source_file) response = self.client.post(self.url, data) eq_(response.status_code, 302) version = Version.objects.get(pk=self.version.pk) assert version.source assert version.addon.admin_review
def test_should_accept_zip_source_file(self): tdir = temp.gettempdir() tmp_file = temp.NamedTemporaryFile with tmp_file(suffix=".zip", dir=tdir) as source_file: source_file.write('a' * (2 ** 21)) source_file.seek(0) data = self.formset(source=source_file) response = self.client.post(self.url, data) eq_(response.status_code, 302) version = Version.objects.get(pk=self.version.pk) assert version.source assert version.addon.admin_review
def setUp(self): self.addon = Addon.objects.get(pk=3615) self.version = self.addon._latest_version tdir = temp.gettempdir() self.source_file = temp.NamedTemporaryFile(suffix=".zip", dir=tdir) self.source_file.write('a' * (2**21)) self.source_file.seek(0) self.version.source = DjangoFile(self.source_file) self.version.save() self.user = UserProfile.objects.get(email="*****@*****.**") self.group = Group.objects.create(name='Editors BinarySource', rules='Editors:BinarySource') self.url = reverse('downloads.source', args=(self.version.pk, ))
def setUp(self): super(TestThumbnailer, self).setUp() dir_ = os.path.abspath(gettempdir()) def mkdir(name): try: os.mkdir(os.path.join(dir_, name)) except: pass mkdir('processed') mkdir('original') mkdir('p') self.fs = LocalDiskFS(dir_).operation()
def test_update_source_file_should_drop_info_request_flag(self): version = Version.objects.get(pk=self.version.pk) version.has_info_request = True version.save() tdir = temp.gettempdir() tmp_file = temp.NamedTemporaryFile with tmp_file(suffix=".zip", dir=tdir) as source_file: source_file.write('a' * (2 ** 21)) source_file.seek(0) data = self.formset(source=source_file) response = self.client.post(self.url, data) version = Version.objects.get(pk=self.version.pk) assert response.status_code == 302 assert not version.has_info_request
def test_submit_source_commits_to_git(self): with temp.NamedTemporaryFile( suffix='.zip', dir=temp.gettempdir()) as source_file: with zipfile.ZipFile(source_file, 'w') as zip_file: zip_file.writestr('foo', 'a' * (2 ** 21)) source_file.seek(0) data = self.formset(source=source_file) response = self.client.post(self.url, data) assert response.status_code == 302 version = Version.objects.get(pk=self.version.pk) assert version.source repo = AddonGitRepository(self.addon.pk, package_type='source') assert os.path.exists(repo.git_repository_path)
def test_submit_source_commits_to_git(self): with temp.NamedTemporaryFile(suffix='.zip', dir=temp.gettempdir()) as source_file: with zipfile.ZipFile(source_file, 'w') as zip_file: zip_file.writestr('foo', 'a' * (2**21)) source_file.seek(0) data = self.formset(source=source_file) response = self.client.post(self.url, data) assert response.status_code == 302 version = Version.objects.get(pk=self.version.pk) assert version.source repo = AddonGitRepository(self.addon.pk, package_type='source') assert os.path.exists(repo.git_repository_path)
def test_existing_source_link(self): tmp_file = temp.NamedTemporaryFile with tmp_file(suffix=".zip", dir=temp.gettempdir()) as source_file: source_file.write('a' * (2**21)) source_file.seek(0) self.version.source = DjangoFile(source_file) self.version.save() response = self.client.get(self.url) doc = pq(response.content) link = doc('.current-source-link') assert link assert link.text() == 'View current' assert link[0].attrib['href'] == reverse('downloads.source', args=(self.version.pk, ))
def setUp(self): self.addon = Addon.objects.get(pk=3615) self.version = self.addon._latest_version tdir = temp.gettempdir() self.source_file = temp.NamedTemporaryFile(suffix=".zip", dir=tdir) self.source_file.write('a' * (2 ** 21)) self.source_file.seek(0) self.version.source = DjangoFile(self.source_file) self.version.save() self.user = UserProfile.objects.get(email="*****@*****.**") self.group = Group.objects.create( name='Editors BinarySource', rules='Editors:BinarySource' ) self.url = reverse('downloads.source', args=(self.version.pk, ))
def setUp(self): self.client.login(username='******', password='******') # Set up test Picture and Gallery. # These must be set up here instead of in fixtures in order to allow Picture # to use a NamedTemporaryFile. tdir = tempfile.gettempdir() file1 = tempfile.NamedTemporaryFile(suffix=".file1", dir=tdir) file1.write('a' * (2 ** 21)) filename = file1.name file1.close() g = Gallery(name="Test Gallery") g.save() p = Picture(name="Test Picture", image=filename, gallery=g) p.save()
def test_existing_source_link(self): tmp_file = temp.NamedTemporaryFile with tmp_file(suffix=".zip", dir=temp.gettempdir()) as source_file: source_file.write('a' * (2 ** 21)) source_file.seek(0) self.version.source = DjangoFile(source_file) self.version.save() response = self.client.get(self.url) doc = pq(response.content) link = doc('.current-source-link') assert link assert link.text() == 'View current' assert link[0].attrib['href'] == reverse( 'downloads.source', args=(self.version.pk, ))
def html2pdf(json_data): file_name = "report.pdf" pdf_absolute_path = tempfile.gettempdir() + "/" + file_name result_file = open(pdf_absolute_path, "w+b") pisa_status = pisa.CreatePDF( json_data, # the HTML to convert dest=result_file) # file handle to recieve result result_file.close() try: pdf = open(pdf_absolute_path, "rb") response = FileResponse(pdf, content_type='application/pdf') except IOError: return HttpResponseNotFound() response['Content-Disposition'] = 'attachment; filename=' + file_name return response
def test_should_accept_zip_source_file(self): tdir = temp.gettempdir() tmp_file = temp.NamedTemporaryFile with tmp_file(suffix=".zip", dir=tdir) as source_file: source_file.write('a' * (2**21)) source_file.seek(0) data = self.formset(source=source_file) response = self.client.post(self.url, data) assert response.status_code == 302 version = Version.objects.get(pk=self.version.pk) assert version.source assert version.addon.needs_admin_code_review # Check that the corresponding automatic activity log has been created. log = ActivityLog.objects.get(action=amo.LOG.SOURCE_CODE_UPLOADED.id) assert log
def get(self, request, token): instance = Instance.objects.select_related( 'project__bundle_identifier', 'project__name').get(token=token) tempdir = temp.gettempdir() zipped_file = ZipFile(instance.ipa_path.path) path = request.GET['path'] extracted_file = zipped_file.extract( 'Payload/' + instance.name + '.app' + path, tempdir) # mime = magic.from_file(extracted_file, mime=True) mime = "application/octet-stream .ipa" response = HttpResponse(open(extracted_file, "rb"), content_type=mime) response['Content-Disposition'] = 'attachment; filename=' + path.split( '/')[-1] return response
def test_should_accept_zip_source_file(self): tdir = temp.gettempdir() tmp_file = temp.NamedTemporaryFile with tmp_file(suffix=".zip", dir=tdir) as source_file: source_file.write('a' * (2 ** 21)) source_file.seek(0) data = self.formset(source=source_file) response = self.client.post(self.url, data) assert response.status_code == 302 version = Version.objects.get(pk=self.version.pk) assert version.source assert version.addon.admin_review # Check that the corresponding automatic activity log has been created. log = ActivityLog.objects.get(action=amo.LOG.SOURCE_CODE_UPLOADED.id) assert log
def test_edit_attachment(self): file_for_upload = make_test_file(content="I am a test file for editing.") post_data = { "title": "Test editing file", "description": "A test file for editing.", "comment": "Initial upload", "file": file_for_upload, } resp = self.client.post(reverse("attachments.new_attachment"), data=post_data) tdir = tempfile.gettempdir() edited_file_for_upload = tempfile.NamedTemporaryFile(suffix=".txt", dir=tdir) edited_file_for_upload.write("I am a new version of the test file for editing.") edited_file_for_upload.seek(0) post_data = { "title": "Test editing file", "description": "A test file for editing.", "comment": "Second revision.", "file": edited_file_for_upload, } attachment = Attachment.objects.get(title="Test editing file") resp = self.client.post( reverse("attachments.edit_attachment", kwargs={"attachment_id": attachment.id}), data=post_data ) eq_(302, resp.status_code) # Re-fetch because it's been updated. attachment = Attachment.objects.get(title="Test editing file") eq_(resp["Location"], "http://testserver%s" % attachment.get_absolute_url()) eq_(2, attachment.revisions.count()) rev = attachment.current_revision eq_("admin", rev.creator.username) eq_("Second revision.", rev.comment) ok_(rev.is_approved) url = attachment.get_file_url() resp = self.client.get(url, HTTP_HOST=settings.ATTACHMENT_HOST) eq_("text/plain", rev.mime_type) ok_("I am a new version of the test file for editing." in resp.content)
def test_existing_source_link(self): with temp.NamedTemporaryFile(suffix='.zip', dir=temp.gettempdir()) as source_file: with zipfile.ZipFile(source_file, 'w') as zip_file: zip_file.writestr('foo', 'a' * (2**21)) source_file.seek(0) self.version.source.save(os.path.basename(source_file.name), DjangoFile(source_file)) self.version.save() response = self.client.get(self.url) doc = pq(response.content) link = doc('.current-source-link') assert link assert link.text() == 'View current' assert link[0].attrib['href'] == reverse('downloads.source', args=(self.version.pk, ))
def setUp(self): super(TestDownloadSource, self).setUp() self.addon = Addon.objects.get(pk=3615) # Make sure non-ascii is ok. self.addon.update(slug=u'crosswarpex-확장') self.version = self.addon.current_version tdir = temp.gettempdir() self.source_file = temp.NamedTemporaryFile(suffix=".zip", dir=tdir) self.source_file.write(b'a' * (2**21)) self.source_file.seek(0) self.version.source = DjangoFile(self.source_file) self.version.save() self.filename = os.path.basename(self.version.source.path) self.user = UserProfile.objects.get(email="*****@*****.**") self.group = Group.objects.create(name='Editors BinarySource', rules='Editors:BinarySource') self.url = reverse('downloads.source', args=(self.version.pk, ))
def test_unicode_file_name(self): tdir = tempfile.gettempdir() # This file contains chinese symbols and an accented char in the name. with open(os.path.join(tdir, UNICODE_FILENAME.encode("utf-8")), "w+b") as file1: file1.write(b"b" * (2 ** 10)) file1.seek(0) post_data = {"file_unicode": file1} response = self.client.post("/file_uploads/unicode_name/", post_data) try: os.unlink(file1.name) except: pass self.assertEqual(response.status_code, 200)
def test_should_accept_zip_source_file(self): tdir = temp.gettempdir() tmp_file = temp.NamedTemporaryFile with tmp_file(suffix=".zip", dir=tdir) as source_file: source_file.write('a' * (2 ** 21)) source_file.seek(0) data = self.formset(source=source_file) response = self.client.post(self.url, data) assert response.status_code == 302 version = Version.objects.get(pk=self.version.pk) assert version.source assert version.addon.admin_review # Check that the corresponding automatic activity log has been created. log = ActivityLog.objects.get(action=amo.LOG.REQUEST_SUPER_REVIEW.id) assert log.details['comments'] == ( u'This version has been automatically flagged as admin review, as ' u'it had some source files attached when submitted.')
def test_large_upload(self): tdir = tempfile.gettempdir() file1 = tempfile.NamedTemporaryFile(suffix=".file1", dir=tdir) file1.write('a' * (2**21)) file1.seek(0) file2 = tempfile.NamedTemporaryFile(suffix=".file2", dir=tdir) file2.write('a' * (10 * 2**20)) file2.seek(0) # This file contains chinese symbols for a name. file3 = open( os.path.join( tdir, u'test_中文_Orl\u00e9ans.jpg'.encode('utf-8')), 'w+b') file3.write('b' * (2**10)) file3.seek(0) post_data = { 'name': 'Ringo', 'file_field1': open(file1.name), 'file_field2': open(file2.name), 'file_unicode': file3, } for key in post_data.keys(): try: post_data[key + '_hash'] = sha_constructor( post_data[key].read()).hexdigest() post_data[key].seek(0) except AttributeError: post_data[key + '_hash'] = sha_constructor( post_data[key]).hexdigest() response = self.client.post('/file_uploads/verify/', post_data) try: os.unlink(file3.name) except: pass self.assertEqual(response.status_code, 200)
def setUp(self): super(TestDownloadSource, self).setUp() self.addon = Addon.objects.get(pk=3615) # Make sure non-ascii is ok. self.addon.update(slug=u'crosswarpex-확장') self.version = self.addon.current_version tdir = temp.gettempdir() self.source_file = temp.NamedTemporaryFile(suffix=".zip", dir=tdir) self.source_file.write('a' * (2 ** 21)) self.source_file.seek(0) self.version.source = DjangoFile(self.source_file) self.version.save() self.filename = os.path.basename(self.version.source.path) self.user = UserProfile.objects.get(email="*****@*****.**") self.group = Group.objects.create( name='Editors BinarySource', rules='Editors:BinarySource' ) self.url = reverse('downloads.source', args=(self.version.pk, ))
def test_mime_type_filtering(self): """Don't allow uploads outside of the explicitly-permitted mime-types.""" # SLIGHT HACK: this requires the default set of allowed # mime-types specified in settings.py. Specifically, adding # 'text/html' to that set will make this test fail. test_user = self.user_model.objects.get(username='******') a = Attachment(title='Test attachment for file type filter', slug='test-attachment-for-file-type-filter') a.save() r = AttachmentRevision( attachment=a, mime_type='text/plain', title=a.title, slug=a.slug, description='', comment='Initial revision.', created=datetime.datetime.now() - datetime.timedelta(seconds=30), creator=test_user, is_approved=True) r.file.save('mime_type_filter_test_file.txt', ContentFile('I am a test file for mime-type filtering')) # Shamelessly stolen from Django's own file-upload tests. tdir = tempfile.gettempdir() file_for_upload = tempfile.NamedTemporaryFile(suffix=".html", dir=tdir) file_for_upload.write('<html>I am a file that tests' 'mime-type filtering.</html>.') file_for_upload.seek(0) post_data = { 'title': 'Test disallowed file type', 'description': 'A file kuma should disallow on type.', 'comment': 'Initial upload', 'file': file_for_upload, } resp = self.client.post(reverse('attachments.edit_attachment', kwargs={'attachment_id': a.id}), data=post_data) eq_(200, resp.status_code) ok_('Files of this type are not permitted.' in resp.content)
def test_unicode_file_name(self): tdir = tempfile.gettempdir() # This file contains chinese symbols and an accented char in the name. with open(os.path.join(tdir, UNICODE_FILENAME), 'w+b') as file1: file1.write(b'b' * (2 ** 10)) file1.seek(0) post_data = { 'file_unicode': file1, } response = self.client.post('/file_uploads/unicode_name/', post_data) try: os.unlink(file1.name) except: pass self.assertEqual(response.status_code, 200)
def test_content_type_extra(self): """Uploaded files may have content type parameters available.""" tdir = tempfile.gettempdir() no_content_type = tempfile.NamedTemporaryFile(suffix=".ctype_extra", dir=tdir) no_content_type.write(b'something') no_content_type.seek(0) simple_file = tempfile.NamedTemporaryFile(suffix=".ctype_extra", dir=tdir) simple_file.write(b'something') simple_file.seek(0) simple_file.content_type = 'text/plain; test-key=test_value' response = self.client.post('/echo_content_type_extra/', { 'no_content_type': no_content_type, 'simple_file': simple_file, }) received = json.loads(response.content.decode('utf-8')) self.assertEqual(received['no_content_type'], {}) self.assertEqual(received['simple_file'], {'test-key': 'test_value'})
def test_should_accept_zip_source_file(self): with temp.NamedTemporaryFile(suffix='.zip', dir=temp.gettempdir()) as source_file: with zipfile.ZipFile(source_file, 'w') as zip_file: zip_file.writestr('foo', 'a' * (2**21)) source_file.seek(0) data = self.formset(source=source_file) response = self.client.post(self.url, data) assert response.status_code == 302 version = Version.objects.get(pk=self.version.pk) assert version.source assert version.addon.needs_admin_code_review # Check that the corresponding automatic activity log has been created. assert ActivityLog.objects.filter( action=amo.LOG.SOURCE_CODE_UPLOADED.id).exists() log = ActivityLog.objects.get(action=amo.LOG.SOURCE_CODE_UPLOADED.id) assert log.user == self.user assert log.details is None assert log.arguments == [self.addon, self.version]
def test_unicode_file_name(self): tdir = tempfile.gettempdir() # This file contains chinese symbols and an accented char in the name. with open(os.path.join(tdir, UNICODE_FILENAME), 'w+b') as file1: file1.write(b'b' * (2 ** 10)) file1.seek(0) post_data = { 'file_unicode': file1, } response = self.client.post('/file_uploads/unicode_name/', post_data) try: os.unlink(file1.name) except OSError: pass self.assertEqual(response.status_code, 200)
def test_content_type_extra(self): """Uploaded files may have content type parameters available.""" tdir = tempfile.gettempdir() no_content_type = tempfile.NamedTemporaryFile(suffix=".ctype_extra", dir=tdir) no_content_type.write(b'something') no_content_type.seek(0) simple_file = tempfile.NamedTemporaryFile(suffix=".ctype_extra", dir=tdir) simple_file.write(b'something') simple_file.seek(0) simple_file.content_type = 'text/plain; test-key=test_value' response = self.client.post('/file_uploads/echo_content_type_extra/', { 'no_content_type': no_content_type, 'simple_file': simple_file, }) received = json.loads(response.content.decode('utf-8')) self.assertEqual(received['no_content_type'], {}) self.assertEqual(received['simple_file'], {'test-key': 'test_value'})
def test_dont_reset_admin_review_flag_if_no_new_source(self): tdir = temp.gettempdir() tmp_file = temp.NamedTemporaryFile with tmp_file(suffix=".zip", dir=tdir) as source_file: source_file.write('a' * (2 ** 21)) source_file.seek(0) data = self.formset(source=source_file) response = self.client.post(self.url, data) assert response.status_code == 302 version = Version.objects.get(pk=self.version.pk) assert version.source assert version.addon.admin_review # Unset the "admin review" flag, and re save the version. It shouldn't # reset the flag, as the source hasn't changed. version.addon.update(admin_review=False) data = self.formset(name='some other name') response = self.client.post(self.url, data) assert response.status_code == 302 version = Version.objects.get(pk=self.version.pk) assert version.source assert not version.addon.admin_review
def test_large_upload(self): tdir = tempfile.gettempdir() file1 = tempfile.NamedTemporaryFile(suffix=".file1", dir=tdir) file1.write('a' * (2 ** 21)) file1.seek(0) file2 = tempfile.NamedTemporaryFile(suffix=".file2", dir=tdir) file2.write('a' * (10 * 2 ** 20)) file2.seek(0) # This file contains chinese symbols for a name. file3 = open(os.path.join(tdir, u'test_中文_Orl\u00e9ans.jpg'.encode('utf-8')), 'w+b') file3.write('b' * (2 ** 10)) file3.seek(0) post_data = { 'name': 'Ringo', 'file_field1': open(file1.name), 'file_field2': open(file2.name), 'file_unicode': file3, } for key in post_data.keys(): try: post_data[key + '_hash'] = sha_constructor(post_data[key].read()).hexdigest() post_data[key].seek(0) except AttributeError: post_data[key + '_hash'] = sha_constructor(post_data[key]).hexdigest() response = self.client.post('/file_uploads/verify/', post_data) try: os.unlink(file3.name) except: pass self.assertEqual(response.status_code, 200)
def test_large_upload(self): tdir = tempfile.gettempdir() file1 = tempfile.NamedTemporaryFile(suffix=".file1", dir=tdir) file1.write(b"a" * (2 ** 21)) file1.seek(0) file2 = tempfile.NamedTemporaryFile(suffix=".file2", dir=tdir) file2.write(b"a" * (10 * 2 ** 20)) file2.seek(0) post_data = {"name": "Ringo", "file_field1": file1, "file_field2": file2} for key in post_data.keys(): try: post_data[key + "_hash"] = hashlib.sha1(post_data[key].read()).hexdigest() post_data[key].seek(0) except AttributeError: post_data[key + "_hash"] = hashlib.sha1(post_data[key]).hexdigest() response = self.client.post("/file_uploads/verify/", post_data) self.assertEqual(response.status_code, 200)
def test_non_utf_post_data(self): BIG5_STRING = u'test-0123456789_中文.jpg' tdir = tempfile.gettempdir() # This file contains chinese symbols in the name. file1 = open(os.path.join(tdir, BIG5_STRING.encode('big5')), 'w+b') file1.write('b' * (2 ** 10)) file1.seek(0) self.assertRaises( UnicodeDecodeError, self.client.post, '/unicode_name/', {'file_unicode': file1} ) file1.close() try: os.unlink(file1.name) except: pass self.assertRaises( UnicodeDecodeError, self.client.post, '/unicode_name/', {BIG5_STRING.encode('big5'): 'string data'} ) self.assertRaises( UnicodeDecodeError, self.client.post, '/unicode_name/', {'string data': BIG5_STRING.encode('big5')} )
def pytest_configure(): BASE_SETTINGS = dict( DATABASES={ 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:', } }, DEBUG=False, SITE_ID=1, EMAIL_BACKEND='django.core.mail.backends.console.EmailBackend', ROOT_URLCONF='sandbox.urls', INSTALLED_APPS=[ 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.admin', 'cid', ], MIDDLEWARE_CLASSES=( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ), STATICFILES_DIRS=(location('static/'),), STATIC_ROOT=location('public'), STATIC_URL='/static/', MEDIA_ROOT=tempfile.gettempdir(), MEDIA_URL='/media/', TEMPLATE_CONTEXT_PROCESSORS=( "django.contrib.auth.context_processors.auth", "django.core.context_processors.request", "django.core.context_processors.debug", "django.core.context_processors.i18n", "django.core.context_processors.media", "django.core.context_processors.static", "django.contrib.messages.context_processors.messages", ), TEMPLATE_DIRS=( location('templates'), ), # Other settings go here LOGGING={ 'version': 1, 'formatters': { 'verbose': { 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(cid)s %(message)s' # noqa }, 'simple': { 'format': '%(levelname)s %(message)s' } }, 'filters': { 'cid': { '()': 'cid.log.CidContextFilter' } }, 'handlers': { 'cid': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'verbose', 'filters': ['cid'] } }, 'loggers': { 'cid': { 'handlers': ['cid'], 'propagate': True, 'level': 'DEBUG' } } } ) conf.settings.configure(**BASE_SETTINGS)
def create_from_animation(self, picture, extensions=None, reset=False): """ Créer des objets animation pour l'objet Picture :type picture: scoop.content.models.Picture | str :param reset: Supprimer les animations existantes et les recréer ? :param extensions: liste des extensions de sortie, sans le point :type reset: bool """ from scoop.content.models import Picture if not os.path.exists('/usr/bin/avconv'): raise ImproperlyConfigured( "You must install anvconv to convert pictures.\nIn Ubuntu 14.04+, run 'apt-get install libav-tools'" ) if isinstance(picture, str): picture = Picture.objects.get_by_uuid(picture) extensions = extensions or [ 'mp4' ] # mp4 pris en charge par tous les navigateurs pop. if isinstance(picture, Picture) and picture.get_extension() in {'.gif'}: if reset is True: picture.get_animations().delete() for extension in [ extension for extension in extensions if extension in AnimationManager.CODECS.keys() ]: # Convertir dans tous les formats de CODECS if not picture.has_extension(extension): sequence_name = uuid_bits(32) temp_dir = gettempdir() new_filename = "{0}.{extension}".format( uuid_bits(48), extension=extension) temp_path = "/{tmp}/{0}".format(new_filename, tmp=temp_dir) subprocess.call([ 'convert', picture.image.path, '-coalesce', '/{tmp}/{0}%05d.jpg'.format(sequence_name, tmp=temp_dir) ], stderr=open(os.devnull, 'wb')) subprocess.call([ 'avconv', '-i', '/tmp/{0}%05d.jpg'.format(sequence_name), '-vf', 'scale=trunc(in_w/2)*2:trunc(in_h/2)*2', '-c', AnimationManager.CODECS[extension], temp_path ], stderr=open(os.devnull, 'wb')) animation = Animation(extension=extension, description=picture.description) animation.picture = picture with open(temp_path, 'rb') as descriptor: animation.file.save(new_filename, File(descriptor)) animation.get_duration( save=True ) # Renvoie ou calcule la durée de l'animation animation.save() # Nettoyer les fichiers temporaires os.popen('rm /{tmp}/{0}*.jpg && rm {1}'.format( sequence_name, temp_path, tmp=temp_dir)) # Remplacer l'image GIF par un aperçu picture.convert() # JPG par défaut picture.animated = True super(Picture, picture).save() else: picture.animated = False super(Picture, picture).save()
class FileFieldTests(TestCase): def test_clearable(self): """ FileField.save_form_data() will clear its instance attribute value if passed False. """ d = Document(myfile="something.txt") self.assertEqual(d.myfile, "something.txt") field = d._meta.get_field("myfile") field.save_form_data(d, False) self.assertEqual(d.myfile, "") def test_unchanged(self): """ FileField.save_form_data() considers None to mean "no change" rather than "clear". """ d = Document(myfile="something.txt") self.assertEqual(d.myfile, "something.txt") field = d._meta.get_field("myfile") field.save_form_data(d, None) self.assertEqual(d.myfile, "something.txt") def test_changed(self): """ FileField.save_form_data(), if passed a truthy value, updates its instance attribute. """ d = Document(myfile="something.txt") self.assertEqual(d.myfile, "something.txt") field = d._meta.get_field("myfile") field.save_form_data(d, "else.txt") self.assertEqual(d.myfile, "else.txt") def test_delete_when_file_unset(self): """ Calling delete on an unset FileField should not call the file deletion process, but fail silently (#20660). """ d = Document() d.myfile.delete() def test_refresh_from_db(self): d = Document.objects.create(myfile="something.txt") d.refresh_from_db() self.assertIs(d.myfile.instance, d) @unittest.skipIf(sys.platform == "win32", "Crashes with OSError on Windows.") def test_save_without_name(self): with tempfile.NamedTemporaryFile(suffix=".txt") as tmp: document = Document.objects.create(myfile="something.txt") document.myfile = File(tmp) msg = f"Detected path traversal attempt in '{tmp.name}'" with self.assertRaisesMessage(SuspiciousFileOperation, msg): document.save() def test_defer(self): Document.objects.create(myfile="something.txt") self.assertEqual( Document.objects.defer("myfile")[0].myfile, "something.txt") def test_unique_when_same_filename(self): """ A FileField with unique=True shouldn't allow two instances with the same name to be saved. """ Document.objects.create(myfile="something.txt") with self.assertRaises(IntegrityError): Document.objects.create(myfile="something.txt") @unittest.skipIf(sys.platform == "win32", "Windows doesn't support moving open files.") # The file's source and destination must be on the same filesystem. @override_settings(MEDIA_ROOT=temp.gettempdir()) def test_move_temporary_file(self): """ The temporary uploaded file is moved rather than copied to the destination. """ with TemporaryUploadedFile("something.txt", "text/plain", 0, "UTF-8") as tmp_file: tmp_file_path = tmp_file.temporary_file_path() Document.objects.create(myfile=tmp_file) self.assertFalse(os.path.exists(tmp_file_path), "Temporary file still exists") def test_open_returns_self(self): """ FieldField.open() returns self so it can be used as a context manager. """ d = Document.objects.create(myfile="something.txt") # Replace the FileField's file with an in-memory ContentFile, so that # open() doesn't write to disk. d.myfile.file = ContentFile(b"", name="bla") self.assertEqual(d.myfile, d.myfile.open()) def test_media_root_pathlib(self): with tempfile.TemporaryDirectory() as tmp_dir: with override_settings(MEDIA_ROOT=Path(tmp_dir)): with TemporaryUploadedFile("foo.txt", "text/plain", 1, "utf-8") as tmp_file: Document.objects.create(myfile=tmp_file) self.assertTrue( os.path.exists( os.path.join(tmp_dir, "unused", "foo.txt"))) def test_pickle(self): with tempfile.TemporaryDirectory() as tmp_dir: with override_settings(MEDIA_ROOT=Path(tmp_dir)): with open(__file__, "rb") as fp: file1 = File(fp, name="test_file.py") document = Document(myfile="test_file.py") document.myfile.save("test_file.py", file1) try: dump = pickle.dumps(document) loaded_document = pickle.loads(dump) self.assertEqual(document.myfile, loaded_document.myfile) self.assertEqual( document.myfile.url, loaded_document.myfile.url, ) self.assertEqual( document.myfile.storage, loaded_document.myfile.storage, ) self.assertEqual( document.myfile.instance, loaded_document.myfile.instance, ) self.assertEqual( document.myfile.field, loaded_document.myfile.field, ) myfile_dump = pickle.dumps(document.myfile) loaded_myfile = pickle.loads(myfile_dump) self.assertEqual(document.myfile, loaded_myfile) self.assertEqual(document.myfile.url, loaded_myfile.url) self.assertEqual( document.myfile.storage, loaded_myfile.storage, ) self.assertEqual( document.myfile.instance, loaded_myfile.instance, ) self.assertEqual(document.myfile.field, loaded_myfile.field) finally: document.myfile.delete() @isolate_apps("model_fields") def test_abstract_filefield_model(self): """ FileField.model returns the concrete model for fields defined in an abstract model. """ class AbstractMyDocument(models.Model): myfile = models.FileField(upload_to="unused") class Meta: abstract = True class MyDocument(AbstractMyDocument): pass document = MyDocument(myfile="test_file.py") self.assertEqual(document.myfile.field.model, MyDocument)
def post(self, request, token): uploaded_file = request.FILES['instance'] log = request.POST['description'] appname = uploaded_file._name[:-4] zipped_file = ZipFile(uploaded_file) tempdir = temp.gettempdir() infoPlist_path = zipped_file.extract( 'Payload/' + appname + '.app/Info.plist', tempdir) provision_path = zipped_file.extract( 'Payload/' + appname + '.app/embedded.mobileprovision', tempdir) info = biplist.readPlist(infoPlist_path) bundle_name = info['CFBundleName'] bundle_version = info['CFBundleVersion'] version_string = info['CFBundleShortVersionString'] print token, info['CFBundleIdentifier'] if token != info['CFBundleIdentifier']: raise Http404 bundle_identifier = info['CFBundleIdentifier'] provision_file = file(provision_path, 'r') provision_content = provision_file.read() provision_file.close() start_index = provision_content.find( '<?xml version="1.0" encoding="UTF-8"?>') end_index = provision_content.find('</plist>') provision_info = plistlib.readPlistFromString( provision_content[start_index:end_index + 8]) allowed_devices = provision_info['ProvisionedDevices'] try: project = Project.objects.get(bundle_identifier=bundle_identifier) except ObjectDoesNotExist: project = Project(name=bundle_name, desc=bundle_name, bundle_identifier=bundle_identifier, token=generate_random_from_vschar_set(20) + ".proj") project.save() emails = [] try: with transaction.atomic(): instance = Instance(project=project, version=version_string, build_version=bundle_version, description=log, token=generate_random_from_vschar_set(20) + ".inst", name=appname) instance.save() for udid in allowed_devices: device, dummy_unused = Device.objects.get_or_create( udid=udid) InstanceAllowedDevice.objects.create(instance=instance, device=device) if device.email and len(device.email) > 0: emails.append(device.email) except Exception: raise try: instance.ipa_path.save(generate_random_from_vschar_set(20), request.FILES['instance'], True) except Exception: InstanceAllowedDevice.objects.filter(instance=instance).delete() instance.delete() raise mail_title = 'iOS Beta:' + appname from_email = "*****@*****.**" html_content = render_to_string( 'Email.html', { 'instance': instance, 'project': project, 'domain': settings.DEPLOY_DOMAIN }) text_content = strip_tags(html_content) msg = EmailMultiAlternatives(mail_title, text_content, from_email, emails) msg.attach_alternative(html_content, "text/html") msg.send() return HttpResponseJson({ 'project_token:': project.token, 'instance_token': instance.token })
def temp_file(self, content): tdir = temp.gettempdir() f = temp.NamedTemporaryFile(dir=tdir) f.write(content) f.seek(0) return f
def pytest_configure(): BASE_SETTINGS = dict( DATABASES={ 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:', } }, DEBUG=False, SITE_ID=1, EMAIL_BACKEND='django.core.mail.backends.console.EmailBackend', ROOT_URLCONF='sandbox.urls', INSTALLED_APPS=[ 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.admin', 'cid', ], MIDDLEWARE_CLASSES=( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ), STATICFILES_DIRS=(location('static/'), ), STATIC_ROOT=location('public'), STATIC_URL='/static/', MEDIA_ROOT=tempfile.gettempdir(), MEDIA_URL='/media/', TEMPLATE_CONTEXT_PROCESSORS=( "django.contrib.auth.context_processors.auth", "django.core.context_processors.request", "django.core.context_processors.debug", "django.core.context_processors.i18n", "django.core.context_processors.media", "django.core.context_processors.static", "django.contrib.messages.context_processors.messages", ), TEMPLATE_DIRS=(location('templates'), ), # Other settings go here LOGGING={ 'version': 1, 'formatters': { 'verbose': { 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(cid)s %(message)s' # noqa }, 'simple': { 'format': '%(levelname)s %(message)s' } }, 'filters': { 'cid': { '()': 'cid.log.CidContextFilter' } }, 'handlers': { 'cid': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'verbose', 'filters': ['cid'] } }, 'loggers': { 'cid': { 'handlers': ['cid'], 'propagate': True, 'level': 'DEBUG' } } }) settings.configure(**BASE_SETTINGS)
class FileFieldTests(TestCase): def test_clearable(self): """ FileField.save_form_data() will clear its instance attribute value if passed False. """ d = Document(myfile='something.txt') self.assertEqual(d.myfile, 'something.txt') field = d._meta.get_field('myfile') field.save_form_data(d, False) self.assertEqual(d.myfile, '') def test_unchanged(self): """ FileField.save_form_data() considers None to mean "no change" rather than "clear". """ d = Document(myfile='something.txt') self.assertEqual(d.myfile, 'something.txt') field = d._meta.get_field('myfile') field.save_form_data(d, None) self.assertEqual(d.myfile, 'something.txt') def test_changed(self): """ FileField.save_form_data(), if passed a truthy value, updates its instance attribute. """ d = Document(myfile='something.txt') self.assertEqual(d.myfile, 'something.txt') field = d._meta.get_field('myfile') field.save_form_data(d, 'else.txt') self.assertEqual(d.myfile, 'else.txt') def test_delete_when_file_unset(self): """ Calling delete on an unset FileField should not call the file deletion process, but fail silently (#20660). """ d = Document() d.myfile.delete() def test_refresh_from_db(self): d = Document.objects.create(myfile='something.txt') d.refresh_from_db() self.assertIs(d.myfile.instance, d) def test_defer(self): Document.objects.create(myfile='something.txt') self.assertEqual( Document.objects.defer('myfile')[0].myfile, 'something.txt') def test_unique_when_same_filename(self): """ A FileField with unique=True shouldn't allow two instances with the same name to be saved. """ Document.objects.create(myfile='something.txt') with self.assertRaises(IntegrityError): Document.objects.create(myfile='something.txt') @unittest.skipIf(sys.platform.startswith('win'), "Windows doesn't support moving open files.") # The file's source and destination must be on the same filesystem. @override_settings(MEDIA_ROOT=temp.gettempdir()) def test_move_temporary_file(self): """ The temporary uploaded file is moved rather than copied to the destination. """ with TemporaryUploadedFile('something.txt', 'text/plain', 0, 'UTF-8') as tmp_file: tmp_file_path = tmp_file.temporary_file_path() Document.objects.create(myfile=tmp_file) self.assertFalse(os.path.exists(tmp_file_path), 'Temporary file still exists')
class FileFieldTests(TestCase): def test_clearable(self): """ FileField.save_form_data() will clear its instance attribute value if passed False. """ d = Document(myfile='something.txt') self.assertEqual(d.myfile, 'something.txt') field = d._meta.get_field('myfile') field.save_form_data(d, False) self.assertEqual(d.myfile, '') def test_unchanged(self): """ FileField.save_form_data() considers None to mean "no change" rather than "clear". """ d = Document(myfile='something.txt') self.assertEqual(d.myfile, 'something.txt') field = d._meta.get_field('myfile') field.save_form_data(d, None) self.assertEqual(d.myfile, 'something.txt') def test_changed(self): """ FileField.save_form_data(), if passed a truthy value, updates its instance attribute. """ d = Document(myfile='something.txt') self.assertEqual(d.myfile, 'something.txt') field = d._meta.get_field('myfile') field.save_form_data(d, 'else.txt') self.assertEqual(d.myfile, 'else.txt') def test_delete_when_file_unset(self): """ Calling delete on an unset FileField should not call the file deletion process, but fail silently (#20660). """ d = Document() d.myfile.delete() def test_refresh_from_db(self): d = Document.objects.create(myfile='something.txt') d.refresh_from_db() self.assertIs(d.myfile.instance, d) def test_defer(self): Document.objects.create(myfile='something.txt') self.assertEqual( Document.objects.defer('myfile')[0].myfile, 'something.txt') def test_unique_when_same_filename(self): """ A FileField with unique=True shouldn't allow two instances with the same name to be saved. """ Document.objects.create(myfile='something.txt') with self.assertRaises(IntegrityError): Document.objects.create(myfile='something.txt') @unittest.skipIf(sys.platform == 'win32', "Windows doesn't support moving open files.") # The file's source and destination must be on the same filesystem. @override_settings(MEDIA_ROOT=temp.gettempdir()) def test_move_temporary_file(self): """ The temporary uploaded file is moved rather than copied to the destination. """ with TemporaryUploadedFile('something.txt', 'text/plain', 0, 'UTF-8') as tmp_file: tmp_file_path = tmp_file.temporary_file_path() Document.objects.create(myfile=tmp_file) self.assertFalse(os.path.exists(tmp_file_path), 'Temporary file still exists') def test_open_returns_self(self): """ FieldField.open() returns self so it can be used as a context manager. """ d = Document.objects.create(myfile='something.txt') # Replace the FileField's file with an in-memory ContentFile, so that # open() doesn't write to disk. d.myfile.file = ContentFile(b'', name='bla') self.assertEqual(d.myfile, d.myfile.open()) def test_media_root_pathlib(self): with tempfile.TemporaryDirectory() as tmp_dir: with override_settings(MEDIA_ROOT=Path(tmp_dir)): with TemporaryUploadedFile('foo.txt', 'text/plain', 1, 'utf-8') as tmp_file: Document.objects.create(myfile=tmp_file) self.assertTrue( os.path.exists( os.path.join(tmp_dir, 'unused', 'foo.txt'))) def test_pickle(self): with open(__file__, 'rb') as fp: file1 = File(fp, name='test_file.py') document = Document(myfile='test_file.py') document.myfile.save('test_file.py', file1) try: dump = pickle.dumps(document) loaded_document = pickle.loads(dump) self.assertEqual(document.myfile, loaded_document.myfile) self.assertEqual(document.myfile.url, loaded_document.myfile.url) self.assertEqual( document.myfile.storage, loaded_document.myfile.storage, ) self.assertEqual( document.myfile.instance, loaded_document.myfile.instance, ) self.assertEqual(document.myfile.field, loaded_document.myfile.field) myfile_dump = pickle.dumps(document.myfile) loaded_myfile = pickle.loads(myfile_dump) self.assertEqual(document.myfile, loaded_myfile) self.assertEqual(document.myfile.url, loaded_myfile.url) self.assertEqual(document.myfile.storage, loaded_myfile.storage) self.assertEqual(document.myfile.instance, loaded_myfile.instance) self.assertEqual(document.myfile.field, loaded_myfile.field) finally: document.myfile.delete()
def process_edited_gatefold_cover(request): ''' process the edited gatefold cover and generate CoverRevision ''' if request.method != 'POST': return render_error( request, 'This page may only be accessed through the proper form', redirect=False) form = GatefoldScanForm(request.POST) if not form.is_valid(): error_text = 'Error: Something went wrong in the file upload.' return render_error(request, error_text, redirect=False) cd = form.cleaned_data tmpdir = tempfile.gettempdir() scan_name = cd['scan_name'] tmp_name = os.path.join(tmpdir, scan_name) if 'discard' in request.POST: os.remove(tmp_name) return HttpResponseRedirect( urlresolvers.reverse('edit_covers', kwargs={'issue_id': cd['issue_id']})) # create OI records changeset = Changeset(indexer=request.user, state=states.OPEN, change_type=CTYPES['cover']) if cd['cover_id']: cover = get_object_or_404(Cover, id=cd['cover_id']) issue = cover.issue # check if there is a pending change for the cover revision_lock = _get_revision_lock(cover) if not revision_lock: return render_error( request, u'Cannot replace %s as it is already reserved.' % cover.issue) changeset.save() revision_lock.changeset = changeset revision_lock.save() revision = CoverRevision(changeset=changeset, issue=issue, cover=cover, file_source=cd['source'], marked=cd['marked'], is_replacement=True) # no cover_id, therefore upload a cover to an issue (first or variant) else: changeset.save() issue = get_object_or_404(Issue, id=cd['issue_id']) cover = None revision = CoverRevision(changeset=changeset, issue=issue, file_source=cd['source'], marked=cd['marked']) revision.save() scan_name = str(revision.id) + os.path.splitext(tmp_name)[1] upload_dir = settings.MEDIA_ROOT + LOCAL_NEW_SCANS + \ changeset.created.strftime('%B_%Y').lower() destination_name = os.path.join(upload_dir, scan_name) try: # essentially only needed at beginning of the month check_cover_dir(upload_dir) except IOError: changeset.delete() error_text = "Problem with file storage for uploaded " + \ "cover, please report an error." return render_error(request, error_text, redirect=False) shutil.move(tmp_name, destination_name) im = pyImage.open(destination_name) revision.is_wraparound = True # convert from scaled to real values width = cd['width'] height = cd['height'] left = cd['left'] top = cd['top'] revision.front_left = left revision.front_right = left + width revision.front_top = top revision.front_bottom = top + height revision.save() generate_sizes(revision, im) return finish_cover_revision(request, revision, cd)