politikorama/app/controller/admin/entity.py

119 lines
4.5 KiB
Python

# encoding: utf-8
from datetime import datetime
from flask import current_app, g, request, redirect, url_for
from flask_admin import BaseView, expose
from slugify import slugify
from app.form.admin import ImportForm
from app.controller.admin.importer import default_import
from app.model.entity import EntityModel
from app.model.type import TypeModel
class ImportEntityView(BaseView):
@expose("/", methods=["GET", "POST"])
def index(self):
g.what = {
"title": "Import entities",
"description": "Importing entities will add unknown ones and update"
" known ones. If an entity is not present in imported file it"
" will not be deleted.",
"endpoint": "entities.index",
"formats": [
"File format accepted is CSV (Comma Separated Values).",
"First line represents column headers.",
"Other lines are values:",
"One column MUST be 'name'",
"One column MUST be 'reference'",
"One column MUST be 'parent_reference'",
"One column MUST be 'start'",
"One column COULD be 'end'",
"One column COULD be 'source'",
"One column COULD be 'type_code'",
"One column COULD be 'type_name'",
],
"examples": [
"name,reference,parent_reference,source,type_code,type_name,start,end",
"Thingy Commission,CO1234,NA123,French National Assembly,COMM,Commission,2020-01-01,",
"Group Assembly,NA123,,French National Assembly,GROU,Group,2019-01-01,2099-12-31",
],
}
headers = {
"name": None,
"reference": None,
"parent_reference": None,
"source": "optional",
"type_code": "optional",
"type_name": "optional",
"start": None,
"end": "optional",
}
reader = default_import(headers)
if len(g.errors) == 0 and reader is not None:
for row in reader:
if headers["type_code"] is not None:
entity_type = TypeModel.query.filter_by(
code=row[headers["type_code"]].upper(),
).first()
if entity_type is None:
entity_type = TypeModel(
code=row[headers["type_code"]].upper(),
name=row[headers["type_name"]],
slug=slugify(row[headers["type_name"]]),
)
entity_type.save()
if headers["source"] is not None:
source = row[headers["source"]]
else:
source = None
if row[headers["start"]] != "":
start = datetime.strptime(
row[headers["start"]], "%Y-%m-%d"
).date()
else:
start = None
end = None
if headers["end"] is not None:
if row[headers["end"]] != "":
end = datetime.strptime(
row[headers["end"]], "%Y-%m-%d"
).date()
parent = EntityModel.query.filter_by(
reference=row[headers["parent_reference"]].upper(),
).first()
if parent is not None:
parent_id = parent.id
else:
parent_id = None
entity = EntityModel.query.filter_by(
reference=row[headers["reference"]].upper()
).first()
if entity is None:
entity = EntityModel(
name = row[headers["name"]],
slug = slugify(row[headers["name"]]),
reference = row[headers["reference"]].upper(),
parent_id = parent_id,
source = source,
type = entity_type,
start = start,
end = end,
active = True if end is None else False,
)
g.messages.append(
f"{row[headers['name']]} added."
)
entity.save()
else:
# updating information ?
pass
return self.render("admin/import.html")