Ingredients auto complete, adding ingredients works. Some template tweaks.
[~kgodey/maayanwich.git] / views.py
index b84a926..51be20d 100644 (file)
--- 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.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 datetime
+import django.utils.simplejson as json
 
 def sidebar_context(request):
        x = Sandwich.objects.order_by('-date_made')
 
 def sidebar_context(request):
        x = Sandwich.objects.order_by('-date_made')
@@ -27,6 +29,13 @@ def add_sandwich(request):
                                newsandwich = form.save(commit=False)
                                newsandwich.user = request.user
                                newsandwich.save()
                                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 = "<p class=\"formthanks\">Thanks! Your sandwich has been added!</p>"
                                return HttpResponse(thankshtml) # Redirect after POST
                else:
                                thankshtml = "<p class=\"formthanks\">Thanks! Your sandwich has been added!</p>"
                                return HttpResponse(thankshtml) # Redirect after POST
                else:
@@ -155,4 +164,18 @@ def create_user(request):
                                return HttpResponse(thankshtml) # Redirect after POST   
        else:
                form = NewUserForm() # An unbound form
                                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