Files
Pinry/pinry/pins/views.py

50 lines
1.5 KiB
Python
Raw Normal View History

2012-04-26 03:44:16 +00:00
from django.template.response import TemplateResponse
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
2012-05-01 05:44:50 +00:00
from django.contrib import messages
2012-04-26 03:44:16 +00:00
from .forms import PinForm
2012-07-24 23:26:38 +00:00
from .models import Pin
2012-04-26 03:44:16 +00:00
2012-04-26 03:44:16 +00:00
def recent_pins(request):
return TemplateResponse(request, 'pins/recent_pins.html', None)
2012-04-26 03:44:16 +00:00
def new_pin(request):
if request.method == 'POST':
form = PinForm(request.POST, request.FILES)
2012-04-26 03:44:16 +00:00
if form.is_valid():
2012-07-24 23:26:38 +00:00
pin = form.save(commit=False)
pin.submitter = request.user
pin.save()
2012-09-28 04:42:13 +00:00
form.save_m2m()
2012-05-01 05:44:50 +00:00
messages.success(request, 'New pin successfully added.')
2012-04-26 03:44:16 +00:00
return HttpResponseRedirect(reverse('pins:recent-pins'))
2012-05-01 05:44:50 +00:00
else:
messages.error(request, 'Pin did not pass validation!')
2012-04-26 03:44:16 +00:00
else:
form = PinForm()
context = {
'form': form,
}
return TemplateResponse(request, 'pins/new_pin.html', context)
2012-07-24 23:26:38 +00:00
def delete_pin(request, pin_id):
try:
pin = Pin.objects.get(id=pin_id)
if pin.submitter == request.user:
pin.delete()
messages.success(request, 'Pin successfully deleted.')
else:
messages.error(request, 'You are not the submitter and can not '
'delete this pin.')
except Pin.DoesNotExist:
messages.error(request, 'Pin with the given id does not exist.')
return HttpResponseRedirect(reverse('pins:recent-pins'))