def test(self): new_upload = Upload(file=self.tempfile) new_upload.save() # Upload has been saved to the database. self.assert_(new_upload.pk) # Upload contains correct content. self.assertEqual(new_upload.file.read(), self.text) # Filename is the hash of the file contents. self.assert_(new_upload.file.name.startswith(self.digest))
def test(self): text = "Spam Spam Spam.\n" digest = hashlib.sha1(text).hexdigest() io = StringIO(text) new_upload = Upload(file=InMemoryUploadedFile( io, 'file', 'spam.txt', 'text/plain', len(text), 'utf-8')) new_upload.save() # Upload has been saved to the database. self.assert_(new_upload.pk) # Upload contains correct content. self.assertEqual(new_upload.file.read(), text) # Filename is the hash of the file contents. self.assert_(new_upload.file.name.startswith(digest))
def upload_action(request): upload_list_No = len(Upload.objects.all()) upload_list_0_No = len(Upload_0.objects.all()) upload_list_1_No = len(Upload_1.objects.all()) dir = 'D:/mysite_2/media/repository/' if not os.path.exists(dir): os.makedirs(dir) if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): for index in range(10): title_index = "title" + str(index) file_index = "file" + str(index) if not request.FILES.has_key(file_index): continue upload_model = Upload(title = request.REQUEST[title_index], file = request.FILES[file_index]) upload_model.save() initial_path = upload_model.file.path new_path = settings.MEDIA_ROOT + '\\repository\\' + request.REQUEST[title_index] + '.jpg' os.rename(initial_path, new_path) upload_model.file = new_path upload_model.file.name = u'repository/' + request.REQUEST[title_index] + '.jpg' upload_model.save() return HttpResponseRedirect(reverse('uploads:index')) else: return render(request, 'uploads/empty_repository.html', { 'str': 'Invalid upload. Please upload some figures to classfy.', 'upload_list_No': upload_list_No, 'upload_list_0_No': upload_list_0_No, 'upload_list_1_No': upload_list_1_No, })