added an error page for "you are not logged in"
[~kgodey/maayanwich.git] / views.py
index 38556df..309aa2f 100644 (file)
--- a/views.py
+++ b/views.py
@@ -49,7 +49,7 @@ def add_sandwich(request):
                        form = SandwichForm(initial={'user': request.user}) # An unbound form
                return render_to_response('sandwich.html', {'sform': form,}, context_instance=RequestContext(request))
        else:
-               return HttpResponseRedirect(reverse('login'))
+               return HttpResponseRedirect(reverse('login2'))
 
 def add_ingredient(request):
        if request.user.is_authenticated():
@@ -139,6 +139,30 @@ def login_view(request):
        except KeyError:
                aform = AuthenticationForm()
                return render_to_response('login.html', {'aform': aform,}, context_instance=RequestContext(request))
+               
+def login_view2(request):
+       x = reverse('index')
+       if 'HTTP_REFERER' in request.META:
+               x = request.META['HTTP_REFERER']
+       if Sandwich.objects.count() > 5:
+               sandwiches = Sandwich.objects.order_by('-date_made')[:5]
+       else:
+               sandwiches = Sandwich.objects.order_by('-date_made')
+       try:
+               username = request.POST['username']
+               password = request.POST['password']
+               user = authenticate(username=username, password=password)
+               if user is not None:
+                       if user.is_active:
+                               login(request, user)
+                               return HttpResponseRedirect(x)
+                       else:
+                               return HttpResponseRedirect(x)
+               else:
+                       return HttpResponseRedirect('login')
+       except KeyError:
+               aform = AuthenticationForm()
+               return render_to_response('pleaselogin.html', {'aform': aform,}, context_instance=RequestContext(request))
 
 
 def create_user(request):