def manage_storage(request, storageid=None, storagename=None): if storageid and storagename: storage = get_object_or_404(filter_by_access(request.user, Storage, manage=True), id=storageid) else: storage = Storage(system='local') if not storage.id: system_choices = [(s, s) for s in settings.STORAGE_SYSTEMS.keys()] else: system_choices = [(storage.system, storage.system)] class StorageForm(forms.ModelForm): system = forms.CharField(widget=forms.Select(choices=system_choices)) def clean_system(self): if not self.instance.id: return self.cleaned_data['system'] else: return self.instance.system class Meta: model = Storage fields = ('title', 'system', 'base', 'credential_id', 'credential_key', 'urlbase', 'deliverybase') if request.method == "POST": if request.POST.get('delete-storage'): if not request.user.is_superuser: raise HttpResponseForbidden() messages.add_message(request, messages.INFO, message="Storage '%s' has been deleted." % storage.title) storage.delete() return HttpResponseRedirect(reverse('storage-manage')) else: form = StorageForm(request.POST, instance=storage) if form.is_valid(): form.save() return HttpResponseRedirect( reverse('storage-manage-storage', kwargs=dict(storageid=form.instance.id, storagename=form.instance.name))) else: form = StorageForm(instance=storage) return render_to_response('storage_edit.html', { 'storage': storage, 'form': form, }, context_instance=RequestContext(request))
def action(self, option): if option == "b": return ListView(Storage.all()) if option == "e": return EditView(self._model) if option == "d": self._deleting = True if self._deleting and option == "y": Storage.delete(self._model.id) return ListView(Storage.all()) if self._deleting and option == "n": self._deleting = False return None
def manage_storage(request, storageid=None, storagename=None): new_storage = False if storageid and storagename: storage = get_object_or_404(filter_by_access(request.user, Storage, manage=True), id=storageid) else: storage = Storage(system='local') new_storage = True if not storage.id: system_choices = [(s,s) for s in settings.STORAGE_SYSTEMS.keys()] else: system_choices = [(storage.system, storage.system)] class StorageForm(forms.ModelForm): system = forms.CharField(widget=forms.Select(choices=system_choices)) def clean_system(self): return self.cleaned_data['system'] if not self.instance.id else self.instance.system class Meta: model = Storage exclude = ('name', 'derivative') if request.method == "POST": if request.POST.get('delete-storage'): if not request.user.is_superuser: raise HttpResponseForbidden() request.user.message_set.create(message="Storage '%s' has been deleted." % storage.title) storage.delete() return HttpResponseRedirect(reverse('storage-manage')) else: form = StorageForm(request.POST, instance=storage) if form.is_valid(): form.save() messages.success(request, "Storage created!") return HttpResponseRedirect(reverse('storage-manage-storage', kwargs=dict( storageid=form.instance.id, storagename=form.instance.name))) else: form = StorageForm(instance=storage) return render_to_response('storage_edit.html', {'storage': storage, 'form': form, 'new': new_storage}, context_instance=RequestContext(request))
def test_files(self): # Attempting to access a FileField from the class raises a descriptive # error self.assertRaises(AttributeError, lambda: Storage.normal) # An object without a file has limited functionality. obj1 = Storage() self.assertEqual(obj1.normal.name, "") self.assertRaises(ValueError, lambda: obj1.normal.size) # Saving a file enables full functionality. obj1.normal.save("django_test.txt", ContentFile("content")) self.assertEqual(obj1.normal.name, "tests/django_test.txt") self.assertEqual(obj1.normal.size, 7) self.assertEqual(obj1.normal.read(), "content") obj1.normal.close() # File objects can be assigned to FileField attributes, but shouldn't # get committed until the model it's attached to is saved. obj1.normal = SimpleUploadedFile("assignment.txt", "content") dirs, files = temp_storage.listdir("tests") self.assertEqual(dirs, []) self.assertEqual(sorted(files), ["default.txt", "django_test.txt"]) obj1.save() dirs, files = temp_storage.listdir("tests") self.assertEqual( sorted(files), ["assignment.txt", "default.txt", "django_test.txt"] ) # Files can be read in a little at a time, if necessary. obj1.normal.open() self.assertEqual(obj1.normal.read(3), "con") self.assertEqual(obj1.normal.read(), "tent") self.assertEqual(list(obj1.normal.chunks(chunk_size=2)), ["co", "nt", "en", "t"]) obj1.normal.close() # Save another file with the same name. obj2 = Storage() obj2.normal.save("django_test.txt", ContentFile("more content")) self.assertEqual(obj2.normal.name, "tests/django_test_1.txt") self.assertEqual(obj2.normal.size, 12) # Push the objects into the cache to make sure they pickle properly cache.set("obj1", obj1) cache.set("obj2", obj2) self.assertEqual(cache.get("obj2").normal.name, "tests/django_test_1.txt") # Deleting an object does not delete the file it uses. obj2.delete() obj2.normal.save("django_test.txt", ContentFile("more content")) self.assertEqual(obj2.normal.name, "tests/django_test_2.txt") # Multiple files with the same name get _N appended to them. objs = [Storage() for i in range(3)] for o in objs: o.normal.save("multiple_files.txt", ContentFile("Same Content")) self.assertEqual( [o.normal.name for o in objs], ["tests/multiple_files.txt", "tests/multiple_files_1.txt", "tests/multiple_files_2.txt"] ) for o in objs: o.delete() # Default values allow an object to access a single file. obj3 = Storage.objects.create() self.assertEqual(obj3.default.name, "tests/default.txt") self.assertEqual(obj3.default.read(), "default content") obj3.default.close() # But it shouldn't be deleted, even if there are no more objects using # it. obj3.delete() obj3 = Storage() self.assertEqual(obj3.default.read(), "default content") obj3.default.close() # Verify the fix for #5655, making sure the directory is only # determined once. obj4 = Storage() obj4.random.save("random_file", ContentFile("random content")) self.assertTrue(obj4.random.name.endswith("/random_file")) # Clean up the temporary files and dir. obj1.normal.delete() obj2.normal.delete() obj3.default.delete() obj4.random.delete()