def load_goals_to_db(): """ fill goals table in database """ for key, value in goals.items(): goal = Goal(name=key, value=value) db.session.add(goal) db.session.commit()
def upgrade(): bind = op.get_bind() session = orm.Session(bind=bind) goals_lst = list(goals.items()) goals_lst.sort(key=lambda item: item[1]) for code, title in goals_lst: session.add(Goal(code=code, title=title)) session.commit()
class RequestForm(FlaskForm): name = StringField( 'name', validators=[InputRequired(message='Укажите как к Вам обращаться')]) client_weekday = StringField('client_weekday', [InputRequired()]) client_time = StringField('client_time', [InputRequired()]) client_teacher = StringField('client_teacher', [InputRequired()]) phone = StringField( 'phone', validators=[ InputRequired(message='Укажите номер телефона для связи с Вами') ]) goal = RadioField( 'goal', validators=[InputRequired()], choices=[(key, value) for key, value in goals.items()], ) free_time = RadioField('free_time', validators=[InputRequired()], choices=free_times)
class RequestForm(ClientForm): goal = RadioField('goal', choices=list(goals.items()), validators=[InputRequired()]) time = RadioField('time', choices=[(value, value) for value in ('1-2', '3-5', '5-7', '7-10')], validators=[InputRequired()])
"fri": ["Пятница", "friday"], "sat": ["Суббота", "saturday"], "sun": ["Воскресенье", "sunday"] } icons = {"travel": "⛱", "study": "🏫", "work": "🏢", "relocate": "🚜"} for key, value in days.items(): day = Day(key_en=key, value_ru=value[0], value_en=value[1]) db.session.add(day) db.session.commit() for key, value in goals.items(): goal = Goal(key_en=key, value_ru=value, icon=icons[key]) db.session.add(goal) db.session.commit() for teacher in teachers: teacher_rec = Teacher(id=teacher['id'], name=teacher['name'], about=teacher['about'], rating=teacher['rating'], picture=teacher['picture'], price=teacher['price'], free=json.dumps(teacher['free'])) for goal in teacher['goals']: goal_rec = db.session.query(Goal).filter(Goal.key_en == goal).first() teacher_rec.goals.append(goal_rec)
from app.models import Goal, Teacher, Schedule, Much_time from data import goals, teachers dotenv_path = os.path.join(os.path.dirname(__file__), ".env") if os.path.exists(dotenv_path): load_dotenv(dotenv_path) engine = create_engine(os.getenv("DEVELOPMENT_DATABASE_URI")) session = sessionmaker() session.configure(bind=engine) s = session() goal_in_db = dict() for g_key, g_value in goals.items(): # Model GOAL g = Goal(name=g_key, name_ru=g_value) s.add(g) goal_in_db[g_key] = g for teacher in teachers: # Model TEACHER t = Teacher( name=teacher["name"], about=teacher["about"], rating=teacher["rating"], picture=teacher["picture"], price=teacher["price"], )
from data import goals, teachers from models import Teacher, Goal, db, teachers_goals_association # TODO без этого костыля не работает данный модуль заполнения таблиц со следующей ошибкой, можно сделать лучше? # No application found. Either work inside a view function or push an application context. # See http://flask-sqlalchemy.pocoo.org/contexts/. from app import app app.app_context().push() # Удаляем данные из таблиц, чтобы исключить дубли db.session.query(Goal).delete(synchronize_session=False) db.session.query(Teacher).delete(synchronize_session=False) db.session.query(teachers_goals_association).delete(synchronize_session=False) db.session.commit() for code, goal in goals.items(): db.session.add(Goal(code=code, goal=goal)) db.session.commit() ## TODO При использвании many to many отношений с таблицей Goals не понял как применять этот вариант "быстрого" заполнения # teachers_to_save = [] # for t in teachers: # teacher = Teacher(id=t['id'], name=t['name'], about=t['about'], rating=t['rating'], picture=t['picture'], # price=t['price'], # goals=json.dumps(t['goals']), # free=json.dumps(t['free'])) # teachers_to_save.append(teacher) # db.session.bulk_save_objects(teachers_to_save) # db.session.commit() for t in teachers:
def transform_goals(): for slug, name in goals.items(): record = Goal(slug=slug, name=name) db.session.add(record) db.session.commit()