diff --git a/pinry/pins/forms.py b/pinry/pins/forms.py index 12f2507..3589832 100644 --- a/pinry/pins/forms.py +++ b/pinry/pins/forms.py @@ -1,15 +1,15 @@ from django import forms -from taggit.forms import TagField - from .models import Pin -class PinForm(forms.Form): - url = forms.CharField(label='URL', required=False) +class PinForm(forms.ModelForm): + url = forms.CharField(label='url', required=False) image = forms.ImageField(label='or Upload', required=False) - description = forms.CharField(label='Description', required=False, widget=forms.Textarea) - tags = TagField() + + class Meta: + model = Pin + fields = ['url', 'description', 'tags'] def check_if_image(self, data): # Test file type diff --git a/pinry/pins/urls.py b/pinry/pins/urls.py index bae91be..52d71c5 100644 --- a/pinry/pins/urls.py +++ b/pinry/pins/urls.py @@ -1,9 +1,12 @@ from django.conf.urls import patterns, url +from .views import RecentPins +from .views import NewPin + urlpatterns = patterns('pinry.pins.views', - url(r'^$', 'recent_pins', name='recent-pins'), - url(r'^tag/.+/$', 'recent_pins', name='tag'), - url(r'^new-pin/$', 'new_pin', name='new-pin'), + url(r'^$', RecentPins.as_view(), name='recent-pins'), + url(r'^tag/.+/$', RecentPins.as_view(), name='tag'), + url(r'^new-pin/$', NewPin.as_view(), name='new-pin'), url(r'^delete-pin/(?P\d+)/$', 'delete_pin', name='delete-pin'), ) diff --git a/pinry/pins/views.py b/pinry/pins/views.py index 155b33a..16c899f 100644 --- a/pinry/pins/views.py +++ b/pinry/pins/views.py @@ -1,33 +1,34 @@ -from django.template.response import TemplateResponse from django.http import HttpResponseRedirect from django.core.urlresolvers import reverse from django.contrib import messages +from django.utils.functional import lazy +from django.views.generic.base import TemplateView +from django.views.generic import CreateView from .forms import PinForm from .models import Pin -def recent_pins(request): - return TemplateResponse(request, 'pins/recent_pins.html', None) +reverse_lazy = lambda name=None, *args : lazy(reverse, str)(name, args=args) -def new_pin(request): - if request.method == 'POST': - form = PinForm(request.POST, request.FILES) - if form.is_valid(): - pin = Pin.objects.create(url=form.cleaned_data['url'], submitter=request.user, - description=form.cleaned_data['description']) - pin.tags.add(*form.cleaned_data['tags']) - messages.success(request, 'New pin successfully added.') - return HttpResponseRedirect(reverse('pins:recent-pins')) - else: - messages.error(request, 'Pin did not pass validation!') - else: - form = PinForm() - context = { - 'form': form, - } - return TemplateResponse(request, 'pins/new_pin.html', context) +class RecentPins(TemplateView): + template_name = 'pins/recent_pins.html' + + +class NewPin(CreateView): + model = Pin + form_class = PinForm + success_url = reverse_lazy('pins:recent-pins') + + def form_valid(self, form): + form.instance.submitter = self.request.user + messages.success(self.request, 'New pin successfully added.') + return super(NewPin, self).form_valid(form) + + def form_invalid(self, form): + messages.error(self.request, 'Pin did not pass validation!') + return super(NewPin, self).form_invalid(form) def delete_pin(request, pin_id): diff --git a/pinry/templates/pins/new_pin.html b/pinry/templates/pins/pin_form.html similarity index 100% rename from pinry/templates/pins/new_pin.html rename to pinry/templates/pins/pin_form.html