moved media to media folder in app
[~kgodey/maayanwich.git] / views.py
index b5d68ff..18f6951 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.contrib.auth import authenticate, login, logout
-from django.contrib.auth.forms import AuthenticationForm
+from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
 from django.contrib.comments.models import Comment
 from django.template import RequestContext
 from django.core import serializers
@@ -54,7 +54,10 @@ def add_sandwich(request):
 
 @login_required
 def edit_sandwich(request, slug):
-       sedit = Sandwich.objects.get(slug=slug)
+       try:
+               sedit = Sandwich.objects.get(slug=slug)
+       except Sandwich.DoesNotExist:
+               raise Http404
        ingred = sedit.ingredients.all()
        if not sedit.user == request.user:
                return render_to_response('nopermission.html', context_instance=RequestContext(request))
@@ -93,9 +96,10 @@ def del_sandwich(request, slug):
                del_sandwich = Sandwich.objects.get(slug=slug)
                if request.user == del_sandwich.user:
                        del_sandwich.delete()
-               return HttpResponseRedirect(reverse('all_sandwiches'))
+               x = request.user.username
+               return HttpResponseRedirect(reverse('sandwich_user', kwargs={'username': x,}))
        else:
-               return HttpResponseRedirect(reverse('all_sandwiches'))
+               raise Http404
 
 
 def all_sandwich(request):
@@ -104,14 +108,23 @@ def all_sandwich(request):
        except Sandwich.DoesNotExist:
                raise Http404
        return render_to_response('allsandwiches.html', {'allsandwiches': allsandwiches,}, context_instance=RequestContext(request))
+       
+def index_sandwich(request):
+       try:
+               allsandwiches = Sandwich.objects.order_by('adjective')
+       except Sandwich.DoesNotExist:
+               raise Http404
+       return render_to_response('indexsandwiches.html', {'allsandwiches': allsandwiches,}, context_instance=RequestContext(request))
 
 
 def sandwich_month(request, year, month):
        try:
                ms = Sandwich.objects.filter(date_made__month=month, date_made__year=year)
+               x = ms[0]
+               xdate = x.date_made
        except Sandwich.DoesNotExist:
                raise Http404
-       return render_to_response('allsandwiches.html', {'allsandwiches': ms,}, context_instance=RequestContext(request))
+       return render_to_response('monthsandwiches.html', {'allsandwiches': ms, 'xdate': xdate, }, context_instance=RequestContext(request))
        
        
 def current_home(request):
@@ -165,29 +178,33 @@ def login_view(request):
 def create_user(request):
        if request.user.is_authenticated():
                return render_to_response('loggedin.html', context_instance=RequestContext(request))
-       elif request.method == 'POST': # If the form has been submitted...
-               form = NewUserForm(request.POST) # A form bound to the POST data
-               if form.is_valid(): # All validation rules pass
-                       username = form.cleaned_data['username']
-                       first_name = form.cleaned_data['first_name']
-                       last_name = form.cleaned_data['last_name']
-                       password = form.cleaned_data['password']
-                       cpassword = form.cleaned_data['confirm_password']
-                       email = form.cleaned_data['email']
-                       if password == cpassword:
-                               user = User.objects.create_user(username, email, password)
-                               user.save()
-                               user.first_name = first_name
-                               user.last_name = last_name
-                               user.save()
-                               return HttpResponseRedirect('index')
-                       else:
-                               return HttpResponseRedirect('signup')   
-       else:
-               form = NewUserForm() # An unbound form
-               return render_to_response('newuser.html', {'cform': form,}, context_instance=RequestContext(request))
+       
+       redirect_to = reverse('index')
+#      if 'next' in request.GET:
+#              redirect_to = request.GET['next']
                
+       if request.method == 'POST':
+               aform = UserCreationForm(data=request.POST)
+               if aform.is_valid():
+                       aform.save()
+                       x = authenticate(username=aform.cleaned_data['username'], password=aform.cleaned_data['password1'])
+                       login(request, x)
+                       return render_to_response('usercreated.html', context_instance=RequestContext(request))
+       else:
+               aform = UserCreationForm()
 
+       return render_to_response('newuser.html', {'aform': aform,}, context_instance=RequestContext(request))
+
+@login_required
+def sandwich_user(request, username):
+       user = User.objects.get(username = username)
+       try:
+               ms = Sandwich.objects.filter(user = user)
+       except Sandwich.DoesNotExist:
+               raise Http404
+       return render_to_response('usersandwiches.html', {'allsandwiches': ms,}, context_instance=RequestContext(request))
+
+@login_required
 def edit_user(request, gusername):
        guser = User.objects.get(username = gusername)
        if not request.user.is_authenticated():
@@ -195,26 +212,28 @@ def edit_user(request, gusername):
        elif request.user == guser:
                if request.method == 'POST': # If the form has been submitted...
                        form = NewUserForm(request.POST) # A form bound to the POST data
-                       username = request.POST['username']
-                       first_name = request.POST['first_name']
-                       last_name = request.POST['last_name']
-                       password = request.POST['password']
-                       cpassword = request.POST['confirm_password']
-                       email = request.POST['email']
-                       guser.username = username
-                       guser.first_name = first_name
-                       guser.last_name = last_name
-                       if not password.strip() == '':
-                               if password == cpassword:
+                       if form.is_valid():
+                               username = request.POST['username']
+                               first_name = request.POST['first_name']
+                               last_name = request.POST['last_name']
+                               password = request.POST['password']
+                               cpassword = request.POST['confirmpassword']
+                               email = request.POST['email']
+                               guser.username = username
+                               guser.first_name = first_name
+                               guser.last_name = last_name
+                               if password != '':
                                        guser.set_password(password)
-                       guser.email = email
-                       guser.save()
-                       return HttpResponseRedirect(reverse('edit_user', kwargs={'gusername':guser.username}))  
+                               guser.email = email
+                               guser.save()
+                               return render_to_response('userchanged.html', context_instance=RequestContext(request)) 
+                       else:
+                               return render_to_response('edituser.html', {'cform': form,}, context_instance=RequestContext(request))
                else:
                        form = NewUserForm(initial={'username': guser.username, 'first_name': guser.first_name, 'last_name': guser.last_name, 'email': guser.email, }) # An unbound form
                        return render_to_response('edituser.html', {'cform': form,}, context_instance=RequestContext(request))
        else:
-               return HttpResponseRedirect(reverse('index'))
+               return render_to_response('nopermission.html', context_instance=RequestContext(request))
                
 
 def comment_posted(request):