diff --git a/pinry/pins/forms.py b/pinry/pins/forms.py index fefad13..8864222 100644 --- a/pinry/pins/forms.py +++ b/pinry/pins/forms.py @@ -4,11 +4,10 @@ from .models import Pin class PinForm(forms.ModelForm): - url = forms.CharField(label='URL') - - def clean_url(self): - data = self.cleaned_data['url'] + url = forms.CharField(label='URL', required=False) + image = forms.ImageField(label='Upload', required=False) + def check_if_image(self, data): # Test file type image_file_types = ['png', 'gif', 'jpeg', 'jpg'] file_type = data.split('.')[-1] @@ -16,27 +15,39 @@ class PinForm(forms.ModelForm): raise forms.ValidationError("Requested URL is not an image file. " "Only images are currently supported.") - # Check if pin already exists - try: - Pin.objects.get(url=data) - raise forms.ValidationError("URL has already been pinned!") - except Pin.DoesNotExist: - protocol = data.split(':')[0] - if protocol == 'http': - opp_data = data.replace('http://', 'https://') - elif protocol == 'https': - opp_data = data.replace('https://', 'http://') - else: - raise forms.ValidationError("Currently only support HTTP and " - "HTTPS protocols, please be sure " - "you include this in the URL.") + def clean(self): + cleaned_data = super(PinForm, self).clean() + url = cleaned_data.get('url') + image = cleaned_data.get('image') + + if url: + self.check_if_image(url) try: - Pin.objects.get(url=opp_data) + Pin.objects.get(url=url) raise forms.ValidationError("URL has already been pinned!") except Pin.DoesNotExist: - return data + protocol = url.split(':')[0] + if protocol == 'http': + opp_url = url.replace('http://', 'https://') + elif protocol == 'https': + opp_url = url.replace('https://', 'http://') + else: + raise forms.ValidationError("Currently only support HTTP and " + "HTTPS protocols, please be sure " + "you include this in the URL.") + + try: + Pin.objects.get(url=opp_url) + raise forms.ValidationError("URL has already been pinned!") + except Pin.DoesNotExist: + pass + elif image: + pass + else: + raise forms.ValidationError("Need either a URL or Upload.") + + return cleaned_data class Meta: model = Pin - exclude = ['image'] diff --git a/pinry/pins/migrations/0003_auto__chg_field_pin_url.py b/pinry/pins/migrations/0003_auto__chg_field_pin_url.py new file mode 100644 index 0000000..0379e54 --- /dev/null +++ b/pinry/pins/migrations/0003_auto__chg_field_pin_url.py @@ -0,0 +1,28 @@ +# -*- 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/models.py b/pinry/pins/models.py index 730870c..2a63835 100644 --- a/pinry/pins/models.py +++ b/pinry/pins/models.py @@ -8,7 +8,7 @@ import urllib2 class Pin(models.Model): - url = models.TextField() + url = models.TextField(blank=True, null=True) description = models.TextField(blank=True, null=True) image = ImageWithThumbsField(upload_to='pins/pin', sizes=((200, 1000),)) diff --git a/pinry/pins/templates/pins/new_pin.html b/pinry/pins/templates/pins/new_pin.html index f07f2bf..53d15ed 100644 --- a/pinry/pins/templates/pins/new_pin.html +++ b/pinry/pins/templates/pins/new_pin.html @@ -10,8 +10,7 @@