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