refactor: import all models instead of one by one

This commit is contained in:
Mindiell 2021-07-27 07:48:45 +02:00
parent d33757e754
commit 1f19252dcc
3 changed files with 54 additions and 64 deletions

View File

@ -11,18 +11,7 @@ from slugify import slugify
from sqlalchemy import or_
from app.form.admin import ImportForm
from app.model.address import AddressModel
from app.model.contact import ContactModel
from app.model.country import CountryModel
from app.model.decision import DecisionModel
from app.model.entity import EntityModel
from app.model.matter import MatterModel
from app.model.membership import MembershipModel
from app.model.recommendation import RecommendationModel
from app.model.representative import RepresentativeModel
from app.model.role import RoleModel
from app.model.stance import StanceModel
from app.model.type import TypeModel
from app import model
def default_import(headers):
@ -91,14 +80,14 @@ class ImportAddressView(BaseView):
}
if len(g.errors) == 0 and reader is not None:
for row in reader:
address_country = CountryModel.query.filter_by(
address_country = model.CountryModel.query.filter_by(
code=row[headers["country_code"]].upper()
).first()
address = AddressModel.query.filter_by(
address = model.AddressModel.query.filter_by(
slug==slugify(row[headers["name"]]),
).first()
if address is None:
address = AddressModel(
address = model.AddressModel(
name = row[headers["name"]],
slug = slugify(row[headers["name"]]),
country = address_country,
@ -195,21 +184,21 @@ class ImportContactView(BaseView):
}
if len(g.errors) == 0 and reader is not None:
for row in reader:
contact_representative = RepresentativeModel.query.filter_by(
contact_representative = model.RepresentativeModel.query.filter_by(
slug=row[headers["representative_slug"]],
).first()
if headers["addres_slug"] is not None:
contact_address = AddressModel.query.filter_by(
contact_address = model.AddressModel.query.filter_by(
slug==row[headers["address_slug"]],
).first()
else:
contact_address = None
contact = ContactModel.query.filter_by(
contact = model.ContactModel.query.filter_by(
slug==slugify(row[headers["name"]]),
representative_id=contact_representative.id,
).first()
if contact is None:
contact = ContactModel(
contact = model.ContactModel(
representative = contact_representative,
address = contact_address,
name = row[headers["name"]],
@ -260,9 +249,9 @@ class ImportCountryView(BaseView):
}
if len(g.errors) == 0 and reader is not None:
for row in reader:
country = CountryModel.query.filter_by(code=row[headers["code"]]).first()
country = model.CountryModel.query.filter_by(code=row[headers["code"]]).first()
if country is None:
country = CountryModel(
country = model.CountryModel(
name = row[headers["name"]],
slug = slugify(row[headers["name"]]),
code = row[headers["code"]].upper(),
@ -311,18 +300,18 @@ class ImportDecisionView(BaseView):
}
if len(g.errors) == 0 and reader is not None:
for row in reader:
decision_representative = RepresentativeModel.query.filter_by(
decision_representative = model.RepresentativeModel.query.filter_by(
slug=row[headers["representative_slug"]],
).first()
decision_recommendation = RecommendationModel.query.filter_by(
decision_recommendation = model.RecommendationModel.query.filter_by(
slug=row[headers["recommendation_slug"]],
).first()
decision = DecisionModel.query.filter_by(
decision = model.DecisionModel.query.filter_by(
representative=decision_representative,
recommendation=decision_recommendation,
).first()
if decision is None:
decision = DecisionModel(
decision = model.DecisionModel(
representative = decision_representative,
recommendation = decision_recommendation,
value = value,
@ -378,13 +367,13 @@ class ImportEntityView(BaseView):
}
if len(g.errors) == 0 and reader is not None:
for row in reader:
entity_type = TypeModel.query.filter_by(
entity_type = model.TypeModel.query.filter_by(
code=row[headers["type_code"]]
).first()
entity_country = CountryModel.query.filter_by(
entity_country = model.CountryModel.query.filter_by(
code=row[headers["country_code"]].upper()
).first()
entity = EntityModel.query.filter_by(
entity = model.EntityModel.query.filter_by(
code=row[headers["code"]]
).first()
if row[headers["start"]] != "":
@ -400,7 +389,7 @@ class ImportEntityView(BaseView):
else:
picture = None
if entity is None:
entity = EntityModel(
entity = model.EntityModel(
type = entity_type,
country = entity_country,
name = row[headers["name"]],
@ -470,7 +459,7 @@ class ImportMatterView(BaseView):
}
if len(g.errors) == 0 and reader is not None:
for row in reader:
matter = MatterModel.query.filter_by(
matter = model.MatterModel.query.filter_by(
slug=slugify(row[headers["name"]]),
).first()
if headers["description"] is not None:
@ -478,7 +467,7 @@ class ImportMatterView(BaseView):
else:
description = None
if matter is None:
matter = DecisionModel(
matter = model.DecisionModel(
name = row[headers["name"]],
slug = slugify(row[headers["name"]]),
description = description,
@ -530,13 +519,13 @@ class ImportMembershipView(BaseView):
}
if len(g.errors) == 0 and reader is not None:
for row in reader:
representative = RepresentativeModel.query.filter_by(
representative = model.RepresentativeModel.query.filter_by(
slug=slugify(row[headers["representative_slug"]]),
).first()
entity = EntityModel.query.filter_by(
entity = model.EntityModel.query.filter_by(
code=row[headers["entity_code"]],
).first()
role = RoleModel.query.filter_by(
role = model.RoleModel.query.filter_by(
code=row[headers["role_code"]],
).first()
if row[headers["start"]] != "":
@ -547,14 +536,14 @@ class ImportMembershipView(BaseView):
end_date = datetime.strptime(row[headers["end"]], "%Y-%m-%d")
else:
end_date = None
membership = MembershipModel.query.filter_by(
membership = model.MembershipModel.query.filter_by(
representative=representative,
entity=entity,
role=role,
start=start_date,
).first()
if membership is None:
membership = MembershipModel(
membership = model.MembershipModel(
representative = representative,
entity = entity,
role = role,
@ -614,10 +603,10 @@ class ImportRecommendationView(BaseView):
}
if len(g.errors) == 0 and reader is not None:
for row in reader:
matter = MatterModel.query.filter_by(
matter = model.MatterModel.query.filter_by(
slug=row[headers["matter_slug"]],
).first()
entity = EntityModel.query.filter_by(
entity = model.EntityModel.query.filter_by(
code=row[headers["entity_code"]],
).first()
if row[headers["date"]] != "":
@ -632,13 +621,13 @@ class ImportRecommendationView(BaseView):
weight = int(row[headers["weight"]])
else:
weight = 1
recommendation = RecommendationModel.query.filter_by(
recommendation = model.RecommendationModel.query.filter_by(
matter=matter,
entity=entity,
code=row[headers["code"]],
).first()
if recommendation is None:
recommendation = RecommendationModel(
recommendation = model.RecommendationModel(
matter = matter,
entity = entity,
name = row[headers["name"]],
@ -719,10 +708,10 @@ class ImportRepresentativeView(BaseView):
if len(g.errors) == 0 and reader is not None:
# Values
for row in reader:
representative = RepresentativeModel.query.filter_by(
representative = model.RepresentativeModel.query.filter_by(
code=row[headers["code"]],
).first()
country = CountryModel.query.filter_by(
country = model.CountryModel.query.filter_by(
code=row[headers["nationality"]].upper(),
).first()
if row[headers["birth_date"]] != "":
@ -730,7 +719,7 @@ class ImportRepresentativeView(BaseView):
else:
birth_date = None
if representative is None:
representative = RepresentativeModel(
representative = model.RepresentativeModel(
code = row[headers["code"]],
name = row[headers["name"]],
slug = slugify(row[headers["name"]]),
@ -800,9 +789,9 @@ class ImportRoleView(BaseView):
if len(g.errors) == 0 and reader is not None:
# Values
for row in reader:
role = RoleModel.query.filter_by(code=row[headers["code"]]).first()
role = model.RoleModel.query.filter_by(code=row[headers["code"]]).first()
if role is None:
role = RoleModel(
role = model.RoleModel(
name = row[headers["name"]],
slug = slugify(row[headers["name"]]),
code = row[headers["code"]],
@ -856,21 +845,21 @@ class ImportStanceView(BaseView):
# Values
for row in reader:
# Representative
representative = RepresentativeModel.query.filter_by(
representative = model.RepresentativeModel.query.filter_by(
slug=row[headers["slug"]],
).first()
if representative is None:
representative = RepresentativeModel(
representative = model.RepresentativeModel(
name = row[headers["name"]],
slug = row[headers["slug"]],
)
representative.save()
# Matter
matter = MatterModel.query.filter_by(
matter = model.MatterModel.query.filter_by(
slug=slugify(row[headers["matter"]]),
).first()
if matter is None:
matter = MatterModel(
matter = model.MatterModel(
name = row[headers["matter"]],
slug = slugify(row[headers["matter"]]),
)
@ -880,7 +869,7 @@ class ImportStanceView(BaseView):
else:
stance_date = None
# Stance
stance = StanceModel.query.filter_by(
stance = model.StanceModel.query.filter_by(
representative=representative,
matter=matter,
subject=row[headers["subject"]],
@ -888,7 +877,7 @@ class ImportStanceView(BaseView):
source_url=row[headers["source_url"]],
).first()
if stance is None:
stance = StanceModel(
stance = model.StanceModel(
representative = representative,
matter = matter,
subject = row[headers["subject"]],
@ -947,9 +936,9 @@ class ImportTypeView(BaseView):
if len(g.errors) == 0 and reader is not None:
# Values
for row in reader:
type_ = TypeModel.query.filter_by(code=row[headers["code"]]).first()
type_ = model.TypeModel.query.filter_by(code=row[headers["code"]]).first()
if type_ is None:
type_ = TypeModel(
type_ = model.TypeModel(
name = row[headers["name"]],
slug = slugify(row[headers["name"]]),
code = row[headers["code"]],
@ -964,3 +953,4 @@ class ImportTypeView(BaseView):
type_.save()
return self.render("admin/import.html")

View File

@ -5,8 +5,8 @@ import random
from flask import g, render_template
from app import model
from app.controller.controller import Controller
import app.model as models
from sqlalchemy import desc
from sqlalchemy.sql.expression import func
@ -15,22 +15,22 @@ class Core(Controller):
def home(self):
random.seed(int(datetime.today().timestamp()/100))
try:
g.motd = random.choice(models.MatterModel.query.filter_by(
g.motd = random.choice(model.MatterModel.query.filter_by(
active=True
).all())
except:
g.motd = None
try:
g.rotd = random.choice(models.RepresentativeModel.query.filter_by(
g.rotd = random.choice(model.RepresentativeModel.query.filter_by(
active=True
).all())
except:
g.rotd = None
g.last_decisions = models.DecisionModel.query.join(
models.DecisionModel.recommendation
).order_by(models.RecommendationModel.date.desc()).limit(10).all()
g.last_stances = models.StanceModel.query.order_by(
models.StanceModel.date.desc()
g.last_decisions = model.DecisionModel.query.join(
model.DecisionModel.recommendation
).order_by(model.RecommendationModel.date.desc()).limit(10).all()
g.last_stances = model.StanceModel.query.order_by(
model.StanceModel.date.desc()
).limit(10).all()
return render_template("core/home.html")

View File

@ -5,8 +5,8 @@ import random
from flask import g, redirect, render_template, url_for
from app import model
from app.controller.controller import Controller
import app.model as models
from sqlalchemy import desc
from sqlalchemy.sql.expression import func
@ -15,9 +15,9 @@ class Representative(Controller):
def view(self, representative_id=None):
if representative_id is None:
representative_id = random.choice(
models.RepresentativeModel.query.filter_by(active=True).all()
model.RepresentativeModel.query.filter_by(active=True).all()
).id
g.representative = models.RepresentativeModel.query.get(representative_id)
g.representative = model.RepresentativeModel.query.get(representative_id)
if g.representative is None:
return redirect(url_for("core.home"))
g.title = g.representative.name