lindyjam / links /

from lindyjam.links.models import Link
from django.http import HttpResponse
from django.shortcuts import render_to_response
import simplejson as json
from django.template import RequestContext

SUBMIT_MESSAGE = """<p class="success">Thanks for the link!  We'll check it out and if we approve it will show up here.</p>"""

def index(request):
    link_list = Link.objects.all().order_by('title')
    link_list = link_list.filter(approved=True)
    return render_to_response('links/index.html', 
                              { 'link_list': link_list },
                              context_instance=RequestContext(request)  )

def submit(request):
    errors = []
    status = 0
    fields = request.POST
    if fields['title'] == '':
        errors.append("You need to give the link a title so people know what it is!")
    if fields['description'] == '':
        errors.append("You need to fill out a description!")
    if fields['url'] == '':
        errors.append("You need to put in a link!  That's the whole point!")
    if len(errors) > 0:
        status = -1
        error_message = '\n'.join(['<p class="error">%s</p>' % (e,) 
                                   for e in errors])
        return HttpResponse(json.dumps( { 'status': status,
                                          'data': error_message }))
    new_link = Link(title=fields['title'], url=fields['url'],
                    description=fields['description'], approved=False)
    return HttpResponse(json.dumps( { 'status': status,
                                      'data': SUBMIT_MESSAGE }))