Changeset - 57edd5668912
[Not reviewed]
default
0 4 3
Branko Majic (branko) - 13 years ago 2013-02-24 17:20:01
branko@majic.rs
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.
7 files changed with 125 insertions and 36 deletions:
0 comments (0 inline, 0 general)
djangoshaker/static/custom.css
Show inline comments
 
.btn-fixed {
 
    width: 98% !important;
 
    text-align: left;
 
}
 
\ No newline at end of file
 
}
 

	
 
.btn-opaque {
 
    opacity: 0.3;
 
}
 

	
 
.btn-opaque:hover {
 
    opacity: 1;
 
}
 

	
djangoshaker/templates/djangoshaker/ingredient_selection.html
Show inline comments
 
new file 100644
 
{% load djangoshaker_html_helpers %}
 

	
 
<ul class="unstyled">
 
  {% for ingredient in ingredients %}
 
    {% if ingredient in user_ingredients %}
 
      <li><a href="{% url remove_user_ingredient ingredient.id %}?next={{request.get_full_path|urlencode}}" class="btn btn-primary btn-mini"><i class="icon-ok-sign icon-white"></i></a>{% html_link 'ingredient' ingredient.name ingredient.id "btn btn-link" %}</li>
 
    {% else %}
 
      <li><a href="{% url add_user_ingredient ingredient.id %}?next={{request.get_full_path|urlencode}}" class="btn btn-primary btn-opaque btn-mini"><i class="icon-ok-sign icon-white"></i></a>{% html_link 'ingredient' ingredient.name ingredient.id "btn btn-link" %}</li>
 
    {% endif %}
 
  {% comment %}
 
  <li>{% html_link 'add_user_ingredient' ingredient.name ingredient.id "btn btn-link btn-fixed" %}</li>
 
  {% endcomment %}
 
  {% endfor %}
 
</ul>
djangoshaker/templates/djangoshaker/pagination.html
Show inline comments
 
new file 100644
 
<div class="pagination">
 
  <ul>
 
    {% if page_obj.has_previous %}
 
    <li><a href="?{{page_param|default:"page"}}={{page_obj.previous_page_number}}{% if extra_param %}&{{extra_param}}{% endif %}">Prev</a></li>
 
    {% else %}
 
    <li class="disabled"><span>Prev</span></li>
 
    {% endif %}
 
    <li class="active"><span>Page {{page_obj.number}} of {{page_obj.paginator.num_pages}}</span></li>
 
    {% if page_obj.has_next %}
 
    <li><a href="?{{page_param|default:"page"}}={{page_obj.next_page_number}}{% if extra_param %}&{{extra_param}}{% endif %}">Next</a></li>
 
    {% else %}
 
    <li class="disabled"><span>Next</span></li>
 
    {% endif %}
 
  </ul>
 
</div>
djangoshaker/templates/djangoshaker/user.html
Show inline comments
 
@@ -3,40 +3,30 @@
 
{% load djangoshaker_html_helpers %}
 

	
 
{% block content %}
 
<div class="row">
 

	
 
  <div class="span6">  
 
{% if available_recipes %}
 
<h1>Drinks that You Can Make</h1>
 

	
 
<h1>Your Drinks</h1>
 
<ul class="unstyled">
 
  {% for recipe in available_recipes %}
 
  <li>{% html_link 'recipe' recipe.name recipe.id "btn btn-link" %}</li>
 
  {% endfor %}
 
</ul>
 

	
 
{% include "djangoshaker/pagination.html" with page_obj=available_recipes page_param="recipe_page" extra_param=ingredient_page_param %}
 
{% endif %}
 

	
 
{% if user_ingredients %}
 
<h1>Your Ingredients</h1>
 

	
 
<div>List of ingredients that you own. Click on the ingredient to remove it from the list.</div>
 
  </div>
 

	
 
<ul class="unstyled">
 
  {% for ingredient in user_ingredients %}
 
  <li>{% html_link 'remove_user_ingredient' ingredient.name ingredient.id "btn btn-link btn-fixed" %}</li>
 
  {% endfor %}
 
</ul>
 
  <div class="span6">
 
<h1>Your Ingredients</h1>
 
{% 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 %}
 
<a class="btn" href="{% url user_ingredient_list %}">Show all ingredients.</a>
 

	
 
{% if ingredients %}
 
<h1>Available Ingredients</h1>
 

	
 
<div>List of ingredients that are available. Click on the ingredient to add it to the list of ingredients you own.</div>
 
  </div>
 
</div>
 

	
 
<ul class="unstyled">
 
  {% for ingredient in ingredients %}
 
  <li>{% html_link 'add_user_ingredient' ingredient.name ingredient.id "btn btn-link btn-fixed" %}</li>
 
  {% endfor %}
 
</ul>
 
{% endif %}
 
{% endblock %}
 

	
djangoshaker/templates/djangoshaker/user_ingredient_list.html
Show inline comments
 
new file 100644
 
{% extends "djangoshaker/template.html" %}
 

	
 
{% load djangoshaker_html_helpers %}
 

	
 
{% block content %}
 
<h1>Ingredients</h1>
 
<div>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.</div>
 
{% 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
djangoshaker/urls.py
Show inline comments
 
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<ingredient_id>\d+)$',
 
        'add_user_ingredient',
 
        name='add_user_ingredient'
 
        'add_user_ingredient', name='add_user_ingredient'
 
        ),
 

	
 
    url(r'^user/remove_ingredient/(?P<ingredient_id>\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'),
 
    )
 

	
djangoshaker/views.py
Show inline comments
 
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)
 

	
0 comments (0 inline, 0 general)