""" Routes for setting up and configuring the webapp. """ from flask import ( Blueprint, current_app, flash, g, redirect, render_template, request, session, url_for, ) import requests from .. import models from ..constants import SEED_DATA bp = Blueprint('setup', __name__) @bp.route('/db/seed') def seed_db(): """ Setup and seed the database with sample data from `../constants.py`. """ try: models.db.drop_all() models.db.create_all() except Exception as e: current_app.logger.error(e) flash('Error dropping or creating database tables.', 'danger') return redirect(url_for('public.landing')) candidate_info = models.CandidateInfo(**SEED_DATA.get('candidate_info', {})) models.db.session.add(candidate_info) models.db.session.flush() seed_resume = None if SEED_DATA.get('resume'): seed_resume = models.Resume(**SEED_DATA['resume']) else: seed_resume = models.Resume(title="My Resume") seed_resume.candidate_info_id = candidate_info.candidate_info_id models.db.session.add(seed_resume) models.db.session.flush() for skill in SEED_DATA.get('skills', []): skill = models.Skill(**skill) models.db.session.add(skill) skill.resume_id = seed_resume.resume_id models.db.session.flush() for employment_data in SEED_DATA.get('employments', []): items_data = employment_data.get('items', []) skills_name = employment_data.get('skills', []) employment = models.Employment( organization_name=employment_data.get('organization_name'), location_display=employment_data.get('location_display'), title=employment_data.get('title'), start_date=employment_data.get('start_date'), end_date=employment_data.get('end_date', None), ) models.db.session.add(employment) models.db.session.flush() for item_data in items_data: item = models.EmploymentItem( employment_id=employment.employment_id, description=item_data, ) models.db.session.add(item) models.db.session.flush() for skill_name in skills_name: skill = models.Skill.query.filter_by(name=skill_name).first() if skill: association = models.EmploymentSkillAssociation( employment_id=employment.employment_id, skill_id=skill.skill_id, ) models.db.session.add(association) resume_employment = models.ResumeEmployment( resume_id=seed_resume.resume_id, employment_id=employment.employment_id, ) models.db.session.add(resume_employment) for education_data in SEED_DATA.get('educations', []): items = education_data.pop('items', []) skills = education_data.pop('skills', []) education = models.Education(**education_data) models.db.session.add(education) models.db.session.flush() for item in items: education_item = models.EducationItem( education_id=education.education_id, description=item.get('description',''), link=item.get('link', None), ) models.db.session.add(education_item) for skill_name in skills: skill = models.Skill.query.filter_by(name=skill_name).first() if skill: association = models.EducationSkillAssociation( education_id=education.education_id, skill_id=skill.skill_id, ) models.db.session.add(association) resume_education = models.ResumeEducation( resume_id=seed_resume.resume_id, education_id=education.education_id, ) models.db.session.add(resume_education) models.db.session.commit() return 'Seeded'