From 9d0f2e4b0e8d40821e3b8a98ae66bc8149597319 Mon Sep 17 00:00:00 2001 From: Kriti Godey Date: Thu, 4 Mar 2010 23:33:29 -0500 Subject: [PATCH 1/1] Default view is now latest sandwich's month's sandwiches. Fixed other minor things. --- templates/base.html | 8 ++++--- urls.py | 2 +- views.py | 53 ++++++++++++++++++++++++++++----------------- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/templates/base.html b/templates/base.html index 2c15dee..b12c087 100644 --- a/templates/base.html +++ b/templates/base.html @@ -9,9 +9,8 @@

Ma'ayanwich Tracker

{% block content %} diff --git a/urls.py b/urls.py index dfab245..08e08e5 100644 --- a/urls.py +++ b/urls.py @@ -11,5 +11,5 @@ urlpatterns = patterns('', (r'^login/$', views.login_view), (r'^logout/$', views.logout_view), (r'^signup/$', views.create_user), - (r'', views.baseview), + (r'', views.current_home), ) diff --git a/views.py b/views.py index 6395205..2a175af 100644 --- a/views.py +++ b/views.py @@ -1,10 +1,9 @@ -from django.http import HttpResponse +from django.http import HttpResponse, Http404 from django.contrib.auth.models import User from forms import SandwichForm, IngredientForm, NewUserForm 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, logout from django.contrib.auth.forms import AuthenticationForm import datetime @@ -12,9 +11,9 @@ import datetime def add_sandwich(request): if Sandwich.objects.count() > 5: - sandwiches = Sandwich.objects.order_by('date_made')[:5] + sandwiches = Sandwich.objects.order_by('-date_made')[:5] else: - sandwiches = Sandwich.objects.order_by('date_made') + sandwiches = Sandwich.objects.order_by('-date_made') if request.user.is_authenticated(): if request.method == 'POST': # If the form has been submitted... form = SandwichForm(request.POST, request.FILES) # A form bound to the POST data @@ -33,9 +32,9 @@ def add_sandwich(request): def add_ingredient(request): if request.user.is_authenticated(): if Sandwich.objects.count() > 5: - sandwiches = Sandwich.objects.order_by('date_made')[:5] + sandwiches = Sandwich.objects.order_by('-date_made')[:5] else: - sandwiches = Sandwich.objects.order_by('date_made') + sandwiches = Sandwich.objects.order_by('-date_made') if request.method == 'POST': # If the form has been submitted... form = IngredientForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass @@ -55,9 +54,9 @@ def all_sandwich(request): try: allsandwiches = Sandwich.objects.all() if Sandwich.objects.count() > 5: - sandwiches = Sandwich.objects.order_by('date_made')[:5] + sandwiches = Sandwich.objects.order_by('-date_made')[:5] else: - sandwiches = Sandwich.objects.order_by('date_made') + sandwiches = Sandwich.objects.order_by('-date_made') except Sandwich.DoesNotExist: raise Http404 return render_to_response('allsandwiches.html', {'allsandwiches': allsandwiches, 'sandwiches': sandwiches, 'user': request.user,}) @@ -66,10 +65,10 @@ def all_sandwich(request): def baseview(request): try: if Sandwich.objects.count() > 5: - sandwiches = Sandwich.objects.order_by('date_made')[:5] + sandwiches = Sandwich.objects.order_by('-date_made')[:5] allsandwiches = Sandwich.objects.all() else: - sandwiches = Sandwich.objects.order_by('date_made') + sandwiches = Sandwich.objects.order_by('-date_made') allsandwiches = Sandwich.objects.all() except Sandwich.DoesNotExist: raise Http404 @@ -80,9 +79,23 @@ def sandwich_month(request, year, month): try: ms = Sandwich.objects.filter(date_made__month=month, date_made__year=year) if Sandwich.objects.count() > 5: - sandwiches = Sandwich.objects.order_by('date_made')[:5] + sandwiches = Sandwich.objects.order_by('-date_made')[:5] else: - sandwiches = Sandwich.objects.order_by('date_made') + sandwiches = Sandwich.objects.order_by('-date_made') + except Sandwich.DoesNotExist: + raise Http404 + return render_to_response('allsandwiches.html', {'allsandwiches': ms, 'sandwiches': sandwiches, 'user': request.user,}) + +def current_home(request): + temp = Sandwich.objects.order_by('-date_made')[0] + curr_month = temp.date_made.month + curr_year = temp.date_made.year + try: + ms = Sandwich.objects.filter(date_made__month=curr_month, date_made__year=curr_year) + if Sandwich.objects.count() > 5: + sandwiches = Sandwich.objects.order_by('-date_made')[:5] + else: + sandwiches = Sandwich.objects.order_by('-date_made') except Sandwich.DoesNotExist: raise Http404 return render_to_response('allsandwiches.html', {'allsandwiches': ms, 'sandwiches': sandwiches, 'user': request.user,}) @@ -92,18 +105,18 @@ def specific_sandwich(request, slug): try: s = Sandwich.objects.get(slug=slug) if Sandwich.objects.count() > 5: - sandwiches = Sandwich.objects.order_by('date_made')[:5] + sandwiches = Sandwich.objects.order_by('-date_made')[:5] else: - sandwiches = Sandwich.objects.order_by('date_made') + sandwiches = Sandwich.objects.order_by('-date_made') except Sandwich.DoesNotExist: raise Http404 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] + sandwiches = Sandwich.objects.order_by('-date_made')[:5] else: - sandwiches = Sandwich.objects.order_by('date_made') + sandwiches = Sandwich.objects.order_by('-date_made') if request.user.is_authenticated(): logout(request) thankshtml = "

You have been logged out.

" @@ -115,9 +128,9 @@ def logout_view(request): def login_view(request): if Sandwich.objects.count() > 5: - sandwiches = Sandwich.objects.order_by('date_made')[:5] + sandwiches = Sandwich.objects.order_by('-date_made')[:5] else: - sandwiches = Sandwich.objects.order_by('date_made') + sandwiches = Sandwich.objects.order_by('-date_made') try: username = request.POST['username'] password = request.POST['password'] @@ -140,9 +153,9 @@ def login_view(request): def create_user(request): if Sandwich.objects.count() > 5: - sandwiches = Sandwich.objects.order_by('date_made')[:5] + sandwiches = Sandwich.objects.order_by('-date_made')[:5] else: - sandwiches = Sandwich.objects.order_by('date_made') + sandwiches = Sandwich.objects.order_by('-date_made') if request.user.is_authenticated(): thankshtml = "

You are already logged in!

" return HttpResponse(thankshtml) -- 2.20.1