Exemplo n.º 1
0
def add_following(current_userid, to_follow_userid):
    user_info_json = UserInfo.get_user_info(current_userid)
    user_info_json["followings"]\
        .append({"userid": to_follow_userid,
                 "email": UserInfo.get_user_email(to_follow_userid)})
    user_info_str = json.dumps(user_info_json)
    redis_store.set(current_userid, user_info_str)
Exemplo n.º 2
0
    def test_remove_curret_user(self):
        with app.test_request_context():
            self.set_session()

            UserInfo.remove_current_user()

            self.assertEqual(len(session), 0)
Exemplo n.º 3
0
def get_posts(userid):
    user_info_json = UserInfo.get_user_info(userid)
    user_posts = user_info_json["posts"]
    user_posts_with_emails = []
    for post in user_posts:
        post["email"] = UserInfo.get_user_email(userid)
        user_posts_with_emails.append(post)
    return user_posts_with_emails[::-1]
Exemplo n.º 4
0
def user_page(userid):
    if not UserInfo.check_user_exists(userid):
        abort(404)
    current_user_userid = UserInfo.get_current_user_userid()
    if userid == current_user_userid:
        return render_current_user_page()
    if userid in get_followings_ids(current_user_userid):
        return render_following_user_page(userid)
    return render_not_following_user_page(userid)
Exemplo n.º 5
0
 def user_info(self):
     user = UserInfo(self)
     self.wait_window(user.win)
     self.age = user.age
     self.sex = user.sex
     self.weight = user.weight
     self.height = user.height
     try:
         self.age_label['text'] = user.age[0]
         self.sex_label['text'] = user.sex[0]
         self.height_label['text'] = str(user.height[0])
         self.weight_label['text'] = str(user.weight[0])
     except Exception:
         self.age1_label['text'] = 'User'
         self.sex1_label['text'] = 'User'
         self.height1_label['text'] = 'User'
         self.weight1_label['text'] = 'User'
     try:
         self.age1_label['text'] = str(user.age[1])
         self.sex1_label['text'] = user.sex[1]
         self.height1_label['text'] = str(user.height[1])
         self.weight1_label['text'] = str(user.weight[1])
     except Exception:
         self.age1_label['text'] = 'User'
         self.sex1_label['text'] = 'User'
         self.height1_label['text'] = 'User'
         self.weight1_label['text'] = 'User'
     del (user)
Exemplo n.º 6
0
def followings():
    current_userid = UserInfo.get_current_user_userid()
    followings = get_followings(current_userid)
    unfollow_form = UnfollowForm()
    return render_over_base_template("followings_page.html",
                                     followings=followings,
                                     unfollow_form=unfollow_form)
Exemplo n.º 7
0
def post_delete(timestamp):
    delete_post_form = DeletePostForm()
    if delete_post_form.validate_on_submit():
        userid = UserInfo.get_current_user_userid()
        delete_post(userid, timestamp)
        return redirect(url_for("profile"))
    return render_current_user_page()
Exemplo n.º 8
0
def new_post():
    new_post_form = NewPostForm()
    if new_post_form.validate_on_submit():
        if new_post_form.tweet.data:
            image = new_post_form.image.data
            add_post(UserInfo.get_current_user_userid(),
                     new_post_form.text.data, image)
            return redirect(url_for("profile"))
    return render_current_user_page(new_post_form=new_post_form)
Exemplo n.º 9
0
def search():
    search_form = SearchForm()
    if request.method == "POST" and search_form.validate_on_submit():
        if search_form.search.data:
            email = search_form.text.data
            userid = UserInfo.get_userid(email)
            return redirect(url_for("user_page", userid=userid))
    return render_over_base_template("search_page.html",
                                     search_form=search_form)
Exemplo n.º 10
0
def render_current_user_page(new_post_form=None):
    current_user_userid = UserInfo.get_current_user_userid()
    posts_to_show = get_posts_to_show(current_user_userid)
    delete_post_form = DeletePostForm()
    if new_post_form is None:
        new_post_form = NewPostForm()
    return render_over_base_template("user_page.html",
                                     userid=current_user_userid,
                                     current_user_page=True,
                                     posts=posts_to_show,
                                     new_post_form=new_post_form,
                                     delete_post_form=delete_post_form)
Exemplo n.º 11
0
    def validate_on_submit(self):
        rv = FlaskForm.validate_on_submit(self)
        if not rv:
            return False

        email = self.text.data
        if not self.is_email(email):
            self.text.errors.append("Specify an email")
            return False
        if not UserInfo.check_user_exists(email):
            self.text.errors.append("User not found")
            return False
        return True
Exemplo n.º 12
0
def delete_following(current_userid, to_unfollow_userid):
    user_info_json = UserInfo.get_user_info(current_userid)
    unfollow_user_index = -1
    for i, following in enumerate(user_info_json["followings"]):
        if following["userid"] == to_unfollow_userid:
            unfollow_user_index = i
            break
    if unfollow_user_index == -1:
        raise ValueError("Cannot unfollow not followed user.")
    else:
        del user_info_json["followings"][unfollow_user_index]
        user_info_str = json.dumps(user_info_json)
        redis_store.set(current_userid, user_info_str)
Exemplo n.º 13
0
def add_post(userid, text, image):
    image_key = None
    if image is not None:
        images_storage = ImagesStorage()
        image_key = images_storage.put_image(image)
    user_info_json = UserInfo.get_user_info(userid)
    user_info_json["posts"].append({
        "text": text,
        "timestamp": time.time(),
        "image_key": image_key
    })
    user_info_str = json.dumps(user_info_json)
    redis_store.set(userid, user_info_str)
Exemplo n.º 14
0
def delete_post(userid, timestamp):
    user_info_json = UserInfo.get_user_info(userid)
    to_delete_post_index = -1
    for i, post in enumerate(user_info_json["posts"]):
        print(post["timestamp"], timestamp)
        print(type(post["timestamp"]), type(timestamp))
        if post["timestamp"] == float(timestamp):
            to_delete_post_index = i
            break
    if to_delete_post_index == -1:
        raise ValueError("Delete post that is not posted.")
    else:
        del user_info_json["posts"][to_delete_post_index]
        user_info_str = json.dumps(user_info_json)
        redis_store.set(userid, user_info_str)
Exemplo n.º 15
0
def get_followings(userid):
    user_info_json = UserInfo.get_user_info(userid)
    user_followings = user_info_json["followings"]
    return user_followings
Exemplo n.º 16
0
def set_user_info(idinfo):
    UserInfo.add_current_user(idinfo["email"])
Exemplo n.º 17
0
 def test_get_user_info(self):
     user_info = UserInfo.get_user_info(MockRedis.USERID_1)
     self.assertEqual(len(user_info["followings"]), 3)
     self.assertEqual(len(user_info["posts"]), 1)
Exemplo n.º 18
0
def logout():
    UserInfo.remove_current_user()
    return redirect(url_for("main_page"))
Exemplo n.º 19
0
    def test_get_currrent_user_email(self):
        with app.test_request_context():
            self.set_session()

            self.assertEqual(UserInfo.get_current_user_email(),
                             UserInfoTest.USER_EMAIL)
Exemplo n.º 20
0
def profile():
    return redirect(
        url_for("user_page", userid=UserInfo.get_current_user_userid()))
Exemplo n.º 21
0
 def test_is_logged_in_if_no(self):
     with app.test_request_context():
         self.assertFalse(UserInfo.is_logged_in())
Exemplo n.º 22
0
 def test_is_logged_in_if_yes(self):
     with app.test_request_context():
         self.set_session()
         self.assertTrue(UserInfo.is_logged_in())
Exemplo n.º 23
0
def following_new(userid):
    follow_form = FollowForm()
    if follow_form.validate_on_submit():
        add_following(UserInfo.get_current_user_userid(), userid)
        return redirect(url_for("user_page", userid=userid))
    return render_not_following_user_page(userid)
Exemplo n.º 24
0
def followings_delete(userid):
    unfollow_form = UnfollowForm()
    if unfollow_form.validate_on_submit():
        delete_following(UserInfo.get_current_user_userid(), userid)
        return redirect(url_for("followings", userid=userid))
    return redirect(url_for("followings", userid=userid))
Exemplo n.º 25
0
def posts():
    current_userid = UserInfo.get_current_user_userid()
    followings_ids = get_followings_ids(current_userid)
    followings_posts = get_followings_posts(followings_ids)
    return render_over_base_template("posts_page.html",
                                     followings_posts=followings_posts)