Example #1
0
def test_empty_story():
    diceset = 'standard'
    dicenum = 6
    dice = DiceSet(diceset, dicenum)
    roll = dice.throw_dice()

    story = ''
    with pytest.raises(NotValidStoryError):
        _check_story(roll, story)
Example #2
0
def test_correct_story():
    diceset = 'standard'
    dicenum = 6
    dice = DiceSet(diceset, dicenum)
    roll = dice.throw_dice()

    story = ''
    for word in roll:
        story += word + ' '

    _check_story(roll, story)
Example #3
0
def test_wrong_word_in_roll():
    roll = ['table', 'window', 'cat', 'chai']
    story = 'The cat is on the board, the chair is near the window.'
    with pytest.raises(NotValidStoryError):
        _check_story(roll, story)
Example #4
0
def test_wrong_story_synonym():
    roll = ['table', 'window', 'cat', 'chair']
    story = 'The cat is on the board, the chair is near the door.'
    with pytest.raises(NotValidStoryError):
        _check_story(roll, story)
Example #5
0
def test_correct_story_synonym():
    roll = ['table', 'window', 'cat', 'chair']
    story = 'The cat is on the board, the chair is near the window.'
    _check_story(roll, story)
Example #6
0
def test_correct_story_with_parentesys():
    roll = ['table', 'window', 'cat', 'chair']
    story = 'The cat is on the table and...the chair, I think, is near the ' \
        'window:)'
    _check_story(roll, story)
Example #7
0
def test_correct_story_with_punctuation():
    roll = ['table', 'window', 'cat', 'chair']
    story = 'The cat is on the table! The chair, I think, is near the window.'
    _check_story(roll, story)
Example #8
0
def _story_edit(storyid):
    '''
    GET
    ---
    Opens the draft with id <storyid> in edit mode.
    
    Returns:
        404 -> the story was not found
        401 -> the user is not the author of the story
        403 -> the story has been posted and it is not a draft anymore
        410 -> the story was deleted
        200 -> provides the story in edit mode


    POST
    ----
    Posts the draft with id <storyid> as a completed story.

    If the operation is succesful, the story is not editable anymore.

    Raises:
        NotValidStoryError -> some of the words (or synonyms) representing the faces
            of the rolled dice were missing in the story, so it has been rejected
    
    Returns:
        404 -> the story was not found
        401 -> the user is not the author of the story
        403 -> the story has been posted and it is not a draft anymore
        410 -> the story was deleted
        200 -> the story has been submitted correctly
    '''
    story = db.session.query(Story).get(storyid)
    if story is None:
        abort(404, f'Story {storyid} not found')
    if story.author_id != current_user.id:
        abort(401, f'You must be the author of story {storyid} to edit')
    if not story.is_draft:
        abort(403, f'Story {storyid} cannot be edited')
    if story.deleted:
        abort(410, f'Story {storyid} was deleted')

    form = StoryForm()
    if form.validate_on_submit():
        form.populate_obj(story)
        if not story.is_draft:
            try:
                _check_story(story.dice_set, story.text)
                db.session.commit()
                print('PRE SEND')
                # Send telegram message
                send_telegram_message(story, current_user.id)
                print('POST SEND')
                return redirect(url_for('stories._get_story', storyid=storyid))
            except NotValidStoryError:
                db.session.rollback()
                form.text.errors.append('The story is not valid.')
        else:
            db.session.commit()
            return redirect(url_for('stories._get_story', storyid=storyid))

    if request.method == 'GET':
        form.text.data = story.text
        form.is_draft.data = story.is_draft

    return render_template('edit_story.html',
                           story_id=storyid,
                           dice=story.dice_set,
                           form=form,
                           story=story)