forked from redsolution/django-tinymce-attachment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
146 lines (120 loc) · 5.62 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
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: utf-8 -*-
import os
from django.db import models
from imagekit.models import ImageModel
from django.utils.translation import ugettext_lazy as _
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from attachment import settings
from .fields import ImagePreviewField
class AttachmentImage(ImageModel):
class Meta:
verbose_name = _('image')
verbose_name_plural = _('images')
ordering = ('position',)
class IKOptions:
spec_module = settings.ATTACHMENT_IKSPECS
cache_dir = settings.ATTACHMENT_CACHE_DIR
cache_filename_format = "%(filename)s-%(specname)s.%(extension)s"
image_field = 'image'
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
position = models.IntegerField(verbose_name=_('position'), default=1, blank=False)
image = ImagePreviewField(verbose_name=_('image'), upload_to=settings.ATTACHMENT_UPLOAD_DIR)
title = models.TextField(verbose_name=_('title'), blank=True, null=True)
group = models.CharField(verbose_name=_('group'), max_length=200, blank=True, null=True)
role = models.CharField(verbose_name=_('role'), max_length=200, blank=True, null=True)
def __unicode__(self):
if self.image:
return os.path.basename(self.image.url)
else:
return u''
class AttachmentFile(models.Model):
class Meta:
verbose_name = _('file')
verbose_name_plural = _('files')
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
position = models.IntegerField(verbose_name=_('position'), default=1, blank=False)
file = models.FileField(verbose_name=_('file'), upload_to=settings.ATTACHMENT_UPLOAD_DIR)
title = models.TextField(verbose_name=_('title'), blank=True, null=True)
def __unicode__(self):
if self.file:
return os.path.basename(self.file.url)
else:
return u''
class AttachmentArchive(models.Model):
class Meta:
verbose_name = u'Архив вложений'
verbose_name_plural = u'Архивы вложений'
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
file_list = models.TextField(verbose_name=u'Список файлов', null=True, blank=True)
uploaded_date = models.DateTimeField(verbose_name=u'Загружен', auto_now_add=True)
unpacked = models.BooleanField(verbose_name=u'Распакован', default=False)
zip_file = models.FileField(
verbose_name=u'ZIP-архив',
upload_to=settings.ATTACHMENT_UPLOAD_DIR,
help_text=u'Внимание: загрузка и распаковка архива может занять длительное время'
)
use_filenames_as_titles = models.BooleanField(
verbose_name=u'Использовать имена файлов в качестве заголовков',
default=False
)
def __unicode__(self):
title = 'Null' if not self.content_object else self.content_object._meta.verbose_name.title()
return u'{model} - {object}'.format(
model=title,
object=self.content_object
)
def files_count(self):
if self.file_list:
return len(self.file_list.splitlines())
return 0
def get_attachments(self):
if self.file_list:
return AttachmentImage.objects.filter(
content_type=self.content_type,
object_id=self.object_id,
image__in=self.file_list.splitlines()
)
return AttachmentImage.objects.none()
def get_attachment_list(self):
return list(self.get_attachments())
def remove_attachments(self):
self.get_attachments().delete()
def remove_source_images(self):
if self.file_list:
for attachment_file in self.file_list.splitlines():
path_to_file = os.path.join(settings.MEDIA_ROOT, attachment_file)
try:
os.remove(path_to_file)
except OSError:
pass
def _get_filename(self, name_ext):
if '.' in os.path.basename(name_ext):
return os.path.basename(name_ext).rpartition('.')[0]
else:
return os.path.basename(name_ext)
def remove_cache_images(self):
if self.file_list:
path_to_cache_dir = os.path.join(settings.MEDIA_ROOT, settings.ATTACHMENT_CACHE_DIR, settings.ATTACHMENT_UPLOAD_DIR)
cache_list = os.listdir(path_to_cache_dir)
for attachment_file in self.file_list.splitlines():
name_without_ext = self._get_filename(attachment_file)
cache_files = []
for image in cache_list:
try:
if name_without_ext in image:
os.path.join(path_to_cache_dir, image)
# если название файла на кириллице
except UnicodeDecodeError:
pass
for path_to_file in cache_files:
try:
os.remove(path_to_file)
except OSError:
pass