Exemplo n.º 1
0
 def test_attribute_assignment(self):
     # File objects can be assigned to FileField attributes, but
     # shouldn't get committed until the model it's attached to is
     # saved.
     obj = Storage()
     obj.normal = SimpleUploadedFile('assignment.txt', 'content')
     dirs, files = temp_storage.listdir('tests')
     self.assertEqual(len(dirs), 0)
     self.assertEqual(files, ['default.txt'])
     obj.save()
     dirs, files = temp_storage.listdir('tests')
     files.sort()
     self.assertEqual(files, ['assignment.txt', 'default.txt'])
Exemplo n.º 2
0
 def test_attribute_assignment(self):
     # File objects can be assigned to FileField attributes, but
     # shouldn't get committed until the model it's attached to is
     # saved.
     obj = Storage()
     obj.normal = SimpleUploadedFile('assignment.txt', 'content')
     dirs, files = temp_storage.listdir('tests')
     self.assertEqual(len(dirs), 0)
     self.assertEqual(files, ['default.txt'])
     obj.save()
     dirs, files = temp_storage.listdir('tests')
     files.sort()
     self.assertEqual(files, ['assignment.txt', 'default.txt'])
Exemplo n.º 3
0
def add(request):
    #request.encoding = 'gb2312'
    index=request.POST['index']
    air=request.POST.getlist('num_air[]')
    soilt=request.POST.getlist('num_soilt[]')
    soilm=request.POST.getlist('num_soilm[]')
    light=request.POST.getlist('num_light[]')
    coz=request.POST.getlist('num_coz[]')
    addr_air=request.POST.getlist('Addr_air[]')
    addr_soilt=request.POST.getlist('Addr_soilt[]')
    addr_soilm=request.POST.getlist('Addr_soilm[]')
    addr_light=request.POST.getlist('Addr_light[]')
    addr_coz=request.POST.getlist('Addr_coz[]')
    removeEmptyItem(air)
    removeEmptyItem(soilt)
    removeEmptyItem(soilm)
    removeEmptyItem(light)
    removeEmptyItem(coz)
    removeEmptyItem(addr_air)
    removeEmptyItem(addr_soilt)
    removeEmptyItem(addr_soilm)
    removeEmptyItem(addr_light)
    removeEmptyItem(addr_coz)

    try:
        Storage.objects.get(index=index)
        request.session['warn1'] = '该库房已存在'
        return redirect("/storagemanage/")
    except ObjectDoesNotExist:
        s=Storage()
        u=request.session.get('user', '')
        u1=User.objects.get(username=u)
        s.owner=u1

        s.num_air = len(air)
        s.num_soilt = len(soilt)
        s.num_soilm = len(soilm)
        s.num_sunlight = len(light)
        s.num_coz = len(coz)
        s.nodeNum = s.num_air + s.num_coz + s.num_soilt + s.num_soilm + s.num_sunlight
        s.index = index
        s.save()
        addSensor(s.num_air, air, addr_air, 'air', s)
        addSensor(s.num_soilm, soilm, addr_soilm, 'soilm', s)
        addSensor(s.num_soilt, soilt, addr_soilt, 'soilt', s)
        addSensor(s.num_sunlight, light, addr_light, 'sun', s)
        addSensor(s.num_coz, coz, addr_coz, 'coz', s)

        return redirect('storagemanage')
Exemplo n.º 4
0
    def action(self, option):
        if option == "c":
            return DetailsView(self._model)
        if option == "e":
            self._name = None
            self._age = None
            self._edited = False
        if option == "s":
            if self._name:
                self._model.name = self._name
            if self._age:
                self._model.age = self._age
            Storage.save()
            return DetailsView(self._model)

        return None
Exemplo n.º 5
0
def home():
    storage = Storage()
    all_items = storage.items

    if request.method == 'POST':
        form = BlogPostForm(request.form)
        if form.validate():
            model = BlogPostModel(form.data, len(all_items) + 1)
            all_items.append(model)
            storage.save()
        else:
            logger.error('Someone have submitted an incorrect form!')
    else:
        form = BlogPostForm()

    return render_template(
        'home.html',
        form=form,
        items=all_items,
    )
Exemplo n.º 6
0
    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()