Commits

Ian Lewis committed 9f21f87

Added lead thumbnails.

  • Participants
  • Parent commits 1a9ccd1

Comments (0)

Files changed (7)

File homepage/blog/models.py

 #:coding=utf8:
 
+import uuid
+import os
+
 from django.contrib.sites.models import Site
 from django.contrib.auth.models import User
 from django.db import models
 from django.utils.translation import ugettext_lazy as _
 
 from tagging.fields import TagField
+from sorl.thumbnail import ImageField
 
 from datetime import datetime
 
     slug = models.SlugField(u"slug", max_length=50, unique=True, db_index=True)
     title = models.TextField(u"title")
     lead = models.TextField(u"lead", blank=True, null=True, default=None, max_length=600)
+    lead_image = ImageField(verbose_name="lead image", null=True, blank=True,
+        upload_to=lambda instance, filename: 'thumbs/%s%s' % (
+            uuid.uuid4().hex,
+            os.path.splitext(filename)[1].lower(),
+        ),
+    )
     content = models.TextField(u"content")
     markup_type = models.CharField(max_length=10, choices=(
         ("html", "HTML"),

File homepage/settings.py

 
     # Third party
     'south',
+    'sorl.thumbnail',
     'filebrowser',
     'pagination',
     'tagging',
             'stream': sys.stderr,
         },
         'stdout': {
-            'level': 'INFO',
+            'level': 'DEBUG',
             'formatter': 'verbose',
             'class': 'logging.StreamHandler', 
             'stream': sys.stdout,
     }
 }
 
+LOGGING['loggers'][''] = {
+    'handlers': ['stdout'],
+    'level': 'DEBUG',
+}
+
 #HGPROXY_REPO_LIST_REQUIRES_LOGIN = True
 #HGPROXY_STATIC_URL = '/hgstatic/' 
 
 )
 
 SOUTH_MIGRATION_MODULES = {
-    "core": "migrations.core",
     "blog": "migrations.blog",
     "tagging": "migrations.tagging",
+    "thumbnail": "migrations.thumbnail",
 }
 
 #try:

File migrations/blog/0005_auto__add_field_post_lead_image.py

+# -*- coding: 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 'Post.lead_image'
+        db.add_column('blog_post', 'lead_image',
+                      self.gf('sorl.thumbnail.fields.ImageField')(max_length=100, null=True, blank=True),
+                      keep_default=False)
+
+
+    def backwards(self, orm):
+        # Deleting field 'Post.lead_image'
+        db.delete_column('blog_post', 'lead_image')
+
+
+    models = {
+        'auth.group': {
+            'Meta': {'object_name': 'Group'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
+            'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
+        },
+        'auth.permission': {
+            'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
+            'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+        },
+        'auth.user': {
+            'Meta': {'object_name': 'User'},
+            'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
+            'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+            'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
+            'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
+        },
+        'blog.post': {
+            'Meta': {'ordering': "('-pub_date',)", 'object_name': 'Post'},
+            'active': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'db_index': 'True'}),
+            'author': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"}),
+            'content': ('django.db.models.fields.TextField', [], {}),
+            'create_date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'lead': ('django.db.models.fields.TextField', [], {'default': 'None', 'max_length': '600', 'null': 'True', 'blank': 'True'}),
+            'lead_image': ('sorl.thumbnail.fields.ImageField', [], {'max_length': '100', 'null': 'True', 'blank': 'True'}),
+            'locale': ('django.db.models.fields.CharField', [], {'default': "'en'", 'max_length': '20', 'db_index': 'True'}),
+            'markup_type': ('django.db.models.fields.CharField', [], {'default': "'html'", 'max_length': '10'}),
+            'pub_date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now', 'db_index': 'True'}),
+            'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '50'}),
+            'tags': ('tagging.fields.TagField', [], {'default': "''"}),
+            'title': ('django.db.models.fields.TextField', [], {})
+        },
+        'contenttypes.contenttype': {
+            'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+            'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+        }
+    }
+
+    complete_apps = ['blog']

File migrations/thumbnail/0001_initial.py

+# -*- coding: 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 model 'KVStore'
+        db.create_table('thumbnail_kvstore', (
+            ('key', self.gf('django.db.models.fields.CharField')(max_length=200, primary_key=True, db_column='key')),
+            ('value', self.gf('django.db.models.fields.TextField')()),
+        ))
+        db.send_create_signal('thumbnail', ['KVStore'])
+
+
+    def backwards(self, orm):
+        # Deleting model 'KVStore'
+        db.delete_table('thumbnail_kvstore')
+
+
+    models = {
+        'thumbnail.kvstore': {
+            'Meta': {'object_name': 'KVStore'},
+            'key': ('django.db.models.fields.CharField', [], {'max_length': '200', 'primary_key': 'True', 'db_column': "'key'"}),
+            'value': ('django.db.models.fields.TextField', [], {})
+        }
+    }
+
+    complete_apps = ['thumbnail']

File migrations/thumbnail/__init__.py

Empty file added.

File requirements.txt

 pygments>=1.0
 html2text==3.200.3
 
+# For lead thumbnails
+sorl-thumbnail==11.12
+
 # For thumbnails (Filebrowser)
 PIL==1.1.7
 

File templates/blog/post_list.html

 {% extends "blog/base.html" %}
-{% load utility_tags pagination_tags tagging_tags blog_tags disqus_tags %} 
+{% load utility_tags pagination_tags tagging_tags blog_tags disqus_tags thumbnail %} 
 
 {% block title %}Ian's Blog{% endblock %}
 
           <div class="post-date">{{ post.pub_date|date:"DATE_FORMAT" }}</div>
         </div>
         <div class="post-content">
-          <p>{{ post|to_lead:400|default:"&nbsp;" }}</p>
+          {% thumbnail post.lead_image "200" crop="center" as thumb %}
+          <p>
+            <img src="{{ thumb.url }}" class="align-left">
+            {{ post|to_lead:400|default:"&nbsp;" }}
+          </p>
+          {% empty %}
+            <p>{{ post|to_lead:400|default:"&nbsp;" }}</p>
+          {% endthumbnail %}
         </div>
         <div class="post-footer">
           <p class="pull-right"><a class="btn" href="{{ post.get_absolute_url }}">Read More &raquo;</a></p>