##// END OF EJS Templates
Properly order attachments across one post
neko259 -
r1759:eaa1de72 default
parent child Browse files
Show More
@@ -1,91 +1,95 b''
1 1 import boards
2 2 from boards.models import STATUS_ARCHIVE
3 3 from django.core.files.images import get_image_dimensions
4 4 from django.db import models
5 5
6 6 from boards import utils
7 7 from boards.models.attachment.viewers import get_viewers, AbstractViewer, \
8 8 FILE_TYPES_IMAGE
9 9 from boards.utils import get_upload_filename, get_extension, cached_result
10 10
11 11
12 12 class AttachmentManager(models.Manager):
13 13 def create_with_hash(self, file):
14 14 file_hash = utils.get_file_hash(file)
15 15 existing = self.filter(hash=file_hash)
16 16 if len(existing) > 0:
17 17 attachment = existing[0]
18 18 else:
19 19 # FIXME Use full mimetype here, need to modify viewers too
20 20 file_type = get_extension(file.name)
21 21 attachment = self.create(file=file, mimetype=file_type,
22 22 hash=file_hash)
23 23
24 24 return attachment
25 25
26 26 def create_from_url(self, url):
27 27 existing = self.filter(url=url)
28 28 if len(existing) > 0:
29 29 attachment = existing[0]
30 30 else:
31 31 attachment = self.create(url=url)
32 32 return attachment
33 33
34 34 def get_random_images(self, count, tags=None):
35 35 images = self.filter(mimetype__in=FILE_TYPES_IMAGE).exclude(
36 36 attachment_posts__thread__status=STATUS_ARCHIVE)
37 37 if tags is not None:
38 38 images = images.filter(attachment_posts__threads__tags__in=tags)
39 39 return images.order_by('?')[:count]
40 40
41 41
42 42 class Attachment(models.Model):
43 43 objects = AttachmentManager()
44 44
45 class Meta:
46 app_label = 'boards'
47 ordering = ('id',)
48
45 49 file = models.FileField(upload_to=get_upload_filename, null=True)
46 50 mimetype = models.CharField(max_length=50, null=True)
47 51 hash = models.CharField(max_length=36, null=True)
48 52 alias = models.TextField(unique=True, null=True)
49 53 url = models.TextField(blank=True, default='')
50 54
51 55 def get_view(self):
52 56 file_viewer = None
53 57 for viewer in get_viewers():
54 58 if viewer.supports(self.mimetype):
55 59 file_viewer = viewer
56 60 break
57 61 if file_viewer is None:
58 62 file_viewer = AbstractViewer
59 63
60 64 return file_viewer(self.file, self.mimetype, self.hash, self.url).get_view()
61 65
62 66 def __str__(self):
63 67 return self.url or self.file.url
64 68
65 69 def get_random_associated_post(self):
66 70 posts = boards.models.Post.objects.filter(attachments__in=[self])
67 71 return posts.order_by('?').first()
68 72
69 73 @cached_result()
70 74 def get_size(self):
71 75 if self.file:
72 76 if self.mimetype in FILE_TYPES_IMAGE:
73 77 return get_image_dimensions(self.file)
74 78 else:
75 79 return 200, 150
76 80
77 81 def get_thumb_url(self):
78 82 split = self.file.url.rsplit('.', 1)
79 83 w, h = 200, 150
80 84 return '%s.%sx%s.%s' % (split[0], w, h, split[1])
81 85
82 86 @cached_result()
83 87 def get_preview_size(self):
84 88 if self.mimetype in FILE_TYPES_IMAGE:
85 89 preview_path = self.file.path.replace('.', '.200x150.')
86 90 return get_image_dimensions(preview_path)
87 91 else:
88 92 return 200, 150
89 93
90 94 def is_internal(self):
91 95 return self.url is None or len(self.url) == 0
General Comments 0
You need to be logged in to leave comments. Login now