1 from django.http import HttpResponse, Http404, HttpResponseRedirect
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.contrib.auth import authenticate, login, logout
8 from django.contrib.auth.forms import AuthenticationForm
9 from django.contrib.comments.models import Comment
10 from django.template import RequestContext
11 from django.core import serializers
12 from slugify import SlugifyUniquely
13 from recipes.settings import MEDIA_URL
15 import django.utils.simplejson as json
16 from django.core.urlresolvers import reverse
18 def sidebar_context(request):
19 x = Sandwich.objects.order_by('-date_made')
24 monthly = Sandwich.objects.dates('date_made', 'month')
25 return {'sandwiches': sandwiches, 'monthly': monthly, 'user': request.user, 'media_url': MEDIA_URL }
28 def add_sandwich(request):
29 if request.user.is_authenticated():
30 if request.method == 'POST': # If the form has been submitted...
31 form = SandwichForm(request.POST, request.FILES) # A form bound to the POST data
32 if form.is_valid(): # All validation rules pass
33 newsandwich = form.save(commit=False)
34 newsandwich.user = request.user
36 x = request.POST['ing']
41 newsandwich.ingredients.add(Ingredient.objects.get(id=n))
42 elif n[:4] == 'new:' and len(n) > 4:
44 newingredient = Ingredient(name=n, slug=SlugifyUniquely(n, Ingredient))
46 newsandwich.ingredients.add(newingredient)
48 return HttpResponseRedirect(newsandwich.get_absolute_url())
50 form = SandwichForm(initial={'user': request.user}) # An unbound form
51 return render_to_response('sandwich.html', {'sform': form,}, context_instance=RequestContext(request))
53 return HttpResponseRedirect(reverse('login2'))
55 def edit_sandwich(request, slug):
56 sedit = Sandwich.objects.get(slug=slug)
57 ingred = sedit.ingredients.all()
58 if request.user.is_authenticated():
59 if not sedit.user == request.user:
60 return HttpResponseRedirect(reverse('all_sandwiches'))
62 if request.method == 'POST':
63 sform = SandwichForm(request.POST, request.FILES, instance=sedit)
64 if sform.is_valid(): # All validation rules pass
65 sedit.adjective = request.POST['adjective']
66 sedit.date_made = request.POST['date_made']
67 sedit.notes = request.POST['notes']
68 for ig in sedit.ingredients.all():
69 sedit.ingredients.remove(ig)
70 if request.POST['picture']:
71 sedit.picture = request.POST['picture']
72 x = request.POST['ing']
77 sedit.ingredients.add(Ingredient.objects.get(id=n))
78 elif n[:4] == 'new:' and len(n) > 4:
80 newingredient = Ingredient(name=n, slug=SlugifyUniquely(n, Ingredient))
82 sedit.ingredients.add(newingredient)
84 return HttpResponseRedirect(sedit.get_absolute_url())
86 sform = SandwichForm(instance=sedit)
87 return render_to_response('editsandwich.html', {'sform': sform, 's':sedit, 'prepop': ingred, }, context_instance=RequestContext(request))
89 return HttpResponseRedirect(reverse('login2'))
91 def del_sandwich(request, slug):
92 if request.user.is_authenticated():
93 if Sandwich.objects.get(slug=slug):
94 del_sandwich = Sandwich.objects.get(slug=slug)
95 if request.user == del_sandwich.user:
97 return HttpResponseRedirect(reverse('all_sandwiches'))
99 return HttpResponseRedirect(reverse('all_sandwiches'))
101 return HttpResponseRedirect(reverse('login2'))
103 def add_ingredient(request):
104 if request.user.is_authenticated():
105 if request.method == 'POST': # If the form has been submitted...
106 form = IngredientForm(request.POST) # A form bound to the POST data
107 if form.is_valid(): # All validation rules pass
108 newsandwich = form.save()
110 thankshtml = "<p class=\"formthanks\">Thanks! Your ingredient has been saved!</p>"
111 return HttpResponse(thankshtml) # Redirect after POST
113 form = IngredientForm() # An unbound form
115 return render_to_response('ingredient.html', {'iform': form,}, context_instance=RequestContext(request))
117 thankshtml = "<p class=\"formthanks\">You are not logged in.</p>"
118 return HttpResponse(thankshtml) # Redirect after POST
120 def all_sandwich(request):
122 allsandwiches = Sandwich.objects.order_by('-date_made')
123 except Sandwich.DoesNotExist:
125 return render_to_response('allsandwiches.html', {'allsandwiches': allsandwiches,}, context_instance=RequestContext(request))
128 def sandwich_month(request, year, month):
130 ms = Sandwich.objects.filter(date_made__month=month, date_made__year=year)
131 except Sandwich.DoesNotExist:
133 return render_to_response('allsandwiches.html', {'allsandwiches': ms,}, context_instance=RequestContext(request))
135 def current_home(request):
136 temp = Sandwich.objects.order_by('-date_made')[0]
137 curr_month = temp.date_made.month
138 curr_year = temp.date_made.year
140 ms = Sandwich.objects.filter(date_made__month=curr_month, date_made__year=curr_year)
141 except Sandwich.DoesNotExist:
143 return render_to_response('allsandwiches.html', {'allsandwiches': ms,}, context_instance=RequestContext(request))
146 def specific_sandwich(request, slug):
148 s = Sandwich.objects.get(slug=slug)
149 if Sandwich.objects.count() > 5:
150 sandwiches = Sandwich.objects.order_by('-date_made')[:5]
152 sandwiches = Sandwich.objects.order_by('-date_made')
153 except Sandwich.DoesNotExist:
155 return render_to_response('onesandwich.html', {'s': s,}, context_instance=RequestContext(request))
157 def logout_view(request):
159 if 'HTTP_REFERER' in request.META:
160 x = request.META['HTTP_REFERER']
161 if request.user.is_authenticated():
163 return HttpResponseRedirect(x)
165 return HttpResponseRedirect(x)
168 def login_view(request):
170 if 'HTTP_REFERER' in request.META:
171 x = request.META['HTTP_REFERER']
172 if Sandwich.objects.count() > 5:
173 sandwiches = Sandwich.objects.order_by('-date_made')[:5]
175 sandwiches = Sandwich.objects.order_by('-date_made')
177 username = request.POST['username']
178 password = request.POST['password']
179 user = authenticate(username=username, password=password)
183 return HttpResponseRedirect(x)
185 return HttpResponseRedirect(x)
187 return HttpResponseRedirect('login')
189 aform = AuthenticationForm()
190 return render_to_response('login.html', {'aform': aform,}, context_instance=RequestContext(request))
192 def login_view2(request):
194 if 'HTTP_REFERER' in request.META:
195 x = request.META['HTTP_REFERER']
196 if Sandwich.objects.count() > 5:
197 sandwiches = Sandwich.objects.order_by('-date_made')[:5]
199 sandwiches = Sandwich.objects.order_by('-date_made')
201 username = request.POST['username']
202 password = request.POST['password']
203 user = authenticate(username=username, password=password)
207 return HttpResponseRedirect(x)
209 return HttpResponseRedirect(x)
211 return HttpResponseRedirect('login')
213 aform = AuthenticationForm()
214 return render_to_response('pleaselogin.html', {'aform': aform,}, context_instance=RequestContext(request))
217 def create_user(request):
218 if Sandwich.objects.count() > 5:
219 sandwiches = Sandwich.objects.order_by('-date_made')[:5]
221 sandwiches = Sandwich.objects.order_by('-date_made')
222 if request.user.is_authenticated():
223 return HttpResponseRedirect('index')
224 elif request.method == 'POST': # If the form has been submitted...
225 form = NewUserForm(request.POST) # A form bound to the POST data
226 if form.is_valid(): # All validation rules pass
227 username = form.cleaned_data['username']
228 first_name = form.cleaned_data['first_name']
229 last_name = form.cleaned_data['last_name']
230 password = form.cleaned_data['password']
231 cpassword = form.cleaned_data['confirm_password']
232 email = form.cleaned_data['email']
233 if password == cpassword:
234 user = User.objects.create_user(username, email, password)
236 user.first_name = first_name
237 user.last_name = last_name
239 return HttpResponseRedirect('index')
241 return HttpResponseRedirect('signup')
243 form = NewUserForm() # An unbound form
244 return render_to_response('newuser.html', {'cform': form,}, context_instance=RequestContext(request))
247 def comment_posted(request):
249 comment_id = request.GET['c']
250 com = Comment.objects.get( pk = comment_id )
251 post = com.content_object
253 return HttpResponseRedirect( post.get_absolute_url() + '#comments' )
256 def ajaxfun(request):
257 if request.method == 'GET':
258 if 'q' in request.GET:
259 query = request.GET['q']
260 ingredients = Ingredient.objects.filter(name__icontains=query).order_by('name')
263 for i in ingredients:
264 responselist.append({'id': str(i.pk), 'name': i.name})
268 responselist.append({'id': 'new:' + query, 'name': query})
269 response = json.dumps(responselist)
270 return HttpResponse(response)
272 return HttpResponse('{}')