1. Steve Losh
  2. lindyjam

Source

lindyjam / quotes / views.py

from lindyjam.quotes.models import Quote
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template import RequestContext
import simplejson as json

SUBMIT_MESSAGE = """<p class="success">Thanks for the quote!  We'll check it out and if we approve it you might see it up at the top.</p>"""

def index(request):
    return render_to_response('quotes/index.html', {}, 
                              context_instance=RequestContext(request))

def submit(request):
    errors = []
    status = 0
    fields = request.POST
    
    if fields['body'] == '':
        errors.append("They must have said <em>something</em>!")
        
    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_quote = Quote(body=fields['body'], approved=False)
    new_quote.save()
    
    return HttpResponse(json.dumps( { 'status': status,
                                      'data': SUBMIT_MESSAGE }))