Show More
@@ -0,0 +1,20 b'' | |||||
|
1 | # -*- coding: utf-8 -*- | |||
|
2 | # Generated by Django 1.9.5 on 2016-11-20 11:44 | |||
|
3 | from __future__ import unicode_literals | |||
|
4 | ||||
|
5 | from django.db import migrations, models | |||
|
6 | ||||
|
7 | ||||
|
8 | class Migration(migrations.Migration): | |||
|
9 | ||||
|
10 | dependencies = [ | |||
|
11 | ('boards', '0051_auto_20161013_1037'), | |||
|
12 | ] | |||
|
13 | ||||
|
14 | operations = [ | |||
|
15 | migrations.AlterField( | |||
|
16 | model_name='attachment', | |||
|
17 | name='url', | |||
|
18 | field=models.TextField(blank=True, null=True), | |||
|
19 | ), | |||
|
20 | ] |
@@ -1,88 +1,88 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 | file = models.FileField(upload_to=get_upload_filename, null=True) |
|
45 | file = models.FileField(upload_to=get_upload_filename, null=True) | |
46 | mimetype = models.CharField(max_length=50, null=True) |
|
46 | mimetype = models.CharField(max_length=50, null=True) | |
47 | hash = models.CharField(max_length=36, null=True) |
|
47 | hash = models.CharField(max_length=36, null=True) | |
48 | alias = models.TextField(unique=True, null=True, blank=True) |
|
48 | alias = models.TextField(unique=True, null=True, blank=True) | |
49 | url = models.TextField(null=True) |
|
49 | url = models.TextField(null=True, blank=True) | |
50 |
|
50 | |||
51 | def get_view(self): |
|
51 | def get_view(self): | |
52 | file_viewer = None |
|
52 | file_viewer = None | |
53 | for viewer in get_viewers(): |
|
53 | for viewer in get_viewers(): | |
54 | if viewer.supports(self.mimetype): |
|
54 | if viewer.supports(self.mimetype): | |
55 | file_viewer = viewer |
|
55 | file_viewer = viewer | |
56 | break |
|
56 | break | |
57 | if file_viewer is None: |
|
57 | if file_viewer is None: | |
58 | file_viewer = AbstractViewer |
|
58 | file_viewer = AbstractViewer | |
59 |
|
59 | |||
60 | return file_viewer(self.file, self.mimetype, self.hash, self.url).get_view() |
|
60 | return file_viewer(self.file, self.mimetype, self.hash, self.url).get_view() | |
61 |
|
61 | |||
62 | def __str__(self): |
|
62 | def __str__(self): | |
63 | return self.url or self.file.url |
|
63 | return self.url or self.file.url | |
64 |
|
64 | |||
65 | def get_random_associated_post(self): |
|
65 | def get_random_associated_post(self): | |
66 | posts = boards.models.Post.objects.filter(attachments__in=[self]) |
|
66 | posts = boards.models.Post.objects.filter(attachments__in=[self]) | |
67 | return posts.order_by('?').first() |
|
67 | return posts.order_by('?').first() | |
68 |
|
68 | |||
69 | @cached_result() |
|
69 | @cached_result() | |
70 | def get_size(self): |
|
70 | def get_size(self): | |
71 | if self.file: |
|
71 | if self.file: | |
72 | if self.mimetype in FILE_TYPES_IMAGE: |
|
72 | if self.mimetype in FILE_TYPES_IMAGE: | |
73 | return get_image_dimensions(self.file) |
|
73 | return get_image_dimensions(self.file) | |
74 | else: |
|
74 | else: | |
75 | return 200, 150 |
|
75 | return 200, 150 | |
76 |
|
76 | |||
77 | def get_thumb_url(self): |
|
77 | def get_thumb_url(self): | |
78 | split = self.file.url.rsplit('.', 1) |
|
78 | split = self.file.url.rsplit('.', 1) | |
79 | w, h = 200, 150 |
|
79 | w, h = 200, 150 | |
80 | return '%s.%sx%s.%s' % (split[0], w, h, split[1]) |
|
80 | return '%s.%sx%s.%s' % (split[0], w, h, split[1]) | |
81 |
|
81 | |||
82 | @cached_result() |
|
82 | @cached_result() | |
83 | def get_preview_size(self): |
|
83 | def get_preview_size(self): | |
84 | if self.mimetype in FILE_TYPES_IMAGE: |
|
84 | if self.mimetype in FILE_TYPES_IMAGE: | |
85 | preview_path = self.file.path.replace('.', '.200x150.') |
|
85 | preview_path = self.file.path.replace('.', '.200x150.') | |
86 | return get_image_dimensions(preview_path) |
|
86 | return get_image_dimensions(preview_path) | |
87 | else: |
|
87 | else: | |
88 | return 200, 150 |
|
88 | return 200, 150 |
General Comments 0
You need to be logged in to leave comments.
Login now