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, UserCreationForm
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
17 from django.contrib.auth.decorators import login_required
19 def sidebar_context(request):
20 x = Sandwich.objects.order_by('-date_made')
25 monthly = Sandwich.objects.dates('date_made', 'month')
26 return {'sandwiches': sandwiches, 'monthly': monthly, 'user': request.user, 'media_url': MEDIA_URL }
30 def add_sandwich(request):
31 if request.method == 'POST': # If the form has been submitted...
32 form = SandwichForm(request.POST, request.FILES) # A form bound to the POST data
33 if form.is_valid(): # All validation rules pass
34 newsandwich = form.save(commit=False)
35 newsandwich.user = request.user
37 x = request.POST['ing']
42 newsandwich.ingredients.add(Ingredient.objects.get(id=n))
43 elif n[:4] == 'new:' and len(n) > 4:
45 newingredient = Ingredient(name=n, slug=SlugifyUniquely(n, Ingredient))
47 newsandwich.ingredients.add(newingredient)
49 return HttpResponseRedirect(newsandwich.get_absolute_url())
51 form = SandwichForm(initial={'user': request.user}) # An unbound form
52 return render_to_response('sandwich.html', {'sform': form,}, context_instance=RequestContext(request))
56 def edit_sandwich(request, slug):
58 sedit = Sandwich.objects.get(slug=slug)
59 except Sandwich.DoesNotExist:
61 ingred = sedit.ingredients.all()
62 if not sedit.user == request.user:
63 return render_to_response('nopermission.html', context_instance=RequestContext(request))
65 if request.method == 'POST':
66 sform = SandwichForm(request.POST, request.FILES, instance=sedit)
67 if sform.is_valid(): # All validation rules pass
68 sedit.adjective = request.POST['adjective']
69 sedit.date_made = request.POST['date_made']
70 sedit.notes = request.POST['notes']
71 for ig in sedit.ingredients.all():
72 sedit.ingredients.remove(ig)
73 if request.POST['picture']:
74 sedit.picture = request.POST['picture']
75 x = request.POST['ing']
80 sedit.ingredients.add(Ingredient.objects.get(id=n))
81 elif n[:4] == 'new:' and len(n) > 4:
83 newingredient = Ingredient(name=n, slug=SlugifyUniquely(n, Ingredient))
85 sedit.ingredients.add(newingredient)
87 return HttpResponseRedirect(sedit.get_absolute_url())
89 sform = SandwichForm(instance=sedit)
90 return render_to_response('editsandwich.html', {'sform': sform, 's':sedit, 'prepop': ingred, }, context_instance=RequestContext(request))
94 def del_sandwich(request, slug):
95 if Sandwich.objects.get(slug=slug):
96 del_sandwich = Sandwich.objects.get(slug=slug)
97 if request.user == del_sandwich.user:
99 x = request.user.username
100 return HttpResponseRedirect(reverse('sandwich_user', kwargs={'username': x,}))
105 def all_sandwich(request):
107 allsandwiches = Sandwich.objects.order_by('-date_made')
108 except Sandwich.DoesNotExist:
110 return render_to_response('allsandwiches.html', {'allsandwiches': allsandwiches,}, context_instance=RequestContext(request))
112 def index_sandwich(request):
114 allsandwiches = Sandwich.objects.order_by('adjective')
115 except Sandwich.DoesNotExist:
117 return render_to_response('indexsandwiches.html', {'allsandwiches': allsandwiches,}, context_instance=RequestContext(request))
120 def sandwich_month(request, year, month):
122 ms = Sandwich.objects.filter(date_made__month=month, date_made__year=year)
125 except Sandwich.DoesNotExist:
127 return render_to_response('monthsandwiches.html', {'allsandwiches': ms, 'xdate': xdate, }, context_instance=RequestContext(request))
130 def current_home(request):
131 temp = Sandwich.objects.order_by('-date_made')[0]
132 curr_month = temp.date_made.month
133 curr_year = temp.date_made.year
135 ms = Sandwich.objects.filter(date_made__month=curr_month, date_made__year=curr_year)
136 except Sandwich.DoesNotExist:
138 return render_to_response('allsandwiches.html', {'allsandwiches': ms,}, context_instance=RequestContext(request))
141 def specific_sandwich(request, slug):
143 s = Sandwich.objects.get(slug=slug)
144 except Sandwich.DoesNotExist:
146 return render_to_response('onesandwich.html', {'s': s,}, context_instance=RequestContext(request))
148 def logout_view(request):
150 if 'HTTP_REFERER' in request.META:
151 x = request.META['HTTP_REFERER']
152 if request.user.is_authenticated():
154 return HttpResponseRedirect(x)
156 return render_to_response('notloggedin.html', context_instance=RequestContext(request))
159 def login_view(request):
160 if request.user.is_authenticated():
161 return render_to_response('loggedin.html', context_instance=RequestContext(request))
163 redirect_to = reverse('index')
164 if 'next' in request.GET:
165 redirect_to = request.GET['next']
167 if request.method == 'POST':
168 aform = AuthenticationForm(data=request.POST)
170 login(request, aform.get_user())
171 return HttpResponseRedirect(redirect_to)
173 aform = AuthenticationForm()
175 return render_to_response('login.html', {'aform': aform,}, context_instance=RequestContext(request))
178 def create_user(request):
179 if request.user.is_authenticated():
180 return render_to_response('loggedin.html', context_instance=RequestContext(request))
182 redirect_to = reverse('index')
183 # if 'next' in request.GET:
184 # redirect_to = request.GET['next']
186 if request.method == 'POST':
187 aform = UserCreationForm(data=request.POST)
190 x = authenticate(username=aform.cleaned_data['username'], password=aform.cleaned_data['password1'])
192 return render_to_response('usercreated.html', context_instance=RequestContext(request))
194 aform = UserCreationForm()
196 return render_to_response('newuser.html', {'aform': aform,}, context_instance=RequestContext(request))
199 def sandwich_user(request, username):
200 user = User.objects.get(username = username)
202 ms = Sandwich.objects.filter(user = user)
203 except Sandwich.DoesNotExist:
205 return render_to_response('usersandwiches.html', {'allsandwiches': ms,}, context_instance=RequestContext(request))
208 def edit_user(request, gusername):
209 guser = User.objects.get(username = gusername)
210 if not request.user.is_authenticated():
211 return HttpResponseRedirect(reverse('login'))
212 elif request.user == guser:
213 if request.method == 'POST': # If the form has been submitted...
214 form = NewUserForm(request.POST) # A form bound to the POST data
216 username = request.POST['username']
217 first_name = request.POST['first_name']
218 last_name = request.POST['last_name']
219 password = request.POST['password']
220 cpassword = request.POST['confirmpassword']
221 email = request.POST['email']
222 guser.username = username
223 guser.first_name = first_name
224 guser.last_name = last_name
226 guser.set_password(password)
229 return render_to_response('userchanged.html', context_instance=RequestContext(request))
231 return render_to_response('edituser.html', {'cform': form,}, context_instance=RequestContext(request))
233 form = NewUserForm(initial={'username': guser.username, 'first_name': guser.first_name, 'last_name': guser.last_name, 'email': guser.email, }) # An unbound form
234 return render_to_response('edituser.html', {'cform': form,}, context_instance=RequestContext(request))
236 return render_to_response('nopermission.html', context_instance=RequestContext(request))
239 def comment_posted(request):
241 comment_id = request.GET['c']
242 com = Comment.objects.get( pk = comment_id )
243 post = com.content_object
245 return HttpResponseRedirect( post.get_absolute_url() + '#comments' )
248 def ajaxfun(request):
249 if request.method == 'GET':
250 if 'q' in request.GET:
251 query = request.GET['q']
252 ingredients = Ingredient.objects.filter(name__icontains=query).order_by('name')
255 for i in ingredients:
256 responselist.append({'id': str(i.pk), 'name': i.name})
260 responselist.append({'id': 'new:' + query, 'name': query})
261 response = json.dumps(responselist)
262 return HttpResponse(response)
264 return HttpResponse('{}')