# HG changeset patch # User Branko Majic # Date 2013-02-24 17:20:01 # Node ID 57edd5668912951985fc79aa418aea9f3eb8592a # Parent b0046f7ebbb87fec738d99c5d29fed5e68588732 Added some custom CSS for buttons (selected/deselected ingredient). Moved the ingredient selection list into its own template (for inclusion). Moved the pagination code to its own template (since it's shared amongst multiple pages). The user page is now spanning two columns. User page now lists only available drinks and user's ingredients, both with pagination. Added new page that includes all the ingredients that are available where use can add or remove the ones he/she has. The add/remove user ingredients view can now redirect to a specific page if the 'next' parameter is provided to it as a GET paramter. diff --git a/djangoshaker/static/custom.css b/djangoshaker/static/custom.css --- a/djangoshaker/static/custom.css +++ b/djangoshaker/static/custom.css @@ -1,4 +1,13 @@ .btn-fixed { width: 98% !important; text-align: left; -} \ No newline at end of file +} + +.btn-opaque { + opacity: 0.3; +} + +.btn-opaque:hover { + opacity: 1; +} + diff --git a/djangoshaker/templates/djangoshaker/ingredient_selection.html b/djangoshaker/templates/djangoshaker/ingredient_selection.html new file mode 100644 --- /dev/null +++ b/djangoshaker/templates/djangoshaker/ingredient_selection.html @@ -0,0 +1,14 @@ +{% load djangoshaker_html_helpers %} + + diff --git a/djangoshaker/templates/djangoshaker/pagination.html b/djangoshaker/templates/djangoshaker/pagination.html new file mode 100644 --- /dev/null +++ b/djangoshaker/templates/djangoshaker/pagination.html @@ -0,0 +1,15 @@ + diff --git a/djangoshaker/templates/djangoshaker/user.html b/djangoshaker/templates/djangoshaker/user.html --- a/djangoshaker/templates/djangoshaker/user.html +++ b/djangoshaker/templates/djangoshaker/user.html @@ -3,40 +3,30 @@ {% load djangoshaker_html_helpers %} {% block content %} +
+
{% if available_recipes %} -

Drinks that You Can Make

- +

Your Drinks

    {% for recipe in available_recipes %}
  • {% html_link 'recipe' recipe.name recipe.id "btn btn-link" %}
  • {% endfor %}
- +{% include "djangoshaker/pagination.html" with page_obj=available_recipes page_param="recipe_page" extra_param=ingredient_page_param %} {% endif %} - -{% if user_ingredients %} -

Your Ingredients

- -
List of ingredients that you own. Click on the ingredient to remove it from the list.
+
- +
+

Your Ingredients

+{% if user_ingredients %} +{% include "djangoshaker/ingredient_selection.html" with ingredients=user_ingredients %} {% endif %} +{% include "djangoshaker/pagination.html" with page_obj=user_ingredients page_param="ingredient_page" extra_param=recipe_page_param %} +Show all ingredients. -{% if ingredients %} -

Available Ingredients

- -
List of ingredients that are available. Click on the ingredient to add it to the list of ingredients you own.
+
+
- -{% endif %} {% endblock %} diff --git a/djangoshaker/templates/djangoshaker/user_ingredient_list.html b/djangoshaker/templates/djangoshaker/user_ingredient_list.html new file mode 100644 --- /dev/null +++ b/djangoshaker/templates/djangoshaker/user_ingredient_list.html @@ -0,0 +1,14 @@ +{% extends "djangoshaker/template.html" %} + +{% load djangoshaker_html_helpers %} + +{% block content %} +

Ingredients

+
This is a full list of ingredients. You may select and deselect ingredients that are availble to you by clicking on the button next to each respective ingredient.
+{% if ingredients %} +{% include "djangoshaker/ingredient_selection.html" with ingredients=ingredients user_ingredients=user_ingredients %} +{% endif %} + +{% include "djangoshaker/pagination.html" %} + +{% endblock %} \ No newline at end of file diff --git a/djangoshaker/urls.py b/djangoshaker/urls.py --- a/djangoshaker/urls.py +++ b/djangoshaker/urls.py @@ -1,7 +1,7 @@ from django.conf.urls.defaults import * from django.views.generic import DetailView, ListView from djangoshaker.models import Brand, Recipe, Ingredient, Measure -from djangoshaker.views import IndexView, UserView +from djangoshaker.views import IndexView, UserView, UserIngredientListView urlpatterns = patterns( 'djangoshaker.views', @@ -59,8 +59,7 @@ urlpatterns = patterns( name="measure"), url(r'^user/add_ingredient/(?P\d+)$', - 'add_user_ingredient', - name='add_user_ingredient' + 'add_user_ingredient', name='add_user_ingredient' ), url(r'^user/remove_ingredient/(?P\d+)$', @@ -69,5 +68,12 @@ urlpatterns = patterns( ), url(r'^user/$', UserView.as_view(), name="user"), + + url(r'^user/ingredients/$', UserIngredientListView.as_view(model = Ingredient, + queryset = Ingredient.objects.all().order_by("name"), + template_name = 'djangoshaker/user_ingredient_list.html', + context_object_name = 'ingredients', + paginate_by = 15), + name='user_ingredient_list'), ) diff --git a/djangoshaker/views.py b/djangoshaker/views.py --- a/djangoshaker/views.py +++ b/djangoshaker/views.py @@ -1,7 +1,6 @@ -from django.views.generic import TemplateView +from django.views.generic import TemplateView, ListView -# Remove after testing -from django.http import HttpResponse +from django.core.paginator import Paginator from django.shortcuts import get_object_or_404, redirect @@ -49,10 +48,49 @@ class UserView(TemplateView): def get_context_data(self, **kwargs): context = super(UserView, self).get_context_data(**kwargs) + context['ingredients'] = Ingredient.objects.all().order_by('name') + + if 'user_ingredients' in self.request.session: + user_ingredients = Ingredient.objects.filter(pk__in = self.request.session['user_ingredients']).order_by('name') + user_ingredients_set = 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_set): + available_recipes.append(recipe) + + recipe_paginator = Paginator(available_recipes, 15) + recipe_page = self.request.GET.get('recipe_page') + if not recipe_page: + available_recipes = recipe_paginator.page(1) + else: + available_recipes = recipe_paginator.page(recipe_page) + + ingredient_paginator = Paginator(user_ingredients, 15) + ingredient_page = self.request.GET.get('ingredient_page') + if not ingredient_page: + user_ingredients = ingredient_paginator.page(1) + else: + user_ingredients = ingredient_paginator.page(ingredient_page) + + if recipe_page: + context['recipe_page_param']="recipe_page=" + recipe_page + if ingredient_page: + context['ingredient_page_param']="ingredient_page=" + ingredient_page + context['available_recipes'] = available_recipes + context['user_ingredients'] = user_ingredients + + return context + +class UserIngredientListView(ListView): + + template_name = "djangoshaker/user_ingredient_list.html" + + def get_context_data(self, **kwargs): + context = super(UserIngredientListView, 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(): @@ -60,8 +98,6 @@ class UserView(TemplateView): if recipe_ingredients.issubset(user_ingredients): available_recipes.append(recipe) context['available_recipes'] = available_recipes - else: - context['ingredients'] = Ingredient.objects.all() return context @@ -79,7 +115,8 @@ def add_user_ingredient(request, ingredi request.session['user_ingredients'] = tmp_ings - return redirect(reverse('user'), request) + redirect_page = request.GET.get('next', reverse('user') ) + return redirect(redirect_page, request) def remove_user_ingredient(request, ingredient_id): if int(ingredient_id) in request.session['user_ingredients']: @@ -87,4 +124,8 @@ def remove_user_ingredient(request, ingr tmp_ings.remove(int(ingredient_id)) request.session['user_ingredients'] = tmp_ings - return redirect(reverse('user'), request) + redirect_page = request.GET.get('from', 'user' ) + + redirect_page = request.GET.get('next', reverse('user') ) + return redirect(redirect_page, request) +