login, signup, logout and add sandwich buttons appear according to whether a user...
authorKriti Godey <kriti.godey@gmail.com>
Fri, 5 Mar 2010 04:13:58 +0000 (23:13 -0500)
committerKriti Godey <kriti.godey@gmail.com>
Fri, 5 Mar 2010 04:13:58 +0000 (23:13 -0500)
templates/base.html
urls.py
views.py

index 12d9efb..2c15dee 100644 (file)
                                <h3 class="navtitle">NAVIGATION</h3>
                                <ul class="nav">
                                        <li><a href="http://127.0.0.1:8000/sandwich/all/" class="navlink">All sandwiches</a></li>
-                                       <li><a href="http://127.0.0.1:8000/sandwich/add/" class="navlink">Add sandwich</a></li>
-                                       <li><a href="http://127.0.0.1:8000/login/" class="navlink">Login</a></li>
-                                       <li><a href="http://127.0.0.1:8000/signup/" class="navlink">Signup</a></li>
-                                       <li>Monthly Links (TODO)</li>
-                                       <li>Logout (TODO)</li>
+                                       {% if user.is_authenticated %}
+                                               <li><a href="http://127.0.0.1:8000/sandwich/add/" class="navlink">Add sandwich</a></li>
+                                               <li><a href="http://127.0.0.1:8000/logout/" class="navlink">Logout</a></li>
+                                       {% else %}
+                                               <li><a href="http://127.0.0.1:8000/login/" class="navlink">Login</a></li>
+                                               <li><a href="http://127.0.0.1:8000/signup/" class="navlink">Signup</a></li>
+                                       {% endif %}
                                </ul>
                                <h3 class="navtitle">NEWEST</h3>
                                <ul class="newest">
                                                <li><a href="{{ s.get_absolute_url }}">{{ s.adjective }}</a></li>
                                        {% endfor %}
                                </ul>
+                               <h3 class="navtitle">BY MONTH</h3>
+                               <ul class="newest">
+                                       <li>January 2010</li>
+                                       <li>February 2010</li>
+                                       <li>To be done.</li>
+                               </ul>
                        </div>
                        <div id="content">
                                {% block content %}     
diff --git a/urls.py b/urls.py
index b370aff..dfab245 100644 (file)
--- a/urls.py
+++ b/urls.py
@@ -9,6 +9,7 @@ urlpatterns = patterns('',
        (r'^sandwich/(?P<year>[-\w]+)/(?P<month>[-\w]+)/$', views.sandwich_month),
        url(r'^sandwich/(?P<slug>[-\w]+)/$', views.specific_sandwich, name='sandwich_by_slug'),
        (r'^login/$', views.login_view),
+       (r'^logout/$', views.logout_view),
        (r'^signup/$', views.create_user),
        (r'', views.baseview),
 )
index 6afbe78..6395205 100644 (file)
--- a/views.py
+++ b/views.py
@@ -5,7 +5,7 @@ from django.shortcuts import render_to_response
 from django.core.files.uploadedfile import SimpleUploadedFile
 from models import Sandwich, Ingredient
 from django.http import Http404
-from django.contrib.auth import authenticate, login
+from django.contrib.auth import authenticate, login, logout
 from django.contrib.auth.forms import AuthenticationForm
 import datetime
 
@@ -25,7 +25,7 @@ def add_sandwich(request):
                                return HttpResponse(thankshtml) # Redirect after POST
                else:
                        form = SandwichForm() # An unbound form
-               return render_to_response('sandwich.html', {'sform': form, 'sandwiches': sandwiches,})
+               return render_to_response('sandwich.html', {'sform': form, 'sandwiches': sandwiches, 'user': request.user,})
        else:
                thankshtml = "<p class=\"formthanks\">You are not logged in.</p>"
                return HttpResponse(thankshtml) # Redirect after POST
@@ -46,7 +46,7 @@ def add_ingredient(request):
                else:
                        form = IngredientForm() # An unbound form
 
-               return render_to_response('ingredient.html', {'iform': form, 'sandwiches': sandwiches})
+               return render_to_response('ingredient.html', {'iform': form, 'sandwiches': sandwiches, 'user': request.user,})
        else:
                thankshtml = "<p class=\"formthanks\">You are not logged in.</p>"
                return HttpResponse(thankshtml) # Redirect after POST
@@ -60,7 +60,7 @@ def all_sandwich(request):
                        sandwiches = Sandwich.objects.order_by('date_made')
        except Sandwich.DoesNotExist:
                raise Http404
-       return render_to_response('allsandwiches.html', {'allsandwiches': allsandwiches, 'sandwiches': sandwiches})
+       return render_to_response('allsandwiches.html', {'allsandwiches': allsandwiches, 'sandwiches': sandwiches, 'user': request.user,})
 
 
 def baseview(request):
@@ -85,7 +85,7 @@ def sandwich_month(request, year, month):
                        sandwiches = Sandwich.objects.order_by('date_made')
        except Sandwich.DoesNotExist:
                raise Http404
-       return render_to_response('allsandwiches.html', {'allsandwiches': ms, 'sandwiches': sandwiches,})
+       return render_to_response('allsandwiches.html', {'allsandwiches': ms, 'sandwiches': sandwiches, 'user': request.user,})
 
 
 def specific_sandwich(request, slug):
@@ -97,7 +97,20 @@ def specific_sandwich(request, slug):
                        sandwiches = Sandwich.objects.order_by('date_made')
        except Sandwich.DoesNotExist:
                raise Http404
-       return render_to_response('onesandwich.html', {'s': s, 'sandwiches': sandwiches,})
+       return render_to_response('onesandwich.html', {'s': s, 'sandwiches': sandwiches, 'user': request.user,})
+
+def logout_view(request):
+       if Sandwich.objects.count() > 5:
+               sandwiches = Sandwich.objects.order_by('date_made')[:5]
+       else:
+               sandwiches = Sandwich.objects.order_by('date_made')
+       if request.user.is_authenticated():
+               logout(request)
+               thankshtml = "<p class=\"formthanks\">You have been logged out.</p>"
+               return HttpResponse(thankshtml)
+       else:
+               thankshtml = "<p class=\"formthanks\">You are not logged in.</p>"
+               return HttpResponse(thankshtml)
 
 
 def login_view(request):
@@ -122,7 +135,7 @@ def login_view(request):
                        return HttpResponse(thankshtml)
        except KeyError:
                aform = AuthenticationForm()
-               return render_to_response('login.html', {'aform': aform, 'sandwiches': sandwiches,})
+               return render_to_response('login.html', {'aform': aform, 'sandwiches': sandwiches, 'user': request.user,})
 
 
 def create_user(request):
@@ -155,4 +168,4 @@ def create_user(request):
                                return HttpResponse(thankshtml) # Redirect after POST   
        else:
                form = NewUserForm() # An unbound form
-               return render_to_response('newuser.html', {'cform': form, 'sandwiches': sandwiches,})
\ No newline at end of file
+               return render_to_response('newuser.html', {'cform': form, 'sandwiches': sandwiches, 'user': request.user,})
\ No newline at end of file