Commit a10d388f authored by Nicolas Seydoux's avatar Nicolas Seydoux
Browse files

Création d'un formulaire de saisie pour les denrées

Ce formulaire sert à s'assurer d'une certaine uniformité dans la saisie des denrées alimentaires.
parent 06d7db53
from enum import Enum
from copy import copy
import re
import json
# - Lien avec le dataset
# - code denrée
# - Données
# - Marque
# - Société
# - Site de fabrication
# - Dénomination légale de ventes
# - Ingrédients
# - Nom
# - Origine
# - Contient des OGM ?
# - Issus de l'agriculture biologique ?
# - Issu d'une pêche responsable ?
# - Métadonnées
# - Date de l'extraction
# - Personne ayant extrait les infos
class FoodAttributes(Enum):
DATE = "date"
NAME = "name"
CODE = "code"
BRAND = "brand"
FIRM = "firm"
MANUF_SITE="manufacturing_site"
LEGAL_NAME="legal_name"
INGREDIENTS="ingredients"
class IngredientAttribute(Enum):
PREFIX="ingredient"
NAME="Name"
ORIGIN="Origin"
GMO="GMO"
ORGANIC="Organic"
FAIR_FISHING="FairFishing"
class Ingredient:
def __init__(self, name="", origin="", has_gmo="", is_organic="", fair_fishing=""):
self.name=name
self.origin=origin
self.has_gmo=has_gmo
self.is_organic=is_organic
self.fair_fishing=fair_fishing
def to_map(self):
return {
IngredientAttribute.NAME.value:self.name, IngredientAttribute.ORIGIN.value:self.origin,
IngredientAttribute.GMO.value:self.has_gmo,
IngredientAttribute.ORGANIC.value:self.is_organic,
IngredientAttribute.FAIR_FISHING.value:self.fair_fishing}
def to_json(self):
return json.dumps(self.to_map())
class Datasheet:
def __init__(self, code="", brand="", company="", manufacturing_site = "", legal_name="", person="", date=""):
self.code=code
self.brand=brand
self.company=company
self.legal_name=legal_name
self.manufacturing_site = manufacturing_site
self.ingredients = []
self.person = person
self.date = date
def add_ingredient(self, i):
self.ingredients.append(i)
def to_json(self):
return json.dumps(
{FoodAttributes.CODE.value:self.code, FoodAttributes.BRAND.value:self.brand,
FoodAttributes.FIRM.value:self.company,
FoodAttributes.MANUF_SITE.value:self.manufacturing_site,
FoodAttributes.LEGAL_NAME.value:self.legal_name,
FoodAttributes.NAME.value:self.person,
FoodAttributes.DATE.value:self.date,
FoodAttributes.INGREDIENTS.value:[i.to_map() for i in self.ingredients]
}, ensure_ascii=True
)
def datasheet_from_form(form):
d = Datasheet()
# This is a temporary accumulator where the retrieved values are stored before being copied to the dataset, when all the fields have been replaced (counted with counter)
i = Ingredient()
# This counter is used to track whenever ingredients have been completely collected, and should be added to the datasheet
counter = 0
for field in form.keys():
if field == FoodAttributes.BRAND.value:
d.brand = form[field]
elif field == FoodAttributes.FIRM.value:
d.company = form[field]
elif field == FoodAttributes.CODE.value:
d.code = form[field]
elif field == FoodAttributes.LEGAL_NAME.value:
d.legal_name = form[field]
elif field == FoodAttributes.NAME.value:
d.extractor = form[field]
elif field == FoodAttributes.DATE.value:
d.extractor = form[field]
elif field.startswith(IngredientAttribute.PREFIX.value):
counter += 1
# If the field is for an ingredient, a regex is used to extract the exact field header
ing_re = re.match("ingredient\d+(.+)$", field)
if ing_re.group(1) == IngredientAttribute.NAME.value:
i.name = form[field]
elif ing_re.group(1) == IngredientAttribute.ORIGIN.value:
i.origin = form[field]
elif ing_re.group(1) == IngredientAttribute.GMO.value:
i.has_gmo=bool(form[field])
elif ing_re.group(1) == IngredientAttribute.ORGANIC.value:
i.is_organic=bool(form[field])
elif ing_re.group(1) == IngredientAttribute.FAIR_FISHING.value:
i.fair_fishing=bool(form[field])
# If all the fields of the ingredient have been parsed, it should be added to the datasheet list
if counter%len(IngredientAttribute) == 0:
d.add_ingredient(copy(i))
counter = 0
return d
#print(field+" : "+request.form[field])
from flask import Flask, request, render_template, url_for
import datasheet
app = Flask(__name__)
def process_form():
if "code" in request.form.keys():
print(request.form["code"])
d = datasheet.datasheet_from_form(request.form)
print(d.to_json())
with open(d.code+".json", "w") as target_file:
target_file.write(d.to_json())
return render_template('form.html', name=request.form['name'], date=request.form['date'])
@app.route('/', methods=['GET', 'POST'])
def receive_form():
url_for('static', filename='form.js')
if request.method == 'POST':
return process_form()
else:
return render_template('form.html', name="")
export FLASK_ENV=development
export FLASK_APP=input_form.py
python -m flask run
var counter=0;
$(function() {
$("#addIngredient").click(function(e) {
console.log("Coucou du JS");
e.preventDefault();
counter+=1;
$("#ingredientList").append("<li>Ingrédient "+counter+" : <ul>");
$("#ingredientList").append("<li>Nom : <input type='text' name='ingredient"+counter+"Name'/></li>");
$("#ingredientList").append("<li>Origine : <input type='text' name='ingredient"+counter+"Origin'/></li>");
$("#ingredientList").append("<li>Contient des OGM ? <input type='radio' name='ingredient"+counter+"GMO' value='true'/> Oui <input type='radio' name='ingredient"+counter+"GMO' value='false'/> Non </li>");
$("#ingredientList").append("<li>Issu de l'agriculture bio. <input type='radio' name='ingredient"+counter+"Organic' value='true'/> Oui <input type='radio' name='ingredient"+counter+"Organic' value='false'/> Non <input type='radio' name='ingredient"+counter+"Organic' value='na'/> N/A</li>");
$("#ingredientList").append("<li>Issu d'une pêche responsable ? <input type='radio' name='ingredient"+counter+"FairFishing' value='true'/> Oui <input type='radio' name='ingredient"+counter+"FairFishing' value='false'/> Non <input type='radio' name='ingredient"+counter+"FairFishing' value='na'/> N/A </li>");
$("#ingredientList").append("</ul></li>");
// $("#ingredientList").append("<li><input type="text" name="ingredientName[]"/></li>");
// $("#ingredientList").append("<li><input type='text' name='name[]' placeholder='Name' /></li>");
// $("#ingredientList").append("<li><input type='text' name='phone[]' placeholder='Phone' /></li>");
// $("#ingredientList").append("<li><input type='text' name='email[]' placeholder='E-Mail' /></li>");
});
});
This diff is collapsed.
<html>
<meta charset="UTF-8">
<script type="text/javascript" src="static/jquery.min.js" ></script>
<script type="text/javascript" src="static/form.js" ></script>
<title>Saisie fiches techniques keskonmange</title>
<!--
- Lien avec le dataset
- code denrée
- Données
- Marque
- Société
- Site de fabrication
- Dénomination légale de ventes
- Ingrédients
- Nom
- Origine
- Contient des OGM ?
- Issus de l'agriculture biologique ?
- Issu d'une pêche responsable ?
- Métadonnées
- Date de l'extraction
- Personne ayant extrait les infos-->
<form method="post" accept-charset="UTF-8">
<ul>
<li> Métadonnées : <ul>
<li>Date : <input type="date" name="date" value="{{ date }}"/></li>
<li>Nom : <input type="text" name="name" value="{{ name }}"/></li>
</ul></li>
<li>Données :
<ul>
<li>Code denrée : <input type="text" name="code"/></li>
<li>Marque : <input type="text" name="brand"/></li>
<li>Société : <input type="text" name="firm"/></li>
<li>Site de fabrication : <input type="text" name="manufacturing_site"/></li>
<li>Dénomination légale de vente : <input type="text" name="legal_name"/></li>
<li>Ingrédients <input type="button" id="addIngredient" value="+"/> :
<ul id="ingredientList">
</ul>
</li>
</ul>
<input type="submit" value="Submit"/>
</form>
</html>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment