Пример #1
0
def test_get_by_interest(client, auth, database, templates, story_actions):
    auth.login()

    example1 = Story()
    example1.theme = 'halloween'
    example1.text = 'Halloween story of test1 user :)'
    example1.author_id = 2
    example1.is_draft = False
    example1.deleted = False
    example1.dice_set = ['a', 'b', 'c']
    database.session.add(example1)
    database.session.commit()

    example2 = Story()
    example2.theme = 'xmas'
    example2.text = 'Xmas story of test2 user :)'
    example2.author_id = 3
    example2.is_draft = False
    example2.deleted = False
    example2.dice_set = ['a', 'b', 'c']
    database.session.add(example2)
    database.session.commit()

    example3 = Story()
    example3.theme = 'xmas'
    example3.theme = 'Old xmas story of test3 user :)'
    example3.date = dt.datetime.now() - dt.timedelta(days=6)
    example3.author_id = 4
    example3.is_draft = False
    example3.deleted = False
    example3.dice_set = ['a', 'b', 'c']
    database.session.add(example3)
    database.session.commit()

    reply = story_actions.get_all_stories(theme='xmas')
    assert reply.status_code == 200
    assert templates[-1]['stories'].all() == [example2]
Пример #2
0
def new_stories():
    if request.method == 'GET':
        dice_themes = retrieve_themes()
        return render_template("new_story.html", themes=dice_themes)
    else:
        stry = Story.query.filter(Story.author_id == current_user.id).filter(
            Story.published == 0).filter(
                Story.theme == request.form["theme"]).first()
        if stry != None:
            return redirect("/write_story/" + str(stry.id), code=302)

        dice_set = retrieve_dice_set(request.form["theme"])
        face_set = dice_set.throw()[:int(request.form["dice_number"])]
        new_story = Story()
        new_story.author_id = current_user.id
        new_story.theme = request.form["theme"]
        new_story.rolls_outcome = json.dumps(face_set)
        db.session.add(new_story)
        db.session.flush()
        db.session.commit()
        db.session.refresh(new_story)
        return redirect('/write_story/' + str(new_story.id), code=302)
Пример #3
0
def _rollDice():
    '''
    Rolls the dice and enables the user to start writing a story.

    The story is created as a draft, so that it can be edited.

    Raises:
        Exception: due to eventual failures during the commit of the
            created story into the database.

    Returns:
        302 -> the user is redirected to the page in which he/she can
                start writing the story on the faces which came out.
    '''

    diceset = request.args.get('diceset', 'standard')
    dicenum = request.args.get('dicenum', 6, type=int)

    try:
        dice = DiceSet(diceset, dicenum)
        roll = dice.throw_dice()
        story = Story()
        story.text = ''
        story.theme = diceset
        story.likes = 0
        story.dislikes = 0
        story.dice_set = roll
        story.author_id = current_user.id
        db.session.add(story)
        db.session.commit()
    except Exception as e:
        print(e)
        db.session.rollback()
        abort(400)

    return redirect(url_for('stories._story_edit', storyid=story.id))