示例#1
0
def add(db):
    """Adds a new site to track"""
    try:
        url = request.POST.get('url')
        frequency = request.POST.get('frequency')
        fail_trigger = request.POST.get('fail_trigger')
        respond_seconds = request.POST.get('respond_seconds')
        state = check_site_state(url)
        new_id = db.execute(
            "INSERT INTO site (url, frequency, respond_seconds, \
            fail_trigger, current_state, state_changed_on, created_on) \
            VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP, \
            CURRENT_TIMESTAMP)",
            (url, frequency, respond_seconds, fail_trigger, state)).lastrowid

        # match tags or create new ones
        tags = request.POST.get('tags', "")
        if tags != "":
            tags = [tag.strip().lower() for tag in tags.strip().split(',')]
            tag_list = unique_list(tags)
            for tag in tag_list:
                if tag != "":
                    sql = "SELECT key FROM tags WHERE label = '%s'" % tag
                    tag_match = db.execute(sql)
                    tag_match = list(tag_match)
                    if tag_match:
                        # match found
                        tag_match = dict(tag_match[0])
                        tag_key = tag_match["key"]
                        link_exists = db.execute(
                            "SELECT item FROM \
                            tagged_item WHERE item = ? AND \
                            item_type = 'site' AND tag = ?",
                            (new_id, tag_key)).fetchone()
                    else:
                        # create a new tag
                        link_exists = False
                        tag_key = db.execute(
                            "INSERT INTO tags (label) \
                                        VALUES (?)", (tag, )).lastrowid

                    if not link_exists:
                        #link site to this tag
                        db.execute(
                            "INSERT INTO tagged_item (tag, item, item_type) \
                            VALUES (?, ?, 'site')", (tag_key, new_id))

            tags = tags_string(tag_list)

        return {
            'error': '',
            'url': url,
            'id': new_id,
            'state': state,
            'tags': tags,
        }
    except Exception, err:
        return {'error': str(err)}
示例#2
0
def add(db):
    """Adds a new site to track"""
    try:
        url = request.POST.get('url')
        frequency = request.POST.get('frequency')
        fail_trigger = request.POST.get('fail_trigger')
        respond_seconds = request.POST.get('respond_seconds')
        state = check_site_state(url)
        new_id = db.execute(
            "INSERT INTO site (url, frequency, respond_seconds, \
            fail_trigger, current_state, state_changed_on, created_on) \
            VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP, \
            CURRENT_TIMESTAMP)", (url, frequency, respond_seconds, fail_trigger,
            state)).lastrowid

        # match tags or create new ones
        tags = request.POST.get('tags', "")
        if tags != "":
            tags = [tag.strip().lower() for tag in tags.strip().split(',')]
            tag_list = unique_list(tags)
            for tag in tag_list:
                if tag != "":
                    sql = "SELECT key FROM tags WHERE label = '%s'" % tag
                    tag_match = db.execute(sql)
                    tag_match = list(tag_match)
                    if tag_match:
                        # match found
                        tag_match = dict(tag_match[0])
                        tag_key = tag_match["key"]
                        link_exists = db.execute("SELECT item FROM \
                            tagged_item WHERE item = ? AND \
                            item_type = 'site' AND tag = ?",
                            (new_id, tag_key)).fetchone()
                    else:
                        # create a new tag
                        link_exists = False
                        tag_key = db.execute("INSERT INTO tags (label) \
                                        VALUES (?)", (tag,)).lastrowid

                    if not link_exists:
                        #link site to this tag
                        db.execute(
                            "INSERT INTO tagged_item (tag, item, item_type) \
                            VALUES (?, ?, 'site')", (tag_key, new_id))

            tags = tags_string(tag_list)

        return {
            'error': '', 'url': url,
            'id': new_id, 'state': state,
            'tags': tags,
        }
    except Exception, err:
        return {'error': str(err)}
示例#3
0
def edit(db):
    """Edits a site"""
    try:
        pk = request.POST.get('id')
        url = request.POST.get('url')
        frequency = request.POST.get('frequency')
        fail_trigger = request.POST.get('fail_trigger')
        respond_seconds = request.POST.get('respond_seconds')

        state = check_site_state(url)
        db.execute(
            "UPDATE site SET url = ?, frequency = ?, fail_trigger = ?, \
            respond_seconds = ?, current_state = ?, \
            state_changed_on = CURRENT_TIMESTAMP WHERE id = ?",
            (url, int(frequency), int(fail_trigger),
                int(respond_seconds), state, int(pk)))

        # reset tags
        db.execute(
            "DELETE FROM tagged_item WHERE item = ? \
            AND item_type = 'site'", (pk,))
        # match tags or create new ones
        tags = request.POST.get('tags', "")
        if tags != "":
            tags = [tag.strip().lower() for tag in tags.strip().split(',')]
            tag_list = unique_list(tags)
            for tag in tag_list:
                if tag != "":
                    sql = "SELECT key FROM tags WHERE label = '%s'" % tag
                    tag_match = db.execute(sql)
                    tag_match = list(tag_match)
                    if tag_match:
                        # match found
                        tag_match = dict(tag_match[0])
                        tag_key = tag_match["key"]
                        link_exists = db.execute("SELECT item FROM \
                                tagged_item WHERE item = ? AND \
                                item_type = 'site' AND tag = ?",
                                (pk, tag_key)).fetchone()
                    else:
                        # create a new tag
                        tag_key = db.execute("INSERT INTO tags (label) \
                                        VALUES (?)", (tag,)).lastrowid

                    if not link_exists:
                        #link site to this tag
                        db.execute(
                            "INSERT INTO tagged_item (tag, item, item_type) \
                            VALUES (?, ?, 'site')", (tag_key, pk))

            tags = tags_string(tag_list)

        #delete tags with no tagged item
        all_tags = db.execute("SELECT tags.* FROM tags").fetchall()
        for tag in all_tags:
            items = db.execute("SELECT * FROM tagged_item \
                WHERE tagged_item.tag = ?", (tag['key'],)).fetchone()
            if not items:
                db.execute("DELETE FROM tags WHERE key = ?",
                    (tag['key'],))

        return {
            'error': '', 'url': url,
            'id': pk, 'state': state,
            'tags': tags,
        }
    except Exception, err:
        return {'error': str(err)}
示例#4
0
def edit(db):
    """Edits a site"""
    try:
        pk = request.POST.get('id')
        url = request.POST.get('url')
        frequency = request.POST.get('frequency')
        fail_trigger = request.POST.get('fail_trigger')
        respond_seconds = request.POST.get('respond_seconds')

        state = check_site_state(url)
        db.execute(
            "UPDATE site SET url = ?, frequency = ?, fail_trigger = ?, \
			respond_seconds = ?, current_state = ?, \
			state_changed_on = CURRENT_TIMESTAMP WHERE id = ?",
            (url, int(frequency), int(fail_trigger), int(respond_seconds),
             state, int(pk)))

        # reset tags
        db.execute(
            "DELETE FROM tagged_item WHERE item = ? \
			AND item_type = 'site'", (pk, ))
        # match tags or create new ones
        tags = request.POST.get('tags', "")
        if tags != "":
            tags = [tag.strip().lower() for tag in tags.strip().split(',')]
            tag_list = unique_list(tags)
            for tag in tag_list:
                if tag != "":
                    sql = "SELECT key FROM tags WHERE label = '%s'" % tag
                    tag_match = db.execute(sql)
                    tag_match = list(tag_match)
                    if tag_match:
                        # match found
                        tag_match = dict(tag_match[0])
                        tag_key = tag_match["key"]
                        link_exists = db.execute(
                            "SELECT item FROM \
								tagged_item WHERE item = ? AND \
								item_type = 'site' AND tag = ?", (pk, tag_key)).fetchone()
                    else:
                        # create a new tag
                        tag_key = db.execute(
                            "INSERT INTO tags (label) \
										VALUES (?)", (tag, )).lastrowid

                    if not link_exists:
                        #link site to this tag
                        db.execute(
                            "INSERT INTO tagged_item (tag, item, item_type) \
							VALUES (?, ?, 'site')", (tag_key, pk))

            tags = tags_string(tag_list)

        #delete tags with no tagged item
        all_tags = db.execute("SELECT tags.* FROM tags").fetchall()
        for tag in all_tags:
            items = db.execute(
                "SELECT * FROM tagged_item \
				WHERE tagged_item.tag = ?", (tag['key'], )).fetchone()
            if not items:
                db.execute("DELETE FROM tags WHERE key = ?", (tag['key'], ))

        return {
            'error': '',
            'url': url,
            'id': pk,
            'state': state,
            'tags': tags,
        }
    except Exception, err:
        return {'error': str(err)}