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