def update(self, request, local_site_name=None, *args, **kwargs): """Updates the file attachment's data. This allows updating information on the file attachment. It also allows the file to be uploaded if this was not done when the file attachment was created. The file attachment's file cannot be updated once it has been uploaded. Attempting to update the file attachment's file if it has already been uploaded will result in a :ref:`webapi2.0-error-111`. The file attachment can only be updated by its owner or by an administrator. It is expected that the client will send the data as part of a :mimetype:`multipart/form-data` mimetype. The file's name and content should be stored in the ``path`` field. A typical request may look like:: -- SoMe BoUnDaRy Content-Disposition: form-data; name=path; filename="foo.zip" <Content here> -- SoMe BoUnDaRy -- """ try: file_attachment = self.get_object(request, local_site_name=local_site_name, *args, **kwargs) except ObjectDoesNotExist: return DOES_NOT_EXIST if not self.has_modify_permissions(request, file_attachment, *args, ** kwargs): return self.get_no_access_error(request) if 'path' in request.FILES and file_attachment.file: return DUPLICATE_ITEM form = UploadUserFileForm(request.POST, request.FILES) if not form.is_valid(): return INVALID_FORM_DATA, { 'fields': self._get_form_errors(form), } file_attachment = form.update(file_attachment) return 200, {self.item_result_key: file_attachment}
def update(self, request, local_site_name=None, *args, **kwargs): """Updates the file attachment's data. This allows updating information on the file attachment. It also allows the file to be uploaded if this was not done when the file attachment was created. The file attachment's file cannot be updated once it has been uploaded. Attempting to update the file attachment's file if it has already been uploaded will result in a :ref:`webapi2.0-error-111`. The file attachment can only be updated by its owner or by an administrator. It is expected that the client will send the data as part of a :mimetype:`multipart/form-data` mimetype. The file's name and content should be stored in the ``path`` field. A typical request may look like:: -- SoMe BoUnDaRy Content-Disposition: form-data; name=path; filename="foo.zip" <Content here> -- SoMe BoUnDaRy -- """ try: file_attachment = self.get_object( request, local_site_name=local_site_name, *args, **kwargs) except ObjectDoesNotExist: return DOES_NOT_EXIST if not self.has_modify_permissions(request, file_attachment, *args, **kwargs): return self.get_no_access_error(request) if 'path' in request.FILES and file_attachment.file: return DUPLICATE_ITEM form = UploadUserFileForm(request.POST, request.FILES) if not form.is_valid(): return INVALID_FORM_DATA, { 'fields': self._get_form_errors(form), } file_attachment = form.update(file_attachment) return 200, { self.item_result_key: file_attachment }
def test_user_file_add_file_after_create(self): """Testing user FileAttachment create without initial file and adding file through update """ user = User.objects.get(username='******') form = UploadUserFileForm(files={}) self.assertTrue(form.is_valid()) file_attachment = form.create(user) self.assertFalse(file_attachment.file) self.assertEqual(file_attachment.user, user) uploaded_file = self.make_uploaded_file() form = UploadUserFileForm(files={ 'path': uploaded_file, }) self.assertTrue(form.is_valid()) file_attachment = form.update(file_attachment) self.assertTrue(os.path.basename(file_attachment.file.name).endswith( '__trophy.png')) self.assertEqual(file_attachment.mimetype, 'image/png')
def test_user_file_add_file_after_create(self): """Testing user FileAttachment create without initial file and adding file through update """ user = User.objects.get(username='******') form = UploadUserFileForm(files={}) self.assertTrue(form.is_valid()) file_attachment = form.create(user) self.assertFalse(file_attachment.file) self.assertEqual(file_attachment.user, user) uploaded_file = self.make_uploaded_file() form = UploadUserFileForm(files={ 'path': uploaded_file, }) self.assertTrue(form.is_valid()) file_attachment = form.update(file_attachment) self.assertTrue( os.path.basename(file_attachment.file.name).endswith('__logo.png')) self.assertEqual(file_attachment.mimetype, 'image/png')