Added the "create new user" view.
[~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         return render_to_response('sandwich.html', {'sform': form,})
25
26
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()
32                         newsandwich.save()
33                         thankshtml = "<p class=\"formthanks\">Thanks! Your ingredient has been saved!</p>"
34                         return HttpResponse(thankshtml) # Redirect after POST
35         else:
36                 form = IngredientForm() # An unbound form
37
38         return render_to_response('ingredient.html', {'iform': form,})
39         
40
41 def all_sandwich(request):
42         try:
43                 sandwiches = Sandwich.objects.all()
44         except Sandwich.DoesNotExist:
45                 raise Http404
46         return render_to_response('allsandwiches.html', {'sandwiches': sandwiches,})
47
48         
49 def newsandwiches(request):
50         try:
51                 if Sandwich.objects.count() > 5:
52                         sandwiches = Sandwich.objects.order_by('date_made')[:5]
53                 else:
54                         sandwiches = Sandwich.objects.order_by('date_made')
55         except Sandwich.DoesNotExist:
56                 raise Http404
57         return render_to_response('allsandwiches.html', {'sandwiches': sandwiches,})
58         
59         
60 def sandwich_month(request, year, month):
61         try:
62                 sandwiches = Sandwich.objects.filter(date_made__month=month, date_made__year=year)
63         except Sandwich.DoesNotExist:
64                 raise Http404
65         return render_to_response('allsandwiches.html', {'sandwiches': sandwiches,})
66         
67         
68 def specific_sandwich(request, slug):
69         try:
70                 sandwiches = Sandwich.objects.get(slug=slug)
71         except Sandwich.DoesNotExist:
72                 raise Http404
73         return render_to_response('onesandwich.html', {'s': sandwiches,})
74
75
76 def login_view(request):
77         try:
78                 username = request.POST['username']
79                 password = request.POST['password']
80                 user = authenticate(username=username, password=password)
81                 if user is not None:
82                         if user.is_active:
83                                 login(request, user)
84                                 thankshtml = "<p class=\"formthanks\">You have been logged in</p>"
85                                 return HttpResponse(thankshtml)
86                         else:
87                                 thankshtml = "<p class=\"formthanks\">Disabled account. Sorry.</p>"
88                                 return HttpResponse(thankshtml)
89                 else:
90                         thankshtml = "<p class=\"formthanks\">Invalid login!</p>"
91                         return HttpResponse(thankshtml)
92         except KeyError:
93                 aform = AuthenticationForm()
94                 return render_to_response('login.html', {'aform': aform,})
95                 
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)
111                                 user.save()
112                                 user.first_name = first_name
113                                 user.last_name = last_name
114                                 user.save()
115                                 thankshtml = "<p class=\"formthanks\">Thanks! You are now a new user!</p>"
116                                 return HttpResponse(thankshtml) # Redirect after POST
117                         else:
118                                 thankshtml = "<p class=\"formthanks\">Your passwords don't match!</p>"
119                                 return HttpResponse(thankshtml) # Redirect after POST   
120         else:
121                 form = NewUserForm() # An unbound form
122                 return render_to_response('newuser.html', {'cform': form,})