Files
@ 6e11735c3a45
Branch filter:
Location: django-shaker/djangoshaker/views.py - annotation
6e11735c3a45
3.3 KiB
text/x-python
Added some missing headings. Some small styling changes. The ingredient page now lists the recipes the ingredient is used in as well.
75f3e26f1f32 75f3e26f1f32 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 75f3e26f1f32 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 11c89784b7d7 11c89784b7d7 11c89784b7d7 53c11e89c145 11c89784b7d7 11c89784b7d7 11c89784b7d7 11c89784b7d7 11c89784b7d7 11c89784b7d7 11c89784b7d7 cdf95f86ba07 cdf95f86ba07 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 c796135bb2e9 c796135bb2e9 c796135bb2e9 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 53c11e89c145 | from django.views.generic import TemplateView
# Remove after testing
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.core.urlresolvers import reverse
from djangoshaker.models import Brand, Recipe, Ingredient
class IndexView(TemplateView):
"""
Custom view used for rendering the index page.
Contexts provided:
recipes_added - List of last added recipes.
recipes_modified - List of last modified recipes.
brands_added - List of last added brands.
brands_modified - List of last modified brands.
@TODO: Currently implementing only return of _all_ recipes. Extend the
recipe model to cater for this, and figure out modified stuff.
"""
template_name = 'djangoshaker/index.html'
def get_context_data(self, **kwargs):
"""
Overrides the default context data retrieval since we return multiple
contexts for the index view.
"""
context = super(IndexView, self).get_context_data(**kwargs)
context['recipes_added'] = Recipe.latest_additions()
context['recipes_modified'] = Recipe.latest_changes()
context['brands_added'] = Brand.latest_additions()
context['brands_modified'] = Brand.latest_changes()
return context
class UserView(TemplateView):
template_name = "djangoshaker/user.html"
def get_context_data(self, **kwargs):
context = super(UserView, self).get_context_data(**kwargs)
if 'user_ingredients' in self.request.session:
context['user_ingredients'] = Ingredient.objects.filter(pk__in = self.request.session['user_ingredients']).order_by('name')
context['ingredients'] = Ingredient.objects.exclude(pk__in = self.request.session['user_ingredients']).order_by('name')
user_ingredients = set(self.request.session['user_ingredients'])
available_recipes = []
for recipe in Recipe.objects.all():
recipe_ingredients = set([recing.ingredient_id for recing in recipe.recipeingredient_set.all()])
if recipe_ingredients.issubset(user_ingredients):
available_recipes.append(recipe)
context['available_recipes'] = available_recipes
else:
context['ingredients'] = Ingredient.objects.all()
return context
def add_user_ingredient(request, ingredient_id):
ingredient = get_object_or_404(Ingredient, pk = ingredient_id)
if 'user_ingredients' not in request.session:
request.session['user_ingredients'] = [ int(ingredient_id) ]
elif not request.session['user_ingredients']:
request.session['user_ingredients'] = [ int(ingredient_id) ]
elif not ingredient_id in request.session['user_ingredients']:
tmp_ings = request.session['user_ingredients']
tmp_ings.append(int(ingredient_id))
tmp_ings.sort()
request.session['user_ingredients'] = tmp_ings
return redirect(reverse('user'), request)
def remove_user_ingredient(request, ingredient_id):
if int(ingredient_id) in request.session['user_ingredients']:
tmp_ings = request.session['user_ingredients']
tmp_ings.remove(int(ingredient_id))
request.session['user_ingredients'] = tmp_ings
return redirect(reverse('user'), request)
|