def get_user(username): """ Returns a user. """ debug('GET user :' + username) if _is_user_created(username): user = {} user[KEY_USER] = db.hget(username + APPEND_KEY_USER, KEY_USER) user[KEY_HASH] = db.hget(username + APPEND_KEY_USER, KEY_HASH) return user else: return None
def delete_tag_from_post(post_id, tag): """ Deletes a tag from the post's set of tags """ debug("DELETE TAG from post. tag:" + tag + ", post #:" + str(post_id)) if _is_post_created(post_id): db.srem(db.hget(post_id + APPEND_KEY_POSTS, KEY_TAGS) + APPEND_KEY_TAG, tag) # Decrement the score _inc_dec_tag(tag, False) # Delete from the sorted set of tag-name -- post-ids _remove_post_tag_name(post_id, tag)
def get_password(username): #OK """ Returns the user's password. None if ain't a user with that username. """ debug("GET PASS for: " + username) if _is_user_created(username): password = db.hget(username + APPEND_KEY_USER, KEY_PASSWORD) debug(username + "'s PASS: " + password) return password else: return None
def delete_post(post_id, username): """ Deletes a post (as well as its related set of tags). """ debug("DELETE POST. username:"******",post:" + str(post_id)) post_id = str(post_id) if _is_post_created(post_id): # Delete each of the tags (to decrement the score in the ranking) for temp_tag in get_post_tags(post_id): delete_tag_from_post(post_id, temp_tag) # Delete the set of tags that the post has tags_id = db.hget(post_id + APPEND_KEY_POSTS, KEY_TAGS) db.delete(tags_id + APPEND_KEY_TAG) # Delete the counter of votes that the post has votes_id = db.hget(post_id + APPEND_KEY_POSTS, KEY_VOTES) db.delete(votes_id + APPEND_KEY_VOTE) # Delete the post from the sorted set of posts by date db.zrem(username + APPEND_SEARCH_POST_TIMEDATE, post_id) # Delete the post id from the user's post list # (1 is the number of items to be removed) db.lrem(username + APPEND_KEY_POSTS, 1, post_id) # Delete the post from the last updates _delete_post_last_updates(post_id) # Delete the post from the global ranking db.zrem(POPULAR_TOP_POSTS, post_id) # Delete the hash of the post db.delete(post_id + APPEND_KEY_POSTS) """ db.hdel(post_id + APPEND_KEY_POSTS, KEY_TAGS) db.hdel(post_id + APPEND_KEY_POSTS, KEY_TITLE) db.hdel(post_id + APPEND_KEY_POSTS, KEY_DATE) db.hdel(post_id + APPEND_KEY_POSTS, KEY_CONTENTS) """ return True else: return False
def insert_tag_post_tags(post_id, tag): """ Inserts a tag to the post's set of tags. """ get_post_tags(str(post_id)) debug("INSERT TAG to post. tag:" + tag + " post #:" + str(post_id)) # Element isn't inserted if present if _is_post_created(post_id): db.sadd(db.hget(post_id + APPEND_KEY_POSTS, KEY_TAGS) + APPEND_KEY_TAG, tag) # Add to global tags _insert_tags_global(tag) # Add to popular _inc_dec_tag(tag, True) # Add to the sorted set of tag-name -- post-ids _insert_post_tag_name(post_id, tag) # Insert to the index of tags _insert_symbol_index(tag[0]) # Insert to the specific index of tag[0] -- tags _insert_tag_index_letter_tags(tag) get_post_tags(str(post_id))
def _vote(post_id, voting_user, positive): #OK """ Private method for handling postivite and negative votes. """ post_id = str(post_id) debug("VOTE POSITIVE. user:"******", post:" + post_id) if _is_post_created(post_id) and _is_user_created(voting_user): debug("\t CURRENT VOTES of USER.") debug("\t\t-user: "******"\t\t-voted to: " + str(db.smembers(voting_user + APPEND_KEY_HAS_VOTED))) # Check if the user can vote if db.sismember(voting_user + APPEND_KEY_HAS_VOTED, post_id) == 0: vote_id = db.hget(post_id + APPEND_KEY_POSTS, KEY_VOTES) debug("\t vote_id: " + str(vote_id)) pipe = db.pipeline() if positive: pipe.incr(vote_id + APPEND_KEY_VOTE) else: pipe.decr(vote_id + APPEND_KEY_VOTE) pipe.sadd(voting_user + APPEND_KEY_HAS_VOTED, post_id) pipe.execute() return True else: return False else: return None
def get_post(key): # OK """ Returns a dictionary representing a post given its integer-id. None is returned if there aren't posts with that id. """ debug("GET POST #: " + str(key)) db_key = str(key) + APPEND_KEY_POSTS if _is_post_created(str(key)): post = {} post[KEY_TITLE] = db.hget(db_key, KEY_TITLE) post[KEY_CONTENTS] = db.hget(db_key, KEY_CONTENTS) post[KEY_DATE] = db.hget(db_key, KEY_DATE) post[KEY_TAGS] = get_post_tags(key) post[KEY_AUTHOR] = db.hget(db_key, KEY_AUTHOR) post[KEY_ID] = db.hget(db_key, KEY_ID) post[KEY_VOTES] = db.get(db.hget(db_key, KEY_VOTES) + APPEND_KEY_VOTE) debug("\treturning post with id:" + str(key)) return post else: return None