← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~julian-edwards/maas/file-storage-overwriting into lp:maas

 

Julian Edwards has proposed merging lp:~julian-edwards/maas/file-storage-overwriting into lp:maas.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~julian-edwards/maas/file-storage-overwriting/+merge/93399
-- 
https://code.launchpad.net/~julian-edwards/maas/file-storage-overwriting/+merge/93399
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~julian-edwards/maas/file-storage-overwriting into lp:maas.
=== modified file 'src/maasserver/api.py'
--- src/maasserver/api.py	2012-02-14 15:10:38 +0000
+++ src/maasserver/api.py	2012-02-16 13:01:18 +0000
@@ -409,9 +409,12 @@
         # As per the comment in FileStorage, this ought to deal in
         # chunks instead of reading the file into memory, but large
         # files are not expected.
-        storage = FileStorage()
+        try:
+            storage = FileStorage.objects.get(filename=filename)
+        except ObjectDoesNotExist:
+            storage = FileStorage()
+
         storage.save_file(filename, uploaded_file)
-        storage.save()
         return HttpResponse('', status=httplib.CREATED)
 
     @classmethod

=== modified file 'src/maasserver/tests/test_api.py'
--- src/maasserver/tests/test_api.py	2012-02-14 15:53:08 +0000
+++ src/maasserver/tests/test_api.py	2012-02-16 13:01:18 +0000
@@ -705,6 +705,23 @@
         self.assertIn('text/plain', response['Content-Type'])
         self.assertEqual("Exactly one file must be supplied", response.content)
 
+    def test_add_file_can_overwrite_existing_file_of_same_name(self):
+        # Write file one.
+        filepath = self.make_file(contents="file one")
+        with open(filepath) as f:
+            response = self.make_API_POST_request("add", "foo", f)
+        self.assertEqual(httplib.CREATED, response.status_code)
+
+        # Write file two with the same name but different contents.
+        filepath = self.make_file(contents="file two")
+        with open(filepath) as f:
+            response = self.make_API_POST_request("add", "foo", f)
+        self.assertEqual(httplib.CREATED, response.status_code)
+
+        # Retrieve the file and check its contents are the new contents.
+        response = self.make_API_GET_request("get", "foo")
+        self.assertEqual("file two", response.content)
+
     def test_get_file_succeeds(self):
         factory.make_file_storage(filename="foofilers", data=b"give me rope")
         response = self.make_API_GET_request("get", "foofilers")