Started working on the skeleton of an actual website. Created barebones templates...
[~kgodey/maayanwich.git] / views.py
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
10 import datetime
11
12
13 def add_sandwich(request):
14         if Sandwich.objects.count() > 5:
15                 sandwiches = Sandwich.objects.order_by('date_made')[:5]
16         else:
17                 sandwiches = Sandwich.objects.order_by('date_made')
18         if request.method == 'POST': # If the form has been submitted...
19                 form = SandwichForm(request.POST, request.FILES) # A form bound to the POST data
20                 if form.is_valid(): # All validation rules pass
21                         newsandwich = form.save()
22                         newsandwich.save()
23                         thankshtml = "<p class=\"formthanks\">Thanks! Your sandwich has been added!</p>"
24                         return HttpResponse(thankshtml) # Redirect after POST
25         else:
26                 form = SandwichForm() # An unbound form
27                 
28         return render_to_response('sandwich.html', {'sform': form, 'sandwiches': sandwiches,})
29
30
31 def add_ingredient(request):
32         if Sandwich.objects.count() > 5:
33                 sandwiches = Sandwich.objects.order_by('date_made')[:5]
34         else:
35                 sandwiches = Sandwich.objects.order_by('date_made')
36         if request.method == 'POST': # If the form has been submitted...
37                 form = IngredientForm(request.POST) # A form bound to the POST data
38                 if form.is_valid(): # All validation rules pass
39                         newsandwich = form.save()
40                         newsandwich.save()
41                         thankshtml = "<p class=\"formthanks\">Thanks! Your ingredient has been saved!</p>"
42                         return HttpResponse(thankshtml) # Redirect after POST
43         else:
44                 form = IngredientForm() # An unbound form
45
46         return render_to_response('ingredient.html', {'iform': form, 'sandwiches': sandwiches})
47         
48
49 def all_sandwich(request):
50         try:
51                 allsandwiches = Sandwich.objects.all()
52                 if Sandwich.objects.count() > 5:
53                         sandwiches = Sandwich.objects.order_by('date_made')[:5]
54                 else:
55                         sandwiches = Sandwich.objects.order_by('date_made')
56         except Sandwich.DoesNotExist:
57                 raise Http404
58         return render_to_response('allsandwiches.html', {'allsandwiches': allsandwiches, 'sandwiches': sandwiches})
59
60         
61 def baseview(request):
62         try:
63                 if Sandwich.objects.count() > 5:
64                         sandwiches = Sandwich.objects.order_by('date_made')[:5]
65                         allsandwiches = Sandwich.objects.all()
66                 else:
67                         sandwiches = Sandwich.objects.order_by('date_made')
68                         allsandwiches = Sandwich.objects.all()
69         except Sandwich.DoesNotExist:
70                 raise Http404
71         return render_to_response('base.html', {'sandwiches': sandwiches, 'all': allsandwiches,})
72         
73         
74 def sandwich_month(request, year, month):
75         try:
76                 sandwiches = Sandwich.objects.filter(date_made__month=month, date_made__year=year)
77         except Sandwich.DoesNotExist:
78                 raise Http404
79         return render_to_response('allsandwiches.html', {'sandwiches': sandwiches,})
80         
81         
82 def specific_sandwich(request, slug):
83         try:
84                 s = Sandwich.objects.get(slug=slug)
85                 if Sandwich.objects.count() > 5:
86                         sandwiches = Sandwich.objects.order_by('date_made')[:5]
87                 else:
88                         sandwiches = Sandwich.objects.order_by('date_made')
89         except Sandwich.DoesNotExist:
90                 raise Http404
91         return render_to_response('onesandwich.html', {'s': s, 'sandwiches': sandwiches,})
92
93
94 def login_view(request):
95         if Sandwich.objects.count() > 5:
96                 sandwiches = Sandwich.objects.order_by('date_made')[:5]
97         else:
98                 sandwiches = Sandwich.objects.order_by('date_made')
99         try:
100                 username = request.POST['username']
101                 password = request.POST['password']
102                 user = authenticate(username=username, password=password)
103                 if user is not None:
104                         if user.is_active:
105                                 login(request, user)
106                                 thankshtml = "<p class=\"formthanks\">You have been logged in</p>"
107                                 return HttpResponse(thankshtml)
108                         else:
109                                 thankshtml = "<p class=\"formthanks\">Disabled account. Sorry.</p>"
110                                 return HttpResponse(thankshtml)
111                 else:
112                         thankshtml = "<p class=\"formthanks\">Invalid login!</p>"
113                         return HttpResponse(thankshtml)
114         except KeyError:
115                 aform = AuthenticationForm()
116                 return render_to_response('login.html', {'aform': aform, 'sandwiches': sandwiches,})
117                 
118 def create_user(request):
119         if Sandwich.objects.count() > 5:
120                 sandwiches = Sandwich.objects.order_by('date_made')[:5]
121         else:
122                 sandwiches = Sandwich.objects.order_by('date_made')
123         if request.user.is_authenticated():
124                 thankshtml = "<p class=\"formthanks\">You are already logged in!</p>"
125                 return HttpResponse(thankshtml)
126         elif request.method == 'POST': # If the form has been submitted...
127                 form = NewUserForm(request.POST) # A form bound to the POST data
128                 if form.is_valid(): # All validation rules pass
129                         username = form.cleaned_data['username']
130                         first_name = form.cleaned_data['first_name']
131                         last_name = form.cleaned_data['last_name']
132                         password = form.cleaned_data['password']
133                         cpassword = form.cleaned_data['confirm_password']
134                         email = form.cleaned_data['email']
135                         if password == cpassword:
136                                 user = User.objects.create_user(username, email, password)
137                                 user.save()
138                                 user.first_name = first_name
139                                 user.last_name = last_name
140                                 user.save()
141                                 thankshtml = "<p class=\"formthanks\">Thanks! You are now a new user!</p>"
142                                 return HttpResponse(thankshtml) # Redirect after POST
143                         else:
144                                 thankshtml = "<p class=\"formthanks\">Your passwords don't match!</p>"
145                                 return HttpResponse(thankshtml) # Redirect after POST   
146         else:
147                 form = NewUserForm() # An unbound form
148                 return render_to_response('newuser.html', {'cform': form, 'sandwiches': sandwiches,})