diff --git a/pinry/api/api.py b/pinry/api/api.py index fa7cc80..56984c9 100644 --- a/pinry/api/api.py +++ b/pinry/api/api.py @@ -20,8 +20,8 @@ class PinResource(ModelResource): # pylint: disable-msg=R0904 } def dehydrate_thumbnail(self, bundle): - pin = Pin.objects.only('image').get(pk=bundle.data['id']) - return pin.image.url_200x1000 + pin = Pin.objects.only('thumbnail').get(pk=bundle.data['id']) + return pin.thumbnail.url class UserResource(ModelResource): diff --git a/pinry/core/migrations/__init__.py b/pinry/core/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pinry/pins/forms.py b/pinry/pins/forms.py index 5a2f95d..0f2ed28 100644 --- a/pinry/pins/forms.py +++ b/pinry/pins/forms.py @@ -61,4 +61,4 @@ class PinForm(forms.ModelForm): class Meta: model = Pin - exclude = ['submitter'] + exclude = ['submitter', 'thumbnail'] diff --git a/pinry/pins/migrations/0001_initial.py b/pinry/pins/migrations/0001_initial.py index 1ce150e..8ef8a2e 100644 --- a/pinry/pins/migrations/0001_initial.py +++ b/pinry/pins/migrations/0001_initial.py @@ -11,52 +11,65 @@ class Migration(SchemaMigration): # Adding model 'Pin' db.create_table('pins_pin', ( ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), - ('url', self.gf('django.db.models.fields.TextField')()), - ('title', self.gf('django.db.models.fields.CharField')(max_length=70)), - ('image', self.gf('thumbs.ImageWithThumbsField')(max_length=100, sizes=((200, 1000),))), + ('submitter', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'])), + ('url', self.gf('django.db.models.fields.TextField')(null=True, blank=True)), + ('description', self.gf('django.db.models.fields.TextField')(null=True, blank=True)), + ('image', self.gf('django.db.models.fields.files.ImageField')(max_length=100)), + ('thumbnail', self.gf('django.db.models.fields.files.ImageField')(max_length=100)), + ('published', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, blank=True)), )) db.send_create_signal('pins', ['Pin']) - # Adding M2M table for field tags on 'Pin' - db.create_table('pins_pin_tags', ( - ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)), - ('pin', models.ForeignKey(orm['pins.pin'], null=False)), - ('tag', models.ForeignKey(orm['pins.tag'], null=False)) - )) - db.create_unique('pins_pin_tags', ['pin_id', 'tag_id']) - - # Adding model 'Tag' - db.create_table('pins_tag', ( - ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), - ('name', self.gf('django.db.models.fields.CharField')(max_length=70)), - ('slug', self.gf('django.db.models.fields.SlugField')(max_length=50)), - )) - db.send_create_signal('pins', ['Tag']) - def backwards(self, orm): # Deleting model 'Pin' db.delete_table('pins_pin') - # Removing M2M table for field tags on 'Pin' - db.delete_table('pins_pin_tags') - - # Deleting model 'Tag' - db.delete_table('pins_tag') - models = { - 'pins.pin': { - 'Meta': {'object_name': 'Pin'}, + 'auth.group': { + 'Meta': {'object_name': 'Group'}, 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), - 'image': ('thumbs.ImageWithThumbsField', [], {'max_length': '100', 'sizes': '((200, 1000),)'}), - 'tags': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['pins.Tag']", 'symmetrical': 'False'}), - 'title': ('django.db.models.fields.CharField', [], {'max_length': '70'}), - 'url': ('django.db.models.fields.TextField', [], {}) + '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'}) }, - 'pins.tag': { - 'Meta': {'object_name': 'Tag'}, + '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': '70'}), - 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '50'}) + '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'}) + }, + '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'}) + }, + 'pins.pin': { + 'Meta': {'ordering': "['-id']", 'object_name': 'Pin'}, + 'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'image': ('django.db.models.fields.files.ImageField', [], {'max_length': '100'}), + 'published': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'submitter': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"}), + 'thumbnail': ('django.db.models.fields.files.ImageField', [], {'max_length': '100'}), + 'url': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}) } } diff --git a/pinry/pins/migrations/0002_auto__del_tag__del_field_pin_title__add_field_pin_description.py b/pinry/pins/migrations/0002_auto__del_tag__del_field_pin_title__add_field_pin_description.py deleted file mode 100644 index c5fdc3b..0000000 --- a/pinry/pins/migrations/0002_auto__del_tag__del_field_pin_title__add_field_pin_description.py +++ /dev/null @@ -1,58 +0,0 @@ -# -*- 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): - # Deleting model 'Tag' - db.delete_table('pins_tag') - - # Deleting field 'Pin.title' - db.delete_column('pins_pin', 'title') - - # Adding field 'Pin.description' - db.add_column('pins_pin', 'description', - self.gf('django.db.models.fields.TextField')(null=True, blank=True), - keep_default=False) - - # Removing M2M table for field tags on 'Pin' - db.delete_table('pins_pin_tags') - - def backwards(self, orm): - # Adding model 'Tag' - db.create_table('pins_tag', ( - ('slug', self.gf('django.db.models.fields.SlugField')(max_length=50)), - ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), - ('name', self.gf('django.db.models.fields.CharField')(max_length=70)), - )) - db.send_create_signal('pins', ['Tag']) - - - # User chose to not deal with backwards NULL issues for 'Pin.title' - raise RuntimeError("Cannot reverse this migration. 'Pin.title' and its values cannot be restored.") - # Deleting field 'Pin.description' - db.delete_column('pins_pin', 'description') - - # Adding M2M table for field tags on 'Pin' - db.create_table('pins_pin_tags', ( - ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)), - ('pin', models.ForeignKey(orm['pins.pin'], null=False)), - ('tag', models.ForeignKey(orm['pins.tag'], null=False)) - )) - db.create_unique('pins_pin_tags', ['pin_id', 'tag_id']) - - models = { - 'pins.pin': { - 'Meta': {'object_name': 'Pin'}, - 'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), - 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), - 'image': ('thumbs.ImageWithThumbsField', [], {'max_length': '100', 'sizes': '((200, 1000),)'}), - 'url': ('django.db.models.fields.TextField', [], {}) - } - } - - complete_apps = ['pins'] \ No newline at end of file diff --git a/pinry/pins/migrations/0003_auto__chg_field_pin_url.py b/pinry/pins/migrations/0003_auto__chg_field_pin_url.py deleted file mode 100644 index 0379e54..0000000 --- a/pinry/pins/migrations/0003_auto__chg_field_pin_url.py +++ /dev/null @@ -1,28 +0,0 @@ -# -*- 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): - - # Changing field 'Pin.url' - db.alter_column('pins_pin', 'url', self.gf('django.db.models.fields.TextField')(null=True)) - def backwards(self, orm): - - # User chose to not deal with backwards NULL issues for 'Pin.url' - raise RuntimeError("Cannot reverse this migration. 'Pin.url' and its values cannot be restored.") - models = { - 'pins.pin': { - 'Meta': {'ordering': "['-id']", 'object_name': 'Pin'}, - 'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), - 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), - 'image': ('thumbs.ImageWithThumbsField', [], {'max_length': '100', 'sizes': '((200, 1000),)'}), - 'url': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}) - } - } - - complete_apps = ['pins'] \ No newline at end of file diff --git a/pinry/pins/migrations/0004_auto__add_field_pin_published.py b/pinry/pins/migrations/0004_auto__add_field_pin_published.py deleted file mode 100644 index 04b37b6..0000000 --- a/pinry/pins/migrations/0004_auto__add_field_pin_published.py +++ /dev/null @@ -1,31 +0,0 @@ -# -*- 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 'Pin.published' - db.add_column('pins_pin', 'published', - self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True, default=datetime.datetime(2012, 7, 12, 0, 0), blank=True), - keep_default=False) - - def backwards(self, orm): - # Deleting field 'Pin.published' - db.delete_column('pins_pin', 'published') - - models = { - 'pins.pin': { - 'Meta': {'ordering': "['-id']", 'object_name': 'Pin'}, - 'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), - 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), - 'image': ('thumbs.ImageWithThumbsField', [], {'max_length': '100', 'sizes': '((200, 1000),)'}), - 'published': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), - 'url': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}) - } - } - - complete_apps = ['pins'] \ No newline at end of file diff --git a/pinry/pins/migrations/0005_auto__add_field_pin_submitter.py b/pinry/pins/migrations/0005_auto__add_field_pin_submitter.py deleted file mode 100644 index e949893..0000000 --- a/pinry/pins/migrations/0005_auto__add_field_pin_submitter.py +++ /dev/null @@ -1,68 +0,0 @@ -# -*- 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 'Pin.submitter' - db.add_column('pins_pin', 'submitter', - self.gf('django.db.models.fields.related.ForeignKey')(default=1, to=orm['auth.User']), - keep_default=False) - - def backwards(self, orm): - # Deleting field 'Pin.submitter' - db.delete_column('pins_pin', 'submitter_id') - - 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'}) - }, - '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'}) - }, - 'pins.pin': { - 'Meta': {'ordering': "['-id']", 'object_name': 'Pin'}, - 'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), - 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), - 'image': ('thumbs.ImageWithThumbsField', [], {'max_length': '100', 'sizes': '((200, 1000),)'}), - 'published': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), - 'submitter': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"}), - 'url': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}) - } - } - - complete_apps = ['pins'] diff --git a/pinry/pins/models.py b/pinry/pins/models.py index 0d1b37a..9f38833 100644 --- a/pinry/pins/models.py +++ b/pinry/pins/models.py @@ -3,29 +3,54 @@ from django.core.files import File from django.core.files.temp import NamedTemporaryFile from django.contrib.auth.models import User -from thumbs import ImageWithThumbsField - import urllib2 +import hashlib +from PIL import Image class Pin(models.Model): submitter = models.ForeignKey(User) url = models.TextField(blank=True, null=True) description = models.TextField(blank=True, null=True) - image = ImageWithThumbsField(upload_to='pins/pin', sizes=((200, 1000),)) + image = models.ImageField(upload_to='pins/pin/originals/') + thumbnail = models.ImageField(upload_to='pins/pin/thumbnails/') published = models.DateTimeField(auto_now_add=True) + def __unicode__(self): return self.url + def save(self, *args, **kwargs): if not self.image: temp_img = NamedTemporaryFile() temp_img.write(urllib2.urlopen(self.url).read()) temp_img.flush() - # pylint: disable-msg=E1101 self.image.save(self.url.split('/')[-1], File(temp_img)) + else: + super(Pin, self).save() + + if not self.thumbnail: + if not self.image: + image = Image.open(temp_img.name) + else: + image = Image.open(self.image.path) + size = image.size + prop = 200 / image.size[0] + size = (prop*image.size[0], prop*image.size[1]) + image.resize(size, Image.ANTIALIAS) + temp_thumb = NamedTemporaryFile() + image.save(temp_thumb.name, 'JPEG') + + if self.url: + name = self.url.split('/')[-1] + else: + name = self.image.name + + self.thumbnail.save(name, File(temp_thumb)) + super(Pin, self).save(*args, **kwargs) + class Meta: ordering = ['-id'] diff --git a/pinry/pins/templates/pins/new_pin.html b/pinry/pins/templates/pins/new_pin.html index 53d15ed..908e6cc 100644 --- a/pinry/pins/templates/pins/new_pin.html +++ b/pinry/pins/templates/pins/new_pin.html @@ -10,7 +10,9 @@