Added month view and url.
[~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 from django.contrib.auth import authenticate, login
8 from django.contrib.auth.forms import AuthenticationForm
9 import datetime
10
11
12 def add_sandwich(request):
13         if request.method == 'POST': # If the form has been submitted...
14                 form = SandwichForm(request.POST, request.FILES) # A form bound to the POST data
15                 if form.is_valid(): # All validation rules pass
16                         newsandwich = form.save()
17                         newsandwich.save()
18                         thankshtml = "<p class=\"formthanks\">Thanks! Your sandwich has been added!</p>"
19                         return HttpResponse(thankshtml) # Redirect after POST
20         else:
21                 form = SandwichForm() # An unbound form
22                 
23         return render_to_response('sandwich.html', {'sform': form,})
24
25
26 def add_ingredient(request):
27         if request.method == 'POST': # If the form has been submitted...
28                 form = IngredientForm(request.POST) # A form bound to the POST data
29                 if form.is_valid(): # All validation rules pass
30                         newsandwich = form.save()
31                         newsandwich.save()
32                         thankshtml = "<p class=\"formthanks\">Thanks! Your ingredient has been saved!</p>"
33                         return HttpResponse(thankshtml) # Redirect after POST
34         else:
35                 form = IngredientForm() # An unbound form
36
37         return render_to_response('ingredient.html', {'iform': form,})
38         
39
40 def all_sandwich(request):
41         try:
42                 sandwiches = Sandwich.objects.all()
43         except Sandwich.DoesNotExist:
44                 raise Http404
45         return render_to_response('allsandwiches.html', {'sandwiches': sandwiches,})
46
47         
48 def newsandwiches(request):
49         try:
50                 if Sandwich.objects.count() > 5:
51                         sandwiches = Sandwich.objects.order_by('date_made')[:5]
52                 else:
53                         sandwiches = Sandwich.objects.order_by('date_made')
54         except Sandwich.DoesNotExist:
55                 raise Http404
56         return render_to_response('allsandwiches.html', {'sandwiches': sandwiches,})
57         
58 def sandwich_month(request, year, month):
59         try:
60                 sandwiches = Sandwich.objects.filter(date_made__month=month, date_made__year=year)
61         except Sandwich.DoesNotExist:
62                 raise Http404
63         return render_to_response('allsandwiches.html', {'sandwiches': sandwiches,})
64         
65         
66 def specific_sandwich(request, slug):
67         try:
68                 sandwiches = Sandwich.objects.get(slug=slug)
69         except Sandwich.DoesNotExist:
70                 raise Http404
71         return render_to_response('onesandwich.html', {'s': sandwiches,})
72
73
74 def login_view(request):
75         try:
76                 username = request.POST['username']
77                 password = request.POST['password']
78                 user = authenticate(username=username, password=password)
79                 if user is not None:
80                         if user.is_active:
81                                 login(request, user)
82                                 thankshtml = "<p class=\"formthanks\">You have been logged in</p>"
83                                 return HttpResponse(thankshtml)
84                         else:
85                                 thankshtml = "<p class=\"formthanks\">Disabled account. Sorry.</p>"
86                                 return HttpResponse(thankshtml)
87                 else:
88                         thankshtml = "<p class=\"formthanks\">Invalid login!</p>"
89                         return HttpResponse(thankshtml)
90         except KeyError:
91                 aform = AuthenticationForm()
92                 return render_to_response('login.html', {'aform': aform,})