Beispiel #1
0
def create_animal(project_id):
    """Adds a new animal to a project"""
    project = ProjectService().get(project_id)
    form = AnimalService().get_form(project)

    if form.validate_on_submit():
        gs = GroupService()
        group = gs.get(form.group.data)
        animal = AnimalService().new(number=form.number.data, age=form.age.data, gender=form.gender.data)
        gs.add_animal(group, animal)
        flash("Successfully added a new animal '%s'" % animal, "success")
        return redirect(url_for("view_project", project_id=project.id))

    if request.method == "POST":
        flash("Unable to add a new animal", "danger")

    return CreateAnimal(Project).render(form)
Beispiel #2
0
def import_animal_csv(project_id):
    """Imports new animals to a project form a csv file"""
    project = ProjectService().get(project_id)
    form = AnimalService().get_import_form(project)

    if form.validate_on_submit():
        csv_file = request.files["csv_file"]
        filename = secure_filename(csv_file.filename)
        csv_file_path = os.path.join(UPLOAD_FOLDER, filename)
        csv_file.save(csv_file_path)
        imported_animals, ignored_animal_data = AnimalService.import_animals_from_csv(project.groups, csv_file_path)
        if len(imported_animals) > 0:
            flash("Successfully imported %d animals" % len(imported_animals), "success")

        if len(ignored_animal_data) > 0:
            flash("Unable to import %d animals" % len(ignored_animal_data), "danger")

        os.remove(csv_file_path)
        return ImportAnimalResult(project).render(imported_animals, ignored_animal_data)

    return ImportAnimalCSV(project).render(form)