def insert_sensor_to_node(node_id, sensor): """ Adds a new sensor to the node. """ debug(CODE, "INSERT SENSOR TO NODE: params (node_id, sensor):" + \ str(node_id) + " " + str(sensor)) sensor_id = _insert_sensor(sensor) db.sadd(KEY_LIST_SENSORS_IN_NODE + str(node_id), sensor_id) return sensor_id
def insert_post(post, username): # OK """ Inserts a new post in the db. """ debug("INSERT POST. username: "******"post: " + str(post)) post_id = str(db.incr(POST_ID)) db_post_id = post_id + APPEND_KEY_POSTS debug("\tASIGNED ID:" + str(post_id)) # Post fields # Set id db.hset(db_post_id, KEY_ID, post_id) # Create the vote counter and set it to 0 votes vote_id = str(db.incr(VOTE_ID)) # get the next vote id debug("\tASIGNED POST-VOTE-ID:" + str(vote_id)) db.set(vote_id + APPEND_KEY_VOTE, "0") # Set the id of the vote counter db.hset(db_post_id, KEY_VOTES, vote_id) # Set author db.hset(db_post_id, KEY_AUTHOR, username) # Set title db.hset(db_post_id, KEY_TITLE, post[KEY_TITLE]) # Set date-time timedatenow = datetime.now() date = timedatenow.strftime(FORMAT_TIME) db.hset(db_post_id, KEY_DATE, date) # Set contents db.hset(db_post_id, KEY_CONTENTS, post[KEY_CONTENTS]) # Set a new set of tags-post to the db tag_id = str(db.incr(TAG_ID)) db_tag_id = tag_id + APPEND_KEY_TAG debug("\tASIGNED POST-TAGS-ID:" + str(tag_id)) for tag in post[KEY_TAGS]: # Add tag to the tag-post db.sadd(db_tag_id, tag) # Add tag to the user's tags insert_tag_user_tags(username, tag) ## Add tag to the 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) # Add link to the tags in the post db.hset(db_post_id, KEY_TAGS, tag_id) # Add post id to the head of the user's post-list db.lpush(username + APPEND_KEY_POSTS, post_id) ## Add post id to the sset of timedate-user-posts _insert_post_user_date_ss(post_id, timedatenow, username) ## Add post id to the capped list of last post updates _insert_post_last_updates(post_id) ## Add post's title to the title search sorted set _insert_title_ss(post[KEY_TITLE], post_id) debug("POST CREATED") return get_post(post_id)
def insert_tag_user_tags(username, tag): """ Inserts a tag to the user's set of tags. """ debug("INSERT TAG :" + tag + ", to:" + username) # Since it is a set, the elements aren't inserted if present db.sadd(username + APPEND_KEY_TAG, tag) # Add to global _insert_tags_global(tag)
def add_favourite(username, post_id): """ Adds the specified post to the user's set of favourites. """ if _is_user_created(username): post_id = str(post_id) if _is_post_created(post_id): # If the id is already present the insertion is ignored db.sadd(username + APPEND_KEY_FAVS, str(post_id)) return True else: return False 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 _insert_symbol_index(symbol): """ Ads a symbol to the index. """ if len(symbol) == 1: db.sadd(GLOBAL_INDEX_LETTER_TAG, symbol.upper())
def _insert_post_tag_name(post_id, tag_name): """ Inserts a post id to the set of tag-name -- post-ids. """ db.sadd(tag_name + APPEND_TAG_NAME_POSTS, str(post_id))