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
|
|
|
|
|
|
|
|
|
|
|
|
|
def recent_pins(request):
|
2012-04-27 19:59:55 +00:00
|
|
|
return TemplateResponse(request, 'pins/recent_pins.html', None)
|
2012-04-26 03:44:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def new_pin(request):
|
|
|
|
|
if request.method == 'POST':
|
2012-07-03 02:24:34 +00:00
|
|
|
form = PinForm(request.POST, request.FILES)
|
2012-04-26 03:44:16 +00:00
|
|
|
if form.is_valid():
|
2013-02-23 20:33:10 +01:00
|
|
|
pin = Pin.objects.create(url=form.cleaned_data['url'], submitter=request.user,
|
|
|
|
|
description=form.cleaned_data['description'])
|
|
|
|
|
pin.tags.add(*form.cleaned_data['tags'])
|
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'))
|