コード例 #1
0
def render_wall(user_id):
    q = db.session.query(User).filter(User.id == user_id)
    user = q.first()
    if user is None:
        return User_not_found()

    stats = Stats(user_id)

    form = SelectDiceSetForm()

    stories = db.session.query(
        Story, User).join(User).filter(Story.author_id == user.id).all()
    stories = 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)), stories))

    rend = render_template("wall.html",
                           message='',
                           form=form,
                           stories=stories,
                           active_button="stories",
                           like_it_url="/stories/reaction",
                           details_url="/stories",
                           error=False,
                           info_bar=False,
                           res_msg=str(''),
                           user=user,
                           stats=stats)

    return rend
コード例 #2
0
ファイル: users.py プロジェクト: SWE-AGGERS/SocialDice
def _users():
    users = db.session.query(Story, User).join(Story).order_by(desc(Story.id))
    users = reduce_list(users.all())

    users = 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)), users))

    return render_template("users.html",
                           stories=users,
                           like_it_url="/stories/reaction",
                           details_url="/stories")
コード例 #3
0
def filter_stories():
    if request.method == 'GET':
        form = StoryFilter()
        return render_template('filter_stories.html', form=form)
    if request.method == 'POST':
        form = StoryFilter()
        if form.validate_on_submit():
            init_date = form.init_date.data
            end_date = form.end_date.data
            f_stories = db.session.query(Story, User)\
                .filter(Story.date >= init_date)\
                .filter(Story.date <= end_date)\
                .join(User)\
                .all()
            if f_stories is not None:
                f_stories = 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)),
                        f_stories))
                return render_template('filter_stories.html',
                                       form=form,
                                       stories=f_stories,
                                       active_button="/stories",
                                       like_it_url="/stories/reaction",
                                       details_url="/stories",
                                       error=False,
                                       info_bar=False)
        else:
            return render_template(
                'filter_stories.html',
                form=form,
                info_bar=True,
                error=True,
                res_msg='Cant travel back in time! Doublecheck dates')
コード例 #4
0
def get_remove_story(storyid, page):
    error = False
    res_msg = ''
    info_bar = False
    # Remove story
    q = db.session.query(Story).filter_by(id=storyid)
    story = q.first()
    if story is not None:
        if story.author_id == current_user.id:
            reactions = Reaction.query.filter_by(story_id=storyid).all()
            if reactions is not None:
                for reac in reactions:
                    db.session.delete(reac)
                    db.session.commit()
            db.session.delete(story)
            db.session.commit()
            #return redirect('/')
            if page == "stories":
                message = "The story has been canceled."
                #return _stories(message)
                form = SelectDiceSetForm()
                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))
                for x in allstories:
                    print(x)

                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))
            else:
                return index()
        else:
            # The user can only delete the stories she/he wrote.
            #abort(404)
            #return redirect('/stories')
            message = "The story was written by another user and cannot be deleted."
            #return _stories(message)
            form = SelectDiceSetForm()
            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))
            for x in allstories:
                print(x)

            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))

    else:
        # Story doensn't exist
        abort(404)
コード例 #5
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))
コード例 #6
0
    def test_followers_list(self):
        global _app
        if _app is None:
            tested_app = create_app(debug=True)
            _app = tested_app
        else:
            tested_app = _app
            restart_db_tables(db, tested_app)

            with tested_app.test_client() as client:
                with client.session_transaction() as session:
                    # push in the users_table 3 users
                    user_a = User()
                    user_a.firstname = "Pippo"
                    user_a.lastname = "Pippo"
                    user_a.email = '*****@*****.**'
                    user_a.set_password('test')

                    user_b = User()
                    user_b.firstname = "Pluto"
                    user_b.lastname = "Mouse"
                    user_b.email = '*****@*****.**'
                    user_b.set_password('test')

                    user_c = User()
                    user_c.email = '*****@*****.**'
                    user_c.set_password('test')

                    db.session.add(user_a)
                    db.session.add(user_b)
                    db.session.add(user_c)
                    db.session.commit()

                    # Get users ID
                    user_a_id = User.query.filter_by(
                        email=user_a.email).first().get_id()
                    user_b_id = User.query.filter_by(
                        email=user_b.email).first().get_id()

                # login as user_1
                login(client, user_a.email, 'test')

                # call /follow/user_id_2
                # assert True, redirtect to user_2 wall
                reply = client.post('/follow/' + str(user_b_id))
                data = "/wall/" + str(user_b_id)
                self.assertTrue(_is_follower(user_a_id, user_b_id))
                self.assertIn(data, str(reply.data))

                # get followed list
                reply = client.get("/followed/list")
                self.assertIn(user_b.firstname, str(reply.data))

                # get followed number
                reply = client.get("/followed")
                assert b'"followed_num":1' in reply.data

                # logout user1
                logout(client)

                # login as user2
                login(client, user_b.email, 'test')

                # get followers list
                reply = client.get("/followers/list")
                self.assertIn(user_a.firstname, str(reply.data))

                # get followers number
                reply = client.get("/followers")
                assert b'"followers_num":1' in reply.data
コード例 #7
0
    def test_unfollow_post_user(self):

        global _app
        if _app is None:
            tested_app = create_app(debug=True)
            _app = tested_app
        else:
            tested_app = _app
        restart_db_tables(db, tested_app)

        with tested_app.test_client() as client:
            with client.session_transaction() as session:
                # push in the users_table 3 users
                user_a = User()
                user_a.email = '*****@*****.**'
                user_a.set_password('test')

                user_b = User()
                user_b.email = '*****@*****.**'
                user_b.set_password('test')

                user_c = User()
                user_c.email = '*****@*****.**'
                user_c.set_password('test')

                db.session.add(user_a)
                db.session.add(user_b)
                db.session.add(user_c)
                db.session.commit()

                # Get users ID
                user_a_id = User.query.filter_by(
                    email=user_a.email).first().get_id()
                user_b_id = User.query.filter_by(
                    email=user_b.email).first().get_id()
                user_c_id = User.query.filter_by(
                    email=user_c.email).first().get_id()

            with client.session_transaction() as session:
                follow_ab = _create_follow(user_a_id, user_b_id)
                follow_ac = _create_follow(user_a_id, user_c_id)
                follow_bc = _create_follow(user_b_id, user_c_id)

                db.session.add(follow_ab)
                db.session.add(follow_ac)
                db.session.add(follow_bc)
                db.session.commit()

            # login as user_1
            login(client, user_a.email, 'test')

            # call /unfollow/user_1
            # assert False
            data = '/wall/' + str(user_a_id)
            reply = client.post('/unfollow/' + str(user_a_id))
            self.assertFalse(_is_follower(user_a_id, user_a_id))
            self.assertIn(data, str(reply.data))

            # call /unfollow/user_2
            # assert OK
            data = '/wall/' + str(user_b_id)
            self.assertTrue(_is_follower(user_a_id, user_b_id))
            reply = client.post('/unfollow/' + str(user_b_id))
            self.assertFalse(_is_follower(user_a_id, user_b_id))
            self.assertIn(data, str(reply.data))

            # call unfollow/user_2
            # assert EXC
            data = '/wall/' + str(user_b_id)
            reply = client.post('/unfollow/' + str(user_b_id))
            self.assertFalse(_is_follower(user_a_id, user_b_id))
            self.assertIn(data, str(reply.data))

            # call unfollow/user_not_exist
            # assert EXC
            user_not_exist_id = 999999999999
            data = '/wall/' + str(user_not_exist_id)
            reply = client.post('/unfollow/' + str(user_not_exist_id))
            self.assertFalse(_is_follower(user_a_id, user_not_exist_id))
            self.assertIn("/stories", str(reply.data))
            logout(client)
コード例 #8
0
    def test_follow_user(self):
        global _app
        if _app is None:
            tested_app = create_app(debug=True)
            _app = tested_app
        else:
            tested_app = _app
        restart_db_tables(db, tested_app)

        with tested_app.test_client() as client:
            with client.session_transaction() as session:
                # push in the users_table 3 users
                user_a = User()
                user_a.email = '*****@*****.**'
                user_a.set_password('test')

                user_b = User()
                user_b.email = '*****@*****.**'
                user_b.set_password('test')

                user_c = User()
                user_c.email = '*****@*****.**'
                user_c.set_password('test')

                db.session.add(user_a)
                db.session.add(user_b)
                db.session.add(user_c)
                db.session.commit()

                # Get users ID
                user_a_id = User.query.filter_by(
                    email=user_a.email).first().get_id()
                user_b_id = User.query.filter_by(
                    email=user_b.email).first().get_id()
                user_c_id = User.query.filter_by(
                    email=user_c.email).first().get_id()

            # login as user_1
            login(client, user_a.email, 'test')

            # call /follow/user_id_2
            # assert True, redirtect to user_2 wall
            reply = client.post('/follow/' + str(user_b_id))
            data = "/wall/" + str(user_b_id)
            self.assertTrue(_is_follower(user_a_id, user_b_id))
            self.assertIn(data, str(reply.data))

            # call /follow/user_id_2
            # assert True, redirtect to user_2 wall
            data = "/wall/" + str(user_b_id)
            reply = client.post('/follow/' + str(user_b_id))
            self.assertTrue(_is_follower(user_a_id, user_b_id))
            self.assertIn(data, str(reply.data))

            # call /follow/user_id_3
            # assert True, redirtect to user_3 wall
            data = "/wall/" + str(user_c_id)
            reply = client.post('/follow/' + str(user_c_id))
            self.assertTrue(_is_follower(user_a_id, user_c_id))
            self.assertIn(data, str(reply.data))

            # call /follow/user_id_2
            # assert True, redirect to user_2 wall
            data = "/wall/" + str(user_b_id)
            reply = client.post('/follow/' + str(user_b_id))
            self.assertTrue(_is_follower(user_a_id, user_b_id))
            self.assertIn(data, str(reply.data))

            # call /follow/user_id_1 (himslef)
            # assert False, redirect to user_1 wall
            data = "/wall/" + str(user_a_id)
            reply = client.post('/follow/' + str(user_a_id))
            self.assertFalse(_is_follower(user_a_id, user_a_id))
            self.assertIn(data, str(reply.data))

            # call /follow/user_not_exist
            # assert False, redirect to stories
            user_not_exist_id = 99999999999
            reply = client.post('/follow/' + str(user_not_exist_id))
            self.assertFalse(_is_follower(user_a_id, user_not_exist_id))
            self.assertIn("/stories", str(reply.data))
            logout(client)