Added a view for "top 5 sandwiches", currently using "all sandwiches" template.
[~kgodey/maayanwich.git] / views.py
1 from django.http import HttpResponse
2 from forms import SandwichForm, IngredientForm
3 from django.shortcuts import render_to_response
4 from django.core.files.uploadedfile import SimpleUploadedFile
5 from models import Sandwich, Ingredient
6 from django.http import Http404
7 import datetime
8
9
10 def add_sandwich(request):
11         if request.method == 'POST': # If the form has been submitted...
12                 form = SandwichForm(request.POST, request.FILES) # A form bound to the POST data
13                 if form.is_valid(): # All validation rules pass
14                         newsandwich = form.save()
15                         newsandwich.save()
16                         thankshtml = "<p class=\"formthanks\">Thanks! Your sandwich has been added!</p>"
17                         return HttpResponse(thankshtml) # Redirect after POST
18         else:
19                 form = SandwichForm() # An unbound form
20                 
21         return render_to_response('sandwich.html', {'sform': form,})
22
23
24 def add_ingredient(request):
25         if request.method == 'POST': # If the form has been submitted...
26                 form = IngredientForm(request.POST) # A form bound to the POST data
27                 if form.is_valid(): # All validation rules pass
28                         newsandwich = form.save()
29                         newsandwich.save()
30                         thankshtml = "<p class=\"formthanks\">Thanks! Your ingredient has been saved!</p>"
31                         return HttpResponse(thankshtml) # Redirect after POST
32         else:
33                 form = IngredientForm() # An unbound form
34
35         return render_to_response('ingredient.html', {'iform': form,})
36         
37
38 def all_sandwich(request):
39         try:
40                 sandwiches = Sandwich.objects.all()
41         except Sandwich.DoesNotExist:
42                 raise Http404
43         return render_to_response('allsandwiches.html', {'sandwiches': sandwiches,})
44         
45 def newsandwiches(request):
46         try:
47                 if Sandwich.objects.count() > 5:
48                         sandwiches = Sandwich.objects.order_by('date_made')[:5]
49                 else:
50                         sandwiches = Sandwich.objects.order_by('date_made')
51         except Sandwich.DoesNotExist:
52                 raise Http404
53         return render_to_response('allsandwiches.html', {'sandwiches': sandwiches,})