diff --git a/boards/migrations/0001_initial.py b/boards/migrations/0001_initial.py --- a/boards/migrations/0001_initial.py +++ b/boards/migrations/0001_initial.py @@ -52,7 +52,7 @@ class Migration(migrations.Migration): ('height', models.IntegerField(default=0)), ('pre_width', models.IntegerField(default=0)), ('pre_height', models.IntegerField(default=0)), - ('image', boards.thumbs.ImageWithThumbsField(height_field='height', width_field='width', upload_to=boards.models.image.PostImage._update_image_filename, blank=True)), + ('image', boards.thumbs.ImageWithThumbsField(height_field='height', width_field='width', blank=True)), ('hash', models.CharField(max_length=36)), ], options={ diff --git a/boards/migrations/0023_auto_20150818_1026.py b/boards/migrations/0023_auto_20150818_1026.py --- a/boards/migrations/0023_auto_20150818_1026.py +++ b/boards/migrations/0023_auto_20150818_1026.py @@ -16,7 +16,7 @@ class Migration(migrations.Migration): name='Attachment', fields=[ ('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)), - ('file', models.FileField(upload_to=boards.models.attachment.Attachment._update_filename)), + ('file', models.FileField()), ('mimetype', models.CharField(max_length=50)), ('hash', models.CharField(max_length=36)), ], diff --git a/boards/migrations/0034_auto_20151014_2253.py b/boards/migrations/0034_auto_20151014_2253.py new file mode 100644 --- /dev/null +++ b/boards/migrations/0034_auto_20151014_2253.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +import boards.thumbs +import boards.utils + + +class Migration(migrations.Migration): + + dependencies = [ + ('boards', '0033_auto_20151014_2224'), + ] + + operations = [ + migrations.AlterField( + model_name='attachment', + name='file', + field=models.FileField(upload_to=boards.utils.get_upload_filename), + ), + migrations.AlterField( + model_name='postimage', + name='image', + field=boards.thumbs.ImageWithThumbsField(blank=True, width_field='width', height_field='height', upload_to=boards.utils.get_upload_filename), + ), + ] diff --git a/boards/models/attachment/__init__.py b/boards/models/attachment/__init__.py --- a/boards/models/attachment/__init__.py +++ b/boards/models/attachment/__init__.py @@ -1,14 +1,8 @@ -import hashlib -import os -import time - -from random import random +from django.db import models -from django.db import models from boards import utils - from boards.models.attachment.viewers import get_viewers, AbstractViewer - +from boards.utils import get_upload_filename FILES_DIRECTORY = 'files/' FILE_EXTENSION_DELIMITER = '.' @@ -31,21 +25,7 @@ class AttachmentManager(models.Manager): class Attachment(models.Model): objects = AttachmentManager() - # TODO Dedup the method - def _update_filename(self, filename): - """ - Gets unique filename - """ - - # TODO Use something other than random number in file name - new_name = '{}{}.{}'.format( - str(int(time.mktime(time.gmtime()))), - str(int(random() * 1000)), - filename.split(FILE_EXTENSION_DELIMITER)[-1:][0]) - - return os.path.join(FILES_DIRECTORY, new_name) - - file = models.FileField(upload_to=_update_filename) + file = models.FileField(upload_to=get_upload_filename) mimetype = models.CharField(max_length=50) hash = models.CharField(max_length=36) diff --git a/boards/models/image.py b/boards/models/image.py --- a/boards/models/image.py +++ b/boards/models/image.py @@ -9,13 +9,12 @@ from django.template.defaultfilters impo from boards import thumbs, utils import boards from boards.models.base import Viewable +from boards.utils import get_upload_filename __author__ = 'neko259' IMAGE_THUMB_SIZE = (200, 150) -IMAGES_DIRECTORY = 'images/' -FILE_EXTENSION_DELIMITER = '.' HASH_LENGTH = 36 CSS_CLASS_IMAGE = 'image' @@ -47,26 +46,13 @@ class PostImage(models.Model, Viewable): app_label = 'boards' ordering = ('id',) - def _update_image_filename(self, filename): - """ - Gets unique image filename - """ - - # TODO Use something other than random number in file name - new_name = '{}{}.{}'.format( - str(int(time.mktime(time.gmtime()))), - str(int(random() * 1000)), - filename.split(FILE_EXTENSION_DELIMITER)[-1:][0]) - - return os.path.join(IMAGES_DIRECTORY, new_name) - width = models.IntegerField(default=0) height = models.IntegerField(default=0) pre_width = models.IntegerField(default=0) pre_height = models.IntegerField(default=0) - image = thumbs.ImageWithThumbsField(upload_to=_update_image_filename, + image = thumbs.ImageWithThumbsField(upload_to=get_upload_filename, blank=True, sizes=(IMAGE_THUMB_SIZE,), width_field='width', height_field='height', diff --git a/boards/models/post/manager.py b/boards/models/post/manager.py --- a/boards/models/post/manager.py +++ b/boards/models/post/manager.py @@ -71,7 +71,8 @@ class PostManager(models.Manager): logger = logging.getLogger('boards.post.create') - logger.info('Created post {} by {}'.format(post, post.poster_ip)) + logger.info('Created post {} with text {} by {}'.format(post, + post.get_text(),post.poster_ip)) # TODO Move this to other place if file: diff --git a/boards/utils.py b/boards/utils.py --- a/boards/utils.py +++ b/boards/utils.py @@ -2,22 +2,21 @@ This module contains helper functions and helper classes. """ import hashlib +from random import random import time import hmac from django.core.cache import cache from django.db.models import Model from django import forms - from django.utils import timezone from django.utils.translation import ugettext_lazy as _ +from portage import os import boards from boards.settings import get_bool - from neboard import settings - CACHE_KEY_DELIMITER = '_' PERMISSION_MODERATE = 'moderation' @@ -29,6 +28,12 @@ SETTING_ANON_MODE = 'AnonymousMode' ANON_IP = '127.0.0.1' +UPLOAD_DIRS ={ + 'PostImage': 'images/', + 'Attachment': 'files/', +} +FILE_EXTENSION_DELIMITER = '.' + def is_anonymous_mode(): return get_bool(SETTING_MESSAGES, SETTING_ANON_MODE) @@ -119,3 +124,15 @@ def validate_file_size(size: int): raise forms.ValidationError( _('File must be less than %s bytes') % str(max_size)) + + +def get_upload_filename(model_instance, old_filename): + # TODO Use something other than random number in file name + new_name = '{}{}.{}'.format( + str(int(time.mktime(time.gmtime()))), + str(int(random() * 1000)), + old_filename.split(FILE_EXTENSION_DELIMITER)[-1:][0]) + + directory = UPLOAD_DIRS[type(model_instance).__name__] + + return os.path.join(directory, new_name)