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