def get_user_by_id(user_id): db = get_db() user = User(user_id, get_username(user_id), get_profile_pic(user_id)) print("PROFILE PIC: " + get_profile_pic(user_id)) user.follower = list(db.smembers(get_follow_key(user_id, User.FOLLOWER))) user.following = list(db.smembers(get_follow_key(user_id, User.FOLLOWING))) return user
def update_tweet(): db = get_db() tweet = request.form tweet_id = tweet[ID] if db.hgetall(tweet_id): db.hset(tweet_id, TEXT, tweet[TEXT]) return jsonify(tweet_jsonify(tweet_id)), 200 return jsonify(error=NOT_FOUND), 404
def delete_tweet(): db = get_db() tweet_id = request.args[ID] if db.zrem(Config.REDIS_TWEET, tweet_id): if db.hget(tweet_id, HAS_IMAGE) == TRUE: my_s3.delete(tweet_id) db.delete(tweet_id) return jsonify(message=tweet_id + " deleted"), 200 return jsonify(error=NOT_FOUND), 404
def save_tweet_to_db(tweet_id, tweet, has_image): db = get_db() date = str(time.time()) db.hset(tweet_id, TEXT, tweet[TEXT]) db.hset(tweet_id, USER, g.current_user["name"]) db.hset(tweet_id, USER_IMAGE, get_profile_pic(g.current_user["id"])) db.hset(tweet_id, HAS_IMAGE, has_image) db.hset(tweet_id, DATE, date) db.zadd(Config.REDIS_TWEET, tweet_id, date)
def tweet_jsonify(i): db = get_db() image_url = my_s3.get_presigned_url(i) if db.hget(i, HAS_IMAGE) == TRUE else None date = get_date(i) return { ID: i, USER: db.hget(i, USER), USER_IMAGE: db.hget(i, USER_IMAGE), TEXT: db.hget(i, TEXT), IMAGE: image_url, DATE: date, }
def test_db(): db = get_db() db.set("Test", "Test") assert db.get("Test") == "Test" db.flushall()
def get_tweet(): db = get_db() tweet_list = [] for i in db.zrevrangebyscore(Config.REDIS_TWEET, time.time(), 0): tweet_list.append(tweet_jsonify(i)) return jsonify(tweet_list)
def add_to_list(user_id): get_db().sadd(User.LIST, user_id)
def get_date(tweet): date = get_db().hget(tweet, DATE) date = time.strftime(DATE_TIME_FORMAT, time.localtime(float(date))) return date
def follow(self, user_id): get_db().sadd(get_follow_key(self.id, User.FOLLOWING), user_id) get_db().sadd(user_id, User.FOLLOWER, self.id)
def get_user_by_client_id(client_id): return get_db().hget(User.CLIENT, client_id)
def test_get_user(): db = get_db() user = User.get_user(123, "TEST_USER", "pic.jpg") # assert user.name == "TEST_USER" assert user.profile_pic == "pic.jpg" db.flushall()
def get_tweet_id(): db = get_db() return "%s:%s" % (Config.REDIS_TWEET, get_id(db, Config.REDIS_TWEET))
def get_user(): db = get_db() user_list = [] for i in db.smembers(User.LIST): user_list.append(get_user_by_id(i).json()) return flask.jsonify(user_list), 200
def map_client_user(client_id, user_id): get_db().hset(User.CLIENT, client_id, user_id)
def get_profile_pic(user_id): return get_db().hget(user_id, User.PROFILE_PIC)
def get_username(user_id): return get_db().hget(user_id, User.NAME)
def test_create_user(): db = get_db() user_id = User.create_user(123, "TEST_USER", "pic.jpg") assert db.hget(user_id, User.NAME) == "TEST_USER" db.flushall()
def create_user(user_id, username, pic): db = get_db() db.hset(user_id, User.NAME, username) db.hset(user_id, User.PROFILE_PIC, pic) return user_id