models.py
1.82 KB
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
import os
from django.db import models
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse_lazy
from topics.models import Resource
def validate_file_extension(value):
valid_formats = [
'image/jpeg','image/x-citrix-jpeg','image/png','image/x-citrix-png','image/x-png',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'application/vnd.ms-excel','text/html','application/msword','application/vnd.oasis.opendocument.presentation',
'application/vnd.oasis.opendocument.spreadsheet','application/vnd.oasis.opendocument.text',
'application/pdf'
]
if hasattr(value.file, 'content_type'):
if not value.file.content_type in valid_formats:
raise ValidationError(_('Please select a valid file. The uploaded file must have one of the following extensions: .doc, .docx, .html, .jpg, .odp, .ods, .odt, .pdf, .png, .ppt, .pptx, .xlx e .xlsx'))
class FileLink(Resource):
file_content = models.FileField(_('File'), blank = True, upload_to = 'files/', validators = [validate_file_extension])
class Meta:
verbose_name = _('File Link')
verbose_name_plural = _('File Links')
@property
def filename(self):
return os.path.basename(self.file_content.name)
def __str__(self):
return self.name
def access_link(self):
return reverse_lazy('file_links:download', args = (), kwargs = {'slug': self.slug})
def update_link(self):
return 'file_links:update'
def delete_link(self):
return 'file_links:delete'
def delete_message(self):
return _('Are you sure you want delete the file link')