Skip to content

Commit

Permalink
Merge pull request #325 from savoirfairelinux/feature/add-restriction…
Browse files Browse the repository at this point in the history
…s-support

Load and save properly restrictions and food prep
  • Loading branch information
manumilou authored Aug 11, 2016
2 parents c5cedc7 + 98351e6 commit c3e1791
Show file tree
Hide file tree
Showing 9 changed files with 281 additions and 42 deletions.
15 changes: 7 additions & 8 deletions django/santropolFeast/member/forms.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django import forms
from django.core.exceptions import ObjectDoesNotExist
from django.utils.translation import ugettext_lazy as _
from meal.models import Ingredient, Component, COMPONENT_GROUP_CHOICES
from meal.models import Ingredient, Component, COMPONENT_GROUP_CHOICES, \
Restricted_item
from order.models import SIZE_CHOICES
from member.models import (
Member, Client, RATE_TYPE, CONTACT_TYPE_CHOICES,
Member, Client, RATE_TYPE, CONTACT_TYPE_CHOICES, Option,
GENDER_CHOICES, PAYMENT_TYPE, DELIVERY_TYPE,
DAYS_OF_WEEK, Route,
)
Expand Down Expand Up @@ -171,18 +172,16 @@ def __init__(self, *args, **kwargs):

restrictions = forms.ModelMultipleChoiceField(
label=_("Restrictions"),
queryset=Ingredient.objects.all(),
queryset=Restricted_item.objects.all(),
required=False,
widget=forms.SelectMultiple(attrs={'class': 'ui dropdown search'})
)

food_preparation = forms.ModelMultipleChoiceField(
label=_("Preparation"),
queryset=Ingredient.objects.all(),
required=False,
widget=forms.SelectMultiple(
attrs={'class': 'ui dropdown search'}
)
queryset=Option.objects.filter(option_group='preparation'),
widget=forms.SelectMultiple(attrs={'class': 'ui dropdown'}),
)

ingredient_to_avoid = forms.ModelMultipleChoiceField(
Expand All @@ -195,7 +194,7 @@ def __init__(self, *args, **kwargs):
)

dish_to_avoid = forms.ModelMultipleChoiceField(
label=_("Dish To Avoid"),
label=_("Dish(es) To Avoid"),
queryset=Component.objects.all(),
required=False,
widget=forms.SelectMultiple(
Expand Down
39 changes: 36 additions & 3 deletions django/santropolFeast/member/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,15 +443,48 @@ def orders(self):
"""
Returns orders associated to this client
"""

return self.client_order.all()

@property
def notes(self):
def restrictions(self):
"""
Returns orders associated to this client
Returns restrictions associated to this client
"""
return Restriction.objects.filter(client=self.id)

@property
def food_preparation(self):
"""
Returns specific food preparation associated to this client
"""
return Client_option.objects.filter(
client=self.id,
option__option_group='preparation'
)

@property
def ingredients_to_avoid(self):
"""
Returns ingredients to avoid associated to this client
"""
return Client_avoid_ingredient.objects.filter(
client=self.id,
)

@property
def components_to_avoid(self):
"""
Returns component(s) to avoid associated to this client
"""
return Client_avoid_component.objects.filter(
client=self.id,
)

@property
def notes(self):
"""
Returns notes associated to this client
"""
return self.client_notes.all()

@property
Expand Down
2 changes: 1 addition & 1 deletion django/santropolFeast/member/templates/client/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ <h3 class="ui header">
{% trans 'Order' %}
</h3>
<div class="ui feed">
{% for order in client.client_order.all %}
{% for order in client.orders %}
<a class="event" href="/order/view/{{order.id}}/">
<div class="content">
<div class="date">
Expand Down
107 changes: 87 additions & 20 deletions django/santropolFeast/member/templates/client/view/allergies.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,107 @@

{% endblock %}

{% block extrajs %}

<script type="text/javascript">
// Generic code to delete any model related to the member
$('.ui.label .delete').on('click', function(){
var model = $(this).attr('data-model');
var id = $(this).attr('data-id');
var label = $(this).parent();
$(this).addClass('loading');
$.ajax({
url: '/member/' + model + '/' + id + '/delete/',
type: "POST",
data: {
'csrfmiddlewaretoken': '{{ csrf_token }}',
},
success: function(response) {
label.hide();
}
})
})

</script>

{% endblock %}


{% block subcontent %}

<div class="ui segment">

<h3 class="ui header">
{% trans 'Meal status' %}
<a href="{% url 'admin:member_client_change' client.id %}">
<i class="write grey icon"></i>
</a>
</h3>
<div class="ui orange label">{{client.get_status_display}}</div>
<h3 class="ui header">
{% trans 'Restrictions' %}
<a href="{% url 'admin:member_client_change' client.id %}">
<i class="write grey icon"></i>
</a>
</h3>
<div>
<h3 class="ui header">
{% trans 'Meal status' %}
<a href="{% url 'admin:member_client_change' client.id %}">
<i class="write grey icon"></i>
</a>
</h3>

<div class="ui yellow label">{{client.get_status_display}}</div>

<h3 class="ui header">
{% trans 'Restrictions' %}
<a href="{% url 'admin:member_client_change' client.id %}">
<i class="write grey icon"></i>
</a>
</h3>
{% for restriction in client.restrictions %}
<div class="ui label">
{% trans 'Tomato' %}
{{ restriction.restricted_item.name }}
<i class="delete icon" data-id={{restriction.id}} data-model="restriction"></i>
</div>
{% empty %}
<div class="ui row">
<em>Yipee! No restriction.</em>
</div>
{% endfor %}

<h3 class="ui header">
{% trans 'Food preparation' %}
<a href="{% url 'admin:member_client_change' client.id %}">
<i class="write grey icon"></i>
</a>
</h3>
{% for food_preparation in client.food_preparation %}
<div class="ui label">
{% trans 'Basil' %}
{{ food_preparation.option }}
<i class="delete icon" data-id={{food_preparation.id}} data-model="client_option"></i>
</div>
{% empty %}
<div class="ui row">
<em>Nope! Nothing specific.</em>
</div>
{% for restriction in client.restrictions.all %}
{% endfor %}

<h3 class="ui header">
{% trans 'Ingredients to avoid' %}
</h3>
{% for ingredient_to_avoid in client.ingredients_to_avoid %}
<div class="ui label">
{% trans 'Tomato' %}
{{ ingredient_to_avoid.ingredient.name }}
<i class="delete icon" data-id={{ingredient_to_avoid.id}} data-model="ingredient_to_avoid"></i>
</div>
{% empty %}
<div class="ui row">
<em>Yeah! Likes everything!</em>
</div>
{% endfor %}

<h3 class="ui header">
{% trans 'Dish(es) to avoid' %}
</h3>
{% for component_to_avoid in client.components_to_avoid %}
<div class="ui label">
{% trans 'Basil' %}
{{ component_to_avoid.component.name }}
<i class="delete icon" data-id={{component_to_avoid.id}} data-model="component_to_avoid"></i>
</div>
{% empty %}
<div class="ui row">
<em>Yeah! Likes everything!</em>
</div>
{% endfor %}
</div>
</div>


<div class="ui segment">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ <h3 class="ui header">
{% trans 'Order' %}
</h3>
<div class="ui feed">
{% for order in client.client_order.all %}
{% for order in client.orders %}
<a class="event" href="/order/view/{{order.id}}/">
<div class="content">
<div class="date">
Expand Down
2 changes: 1 addition & 1 deletion django/santropolFeast/member/templates/forms/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ <h2 class="ui header">New Client</h2>

{% include 'forms/step.html' with step=4 icon='payment' name=_('Payment') description=_('Payment method, card, ...') %}

{% include 'forms/step.html' with step=5 icon='food' name=_('Allergies') description=_('Restrictions, allergies, ...') %}
{% include 'forms/step.html' with step=5 icon='food' name=_('Preferences') description=_('Deliveries, restrictions, ...') %}

{% include 'forms/step.html' with step=6 icon='first aid' name=_('Emergency') description=_('Emergency contact information') %}

Expand Down
65 changes: 61 additions & 4 deletions django/santropolFeast/member/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.core.urlresolvers import reverse, reverse_lazy
from order.models import Order
from member.factories import RouteFactory
from meal.factories import IngredientFactory, ComponentFactory


class MemberEmptyContact(TestCase):
Expand Down Expand Up @@ -383,6 +384,14 @@ def setUpTestData(cls):
lastname='Member'
)
cls.route = RouteFactory()
cls.restricted_item_1 = Restricted_item.objects.create(
name='pork', restricted_item_group='meat')
cls.restricted_item_2 = Restricted_item.objects.create(
name='soya', restricted_item_group='other')
cls.food_preparation = Option.objects.create(
name='PUREE ALL', option_group='preparation')
cls.ingredient = IngredientFactory()
cls.component = ComponentFactory()

def setUp(self):
self.client.login(username=self.admin.username, password='test1234')
Expand Down Expand Up @@ -516,6 +525,11 @@ def test_form_save_data_all_different_members(self):
"dietary_restriction-delivery_type": "O",
"dietary_restriction-delivery_schedule": "monday",
"dietary_restriction-meal_default": "1",
"dietary_restriction-restrictions":
[self.restricted_item_1.id, self.restricted_item_2.id],
"dietary_restriction-food_preparation": self.food_preparation.id,
"dietary_restriction-ingredient_to_avoid": self.ingredient.id,
"dietary_restriction-dish_to_avoid": self.component.id,
"wizard_goto_step": ""
}

Expand Down Expand Up @@ -549,7 +563,9 @@ def test_form_save_data_all_different_members(self):
client = Client.objects.get(member=member)
self._test_assert_client_info_all_different_members(client)

# Test the client view
self._test_client_detail_view_all_different_members(client)
self._test_client_view_preferences(client)

def _test_assert_member_info_all_different_members(self, member):
# test firstname and lastname
Expand Down Expand Up @@ -643,9 +659,52 @@ def _test_assert_client_info_all_different_members(self, client):
"555-444-5555"
)

# test_restrictions
restriction_1 = Restriction.objects.get(
client=client, restricted_item=self.restricted_item_1)
restriction_2 = Restriction.objects.get(
client=client, restricted_item=self.restricted_item_2)
self.assertTrue(self.restricted_item_1.name in str(restriction_1))
self.assertTrue(self.restricted_item_2.name in str(restriction_2))

# Test food preparation
food_preparation = Client_option.objects.get(
client=client,
option=self.food_preparation
)
self.assertTrue(self.food_preparation.name in str(food_preparation))

# Test for ingredients to avoid
ingredients = Client_avoid_ingredient.objects.filter(
client=client.id,
)
self.assertTrue(self.ingredient.name in str(ingredients))

# Test for components to avoid
components = Client_avoid_component.objects.filter(
client=client.id,
)
self.assertTrue(self.component.name in str(components))

"""
Test that the meals preferences are properly displayed.
"""

def _test_client_view_preferences(self, client):
resp = self.client.get(
reverse_lazy('member:client_allergies', kwargs={'pk': client.id})
)

# self.assertContains(resp, client.get_status_display)
self.assertContains(resp, self.restricted_item_1)
self.assertContains(resp, self.restricted_item_2)
self.assertContains(resp, self.food_preparation)
self.assertContains(resp, self.ingredient.name)
self.assertContains(resp, self.component.name)

def _test_client_detail_view_all_different_members(self, client):
response = self.client.get(
reverse_lazy('member:view', kwargs={'pk': client.id})
reverse_lazy('member:client_information', kwargs={'pk': client.id})
)

self.assertTrue(b"User" in response.content)
Expand All @@ -655,7 +714,6 @@ def _test_client_detail_view_all_different_members(self, client):
self.assertTrue(b"H3C2C2" in response.content)
self.assertTrue(b"montreal" in response.content)
self.assertTrue(b"Testing alert message" in response.content)
self.assertTrue(b"Testing referral reason" in response.content)
self.assertTrue(b"555-444-5555" in response.content)

def test_form_save_data_same_members(self):
Expand Down Expand Up @@ -854,7 +912,7 @@ def _test_assert_client_info_same_members(self, client):

def _test_client_detail_view_same_members(self, client):
response = self.client.get(
reverse_lazy('member:view', kwargs={'pk': client.id})
reverse_lazy('member:client_information', kwargs={'pk': client.id})
)
self.assertTrue(b"User" in response.content)
self.assertTrue(b"Same" in response.content)
Expand All @@ -863,7 +921,6 @@ def _test_client_detail_view_same_members(self, client):
self.assertTrue(b"H8C6C8" in response.content)
self.assertTrue(b"Montreal" in response.content)
self.assertTrue(b"Testing alert message" in response.content)
self.assertTrue(b"Testing referral reason" in response.content)
self.assertTrue(b"514-868-8686" in response.content)

def _test_client_list_view_same_members(self):
Expand Down
Loading

0 comments on commit c3e1791

Please sign in to comment.