File: models.py

package info (click to toggle)
python-django 3%3A5.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 61,236 kB
  • sloc: python: 361,585; javascript: 19,250; xml: 211; makefile: 182; sh: 28
file content (84 lines) | stat: -rw-r--r-- 2,678 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
Storing files according to a custom storage system

``FileField`` and its variations can take a ``storage`` argument to specify how
and where files should be stored.
"""

import random
import tempfile
from pathlib import Path

from django.core.files.storage import FileSystemStorage, default_storage
from django.db import models


class CustomValidNameStorage(FileSystemStorage):
    def get_valid_name(self, name):
        # mark the name to show that this was called
        return name + "_valid"


temp_storage_location = tempfile.mkdtemp()
temp_storage = FileSystemStorage(location=temp_storage_location)


def callable_storage():
    return temp_storage


def callable_default_storage():
    return default_storage


class CallableStorage(FileSystemStorage):
    def __call__(self):
        # no-op implementation.
        return self


class Storage(models.Model):
    def custom_upload_to(self, filename):
        return "foo"

    def random_upload_to(self, filename):
        # This returns a different result each time,
        # to make sure it only gets called once.
        return "%s/%s" % (random.randint(100, 999), filename)

    def pathlib_upload_to(self, filename):
        return Path("bar") / filename

    normal = models.FileField(storage=temp_storage, upload_to="tests")
    custom = models.FileField(storage=temp_storage, upload_to=custom_upload_to)
    pathlib_callable = models.FileField(
        storage=temp_storage, upload_to=pathlib_upload_to
    )
    pathlib_direct = models.FileField(storage=temp_storage, upload_to=Path("bar"))
    random = models.FileField(storage=temp_storage, upload_to=random_upload_to)
    custom_valid_name = models.FileField(
        storage=CustomValidNameStorage(location=temp_storage_location),
        upload_to=random_upload_to,
    )
    storage_callable = models.FileField(
        storage=callable_storage, upload_to="storage_callable"
    )
    storage_callable_class = models.FileField(
        storage=CallableStorage, upload_to="storage_callable_class"
    )
    storage_callable_default = models.FileField(
        storage=callable_default_storage, upload_to="storage_callable_default"
    )
    default = models.FileField(
        storage=temp_storage, upload_to="tests", default="tests/default.txt"
    )
    db_default = models.FileField(
        storage=temp_storage, upload_to="tests", db_default="tests/db_default.txt"
    )
    empty = models.FileField(storage=temp_storage)
    limited_length = models.FileField(
        storage=temp_storage, upload_to="tests", max_length=20
    )
    extended_length = models.FileField(
        storage=temp_storage, upload_to="tests", max_length=1024
    )