mirror of
https://github.com/pinry/pinry.git
synced 2026-03-05 03:31:09 +01:00
Added an upload field for new images instead of just requiring a URL.
This commit is contained in:
@@ -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']
|
||||
|
||||
28
pinry/pins/migrations/0003_auto__chg_field_pin_url.py
Normal file
28
pinry/pins/migrations/0003_auto__chg_field_pin_url.py
Normal file
@@ -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']
|
||||
@@ -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),))
|
||||
|
||||
|
||||
@@ -10,8 +10,7 @@
|
||||
<div class="row">
|
||||
<div class="span6 offset3" id="form">
|
||||
<h1>New Pin</h1>
|
||||
<form action="{% url pins:new-pin %}" method="post" class="form-horizontal">
|
||||
{% csrf_token %}
|
||||
<form enctype="multipart/form-data" action="{% url pins:new-pin %}" method="post" class="form-horizontal">
|
||||
{% for field in form %}
|
||||
{% bootstrap_field field %}
|
||||
{% endfor %}
|
||||
|
||||
@@ -12,7 +12,7 @@ def recent_pins(request):
|
||||
|
||||
def new_pin(request):
|
||||
if request.method == 'POST':
|
||||
form = PinForm(request.POST)
|
||||
form = PinForm(request.POST, request.FILES)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
messages.success(request, 'New pin successfully added.')
|
||||
|
||||
Reference in New Issue
Block a user