paperclip 2.7.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- paperclip/__init__.py +0 -0
- paperclip/admin.py +7 -0
- paperclip/forms.py +121 -0
- paperclip/locale/en/LC_MESSAGES/django.mo +0 -0
- paperclip/locale/fr/LC_MESSAGES/django.mo +0 -0
- paperclip/models.py +223 -0
- paperclip/settings.py +36 -0
- paperclip/static/paperclip/bootstrap-3-confirm.js +47 -0
- paperclip/static/paperclip/bootstrap-4-confirm.js +47 -0
- paperclip/static/paperclip/bootstrap-confirm.js +34 -0
- paperclip/static/paperclip/fileicons/7z.png +0 -0
- paperclip/static/paperclip/fileicons/ai.png +0 -0
- paperclip/static/paperclip/fileicons/aiff.png +0 -0
- paperclip/static/paperclip/fileicons/asc.png +0 -0
- paperclip/static/paperclip/fileicons/audio.png +0 -0
- paperclip/static/paperclip/fileicons/bin.png +0 -0
- paperclip/static/paperclip/fileicons/bz2.png +0 -0
- paperclip/static/paperclip/fileicons/c.png +0 -0
- paperclip/static/paperclip/fileicons/cfc.png +0 -0
- paperclip/static/paperclip/fileicons/cfm.png +0 -0
- paperclip/static/paperclip/fileicons/chm.png +0 -0
- paperclip/static/paperclip/fileicons/class.png +0 -0
- paperclip/static/paperclip/fileicons/conf.png +0 -0
- paperclip/static/paperclip/fileicons/cpp.png +0 -0
- paperclip/static/paperclip/fileicons/cs.png +0 -0
- paperclip/static/paperclip/fileicons/css.png +0 -0
- paperclip/static/paperclip/fileicons/csv.png +0 -0
- paperclip/static/paperclip/fileicons/deb.png +0 -0
- paperclip/static/paperclip/fileicons/divx.png +0 -0
- paperclip/static/paperclip/fileicons/doc.png +0 -0
- paperclip/static/paperclip/fileicons/dot.png +0 -0
- paperclip/static/paperclip/fileicons/eml.png +0 -0
- paperclip/static/paperclip/fileicons/enc.png +0 -0
- paperclip/static/paperclip/fileicons/file.png +0 -0
- paperclip/static/paperclip/fileicons/gif.png +0 -0
- paperclip/static/paperclip/fileicons/gpx.png +0 -0
- paperclip/static/paperclip/fileicons/gz.png +0 -0
- paperclip/static/paperclip/fileicons/hlp.png +0 -0
- paperclip/static/paperclip/fileicons/htm.png +0 -0
- paperclip/static/paperclip/fileicons/html.png +0 -0
- paperclip/static/paperclip/fileicons/image.png +0 -0
- paperclip/static/paperclip/fileicons/iso.png +0 -0
- paperclip/static/paperclip/fileicons/jar.png +0 -0
- paperclip/static/paperclip/fileicons/java.png +0 -0
- paperclip/static/paperclip/fileicons/jpeg.png +0 -0
- paperclip/static/paperclip/fileicons/jpg.png +0 -0
- paperclip/static/paperclip/fileicons/js.png +0 -0
- paperclip/static/paperclip/fileicons/kml.png +0 -0
- paperclip/static/paperclip/fileicons/lua.png +0 -0
- paperclip/static/paperclip/fileicons/m.png +0 -0
- paperclip/static/paperclip/fileicons/mm.png +0 -0
- paperclip/static/paperclip/fileicons/mov.png +0 -0
- paperclip/static/paperclip/fileicons/mp3.png +0 -0
- paperclip/static/paperclip/fileicons/mpeg.png +0 -0
- paperclip/static/paperclip/fileicons/mpg.png +0 -0
- paperclip/static/paperclip/fileicons/odc.png +0 -0
- paperclip/static/paperclip/fileicons/odf.png +0 -0
- paperclip/static/paperclip/fileicons/odg.png +0 -0
- paperclip/static/paperclip/fileicons/odi.png +0 -0
- paperclip/static/paperclip/fileicons/odp.png +0 -0
- paperclip/static/paperclip/fileicons/ods.png +0 -0
- paperclip/static/paperclip/fileicons/odt.png +0 -0
- paperclip/static/paperclip/fileicons/ogg.png +0 -0
- paperclip/static/paperclip/fileicons/pdf.png +0 -0
- paperclip/static/paperclip/fileicons/pgp.png +0 -0
- paperclip/static/paperclip/fileicons/php.png +0 -0
- paperclip/static/paperclip/fileicons/pl.png +0 -0
- paperclip/static/paperclip/fileicons/png.png +0 -0
- paperclip/static/paperclip/fileicons/ppt.png +0 -0
- paperclip/static/paperclip/fileicons/ps.png +0 -0
- paperclip/static/paperclip/fileicons/py.png +0 -0
- paperclip/static/paperclip/fileicons/ram.png +0 -0
- paperclip/static/paperclip/fileicons/rar.png +0 -0
- paperclip/static/paperclip/fileicons/rb.png +0 -0
- paperclip/static/paperclip/fileicons/rm.png +0 -0
- paperclip/static/paperclip/fileicons/rpm.png +0 -0
- paperclip/static/paperclip/fileicons/rtf.png +0 -0
- paperclip/static/paperclip/fileicons/shp.png +0 -0
- paperclip/static/paperclip/fileicons/sig.png +0 -0
- paperclip/static/paperclip/fileicons/sql.png +0 -0
- paperclip/static/paperclip/fileicons/swf.png +0 -0
- paperclip/static/paperclip/fileicons/sxc.png +0 -0
- paperclip/static/paperclip/fileicons/sxd.png +0 -0
- paperclip/static/paperclip/fileicons/sxi.png +0 -0
- paperclip/static/paperclip/fileicons/sxw.png +0 -0
- paperclip/static/paperclip/fileicons/tar.png +0 -0
- paperclip/static/paperclip/fileicons/tex.png +0 -0
- paperclip/static/paperclip/fileicons/tgz.png +0 -0
- paperclip/static/paperclip/fileicons/txt.png +0 -0
- paperclip/static/paperclip/fileicons/vcf.png +0 -0
- paperclip/static/paperclip/fileicons/video.png +0 -0
- paperclip/static/paperclip/fileicons/vsd.png +0 -0
- paperclip/static/paperclip/fileicons/wav.png +0 -0
- paperclip/static/paperclip/fileicons/wma.png +0 -0
- paperclip/static/paperclip/fileicons/wmv.png +0 -0
- paperclip/static/paperclip/fileicons/xls.png +0 -0
- paperclip/static/paperclip/fileicons/xml.png +0 -0
- paperclip/static/paperclip/fileicons/xpi.png +0 -0
- paperclip/static/paperclip/fileicons/xvid.png +0 -0
- paperclip/static/paperclip/fileicons/zip.png +0 -0
- paperclip/static/paperclip/paperclip.js +155 -0
- paperclip/static/paperclip/spin.min.js +2 -0
- paperclip/static/paperclip/star-off.svg +51 -0
- paperclip/static/paperclip/star-on.svg +75 -0
- paperclip/templates/paperclip/_attachment_form.html +27 -0
- paperclip/templates/paperclip/_attachment_table.html +82 -0
- paperclip/templates/paperclip/attachment_list.html +39 -0
- paperclip/templatetags/__init__.py +0 -0
- paperclip/templatetags/attachments_tags.py +140 -0
- paperclip/urls.py +21 -0
- paperclip/utils.py +13 -0
- paperclip/validators.py +52 -0
- paperclip/views.py +170 -0
- paperclip-2.7.3.dist-info/LICENSE +165 -0
- paperclip-2.7.3.dist-info/METADATA +602 -0
- paperclip-2.7.3.dist-info/RECORD +118 -0
- paperclip-2.7.3.dist-info/WHEEL +5 -0
- paperclip-2.7.3.dist-info/top_level.txt +1 -0
paperclip/__init__.py
ADDED
|
File without changes
|
paperclip/admin.py
ADDED
paperclip/forms.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
from django import forms
|
|
2
|
+
from django.core.files.images import get_image_dimensions
|
|
3
|
+
from django.urls import reverse
|
|
4
|
+
from django.utils.translation import gettext_lazy as _
|
|
5
|
+
|
|
6
|
+
from paperclip import settings
|
|
7
|
+
from paperclip.utils import is_an_image, mimetype
|
|
8
|
+
|
|
9
|
+
MODE_CHOICED = [('File', _('File')), ]
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class AttachmentForm(forms.ModelForm):
|
|
13
|
+
|
|
14
|
+
if settings.PAPERCLIP_ENABLE_VIDEO:
|
|
15
|
+
MODE_CHOICED.append(('Youtube', _('Youtube/Soundcloud URL')))
|
|
16
|
+
|
|
17
|
+
if settings.PAPERCLIP_ENABLE_LINK:
|
|
18
|
+
MODE_CHOICED.append(('Link', _('External picture link')))
|
|
19
|
+
|
|
20
|
+
if settings.PAPERCLIP_ENABLE_VIDEO or settings.PAPERCLIP_ENABLE_LINK:
|
|
21
|
+
embed = forms.TypedChoiceField(
|
|
22
|
+
label=_("Mode"),
|
|
23
|
+
choices=MODE_CHOICED,
|
|
24
|
+
widget=forms.RadioSelect(), initial=False)
|
|
25
|
+
next = forms.CharField(widget=forms.HiddenInput())
|
|
26
|
+
|
|
27
|
+
class Meta:
|
|
28
|
+
model = settings.get_attachment_model()
|
|
29
|
+
if settings.PAPERCLIP_ENABLE_VIDEO and not settings.PAPERCLIP_ENABLE_LINK:
|
|
30
|
+
fields = ('embed', 'attachment_file', 'attachment_video',
|
|
31
|
+
'filetype', 'license', 'author', 'title', 'legend')
|
|
32
|
+
elif settings.PAPERCLIP_ENABLE_VIDEO and settings.PAPERCLIP_ENABLE_LINK:
|
|
33
|
+
fields = ('embed', 'attachment_file', 'attachment_video', 'attachment_link',
|
|
34
|
+
'filetype', 'license', 'author', 'title', 'legend')
|
|
35
|
+
elif settings.PAPERCLIP_ENABLE_LINK and not settings.PAPERCLIP_ENABLE_VIDEO:
|
|
36
|
+
fields = ('embed', 'attachment_file', 'attachment_link',
|
|
37
|
+
'filetype', 'license', 'author', 'title', 'legend')
|
|
38
|
+
else:
|
|
39
|
+
fields = ('attachment_file', 'filetype', 'license', 'author', 'title',
|
|
40
|
+
'legend')
|
|
41
|
+
|
|
42
|
+
def __init__(self, request, *args, **kwargs):
|
|
43
|
+
self._object = kwargs.pop('object', None)
|
|
44
|
+
next_url = kwargs.pop('next_url', None)
|
|
45
|
+
|
|
46
|
+
super().__init__(*args, **kwargs)
|
|
47
|
+
self.fields['legend'].widget.attrs['placeholder'] = _('Sunset on lake')
|
|
48
|
+
|
|
49
|
+
self.redirect_on_error = False
|
|
50
|
+
# Allow to override filetype choices
|
|
51
|
+
filetype_model = self.fields['filetype'].queryset.model
|
|
52
|
+
self.fields['filetype'].queryset = filetype_model.objects_for(request)
|
|
53
|
+
|
|
54
|
+
# Detect fields errors without uploading (using HTML5)
|
|
55
|
+
self.fields['filetype'].widget.attrs['required'] = 'required'
|
|
56
|
+
self.fields['author'].widget.attrs['pattern'] = r'^\S.*'
|
|
57
|
+
self.fields['legend'].widget.attrs['pattern'] = r'^\S.*'
|
|
58
|
+
|
|
59
|
+
self.fields['attachment_file'].widget = forms.FileInput()
|
|
60
|
+
|
|
61
|
+
next_url = request.POST.get('next') or next_url
|
|
62
|
+
next_url = next_url or request.GET.get('next', '/')
|
|
63
|
+
self.fields['next'].initial = next_url
|
|
64
|
+
|
|
65
|
+
self.is_creation = not self.instance.pk
|
|
66
|
+
|
|
67
|
+
if self.is_creation:
|
|
68
|
+
self.form_url = reverse('add_attachment', kwargs={
|
|
69
|
+
'app_label': self._object._meta.app_label,
|
|
70
|
+
'model_name': self._object._meta.model_name,
|
|
71
|
+
'pk': self._object.pk
|
|
72
|
+
})
|
|
73
|
+
else:
|
|
74
|
+
# When editing an attachment, changing its title won't rename!
|
|
75
|
+
self.fields['title'].widget.attrs['readonly'] = True
|
|
76
|
+
self.form_url = reverse('update_attachment', kwargs={
|
|
77
|
+
'attachment_pk': self.instance.pk
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
def clean(self):
|
|
81
|
+
cleaned_data = super().clean()
|
|
82
|
+
if settings.PAPERCLIP_ENABLE_VIDEO or settings.PAPERCLIP_ENABLE_LINK:
|
|
83
|
+
if cleaned_data['embed'] == 'Youtube' or cleaned_data['embed'] == 'Link':
|
|
84
|
+
cleaned_data['attachment_file'] = ''
|
|
85
|
+
else:
|
|
86
|
+
cleaned_data['attachment_video'] = ''
|
|
87
|
+
cleaned_data['attachment_link'] = ''
|
|
88
|
+
return cleaned_data
|
|
89
|
+
|
|
90
|
+
def clean_attachment_file(self):
|
|
91
|
+
uploaded_image = self.cleaned_data.get("attachment_file", False)
|
|
92
|
+
is_image = is_an_image(mimetype(uploaded_image))
|
|
93
|
+
if not self.is_creation:
|
|
94
|
+
try:
|
|
95
|
+
uploaded_image.file.readline()
|
|
96
|
+
except FileNotFoundError:
|
|
97
|
+
return uploaded_image
|
|
98
|
+
if settings.PAPERCLIP_MAX_BYTES_SIZE_IMAGE and uploaded_image.size and settings.PAPERCLIP_MAX_BYTES_SIZE_IMAGE < uploaded_image.size:
|
|
99
|
+
raise forms.ValidationError(_('The uploaded file is too large'))
|
|
100
|
+
if not is_image:
|
|
101
|
+
return uploaded_image
|
|
102
|
+
width, height = get_image_dimensions(uploaded_image)
|
|
103
|
+
if settings.PAPERCLIP_MIN_IMAGE_UPLOAD_WIDTH and width and settings.PAPERCLIP_MIN_IMAGE_UPLOAD_WIDTH > width:
|
|
104
|
+
raise forms.ValidationError(_('The uploaded file is not wide enough'))
|
|
105
|
+
if settings.PAPERCLIP_MIN_IMAGE_UPLOAD_HEIGHT and height and settings.PAPERCLIP_MIN_IMAGE_UPLOAD_HEIGHT > height:
|
|
106
|
+
raise forms.ValidationError(_('The uploaded file is not tall enough'))
|
|
107
|
+
return uploaded_image
|
|
108
|
+
|
|
109
|
+
def success_url(self):
|
|
110
|
+
return self.cleaned_data.get('next')
|
|
111
|
+
|
|
112
|
+
def save(self, request, *args, **kwargs):
|
|
113
|
+
obj = self._object
|
|
114
|
+
self.instance.creator = request.user
|
|
115
|
+
self.instance.content_object = obj
|
|
116
|
+
if "attachment_file" in self.changed_data:
|
|
117
|
+
# New file : regenerate new random name for this attachment
|
|
118
|
+
instance = super().save(commit=False)
|
|
119
|
+
instance.save(**{'force_refresh_suffix': True})
|
|
120
|
+
return instance
|
|
121
|
+
return super().save(*args, **kwargs)
|
|
Binary file
|
|
Binary file
|
paperclip/models.py
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import random
|
|
3
|
+
import string
|
|
4
|
+
from io import BytesIO
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from django.conf import settings
|
|
8
|
+
from django.contrib.contenttypes.fields import GenericForeignKey
|
|
9
|
+
from django.contrib.contenttypes.models import ContentType
|
|
10
|
+
from django.core.files import File
|
|
11
|
+
from django.core.files.base import ContentFile
|
|
12
|
+
from django.db import models
|
|
13
|
+
from django.template.defaultfilters import slugify
|
|
14
|
+
from django.utils.translation import gettext_lazy as _
|
|
15
|
+
from embed_video.fields import EmbedVideoField
|
|
16
|
+
from PIL import Image
|
|
17
|
+
|
|
18
|
+
from paperclip.settings import (PAPERCLIP_ENABLE_LINK, PAPERCLIP_ENABLE_VIDEO,
|
|
19
|
+
PAPERCLIP_FILETYPE_MODEL,
|
|
20
|
+
PAPERCLIP_LICENSE_MODEL,
|
|
21
|
+
PAPERCLIP_MAX_ATTACHMENT_HEIGHT,
|
|
22
|
+
PAPERCLIP_MAX_ATTACHMENT_WIDTH,
|
|
23
|
+
PAPERCLIP_RESIZE_ATTACHMENTS_ON_UPLOAD,
|
|
24
|
+
PAPERCLIP_RANDOM_SUFFIX_SIZE)
|
|
25
|
+
from paperclip.utils import is_an_image, mimetype
|
|
26
|
+
|
|
27
|
+
from .validators import FileMimetypeValidator
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class FileType(models.Model):
|
|
31
|
+
type = models.CharField(max_length=128, verbose_name=_("File type"))
|
|
32
|
+
|
|
33
|
+
class Meta:
|
|
34
|
+
abstract = True
|
|
35
|
+
verbose_name = _("File type")
|
|
36
|
+
verbose_name_plural = _("File types")
|
|
37
|
+
ordering = ['type']
|
|
38
|
+
|
|
39
|
+
@classmethod
|
|
40
|
+
def objects_for(cls, request):
|
|
41
|
+
# request ignored by default
|
|
42
|
+
return cls.objects.all()
|
|
43
|
+
|
|
44
|
+
def __str__(self):
|
|
45
|
+
return self.type
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class License(models.Model):
|
|
49
|
+
|
|
50
|
+
label = models.CharField(max_length=128, verbose_name=_("License name"), null=False, blank=False, unique=True)
|
|
51
|
+
|
|
52
|
+
def __str__(self):
|
|
53
|
+
return self.label
|
|
54
|
+
|
|
55
|
+
class Meta:
|
|
56
|
+
abstract = True
|
|
57
|
+
verbose_name = _("Attachment license")
|
|
58
|
+
verbose_name_plural = _("Attachment licenses")
|
|
59
|
+
ordering = ['label']
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class AttachmentManager(models.Manager):
|
|
63
|
+
def attachments_for_object(self, obj):
|
|
64
|
+
object_type = ContentType.objects.get_for_model(obj)
|
|
65
|
+
return self.filter(content_type__pk=object_type.id,
|
|
66
|
+
object_id=obj.id)
|
|
67
|
+
|
|
68
|
+
def attachments_for_object_only_type(self, obj, filetype):
|
|
69
|
+
object_type = ContentType.objects.get_for_model(obj)
|
|
70
|
+
return self.filter(content_type__pk=object_type.id,
|
|
71
|
+
object_id=obj.id,
|
|
72
|
+
filetype=filetype)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def random_suffix_regexp():
|
|
76
|
+
return f"-[a-z0-9]{{{PAPERCLIP_RANDOM_SUFFIX_SIZE}}}"
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def attachment_upload(instance, filename):
|
|
80
|
+
_, name = os.path.split(filename)
|
|
81
|
+
name, ext = os.path.splitext(name)
|
|
82
|
+
name = slugify(name) + ext
|
|
83
|
+
return 'paperclip/%s/%s/%s' % (
|
|
84
|
+
'%s_%s' % (instance.content_object._meta.app_label,
|
|
85
|
+
instance.content_object._meta.model_name),
|
|
86
|
+
instance.content_object.pk,
|
|
87
|
+
name)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
class Attachment(models.Model):
|
|
91
|
+
objects = AttachmentManager()
|
|
92
|
+
|
|
93
|
+
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
|
|
94
|
+
object_id = models.PositiveIntegerField()
|
|
95
|
+
content_object = GenericForeignKey('content_type', 'object_id')
|
|
96
|
+
|
|
97
|
+
attachment_file = models.FileField(_('File'), blank=True,
|
|
98
|
+
upload_to=attachment_upload,
|
|
99
|
+
max_length=512,
|
|
100
|
+
validators=[FileMimetypeValidator()])
|
|
101
|
+
if PAPERCLIP_ENABLE_VIDEO:
|
|
102
|
+
attachment_video = EmbedVideoField(_('Video URL'), blank=True)
|
|
103
|
+
if PAPERCLIP_ENABLE_LINK:
|
|
104
|
+
attachment_link = models.URLField(_('Picture URL'), blank=True)
|
|
105
|
+
filetype = models.ForeignKey(PAPERCLIP_FILETYPE_MODEL, verbose_name=_('File type'), on_delete=models.CASCADE)
|
|
106
|
+
|
|
107
|
+
creator = models.ForeignKey(settings.AUTH_USER_MODEL,
|
|
108
|
+
related_name="created_attachments",
|
|
109
|
+
verbose_name=_('Creator'),
|
|
110
|
+
help_text=_("User that uploaded"), on_delete=models.CASCADE)
|
|
111
|
+
license = models.ForeignKey(PAPERCLIP_LICENSE_MODEL,
|
|
112
|
+
verbose_name=_("License"),
|
|
113
|
+
null=True,
|
|
114
|
+
blank=True,
|
|
115
|
+
on_delete=models.SET_NULL)
|
|
116
|
+
author = models.CharField(blank=True, default='', max_length=128,
|
|
117
|
+
verbose_name=_('Author'),
|
|
118
|
+
help_text=_("Original creator"))
|
|
119
|
+
title = models.CharField(blank=True, default='', max_length=128,
|
|
120
|
+
verbose_name=_("Filename"),
|
|
121
|
+
help_text=_("Renames the file"))
|
|
122
|
+
legend = models.CharField(blank=True, default='', max_length=128,
|
|
123
|
+
verbose_name=_("Legend"),
|
|
124
|
+
help_text=_("Details displayed"))
|
|
125
|
+
starred = models.BooleanField(default=False,
|
|
126
|
+
verbose_name=_("Starred"),
|
|
127
|
+
help_text=_("Mark as starred"))
|
|
128
|
+
is_image = models.BooleanField(editable=False, default=False, verbose_name=_("Is image"),
|
|
129
|
+
help_text=_("Is an image file"), db_index=True)
|
|
130
|
+
date_insert = models.DateTimeField(editable=False, auto_now_add=True,
|
|
131
|
+
verbose_name=_("Insertion date"))
|
|
132
|
+
date_update = models.DateTimeField(editable=False, auto_now=True,
|
|
133
|
+
verbose_name=_("Update date"))
|
|
134
|
+
random_suffix = models.CharField(null=False, blank=True, default='', max_length=128)
|
|
135
|
+
|
|
136
|
+
def save(self, *args, **kwargs):
|
|
137
|
+
force_refresh_suffix = kwargs.pop("force_refresh_suffix", False)
|
|
138
|
+
if self.attachment_file:
|
|
139
|
+
basename = kwargs.pop("basename", None)
|
|
140
|
+
self.is_image = self.is_an_image()
|
|
141
|
+
if not self.pk or force_refresh_suffix:
|
|
142
|
+
self.random_suffix = None
|
|
143
|
+
name = self.prepare_file_suffix(basename=basename)
|
|
144
|
+
self.attachment_file.name = attachment_upload(self, name)
|
|
145
|
+
if not kwargs.pop("skip_file_save", False) and PAPERCLIP_RESIZE_ATTACHMENTS_ON_UPLOAD and self.attachment_file and self.is_image and 'svg' not in mimetype(self.attachment_file).split('/')[-1]:
|
|
146
|
+
# Resize image
|
|
147
|
+
image = Image.open(self.attachment_file).convert('RGB')
|
|
148
|
+
image.thumbnail((PAPERCLIP_MAX_ATTACHMENT_WIDTH, PAPERCLIP_MAX_ATTACHMENT_HEIGHT))
|
|
149
|
+
# Write resized image
|
|
150
|
+
output = BytesIO()
|
|
151
|
+
ext = Path(self.attachment_file.name).suffix.split('.')[-1] # JPEG, PNG..
|
|
152
|
+
if ext == 'jpg' or ext == 'JPG': # PIL does not know JPGs are JPEGs
|
|
153
|
+
ext = 'jpeg'
|
|
154
|
+
image.save(output, format=ext)
|
|
155
|
+
output.seek(0)
|
|
156
|
+
# Replace attachment
|
|
157
|
+
content_file = ContentFile(output.read())
|
|
158
|
+
f = File(content_file)
|
|
159
|
+
name = self.prepare_file_suffix(basename=basename)
|
|
160
|
+
self.attachment_file.name = attachment_upload(self, name)
|
|
161
|
+
self.attachment_file.save(name, f, save=False)
|
|
162
|
+
super().save(*args, **kwargs)
|
|
163
|
+
|
|
164
|
+
class Meta:
|
|
165
|
+
abstract = True
|
|
166
|
+
ordering = ['-date_insert']
|
|
167
|
+
verbose_name = _("Attachment")
|
|
168
|
+
verbose_name_plural = _("Attachments")
|
|
169
|
+
default_permissions = ()
|
|
170
|
+
permissions = (
|
|
171
|
+
('add_attachment', _('Can add attachments')),
|
|
172
|
+
('change_attachment', _('Can change attachments')),
|
|
173
|
+
('delete_attachment', _('Can delete attachments')),
|
|
174
|
+
('read_attachment', _('Can read attachments')),
|
|
175
|
+
('delete_attachment_others', _("Can delete others' attachments")),
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
def __str__(self):
|
|
179
|
+
return '{} attached {}'.format(
|
|
180
|
+
self.creator.username,
|
|
181
|
+
self.attachment_file.name
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
@property
|
|
185
|
+
def filename(self):
|
|
186
|
+
return os.path.split(self.attachment_file.name)[1]
|
|
187
|
+
|
|
188
|
+
@property
|
|
189
|
+
def mimetype(self):
|
|
190
|
+
return mimetype(self.attachment_file)
|
|
191
|
+
|
|
192
|
+
def is_an_image(self):
|
|
193
|
+
return is_an_image(mimetype(self.attachment_file))
|
|
194
|
+
|
|
195
|
+
def prepare_file_suffix(self, basename=None):
|
|
196
|
+
""" Add random file suffix and return new filename to use in attachment_file.save
|
|
197
|
+
"""
|
|
198
|
+
if self.attachment_file or basename:
|
|
199
|
+
if not self.random_suffix:
|
|
200
|
+
# Create random suffix
|
|
201
|
+
# #### /!\ If you change this line, make sure to update 'random_suffix_regexp' method above
|
|
202
|
+
self.random_suffix = '-' + ''.join(random.choices(string.ascii_lowercase + string.digits, k=PAPERCLIP_RANDOM_SUFFIX_SIZE))
|
|
203
|
+
# #### /!\ If you change this line, make sure to update 'random_suffix_regexp' method above
|
|
204
|
+
if basename:
|
|
205
|
+
_, basename = os.path.split(basename)
|
|
206
|
+
basename, ext = os.path.splitext(basename)
|
|
207
|
+
else:
|
|
208
|
+
_, name = os.path.split(self.attachment_file.name)
|
|
209
|
+
name, ext = os.path.splitext(name)
|
|
210
|
+
subfolder = '%s/%s' % (
|
|
211
|
+
'%s_%s' % (self.content_object._meta.app_label,
|
|
212
|
+
self.content_object._meta.model_name),
|
|
213
|
+
self.content_object.pk)
|
|
214
|
+
# Compute maximum size left for filename
|
|
215
|
+
max_filename_size = self._meta.get_field('attachment_file').max_length - len('paperclip/') - PAPERCLIP_RANDOM_SUFFIX_SIZE - len(subfolder) - len(ext) - 1
|
|
216
|
+
# In case PAPERCLIP_RANDOM_SUFFIX_SIZE is too big
|
|
217
|
+
max_filename_size = max(0, max_filename_size)
|
|
218
|
+
# Create new name with suffix and proper size
|
|
219
|
+
name = slugify(basename or self.title or name)[:max_filename_size]
|
|
220
|
+
return name + self.random_suffix + ext
|
|
221
|
+
_, name = os.path.split(self.attachment_file.name)
|
|
222
|
+
return name
|
|
223
|
+
return None
|
paperclip/settings.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
from django.apps import apps
|
|
2
|
+
from django.conf import settings
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
PAPERCLIP_ENABLE_VIDEO = getattr(settings, 'PAPERCLIP_ENABLE_VIDEO', False)
|
|
6
|
+
PAPERCLIP_ENABLE_LINK = getattr(settings, 'PAPERCLIP_ENABLE_LINK', False)
|
|
7
|
+
PAPERCLIP_ACTION_HISTORY_ENABLED = getattr(settings, 'PAPERCLIP_ACTION_HISTORY_ENABLED', True)
|
|
8
|
+
PAPERCLIP_FILETYPE_MODEL = settings.PAPERCLIP_FILETYPE_MODEL
|
|
9
|
+
PAPERCLIP_ATTACHMENT_MODEL = settings.PAPERCLIP_ATTACHMENT_MODEL
|
|
10
|
+
PAPERCLIP_LICENSE_MODEL = settings.PAPERCLIP_LICENSE_MODEL
|
|
11
|
+
PAPERCLIP_MAX_ATTACHMENT_WIDTH = getattr(settings, 'PAPERCLIP_MAX_ATTACHMENT_WIDTH', 1280)
|
|
12
|
+
PAPERCLIP_MAX_ATTACHMENT_HEIGHT = getattr(settings, 'PAPERCLIP_MAX_ATTACHMENT_HEIGHT', 1280)
|
|
13
|
+
PAPERCLIP_ALLOWED_EXTENSIONS = getattr(settings, 'PAPERCLIP_ALLOWED_EXTENSIONS', None)
|
|
14
|
+
PAPERCLIP_EXTRA_ALLOWED_MIMETYPES = getattr(settings, 'PAPERCLIP_EXTRA_ALLOWED_MIMETYPES', {})
|
|
15
|
+
PAPERCLIP_MIN_IMAGE_UPLOAD_WIDTH = getattr(settings, 'PAPERCLIP_MIN_IMAGE_UPLOAD_WIDTH', None)
|
|
16
|
+
PAPERCLIP_MIN_IMAGE_UPLOAD_HEIGHT = getattr(settings, 'PAPERCLIP_MIN_IMAGE_UPLOAD_HEIGHT', None)
|
|
17
|
+
PAPERCLIP_RANDOM_SUFFIX_SIZE = getattr(settings, 'PAPERCLIP_RANDOM_SUFFIX_SIZE', 12)
|
|
18
|
+
PAPERCLIP_MAX_BYTES_SIZE_IMAGE = getattr(settings, 'PAPERCLIP_MAX_BYTES_SIZE_IMAGE', None)
|
|
19
|
+
PAPERCLIP_RESIZE_ATTACHMENTS_ON_UPLOAD = getattr(settings, 'PAPERCLIP_RESIZE_ATTACHMENTS_ON_UPLOAD', False)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def get_filetype_model():
|
|
23
|
+
return apps.get_model(*PAPERCLIP_FILETYPE_MODEL.split('.'))
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def get_attachment_model():
|
|
27
|
+
return apps.get_model(*PAPERCLIP_ATTACHMENT_MODEL.split('.'))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def get_license_model():
|
|
31
|
+
return apps.get_model(*PAPERCLIP_LICENSE_MODEL.split('.'))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def get_attachment_permission(action):
|
|
35
|
+
model = get_attachment_model()
|
|
36
|
+
return '{app}.{action}'.format(app=model._meta.app_label, action=action)
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
(function ($) {
|
|
2
|
+
$.fn.extend({
|
|
3
|
+
//pass the options variable to the function
|
|
4
|
+
confirmModal: function (options) {
|
|
5
|
+
var html = '<div class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">' +
|
|
6
|
+
'<div class="modal-dialog">' +
|
|
7
|
+
'<div class="modal-content">' +
|
|
8
|
+
'<div class="modal-header">' +
|
|
9
|
+
'<button type="button" class="close" data-dismiss="modal">' +
|
|
10
|
+
'<span aria-hidden="true">×</span><span class="sr-only">Close</span>' +
|
|
11
|
+
'</button>' +
|
|
12
|
+
'<h4 class="modal-title">#Heading#</h4>' +
|
|
13
|
+
'</div>' +
|
|
14
|
+
'<div class="modal-body">' +
|
|
15
|
+
'#Body#' +
|
|
16
|
+
'</div>' +
|
|
17
|
+
'<div class="modal-footer">' +
|
|
18
|
+
'<button type="button" class="btn btn-default" data-dismiss="modal">#Close#</button>' +
|
|
19
|
+
'<button type="button" class="btn btn-primary" id="confirmYesBtn">#Confirm#</button>' +
|
|
20
|
+
'</div>' +
|
|
21
|
+
'</div>' +
|
|
22
|
+
'</div>' +
|
|
23
|
+
'</div>';
|
|
24
|
+
|
|
25
|
+
var defaults = {
|
|
26
|
+
heading: 'Please confirm',
|
|
27
|
+
body:'Body contents',
|
|
28
|
+
closeBtnText: 'Close',
|
|
29
|
+
confirmBtnText: 'Confirm',
|
|
30
|
+
callback : null
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
var options = $.extend(defaults, options);
|
|
34
|
+
html = html.replace('#Heading#',options.heading).replace('#Body#',options.body);
|
|
35
|
+
html = html.replace('#Close#',options.closeBtnText).replace('#Confirm#',options.confirmBtnText);
|
|
36
|
+
$(this).html(html);
|
|
37
|
+
$(this).find('.modal').modal('show');
|
|
38
|
+
var context = $(this);
|
|
39
|
+
$('#confirmYesBtn',this).click(function(){
|
|
40
|
+
if(options.callback!=null)
|
|
41
|
+
options.callback();
|
|
42
|
+
$(context).modal('hide');
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
});
|
|
47
|
+
})(jQuery);
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
(function ($) {
|
|
2
|
+
$.fn.extend({
|
|
3
|
+
//pass the options variable to the function
|
|
4
|
+
confirmModal: function (options) {
|
|
5
|
+
var html = '<div class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">' +
|
|
6
|
+
'<div class="modal-dialog">' +
|
|
7
|
+
'<div class="modal-content">' +
|
|
8
|
+
'<div class="modal-header">' +
|
|
9
|
+
'<h5 class="modal-title">#Heading#</h5>' +
|
|
10
|
+
'<button type="button" class="close" data-dismiss="modal" aria-label="Close">' +
|
|
11
|
+
'<span aria-hidden="true">×</span>' +
|
|
12
|
+
'</button>' +
|
|
13
|
+
'</div>' +
|
|
14
|
+
'<div class="modal-body">' +
|
|
15
|
+
'#Body#' +
|
|
16
|
+
'</div>' +
|
|
17
|
+
'<div class="modal-footer">' +
|
|
18
|
+
'<button type="button" class="btn btn-default" data-dismiss="modal">#Close#</button>' +
|
|
19
|
+
'<button type="button" class="btn btn-primary" id="confirmYesBtn">#Confirm#</button>' +
|
|
20
|
+
'</div>' +
|
|
21
|
+
'</div>' +
|
|
22
|
+
'</div>' +
|
|
23
|
+
'</div>';
|
|
24
|
+
|
|
25
|
+
var defaults = {
|
|
26
|
+
heading: 'Please confirm',
|
|
27
|
+
body:'Body contents',
|
|
28
|
+
closeBtnText: 'Close',
|
|
29
|
+
confirmBtnText: 'Confirm',
|
|
30
|
+
callback : null
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
var options = $.extend(defaults, options);
|
|
34
|
+
html = html.replace('#Heading#',options.heading).replace('#Body#',options.body);
|
|
35
|
+
html = html.replace('#Close#',options.closeBtnText).replace('#Confirm#',options.confirmBtnText);
|
|
36
|
+
$(this).html(html);
|
|
37
|
+
$(this).find('.modal').modal('show');
|
|
38
|
+
var context = $(this);
|
|
39
|
+
$('#confirmYesBtn',this).click(function(){
|
|
40
|
+
if(options.callback!=null)
|
|
41
|
+
options.callback();
|
|
42
|
+
$(context).modal('hide');
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
});
|
|
47
|
+
})(jQuery);
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
(function ($) {
|
|
2
|
+
$.fn.extend({
|
|
3
|
+
//pass the options variable to the function
|
|
4
|
+
confirmModal: function (options) {
|
|
5
|
+
var html = '<div class="modal" id="confirmContainer"><div class="modal-header"><a class="close" data-dismiss="modal">×</a>' +
|
|
6
|
+
'<h3>#Heading#</h3></div><div class="modal-body">' +
|
|
7
|
+
'#Body#</div><div class="modal-footer">' +
|
|
8
|
+
'<a href="#" class="btn btn-primary" id="confirmYesBtn">#Confirm#</a>' +
|
|
9
|
+
'<a href="#" class="btn" data-dismiss="modal">#Close#</a></div></div>';
|
|
10
|
+
|
|
11
|
+
var defaults = {
|
|
12
|
+
heading: 'Please confirm',
|
|
13
|
+
body:'Body contents',
|
|
14
|
+
closeBtnText: 'Close',
|
|
15
|
+
confirmBtnText: 'Confirm',
|
|
16
|
+
callback : null
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
var options = $.extend(defaults, options);
|
|
20
|
+
html = html.replace('#Heading#',options.heading).replace('#Body#',options.body);
|
|
21
|
+
html = html.replace('#Close#',options.closeBtnText).replace('#Confirm#',options.confirmBtnText);
|
|
22
|
+
$(this).html(html);
|
|
23
|
+
$(this).find('.modal').modal('show');
|
|
24
|
+
var context = $(this);
|
|
25
|
+
$('#confirmYesBtn',this).click(function(){
|
|
26
|
+
if(options.callback!=null)
|
|
27
|
+
options.callback();
|
|
28
|
+
$(context).modal('hide');
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
})(jQuery);
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|