mirror of
				https://gitlab.crans.org/bde/nk20
				synced 2025-10-30 23:39:54 +01:00 
			
		
		
		
	Add manage ingredient feature, fix some bug
This commit is contained in:
		| @@ -39,6 +39,11 @@ SPDX-License-Identifier: GPL-3.0-or-later | ||||
| 	<a class="btn btn-sm btn-primary" href="{% url "food:add_ingredient" pk=food.pk %}"> | ||||
| 	  {% trans "Add to a meal" %} | ||||
| 	</a> | ||||
|       {% endif %} | ||||
|       {% if manage_ingredients %} | ||||
|         <a class="btn btn-sm btn-secondary" href="{% url "food:manage_ingredients" pk=food.pk %}"> | ||||
| 	  {% trans "Manage ingredients" %} | ||||
| 	</a> | ||||
|       {% endif %} | ||||
| 	<a class="btn btn-sm btn-primary" href="{% url "food:food_list" %}"> | ||||
| 	  {% trans "Return to the food list" %} | ||||
|   | ||||
							
								
								
									
										116
									
								
								apps/food/templates/food/manage_ingredients.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										116
									
								
								apps/food/templates/food/manage_ingredients.html
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,116 @@ | ||||
| {% extends "base.html" %} | ||||
| {% comment %} | ||||
| Copyright (C) 2018-2025 by BDE ENS Paris-Saclay | ||||
| SPDX-License-Identifier: GPL-3.0-or-later | ||||
| {% endcomment %} | ||||
| {% load i18n crispy_forms_tags %} | ||||
|  | ||||
| {% block content %} | ||||
| <div class="card bg-white mb-3"> | ||||
|   <h3 class="card-header text-center"> | ||||
|     {{ title }} | ||||
|   </h3> | ||||
|   <div class="card-body" id="form"></div> | ||||
|   <form method="post" action=""> | ||||
|     {% csrf_token %} | ||||
|     <table class="table table-condensed table-striped"> | ||||
|       {# Fill initial data #} | ||||
|       {% for display, form in formset %} | ||||
|       {% if forloop.first %} | ||||
|       <thead> | ||||
| 	<tr> | ||||
| 	  <th>{{ form.name.label }}</th> | ||||
| 	  <th>{{ form.qrcode.label }}</th> | ||||
| 	  <th>{{ form.fully_used.label }}</th> | ||||
| 	</tr> | ||||
|       </thead> | ||||
|       <tbody id="form_body"> | ||||
| 	{% endif %} | ||||
| 	{% if display %} | ||||
| 	<tr class="row-formset ingredients"> | ||||
| 	{% else %} | ||||
| 	<tr class="row-formset ingredients" style="display: none"> | ||||
| 	{% endif %} | ||||
| 	  <td>{{ form.name }}</td> | ||||
| 	  <td>{{ form.qrcode }}</td> | ||||
| 	  <td>{{ form.fully_used }}</td> | ||||
| 	</tr> | ||||
|       {% endfor %} | ||||
|       </tbody> | ||||
|     </table> | ||||
|  | ||||
|     {# Display buttons to add and remove ingredients #} | ||||
|     <div class="card-body"> | ||||
|       <div class="btn-group btn-block" role="group"> | ||||
| 	<button type="button" id="add_more" class="btn btn-success">{% trans "Add ingredient" %}</button> | ||||
| 	<button type="button" id="remove_one" class="btn btn-danger">{% trans "Remove ingredient" %}</button> | ||||
|       </div> | ||||
|     <button class="btn btn-primary" type="submit">{% trans "Submit"%}</button> | ||||
|     </div> | ||||
|   </form> | ||||
| </div> | ||||
|  | ||||
| {% endblock %} | ||||
| {% block extrajavascript %}  | ||||
| <script> | ||||
| /* script that handles add and remove lines */ | ||||
|  | ||||
| const foods = {{ ingredients | safe }}; | ||||
|  | ||||
| function set_ingredient_id () { | ||||
| 	let ingredients = document.getElementsByClassName('ingredients'); | ||||
| 	for (var i = 0; i < ingredients.length; i++) { | ||||
| 		ingredients[i].id = 'ingredients-' + parseInt(i); | ||||
| 	}; | ||||
| } | ||||
| set_ingredient_id(); | ||||
|  | ||||
| function prepopulate () { | ||||
| 	for (var i = 0; i < {{ ingredients_count }}; i++) { | ||||
| 		let prefix = 'id_form-' + parseInt(i) + '-'; | ||||
| 		document.getElementById(prefix + 'name_pk').value = parseInt(foods[i]['food_pk']); | ||||
| 		document.getElementById(prefix + 'name').value = foods[i]['food_name']; | ||||
| 		document.getElementById(prefix + 'qrcode_pk').value = parseInt(foods[i]['qr_pk']); | ||||
| 		if (foods[i]['qr_number'] === '') { | ||||
| 			document.getElementById(prefix + 'qrcode').value = ''; | ||||
| 		} | ||||
| 		else { | ||||
| 		document.getElementById(prefix + 'qrcode').value = parseInt(foods[i]['qr_number']); | ||||
| 		}; | ||||
| 		document.getElementById(prefix + 'fully_used').checked = Boolean(foods[i]['fully_used']); | ||||
| 	}; | ||||
| } | ||||
| prepopulate(); | ||||
|  | ||||
| function delete_form_data (form_id) { | ||||
| 	let prefix = "id_form-" + parseInt(form_id) + "-"; | ||||
| 	document.getElementById(prefix + "name_pk").value = ""; | ||||
| 	document.getElementById(prefix + "name").value = ""; | ||||
| 	document.getElementById(prefix + "qrcode_pk").value = ""; | ||||
| 	document.getElementById(prefix + "qrcode").value = ""; | ||||
| 	document.getElementById(prefix + "fully_used").checked = true; | ||||
| } | ||||
| var form_count = {{ ingredients_count }} + 1; | ||||
|  | ||||
| $('#add_more').click(function () { | ||||
| 	let ingredient_form = document.getElementById('ingredients-' + parseInt(form_count)); | ||||
| 	if (ingredient_form === null) { | ||||
| 		addMsg(gettext("You can't add more ingredient"), "danger",  5000); | ||||
| 		return;}; | ||||
| 	ingredient_form.style = "display: true"; | ||||
| 	form_count += 1; | ||||
| }); | ||||
|    | ||||
| $('#remove_one').click(function () { | ||||
| 	let ingredient_form = document.getElementById('ingredients-' + parseInt(form_count - 1)); | ||||
| 	if (ingredient_form === null) { | ||||
| 		return;}; | ||||
| 	ingredient_form.style = "display: none"; | ||||
| 	delete_form_data(form_count - 1); | ||||
| 	form_count -= 1; | ||||
| }); | ||||
|  | ||||
| addMsg(gettext("Add ingredient with their name or their qrcode, if two different priority is given to qrcode"), "warning");  | ||||
|  | ||||
| </script> | ||||
| {% endblock %} | ||||
							
								
								
									
										87
									
								
								apps/food/templates/food/transformedfood_update.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										87
									
								
								apps/food/templates/food/transformedfood_update.html
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,87 @@ | ||||
| {% extends "base.html" %} | ||||
| {% comment %} | ||||
| Copyright (C) by BDE ENS Paris-Saclay | ||||
| SPDX-License-Identifier: GPL-3.0-or-later | ||||
| {% endcomment %} | ||||
| {% load i18n crispy_forms_tags %} | ||||
|  | ||||
| {% block content %} | ||||
| <div class="card bg-white mb-3"> | ||||
|   <h3 class="card-header text-center"> | ||||
|     {{ title }} | ||||
|   </h3> | ||||
|   <div class="card-body" id="form"> | ||||
|     <form method="post"> | ||||
|       {% csrf_token %} | ||||
|       {{ form | crispy }} | ||||
|       <table class="table table-condensed table-striped"> | ||||
|         {# Fill initial data #} | ||||
|         {% for ingredient_form in formset %} | ||||
|         {% if forloop.first %} | ||||
|         <thead> | ||||
|           <tr> | ||||
| 	    <th>{% trans "Name" %}</th> | ||||
| 	    <th>{% trans "QR-code number" %}</th> | ||||
| 	    <th>{% trans "Fully used" %}<th> | ||||
|           </tr> | ||||
|         </thead> | ||||
|         <tbody id="form_body"> | ||||
|         {% endif %} | ||||
|         <tr class="row-formset"> | ||||
| 		{{ ingredient_form | crispy }} | ||||
|           <td>{{ ingredient_form.name }}</td> | ||||
| 	  <td>{{ ingredient_form.qrcode }}</td> | ||||
| 	  <td>{{ ingredient_form.fully_used }}</td> | ||||
|         </tr> | ||||
|         {% endfor %} | ||||
|         </tbody> | ||||
|       </table> | ||||
|       {# Display buttons to add and remove products #} | ||||
|       <div class="card-body"> | ||||
|         <div class="btn-group btn-block" role="group"> | ||||
|           <button type="button" id="add_more" class="btn btn-success">{% trans "Add ingredient" %}</button> | ||||
| 	  <button type="button" id="remove_one" class="btn btn-danger">{% trans "Remove ingredient" %}</button> | ||||
|         </div> | ||||
|         <button type="submit" class="btn btn-block btn-primary">{% trans "Submit" %}</button> | ||||
|       </div> | ||||
|     </form> | ||||
|   </div> | ||||
| </div> | ||||
|  | ||||
| {# Hidden div that store an empty product form, to be copied into new forms #} | ||||
| <div id="empty_form" style="display: none;"> | ||||
|     <table class='no_error'> | ||||
|         <tbody id="for_real"> | ||||
|             <tr class="row-formset"> | ||||
|                 <td>{{ formset.empty_form.name }}</td> | ||||
| 		<td>{{ formset.empty_form.qrcode }}</td> | ||||
| 		<td>{{ formset.empty_form.fully_used }}</td> | ||||
|             </tr> | ||||
|         </tbody> | ||||
|     </table> | ||||
| </div> | ||||
| {% endblock %} | ||||
| {% block extrajavascript %} | ||||
| <script> | ||||
|     /* script that handles add and remove lines */ | ||||
|     IDS = {}; | ||||
|  | ||||
|     $("#id_form-TOTAL_FORMS").val($(".row-formset").length - 1); | ||||
|  | ||||
|     $('#add_more').click(function () { | ||||
|         let form_idx = $('#id_form-TOTAL_FORMS').val(); | ||||
|         $('#form_body').append($('#for_real').html().replace(/__prefix__/g, form_idx)); | ||||
|         $('#id_form-TOTAL_FORMS').val(parseInt(form_idx) + 1); | ||||
|         $('#id_form-' + parseInt(form_idx) + '-id').val(IDS[parseInt(form_idx)]); | ||||
|     }); | ||||
|  | ||||
|     $('#remove_one').click(function () { | ||||
|         let form_idx = $('#id_form-TOTAL_FORMS').val(); | ||||
|         if (form_idx > 0) { | ||||
|             IDS[parseInt(form_idx) - 1] = $('#id_form-' + (parseInt(form_idx) - 1) + '-id').val(); | ||||
|             $('#form_body tr:last-child').remove(); | ||||
|             $('#id_form-TOTAL_FORMS').val(parseInt(form_idx) - 1); | ||||
|         } | ||||
|     }); | ||||
| </script> | ||||
| {% endblock %} | ||||
		Reference in New Issue
	
	Block a user