1 from django.http import HttpResponse
2 from django.contrib.auth.models import User
3 from forms import SandwichForm, IngredientForm, NewUserForm
4 from django.shortcuts import render_to_response
5 from django.core.files.uploadedfile import SimpleUploadedFile
6 from models import Sandwich, Ingredient
7 from django.http import Http404
8 from django.contrib.auth import authenticate, login
9 from django.contrib.auth.forms import AuthenticationForm
13 def add_sandwich(request):
14 if request.method == 'POST': # If the form has been submitted...
15 form = SandwichForm(request.POST, request.FILES) # A form bound to the POST data
16 if form.is_valid(): # All validation rules pass
17 newsandwich = form.save()
19 thankshtml = "<p class=\"formthanks\">Thanks! Your sandwich has been added!</p>"
20 return HttpResponse(thankshtml) # Redirect after POST
22 form = SandwichForm() # An unbound form
24 return render_to_response('sandwich.html', {'sform': form,})
27 def add_ingredient(request):
28 if request.method == 'POST': # If the form has been submitted...
29 form = IngredientForm(request.POST) # A form bound to the POST data
30 if form.is_valid(): # All validation rules pass
31 newsandwich = form.save()
33 thankshtml = "<p class=\"formthanks\">Thanks! Your ingredient has been saved!</p>"
34 return HttpResponse(thankshtml) # Redirect after POST
36 form = IngredientForm() # An unbound form
38 return render_to_response('ingredient.html', {'iform': form,})
41 def all_sandwich(request):
43 sandwiches = Sandwich.objects.all()
44 except Sandwich.DoesNotExist:
46 return render_to_response('allsandwiches.html', {'sandwiches': sandwiches,})
49 def newsandwiches(request):
51 if Sandwich.objects.count() > 5:
52 sandwiches = Sandwich.objects.order_by('date_made')[:5]
54 sandwiches = Sandwich.objects.order_by('date_made')
55 except Sandwich.DoesNotExist:
57 return render_to_response('allsandwiches.html', {'sandwiches': sandwiches,})
60 def sandwich_month(request, year, month):
62 sandwiches = Sandwich.objects.filter(date_made__month=month, date_made__year=year)
63 except Sandwich.DoesNotExist:
65 return render_to_response('allsandwiches.html', {'sandwiches': sandwiches,})
68 def specific_sandwich(request, slug):
70 sandwiches = Sandwich.objects.get(slug=slug)
71 except Sandwich.DoesNotExist:
73 return render_to_response('onesandwich.html', {'s': sandwiches,})
76 def login_view(request):
78 username = request.POST['username']
79 password = request.POST['password']
80 user = authenticate(username=username, password=password)
84 thankshtml = "<p class=\"formthanks\">You have been logged in</p>"
85 return HttpResponse(thankshtml)
87 thankshtml = "<p class=\"formthanks\">Disabled account. Sorry.</p>"
88 return HttpResponse(thankshtml)
90 thankshtml = "<p class=\"formthanks\">Invalid login!</p>"
91 return HttpResponse(thankshtml)
93 aform = AuthenticationForm()
94 return render_to_response('login.html', {'aform': aform,})
96 def create_user(request):
97 if request.user.is_authenticated():
98 thankshtml = "<p class=\"formthanks\">You are already logged in!</p>"
99 return HttpResponse(thankshtml)
100 elif request.method == 'POST': # If the form has been submitted...
101 form = NewUserForm(request.POST) # A form bound to the POST data
102 if form.is_valid(): # All validation rules pass
103 username = form.cleaned_data['username']
104 first_name = form.cleaned_data['first_name']
105 last_name = form.cleaned_data['last_name']
106 password = form.cleaned_data['password']
107 cpassword = form.cleaned_data['confirm_password']
108 email = form.cleaned_data['email']
109 if password == cpassword:
110 user = User.objects.create_user(username, email, password)
112 user.first_name = first_name
113 user.last_name = last_name
115 thankshtml = "<p class=\"formthanks\">Thanks! You are now a new user!</p>"
116 return HttpResponse(thankshtml) # Redirect after POST
118 thankshtml = "<p class=\"formthanks\">Your passwords don't match!</p>"
119 return HttpResponse(thankshtml) # Redirect after POST
121 form = NewUserForm() # An unbound form
122 return render_to_response('newuser.html', {'cform': form,})