Example #1
0
def _stories(message='', error=False, res_msg='', info_bar=False):
    form = SelectDiceSetForm()
    if 'POST' == request.method:
        # Create a new story
        new_story = Story()
        new_story.author_id = current_user.id
        new_story.likes = 0
        new_story.dislikes = 0

        if form.validate_on_submit():
            text = request.form.get('text')
            roll = request.form.get('roll')
            # for the tests
            if re.search('"', roll):
                roll = json.loads(request.form.get('roll'))

        if (type(roll) is str):
            roll = roll.replace("[", "")
            roll = roll.replace("]", "")
            roll = roll.replace("'", "")
            roll = roll.replace(" ", "")
            aux = roll.split(",")
            roll = aux

        dicenumber = len(roll)
        try:
            check_storyV2(text, roll)
            new_story.text = text
            new_story.roll = {'dice': roll}
            new_story.dicenumber = dicenumber
            db.session.add(new_story)
            db.session.commit()
        except WrongFormatStoryError:
            # print('ERROR 1', file=sys.stderr)
            message = "There was an error. Try again."

        except WrongFormatDiceError:
            # print('ERROR 2', file=sys.stderr)
            message = "There was an error. Try again."

        except TooLongStoryError:
            # print('ERROR 3', file=sys.stderr)
            message = "The story is too long. The length is > 1000 characters."

        except TooSmallStoryError:
            # print('ERROR 4', file=sys.stderr)
            message = "The number of words of the story must greater or equal of the number of resulted faces."

        except WrongFormatSingleDiceError:
            # print('ERROR 5', file=sys.stderr)
            message = "There was an error. Try again."

        except InvalidStory:
            # print('ERROR 6', file=sys.stderr)
            message = "Invalid story. Try again!"

        allstories = db.session.query(Story, User).join(User).all()
        allstories = list(
            map(
                lambda x:
                (x[0], x[1], "hidden"
                 if x[1].id == current_user.id else "", "unfollow"
                 if _is_follower(current_user.id, x[1].id) else "follow",
                 reacted(current_user.id, x[0].id)), allstories))
        return render_template("stories.html",
                               message=message,
                               form=form,
                               stories=allstories,
                               active_button="stories",
                               like_it_url="/stories/reaction",
                               details_url="/stories",
                               error=error,
                               info_bar=info_bar,
                               res_msg=str(res_msg))
    elif 'GET' == request.method:
        allstories = db.session.query(Story, User).join(User).all()
        allstories = list(
            map(
                lambda x:
                (x[0], x[1], "hidden"
                 if x[1].id == current_user.id else "", "unfollow"
                 if _is_follower(current_user.id, x[1].id) else "follow",
                 reacted(current_user.id, x[0].id)), allstories))

        return render_template("stories.html",
                               message=message,
                               form=form,
                               stories=allstories,
                               active_button="stories",
                               like_it_url="/stories/reaction",
                               details_url="/stories",
                               error=error,
                               info_bar=info_bar,
                               res_msg=str(res_msg))
Example #2
0
    def test1(self):
        global _app
        tested_app = create_app(debug=True)
        _app = tested_app
        with tested_app.test_client() as client:
            with client.session_transaction() as sess:
                db.drop_all()
                db.create_all()

                # create user
                user_a = User()
                user_a.email = '*****@*****.**'
                user_a.set_password('test')
                db.session.add(user_a)
                db.session.commit()

                user_b = User()
                user_b.email = '*****@*****.**'
                user_b.set_password('test')
                db.session.add(user_b)
                db.session.commit()

                # create story
                story = Story()
                story.text = 'Text a'
                story.likes = 0
                story.dislikes = 0
                story.author_id = user_a.get_id()
                story.roll = {
                    'dice':
                    ['bike', 'tulip', 'happy', 'cat', 'ladder', 'rain']
                }
                db.session.add(story)
                db.session.commit()

                # add like
                like = Reaction()
                like.marked = 0
                like.story_id = story.id
                like.user_id = user_b.id
                like.type = 1
                db.session.add(story)
                db.session.commit()

                # create 1000 user and like the story
                users = []
                for i in range(10):
                    user = User()
                    user.email = 'user' + str(i) + '@test.com'
                    user.set_password('test')
                    db.session.add(user)
                    users.append(user)
                    db.session.add(story)

                db.session.commit()
                for u in users:
                    add_reaction(u.id, 1, 1)
                #
                # reaction = Reaction.query.count()
                # print(str(reaction)))
                res = update_reactions.apply_async(args=[1], time_limit=3)
                res.get()
                q = Story.query.filter_by(author_id=1).first()
                self.assertEqual(int(q.likes), 10)