X-Git-Url: http://git.ithinksw.org/~kgodey/maayanwich.git/blobdiff_plain/6257e70f7b73d39922d5160c648b418c4ac180a2..f3cb6c9aac4ebeec54e8a82959cf8d56b30b15b5:/views.py diff --git a/views.py b/views.py index 8615ffc..51be20d 100644 --- a/views.py +++ b/views.py @@ -7,7 +7,9 @@ from models import Sandwich, Ingredient from django.contrib.auth import authenticate, login, logout from django.contrib.auth.forms import AuthenticationForm from django.template import RequestContext +from django.core import serializers import datetime +import django.utils.simplejson as json def sidebar_context(request): x = Sandwich.objects.order_by('-date_made') @@ -27,11 +29,17 @@ def add_sandwich(request): newsandwich = form.save(commit=False) newsandwich.user = request.user newsandwich.save() + x = request.POST['ing'] + x = x.strip() + y = x.split(',') + for n in y: + if n.isdigit(): + newsandwich.ingredients.add(Ingredient.objects.get(id=n)) + newsandwich.save() thankshtml = "

Thanks! Your sandwich has been added!

" return HttpResponse(thankshtml) # Redirect after POST else: form = SandwichForm(initial={'user': request.user}) # An unbound form - form.base_fields['ingredients'].help_text = '' return render_to_response('sandwich.html', {'sform': form,}, context_instance=RequestContext(request)) else: thankshtml = "

You are not logged in.

" @@ -156,4 +164,18 @@ def create_user(request): return HttpResponse(thankshtml) # Redirect after POST else: form = NewUserForm() # An unbound form - return render_to_response('newuser.html', {'cform': form,}, context_instance=RequestContext(request)) \ No newline at end of file + return render_to_response('newuser.html', {'cform': form,}, context_instance=RequestContext(request)) + + +def ajaxfun(request): + if request.method == 'GET': + if 'q' in request.GET: + query = request.GET['q'] + ingredients = Ingredient.objects.filter(name__icontains=query).order_by('name') + responselist = [] + for i in ingredients: + responselist.append({'id': str(i.pk), 'name': i.name}) + response = json.dumps(responselist) + return HttpResponse(response) + else: + return HttpResponse('{}') \ No newline at end of file