157 lines
6.9 KiB
Python
157 lines
6.9 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 sqlalchemy import or_
|
|
|
|
from app.form.admin import ImportForm
|
|
from app.controller.admin.importer import default_import
|
|
from app.model.country import CountryModel
|
|
from app.model.representative import RepresentativeModel
|
|
from app.model.source import SourceModel
|
|
from app.model.representative_origin import RepresentativeOriginModel
|
|
|
|
|
|
class ImportRepresentativeView(BaseView):
|
|
@expose("/", methods=["GET", "POST"])
|
|
def index(self):
|
|
g.what = {
|
|
"title": "Import representatives",
|
|
"description": "Importing representatives will add unknown ones and update"
|
|
" known ones. If a representative is not present in imported file it"
|
|
" will not be deleted.",
|
|
"endpoint": "representatives.index",
|
|
"formats": [
|
|
"File format accepted is CSV (Comma Separated Values).",
|
|
"First line represents column headers.",
|
|
"Other lines are values:",
|
|
"One column MUST be 'source'",
|
|
"One column MUST be 'reference'",
|
|
"One column MUST be 'first_name'",
|
|
"One column MUST be 'last_name'",
|
|
"One column COULD be 'picture' and be a URI",
|
|
"One column COULD be 'birth_date'",
|
|
"One column COULD be 'birth_place'",
|
|
"One column COULD be 'birth_region'",
|
|
"One column COULD be 'birth_country'",
|
|
"One column COULD be 'nationality'",
|
|
"One column COULD be 'job'",
|
|
],
|
|
"examples": [
|
|
"source,reference,first_name,last_name,job",
|
|
"somewhere,123,Victor,Hugo,Writer",
|
|
"somewhere,456,Antonio,Vivaldi,Musician",
|
|
],
|
|
}
|
|
headers = {
|
|
"source": None,
|
|
"reference": None,
|
|
"first_name": None,
|
|
"last_name": None,
|
|
"picture": "optional",
|
|
"birth_date": "optional",
|
|
"birth_place": "optional",
|
|
"birth_region": "optional",
|
|
"birth_country": "optional",
|
|
"nationality": "optional",
|
|
"job": "optional",
|
|
}
|
|
reader = default_import(headers)
|
|
if len(g.errors) == 0 and reader is not None:
|
|
for row in reader:
|
|
if row[headers["nationality"]] !="":
|
|
nationality = CountryModel.query.filter(or_(
|
|
CountryModel.slug == slugify(row[headers["nationality"]]),
|
|
CountryModel.alpha_2 == row[headers["nationality"]].upper(),
|
|
)).first()
|
|
if nationality is None:
|
|
if len(row[headers["nationality"]])==2:
|
|
nationality = CountryModel(
|
|
name=row[headers["nationality"]],
|
|
slug=slugify(row[headers["nationality"]]),
|
|
alpha_2=row[headers["nationality"]].upper(),
|
|
)
|
|
else:
|
|
nationality = CountryModel(
|
|
name=row[headers["nationality"]],
|
|
slug=slugify(row[headers["nationality"]]),
|
|
)
|
|
nationality.save()
|
|
|
|
if row[headers["birth_country"]] !="":
|
|
birth_country = CountryModel.query.filter(or_(
|
|
CountryModel.slug == slugify(row[headers["birth_country"]]),
|
|
CountryModel.alpha_2 == row[headers["birth_country"]].upper(),
|
|
)).first()
|
|
if birth_country is None:
|
|
if len(row[headers["birth_country"]])==2:
|
|
birth_country = CountryModel(
|
|
name=row[headers["birth_country"]],
|
|
slug=slugify(row[headers["birth_country"]]),
|
|
alpha_2=row[headers["birth_country"]].upper(),
|
|
)
|
|
else:
|
|
birth_country = CountryModel(
|
|
name=row[headers["birth_country"]],
|
|
slug=slugify(row[headers["birth_country"]]),
|
|
)
|
|
birth_country.save()
|
|
|
|
if row[headers["birth_date"]] != "":
|
|
birth_date = datetime.strptime(
|
|
row[headers["birth_date"]], "%Y-%m-%d"
|
|
).date()
|
|
else:
|
|
birth_date = None
|
|
|
|
source = SourceModel.query.filter_by(
|
|
slug=slugify(row[headers["source"]]),
|
|
).first()
|
|
if source is None:
|
|
source = SourceModel(
|
|
name = row[headers["source"]],
|
|
slug = slugify(row[headers["source"]]),
|
|
)
|
|
source.save()
|
|
|
|
representative = RepresentativeModel.query.filter_by(
|
|
last_name=row[headers["last_name"]],
|
|
).filter_by(
|
|
first_name=row[headers["first_name"]],
|
|
).first()
|
|
if representative is None:
|
|
representative = RepresentativeModel(
|
|
first_name = row[headers["first_name"]],
|
|
last_name = row[headers["last_name"]],
|
|
birth_date = birth_date,
|
|
birth_place = row[headers["birth_place"]],
|
|
birth_region = row[headers["birth_region"]],
|
|
birth_country = birth_country,
|
|
job = row[headers["job"]],
|
|
nationality = nationality,
|
|
picture = row[headers["picture"]],
|
|
)
|
|
g.messages.append(
|
|
f"{row[headers['first_name']]} {row[headers['last_name']]}"
|
|
" added."
|
|
)
|
|
representative.save()
|
|
|
|
representative_origin = RepresentativeOriginModel.query.filter_by(
|
|
source_id=source.id,
|
|
).filter_by(
|
|
reference=row[headers["reference"]].upper(),
|
|
).first()
|
|
if representative_origin is None:
|
|
representative_origin = RepresentativeOriginModel(
|
|
source_id = source.id,
|
|
representative_id = representative.id,
|
|
reference = row[headers["reference"]].upper(),
|
|
)
|
|
representative_origin.save()
|
|
|
|
return self.render("admin/import.html")
|