Advertisement
Guest User

Untitled

a guest
Aug 25th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. ...
  2. if (request.method == 'POST'):
  3. if request.FILES.has_key('file'):
  4. file = request.FILES['file']
  5. with open(settings.destfolder+'/%s' % file.name, 'wb+') as dest:
  6. for chunk in file.chunks():
  7. dest.write(chunk)
  8.  
  9. c = Client()
  10. with open('wishlist.doc') as fp:
  11. c.post('/customers/wishes/', {'name': 'fred', 'attachment': fp})
  12.  
  13. from django.core.files.uploadedfile import SimpleUploadedFile
  14.  
  15. def test_upload_video(self):
  16. video = SimpleUploadedFile("file.mp4", "file_content", content_type="video/mp4")
  17. self.client.post(reverse('app:some_view'), {'video': video})
  18. # some important assertions ...
  19.  
  20. def upload_file_to_location(request, location=None): # Can use the default configured
  21.  
  22. import tempfile, csv, os
  23.  
  24. class UploadPaperTest(TestCase):
  25.  
  26. def generate_file(self):
  27. try:
  28. myfile = open('test.csv', 'wb')
  29. wr = csv.writer(myfile)
  30. wr.writerow(('Paper ID','Paper Title', 'Authors'))
  31. wr.writerow(('1','Title1', 'Author1'))
  32. wr.writerow(('2','Title2', 'Author2'))
  33. wr.writerow(('3','Title3', 'Author3'))
  34. finally:
  35. myfile.close()
  36.  
  37. return myfile
  38.  
  39. def setUp(self):
  40. self.user = create_fuser()
  41. self.profile = ProfileFactory(user=self.user)
  42. self.event = EventFactory()
  43. self.client = Client()
  44. self.module = ModuleFactory()
  45. self.event_module = EventModule.objects.get_or_create(event=self.event,
  46. module=self.module)[0]
  47. add_to_admin(self.event, self.user)
  48.  
  49. def test_paper_upload(self):
  50. response = self.client.login(username=self.user.email, password='foz')
  51. self.assertTrue(response)
  52.  
  53. myfile = self.generate_file()
  54. file_path = myfile.name
  55. f = open(file_path, "r")
  56.  
  57. url = reverse('registration_upload_papers', args=[self.event.slug])
  58.  
  59. # post wrong data type
  60. post_data = {'uploaded_file': i}
  61. response = self.client.post(url, post_data)
  62. self.assertContains(response, 'File type is not supported.')
  63.  
  64. post_data['uploaded_file'] = f
  65. response = self.client.post(url, post_data)
  66.  
  67. import_file = SubmissionImportFile.objects.all()[0]
  68. self.assertEqual(SubmissionImportFile.objects.all().count(), 1)
  69. #self.assertEqual(import_file.uploaded_file.name, 'files/registration/{0}'.format(file_path))
  70.  
  71. os.remove(myfile.name)
  72. file_path = import_file.uploaded_file.path
  73. os.remove(file_path)
  74.  
  75. TypeError: sequence item 4: expected bytes, bytearray, or an object with the buffer interface, str found
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement