Example #1
0
def modify_sensor(new_sensor):
	""" Modifies the sensor's data."""
	db.hset(KEY_SENSORS + new_sensor[S_ID], S_NAME, new_sensor[S_NAME])
	db.hset(KEY_SENSORS + new_sensor[S_ID], S_SIGNAL, new_sensor[S_SIGNAL])
	db.hset(KEY_SENSORS + new_sensor[S_ID], S_PIN, str(new_sensor[S_PIN]))
	db.hset(KEY_SENSORS + new_sensor[S_ID], S_DIRECTION, new_sensor[S_DIRECTION])
	db.hset(KEY_SENSORS + new_sensor[S_ID], S_REFRESH, str(new_sensor[S_REFRESH]))
Example #2
0
def change_email(username, new_email):
    """ Changes the user's email. Returns False if the user is not created. """
    if _is_user_created(username):
        db.hset(username + APPEND_KEY_USER, KEY_EMAIL, new_email)
        # Generate the md5 also.
        db.hset(username + APPEND_KEY_USER, KEY_HASH,
                hashlib.md5(new_email.encode('utf-8')).hexdigest())
        return True
    else:
        return False
Example #3
0
def change_password(username, new_pass): #OK
    """
     Changes the user's password. Returns False if the db hasn't that user.
    """
    debug("CHANGE " + username + "'s password to " + new_pass)
    if _is_user_created(username):
        db.hset(username + APPEND_KEY_USER, KEY_PASSWORD,
                generate_password_hash(new_pass))
        return True
    else:
        return False
Example #4
0
def modify_node(new_node):
	""" Modifies simple data (name, board_type, nic) from the node. """
	db.hset(KEY_NODES + str(new_node[N_ID]), N_NAME, new_node[N_NAME])
	db.hset(KEY_NODES + str(new_node[N_ID]), N_BOARD, new_node[N_BOARD])
	db.hset(KEY_NODES + str(new_node[N_ID]), N_NIC, new_node[N_NIC])
	if new_node.get(N_ADDR, None) != None:
		db.hset(KEY_NODES + str(new_node[N_ID]), N_ADDR, new_node[N_ADDR])
	return True
Example #5
0
def _insert_title_ss(title, post_id): # CHANGE - see post UPDATE
    """
    Inserts a title to the sorted set of titles (later on used to find all the
    posts with the given title), and associates the title with the post id.
    """
    title = title.upper()
    # pipe = db.pipeline()
    keys = db.hkeys(title + APPEND_SEARH_POSTS_TITLE_GET_IDS)
    # We simulate a list of post-ids: the fields of the hash are incremented
    # by one with each post_id.
    if keys == None or len(keys) == 0:
        db.hset(title + APPEND_SEARH_POSTS_TITLE_GET_IDS,
                "0", str(post_id))
    else:
        db.hset(title + APPEND_SEARH_POSTS_TITLE_GET_IDS, int(max(keys)) + 1,
                str(post_id))
    db.zadd(SEARCH_POSTS_TITLE, 0, title)
Example #6
0
def update_post(post, post_id, username): #OK
    """
    Updates a post in the db.
    """
    post_id = str(post_id)
    debug("UPDATE POST. username:"******",post:" + post_id + \
         "\n\t values:" + str(post))
    # The title has changed.
    if post[KEY_TITLE] is not None:
        # ------------------------------------------------------------------
        # The title has changed, so CHANGE in the structure of search-title
        debug("\t-UPDATE title")
        db.hset(post_id + APPEND_KEY_POSTS, KEY_TITLE, post[KEY_TITLE])
        ## Add post's title to the title search sorted set
        _insert_title_ss(post[KEY_TITLE], post_id)
    # The content has changed
    if post[KEY_CONTENTS] is not None:
        debug("\t-UPDATE contents")
        db.hset(post_id + APPEND_KEY_POSTS, KEY_CONTENTS, post[KEY_CONTENTS])
    # The tags have changed.
    if post[KEY_TAGS] is not None:
        debug("\t-UPDATE tags")
        # tag_id = db.hget(post_id + APPEND_KEY_POSTS, KEY_TAGS)
        # Get the old set of tags from the post
        tags_used = dict([(i, False) for i in get_post_tags(post_id)])
        # Check the ones that are used and insert the new ones.
        for tag in post[KEY_TAGS]:
            # The tag does not exist
            if tags_used.get(tag) == None:
                if len(tag) > 0:
                    debug("\t new tag on post: " + tag)
                    insert_tag_post_tags(post_id, tag)
            else:
                tags_used[tag] = True
        # If an old tag is not used it has been deleted.
        for tag in tags_used.keys():
            if not tags_used[tag]:
                debug("\t deleting unused tag: " + tag)
                delete_tag_from_post(post_id, tag)

    return get_post(post_id)
Example #7
0
def _insert_sensor(sensor):
	"""
	Inserts a sensor to the db.
	Returns the new generated id for the sensor.
	"""
	debug(CODE, "INSERT SENSOR: params (sensor):" + str(sensor))
	id = _increment_key_sensor()
	debug(CODE, "INSERT SENSOR: generated id:" + str(id))
	db.hset(KEY_SENSORS + id, S_ID, id)
	db.hset(KEY_SENSORS + id, S_NAME, sensor[S_NAME])
	db.hset(KEY_SENSORS + id, S_SIGNAL, sensor[S_SIGNAL])
	db.hset(KEY_SENSORS + id, S_PIN, str(sensor[S_PIN]))
	db.hset(KEY_SENSORS + id, S_DIRECTION, sensor[S_DIRECTION])
	if sensor.get(S_REFRESH, None) != None and len(str(sensor[S_REFRESH])) > 0:
		db.hset(KEY_SENSORS + id, S_REFRESH, str(sensor[S_REFRESH]))
	return id
Example #8
0
def insert_node(node):
	"""
	Inserts a node into the db.
	If the node has sensors they are also inserted.
	Returns the new node's id
	"""
	debug(CODE, "INSERT NODE: (node): " + str(node))
	id = _increment_key_node()
	debug(CODE, "INSERT NODE: assigned id: " + str(id))
	db.hset(KEY_NODES + id, N_ID, id)
	db.hset(KEY_NODES + id, N_NAME, node[N_NAME])
	db.hset(KEY_NODES + id, N_BOARD, node[N_BOARD])
	db.hset(KEY_NODES + id, N_NIC, node[N_NIC])
	db.hset(KEY_NODES + id, N_SENSORS, id)
	if node.get(N_ADDR, None) != None:
		debug(CODE, "INSERT NODE: The node has an address")
		db.hset(KEY_NODES + id, N_ADDR, node[N_ADDR])
	if node.get(N_SENSORS) != None:
		debug(CODE, "INSERT NODE: The node has sensors")
		for sensor in node[N_SENSORS]:
			insert_sensor_to_node(id, sensor)
	return id
Example #9
0
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)