-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmodels.py
134 lines (110 loc) · 5.33 KB
/
models.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import uuid
import datetime
from django.db import models
from django.db.models.fields.files import FieldFile
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from django_minio_backend import MinioBackend, iso_date_prefix
def get_iso_date() -> str:
"""Get current date in ISO8601 format [year-month-day] as string"""
now = datetime.datetime.now(datetime.UTC)
return f"{now.year}-{now.month}-{now.day}"
class Image(models.Model):
"""
This is just for uploaded image
"""
objects = models.Manager()
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
image = models.ImageField(upload_to=iso_date_prefix, storage=MinioBackend(bucket_name='django-backend-dev-public'))
def delete(self, *args, **kwargs):
"""
Delete must be overridden because the inherited delete method does not call `self.image.delete()`.
"""
# noinspection PyUnresolvedReferences
self.image.delete()
super(Image, self).delete(*args, **kwargs)
class GenericAttachment(models.Model):
"""
This is for demonstrating uploads to the default file storage
"""
objects = models.Manager()
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
file = models.FileField(verbose_name="Object Upload (to default storage)")
def delete(self, *args, **kwargs):
"""
Delete must be overridden because the inherited delete method does not call `self.image.delete()`.
"""
# noinspection PyUnresolvedReferences
self.file.delete()
super(GenericAttachment, self).delete(*args, **kwargs)
# Create your models here.
class PublicAttachment(models.Model):
def set_file_path_name(self, file_name_ext: str) -> str:
"""
Defines the full absolute path to the file in the bucket. The original content's type is used as parent folder.
:param file_name_ext: (str) File name + extension. ie.: cat.png OR images/animals/2019/cat.png
:return: (str) Absolute path to file in Minio Bucket
"""
return f"{get_iso_date()}/{self.content_type.name}/{file_name_ext}"
def delete(self, *args, **kwargs):
"""
Delete must be overridden because the inherited delete method does not call `self.file.delete()`.
"""
self.file.delete()
super(PublicAttachment, self).delete(*args, **kwargs)
@property
def file_name(self):
try:
return self.file.name.split("/")[-1]
except AttributeError:
return "[Deleted Object]"
@property
def file_size(self):
return self.file.size
def __str__(self):
return str(self.file)
id = models.AutoField(primary_key=True, verbose_name="Public Attachment ID")
content_type: ContentType = models.ForeignKey(ContentType, null=True, blank=True, on_delete=models.CASCADE,
verbose_name="Content Type")
object_id = models.PositiveIntegerField(null=True, blank=True, verbose_name="Related Object's ID")
content_object = GenericForeignKey("content_type", "object_id")
file: FieldFile = models.FileField(verbose_name="Object Upload",
storage=MinioBackend( # Configure MinioBackend as storage backend here
bucket_name='django-backend-dev-public',
),
upload_to=iso_date_prefix)
class PrivateAttachment(models.Model):
def set_file_path_name(self, file_name_ext: str) -> str:
"""
Defines the full absolute path to the file in the bucket. The original content's type is used as parent folder.
:param file_name_ext: (str) File name + extension. ie.: cat.png OR images/animals/2019/cat.png
:return: (str) Absolute path to file in Minio Bucket
"""
return f"{get_iso_date()}/{self.content_type.name}/{file_name_ext}"
def delete(self, *args, **kwargs):
"""
Delete must be overridden because the inherited delete method does not call `self.file.delete()`.
"""
self.file.delete()
super(PrivateAttachment, self).delete(*args, **kwargs)
@property
def file_name(self):
try:
return self.file.name.split("/")[-1]
except AttributeError:
return "[Deleted Object]"
@property
def file_size(self):
return self.file.size
def __str__(self):
return str(self.file)
id = models.AutoField(primary_key=True, verbose_name="Public Attachment ID")
content_type: ContentType = models.ForeignKey(ContentType, null=True, blank=True, on_delete=models.CASCADE,
verbose_name="Content Type")
object_id = models.PositiveIntegerField(null=True, blank=True, verbose_name="Related Object's ID")
content_object = GenericForeignKey("content_type", "object_id")
file: FieldFile = models.FileField(verbose_name="Object Upload",
storage=MinioBackend( # Configure MinioBackend as storage backend here
bucket_name='django-backend-dev-private',
),
upload_to=set_file_path_name)