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)
def test_remove_curret_user(self): with app.test_request_context(): self.set_session() UserInfo.remove_current_user() self.assertEqual(len(session), 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]
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)
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)
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)
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()
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)
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)
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)
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
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)
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)
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)
def get_followings(userid): user_info_json = UserInfo.get_user_info(userid) user_followings = user_info_json["followings"] return user_followings
def set_user_info(idinfo): UserInfo.add_current_user(idinfo["email"])
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)
def logout(): UserInfo.remove_current_user() return redirect(url_for("main_page"))
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)
def profile(): return redirect( url_for("user_page", userid=UserInfo.get_current_user_userid()))
def test_is_logged_in_if_no(self): with app.test_request_context(): self.assertFalse(UserInfo.is_logged_in())
def test_is_logged_in_if_yes(self): with app.test_request_context(): self.set_session() self.assertTrue(UserInfo.is_logged_in())
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)
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))
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)