예제 #1
0
def create_choice(thid):
    """Create a choice."""
    form = ThreadForm()
    form["csrf_token"].data = request.cookies["csrf_token"]
    
    if form.validate_on_submit():
        image_filename = upload_file(form["image"].data)
        choice = Choice(
            title=form["title"].data,
            prev_thread_id=form["prev_thread"].data,
            next_thread_id=form["next_thread"].data,
            color=form["color"].data,
            icon=form["icon"].data,
            image=image_filename
        )
        db.session.add(choice)
        db.session.commit()
        return choice.to_dict()
    else:
        return {"errors": validation_errors_to_messages(form.errors)}, 401
예제 #2
0
def createChoicesFromString(choices_string, thread):
    """
    Convert json data into new thread_choice records in the database,
    then return a dictionary compilation of the choices.
    """
    # Create choices that new thread connects to
    choices_dict = {}
    choices = json.loads(choices_string)
    print(choices, choices_string)
    for choice in choices:
        # TODO Check if 'choices' is object so as to grab alt title.
        thread_choice = Choice(
            title=" ".join(choice.split(" ")[1:]),
            prev_thread=thread,
            next_thread_id=int(choice.split(" ")[0]),
        )
        db.session.add(thread_choice)
        db.session.commit()
        choices_dict[thread_choice.id] = thread_choice.to_dict()
    return choices_dict
예제 #3
0
def createChoices(choices_data, thread):
    """
    Convert json data into new thread_choice records in the database,
    then return a dictionary compilation of the choices.
    """
    # Create choices that new thread connects to
    choices = {}
    for choice in choices_data:
        # TODO Check if 'choices' is object so as to grab alt title.
        thread_choice = Choice(
            title=choice["title"],
            description=choice["description"],
            prev_thread=thread,
            next_thread_id=choice["next_thread_id"],
            color=choice["color"],
            icon=choice["icon"],
            image=choice["image"],
        )
        db.session.add(thread_choice)
        db.session.commit()
        choices[thread_choice.id] = thread_choice.to_dict()
    return choices