def save_state(session, state_id): """Save state to database""" state = State() state.id = state_id session.add(state) session.commit() return state
def get_department(session, state_id, department_type): """Get department from database""" department = session.query(Department).filter( Department.state_id == state_id).filter( Department.department_type == department_type).first() if department is None: department = Department() state = session.query(State).get(state_id) if not state: state = State() state.id = state_id session.add(state) department.state_id = state.id department.department_type = department_type session.add(department) session.commit() return department