##// END OF EJS Templates
Deduplicated upload_to method for images and file attachments
neko259 -
r1368:3a697667 default
parent child Browse files
Show More
@@ -0,0 +1,26 b''
1 # -*- coding: utf-8 -*-
2 from __future__ import unicode_literals
3
4 from django.db import migrations, models
5 import boards.thumbs
6 import boards.utils
7
8
9 class Migration(migrations.Migration):
10
11 dependencies = [
12 ('boards', '0033_auto_20151014_2224'),
13 ]
14
15 operations = [
16 migrations.AlterField(
17 model_name='attachment',
18 name='file',
19 field=models.FileField(upload_to=boards.utils.get_upload_filename),
20 ),
21 migrations.AlterField(
22 model_name='postimage',
23 name='image',
24 field=boards.thumbs.ImageWithThumbsField(blank=True, width_field='width', height_field='height', upload_to=boards.utils.get_upload_filename),
25 ),
26 ]
@@ -52,7 +52,7 b' class Migration(migrations.Migration):'
52 ('height', models.IntegerField(default=0)),
52 ('height', models.IntegerField(default=0)),
53 ('pre_width', models.IntegerField(default=0)),
53 ('pre_width', models.IntegerField(default=0)),
54 ('pre_height', models.IntegerField(default=0)),
54 ('pre_height', models.IntegerField(default=0)),
55 ('image', boards.thumbs.ImageWithThumbsField(height_field='height', width_field='width', upload_to=boards.models.image.PostImage._update_image_filename, blank=True)),
55 ('image', boards.thumbs.ImageWithThumbsField(height_field='height', width_field='width', blank=True)),
56 ('hash', models.CharField(max_length=36)),
56 ('hash', models.CharField(max_length=36)),
57 ],
57 ],
58 options={
58 options={
@@ -16,7 +16,7 b' class Migration(migrations.Migration):'
16 name='Attachment',
16 name='Attachment',
17 fields=[
17 fields=[
18 ('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)),
18 ('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)),
19 ('file', models.FileField(upload_to=boards.models.attachment.Attachment._update_filename)),
19 ('file', models.FileField()),
20 ('mimetype', models.CharField(max_length=50)),
20 ('mimetype', models.CharField(max_length=50)),
21 ('hash', models.CharField(max_length=36)),
21 ('hash', models.CharField(max_length=36)),
22 ],
22 ],
@@ -1,14 +1,8 b''
1 import hashlib
1 from django.db import models
2 import os
3 import time
4
5 from random import random
6
2
7 from django.db import models
8 from boards import utils
3 from boards import utils
9
10 from boards.models.attachment.viewers import get_viewers, AbstractViewer
4 from boards.models.attachment.viewers import get_viewers, AbstractViewer
11
5 from boards.utils import get_upload_filename
12
6
13 FILES_DIRECTORY = 'files/'
7 FILES_DIRECTORY = 'files/'
14 FILE_EXTENSION_DELIMITER = '.'
8 FILE_EXTENSION_DELIMITER = '.'
@@ -31,21 +25,7 b' class AttachmentManager(models.Manager):'
31 class Attachment(models.Model):
25 class Attachment(models.Model):
32 objects = AttachmentManager()
26 objects = AttachmentManager()
33
27
34 # TODO Dedup the method
28 file = models.FileField(upload_to=get_upload_filename)
35 def _update_filename(self, filename):
36 """
37 Gets unique filename
38 """
39
40 # TODO Use something other than random number in file name
41 new_name = '{}{}.{}'.format(
42 str(int(time.mktime(time.gmtime()))),
43 str(int(random() * 1000)),
44 filename.split(FILE_EXTENSION_DELIMITER)[-1:][0])
45
46 return os.path.join(FILES_DIRECTORY, new_name)
47
48 file = models.FileField(upload_to=_update_filename)
49 mimetype = models.CharField(max_length=50)
29 mimetype = models.CharField(max_length=50)
50 hash = models.CharField(max_length=36)
30 hash = models.CharField(max_length=36)
51
31
@@ -9,13 +9,12 b' from django.template.defaultfilters impo'
9 from boards import thumbs, utils
9 from boards import thumbs, utils
10 import boards
10 import boards
11 from boards.models.base import Viewable
11 from boards.models.base import Viewable
12 from boards.utils import get_upload_filename
12
13
13 __author__ = 'neko259'
14 __author__ = 'neko259'
14
15
15
16
16 IMAGE_THUMB_SIZE = (200, 150)
17 IMAGE_THUMB_SIZE = (200, 150)
17 IMAGES_DIRECTORY = 'images/'
18 FILE_EXTENSION_DELIMITER = '.'
19 HASH_LENGTH = 36
18 HASH_LENGTH = 36
20
19
21 CSS_CLASS_IMAGE = 'image'
20 CSS_CLASS_IMAGE = 'image'
@@ -47,26 +46,13 b' class PostImage(models.Model, Viewable):'
47 app_label = 'boards'
46 app_label = 'boards'
48 ordering = ('id',)
47 ordering = ('id',)
49
48
50 def _update_image_filename(self, filename):
51 """
52 Gets unique image filename
53 """
54
55 # TODO Use something other than random number in file name
56 new_name = '{}{}.{}'.format(
57 str(int(time.mktime(time.gmtime()))),
58 str(int(random() * 1000)),
59 filename.split(FILE_EXTENSION_DELIMITER)[-1:][0])
60
61 return os.path.join(IMAGES_DIRECTORY, new_name)
62
63 width = models.IntegerField(default=0)
49 width = models.IntegerField(default=0)
64 height = models.IntegerField(default=0)
50 height = models.IntegerField(default=0)
65
51
66 pre_width = models.IntegerField(default=0)
52 pre_width = models.IntegerField(default=0)
67 pre_height = models.IntegerField(default=0)
53 pre_height = models.IntegerField(default=0)
68
54
69 image = thumbs.ImageWithThumbsField(upload_to=_update_image_filename,
55 image = thumbs.ImageWithThumbsField(upload_to=get_upload_filename,
70 blank=True, sizes=(IMAGE_THUMB_SIZE,),
56 blank=True, sizes=(IMAGE_THUMB_SIZE,),
71 width_field='width',
57 width_field='width',
72 height_field='height',
58 height_field='height',
@@ -71,7 +71,8 b' class PostManager(models.Manager):'
71
71
72 logger = logging.getLogger('boards.post.create')
72 logger = logging.getLogger('boards.post.create')
73
73
74 logger.info('Created post {} by {}'.format(post, post.poster_ip))
74 logger.info('Created post {} with text {} by {}'.format(post,
75 post.get_text(),post.poster_ip))
75
76
76 # TODO Move this to other place
77 # TODO Move this to other place
77 if file:
78 if file:
@@ -2,22 +2,21 b''
2 This module contains helper functions and helper classes.
2 This module contains helper functions and helper classes.
3 """
3 """
4 import hashlib
4 import hashlib
5 from random import random
5 import time
6 import time
6 import hmac
7 import hmac
7
8
8 from django.core.cache import cache
9 from django.core.cache import cache
9 from django.db.models import Model
10 from django.db.models import Model
10 from django import forms
11 from django import forms
11
12 from django.utils import timezone
12 from django.utils import timezone
13 from django.utils.translation import ugettext_lazy as _
13 from django.utils.translation import ugettext_lazy as _
14 from portage import os
14
15
15 import boards
16 import boards
16 from boards.settings import get_bool
17 from boards.settings import get_bool
17
18 from neboard import settings
18 from neboard import settings
19
19
20
21 CACHE_KEY_DELIMITER = '_'
20 CACHE_KEY_DELIMITER = '_'
22 PERMISSION_MODERATE = 'moderation'
21 PERMISSION_MODERATE = 'moderation'
23
22
@@ -29,6 +28,12 b" SETTING_ANON_MODE = 'AnonymousMode'"
29
28
30 ANON_IP = '127.0.0.1'
29 ANON_IP = '127.0.0.1'
31
30
31 UPLOAD_DIRS ={
32 'PostImage': 'images/',
33 'Attachment': 'files/',
34 }
35 FILE_EXTENSION_DELIMITER = '.'
36
32
37
33 def is_anonymous_mode():
38 def is_anonymous_mode():
34 return get_bool(SETTING_MESSAGES, SETTING_ANON_MODE)
39 return get_bool(SETTING_MESSAGES, SETTING_ANON_MODE)
@@ -119,3 +124,15 b' def validate_file_size(size: int):'
119 raise forms.ValidationError(
124 raise forms.ValidationError(
120 _('File must be less than %s bytes')
125 _('File must be less than %s bytes')
121 % str(max_size))
126 % str(max_size))
127
128
129 def get_upload_filename(model_instance, old_filename):
130 # TODO Use something other than random number in file name
131 new_name = '{}{}.{}'.format(
132 str(int(time.mktime(time.gmtime()))),
133 str(int(random() * 1000)),
134 old_filename.split(FILE_EXTENSION_DELIMITER)[-1:][0])
135
136 directory = UPLOAD_DIRS[type(model_instance).__name__]
137
138 return os.path.join(directory, new_name)
General Comments 0
You need to be logged in to leave comments. Login now