def add_initial_values(): _, Ses = run_database() session = Ses() user1 = User("aala", "test") user2 = User("ela", "password") user3 = User("ala", "test") user4 = User("ela2", "kot") session.add(user1) session.add(user2) session.add(user3) session.add(user4) trip = [Trip("Paris", date(2020, 6, 5), date(2020, 6, 10), user1), Trip("Warsaw", date(2020, 5, 30), date(2020, 6, 7), user1), Trip("Cracow", date(2020, 7, 10), date(2020, 7, 12), user2), Trip("Brussels", date(2020, 6, 11), date(2020, 6, 13), user2), Trip("Warsaw", date(2020, 6, 15), date(2020, 6, 20), user3), Trip("Liverpool", date(2020, 8, 10), date(2020, 8, 15), user3), Trip("Malta", date(2020, 6, 5), date(2020, 6, 7), user3), Trip("London", date(2020, 6, 9), date(2020, 6, 10), user3)] for t in trip: session.add(t) participants = [Participant(user1, trip[2]), Participant(user4, trip[3]), Participant(user3, trip[2]), Participant(user2, trip[0]), Participant(user4, trip[0]), Participant(user2, trip[1]), Participant(user4, trip[1]), Participant(user3, trip[1])] for p in participants: session.add(p) commit_and_close(session)
def update_participants(participants, trip, session): for p in participants: user = session.query(User).filter_by(username=p).first() if user is not None: participant = session.query(Participant).filter_by( username=user.username, trip_id=trip.trip_id).first() if participant is None: new_participant = Participant(user, trip) session.add(new_participant)
def add_trip(username): if not request.json or ('trip_name' not in request.json or 'date_from' not in request.json or 'date_to' not in request.json or 'participants' not in request.json): return make_response(jsonify({'error': 'Missing required parameter'}), 422) datetime_format = '%Y-%m-%d' # The format participants = request.json.get('participants') date_from = datetime.strptime(request.json.get('date_from'), datetime_format).date() date_to = datetime.strptime(request.json.get('date_to'), datetime_format).date() trip_name = request.json.get('trip_name') e = create_engine("sqlite:///trip_communicator.db") Ses = sessionmaker(bind=e) session = Ses() user = session.query(User).filter_by(username=username).first() if user is None: commit_and_close(session) return make_response(jsonify({'Response': 'Incorrect username'}), 400) trip = Trip(trip_name, date_from, date_to, user) session.add(trip) session.commit() for p in participants: participant = add_participant_from_response(session, p, trip) if participant is not None: session.add(participant) owner_participant = Participant(user, trip) session.add(owner_participant) session.commit() trip_id = trip.trip_id session.close() return make_response(jsonify({'Trip id': trip_id}), 201)
def delete_state(part_id): user = Participant.get_participant(part_id) state = State.get(user=user) state.delete() return True
def inc_state(part_id): user = Participant.get_participant(part_id) state = State.get(user=user) if state is not None: state.q_numb += 1 return state
def create_state(part_id, qstn_id): p = Participant.get_participant(part_id) q = Questionnaire[qstn_id] state = State(user=p, qstnnr=q, q_numb=0) return state
def get_state(part_id): user = Participant.get_participant(part_id) state = State.get(user=user) return state
def add_participant_from_response(session, json_participant, trip): user = session.query(User).filter_by(username=json_participant).first() if user is not None: return Participant(user, trip) return None
def create_feedback(q_id, p_id, a): q = Question[q_id] p = Participant.get_participant(p_id) f = Feedback(question=q, participant=p, answer=a) return f