Преглед на файлове

Rewrite new_pin into a Class Based View

tags/v1.0.0
Krzysztof Klimonda преди 11 години
родител
ревизия
98bdbfd900
променени са 4 файла, в които са добавени 33 реда и са изтрити 29 реда
  1. +6
    -6
      pinry/pins/forms.py
  2. +6
    -3
      pinry/pins/urls.py
  3. +21
    -20
      pinry/pins/views.py
  4. +0
    -0
      pinry/templates/pins/pin_form.html

+ 6
- 6
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


+ 6
- 3
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<pin_id>\d+)/$', 'delete_pin', name='delete-pin'),
)

+ 21
- 20
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):


pinry/templates/pins/new_pin.html → pinry/templates/pins/pin_form.html Целия файл


Зареждане…
Отказ
Запис