mirror of
https://github.com/jazzband/django-categories.git
synced 2026-03-16 22:30:24 +00:00
Refactored the thumbnail from imagefield to filefield.
Why? ImageField causes hits to storage to fill out certain fields. Added a storage class and width/height fields so it is possible to scale the thumbnails and store them somewhere besides the filesystem.
This commit is contained in:
parent
8e843fbcd1
commit
efe3cdd06b
3 changed files with 82 additions and 2 deletions
|
|
@ -0,0 +1,55 @@
|
|||
# encoding: utf-8
|
||||
import datetime
|
||||
from south.db import db
|
||||
from south.v2 import SchemaMigration
|
||||
from django.db import models
|
||||
|
||||
class Migration(SchemaMigration):
|
||||
|
||||
def forwards(self, orm):
|
||||
|
||||
# Adding field 'Category.thumbnail_width'
|
||||
db.add_column('categories_category', 'thumbnail_width', self.gf('django.db.models.fields.IntegerField')(null=True, blank=True), keep_default=False)
|
||||
|
||||
# Adding field 'Category.thumbnail_height'
|
||||
db.add_column('categories_category', 'thumbnail_height', self.gf('django.db.models.fields.IntegerField')(null=True, blank=True), keep_default=False)
|
||||
|
||||
# Changing field 'Category.thumbnail'
|
||||
db.alter_column('categories_category', 'thumbnail', self.gf('django.db.models.fields.files.FileField')(max_length=100, null=True))
|
||||
|
||||
|
||||
def backwards(self, orm):
|
||||
|
||||
# Deleting field 'Category.thumbnail_width'
|
||||
db.delete_column('categories_category', 'thumbnail_width')
|
||||
|
||||
# Deleting field 'Category.thumbnail_height'
|
||||
db.delete_column('categories_category', 'thumbnail_height')
|
||||
|
||||
# Changing field 'Category.thumbnail'
|
||||
db.alter_column('categories_category', 'thumbnail', self.gf('django.db.models.fields.files.ImageField')(max_length=100, null=True))
|
||||
|
||||
|
||||
models = {
|
||||
'categories.category': {
|
||||
'Meta': {'ordering': "('tree_id', 'lft')", 'unique_together': "(('parent', 'name'),)", 'object_name': 'Category'},
|
||||
'alternate_title': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '100', 'blank': 'True'}),
|
||||
'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
|
||||
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
|
||||
'level': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
|
||||
'lft': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
|
||||
'meta_extra': ('django.db.models.fields.TextField', [], {'default': "''", 'blank': 'True'}),
|
||||
'meta_keywords': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '255', 'blank': 'True'}),
|
||||
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
|
||||
'order': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
|
||||
'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': "orm['categories.Category']"}),
|
||||
'rght': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
|
||||
'slug': ('django.db.models.fields.SlugField', [], {'max_length': '50', 'db_index': 'True'}),
|
||||
'thumbnail': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'null': 'True', 'blank': 'True'}),
|
||||
'thumbnail_height': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
|
||||
'thumbnail_width': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
|
||||
'tree_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'})
|
||||
}
|
||||
}
|
||||
|
||||
complete_apps = ['categories']
|
||||
|
|
@ -5,11 +5,16 @@ from django.db import models
|
|||
from django.utils.encoding import force_unicode
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.contrib.contenttypes import generic
|
||||
from django.core.files.storage import get_storage_class
|
||||
from django.template.defaultfilters import slugify
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
from mptt.models import MPTTModel
|
||||
|
||||
from settings import RELATION_MODELS, RELATIONS, THUMBNAIL_UPLOAD_PATH
|
||||
from settings import (RELATION_MODELS, RELATIONS, THUMBNAIL_UPLOAD_PATH,
|
||||
THUMBNAIL_STORAGE)
|
||||
|
||||
STORAGE = get_storage_class(THUMBNAIL_STORAGE)
|
||||
|
||||
class Category(MPTTModel):
|
||||
parent = models.ForeignKey('self',
|
||||
|
|
@ -19,7 +24,12 @@ class Category(MPTTModel):
|
|||
help_text="Leave this blank for an Category Tree",
|
||||
verbose_name='Parent')
|
||||
name = models.CharField(max_length=100)
|
||||
thumbnail = models.ImageField(upload_to=THUMBNAIL_UPLOAD_PATH, null=True, blank=True)
|
||||
thumbnail = models.FileField(
|
||||
upload_to=THUMBNAIL_UPLOAD_PATH,
|
||||
null=True, blank=True,
|
||||
storage=STORAGE(),)
|
||||
thumbnail_width = models.IntegerField(blank=True, null=True)
|
||||
thumbnail_height = models.IntegerField(blank=True, null=True)
|
||||
order = models.IntegerField(blank=True, null=True)
|
||||
slug = models.SlugField()
|
||||
alternate_title = models.CharField(
|
||||
|
|
@ -63,6 +73,20 @@ class Category(MPTTModel):
|
|||
"""
|
||||
return self.storyrelation_set.filter(relation_type=relation_type)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.slug:
|
||||
self.slug = slugify(self.name)[:50]
|
||||
if self.thumbnail:
|
||||
from django.core.files.images import get_image_dimensions
|
||||
width, height = get_image_dimensions(self.thumbnail.file, close=True)
|
||||
else:
|
||||
width, height = None, None
|
||||
|
||||
self.thumbnail_width = width
|
||||
self.thumbnail_height = height
|
||||
|
||||
super(Category, self).save(*args, **kwargs)
|
||||
|
||||
class Meta:
|
||||
verbose_name_plural = 'categories'
|
||||
unique_together = ('parent', 'name')
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ DEFAULT_SETTINGS = {
|
|||
'M2M_REGISTRY': [],
|
||||
'FK_REGISTRY': [],
|
||||
'THUMBNAIL_UPLOAD_PATH': 'uploads/categories/thumbnails',
|
||||
'THUMBNAIL_STORAGE': settings.DEFAULT_FILE_STORAGE,
|
||||
}
|
||||
|
||||
DEFAULT_SETTINGS.update(getattr(settings, 'CATEGORIES_SETTINGS', {}))
|
||||
|
|
|
|||
Loading…
Reference in a new issue