Beispiel #1
0
def delete_category(id):
    if request.method == 'OPTIONS':
        return setHTTPResponse(status=200)
    id = convert_to_integer(id)
    if id == ErrorMessages.NOT_NUMBER:
        return setHTTPResponse(status=406)
    c = db.query(Category).get(id)
    _fire_hydrants = db.query(FireHydrant).filter_by(category_id=id)
    if _fire_hydrants:
        for fh in _fire_hydrants:
            try:
                db.delete(fh)
                db.commit()
            except:
                db.rollback()
                traceback.print_exc()
                return setHTTPResponse(500, body={"msg": "CHILD_FAILED"})

    if not c:
        return setHTTPResponse(status=404)
    try:
        db.delete(c)
        db.commit()
        return setHTTPResponse(status=200)
    except:
        db.rollback()
        traceback.print_exc()
        return setHTTPResponse(status=500)
Beispiel #2
0
def new_fire_hydrant():
    if request.method == "OPTIONS":
        return setHTTPResponse(status=200)
    req_flag = False
    for req in required_values:
        if not req in request.forms:
            req_flag = True
    if req_flag:
        return setHTTPResponse(status=400)
    cat_id = request.forms.get('category_id')
    lat = request.forms.get('latitude')
    long = request.forms.get('longitude')
    description = ""
    try:
        description = request.forms.get('description')
    except:
        pass
    trunk_line = ""
    try:
        trunk_line = request.forms.get('trunk_line_diameter')
    except:
        pass
    fh = FireHydrant(description, trunk_line, cat_id, lat, long)
    errs = fh.validate()
    if errs:
        return setHTTPResponse(status=406, body=errs)
    try:
        db.add(fh)
        db.commit()
        response.content_type = "application/json"
        return fh.to_json()
    except:
        db.rollback()
        traceback.print_exc()
        return setHTTPResponse(status=500)
Beispiel #3
0
def update_fire_hydrant(id):
    if request.method == "OPTIONS":
        return setHTTPResponse(status=200)
    id = convert_to_integer(id)
    if id == ErrorMessages.NOT_NUMBER:
        return setHTTPResponse(status=406,
                               body=json.dumps({'id': 'NOT_NUMBER'}))
    req_flag = False
    for req in required_values:
        if not req in request.forms:
            req_flag = True
    if req_flag:
        return setHTTPResponse(status=400,
                               body=json.dumps({'msg': 'REQUIRED_VALUES'}))
    cat_id = request.forms.get('category_id')
    lat = request.forms.get('latitude')
    long = request.forms.get('longitude')
    description = ""
    try:
        description = request.forms.get('description')
    except:
        pass
    trunk_line = ""
    try:
        trunk_line = request.forms.get('trunk_line_diameter')
    except:
        pass
    fh = db.query(FireHydrant).get(id)
    if not fh:
        return setHTTPResponse(status=404)
    #Store previous values
    _cat_id = fh.category_id
    _lat = fh.latitude
    _long = fh.longitude
    _desc = fh.description
    _trunk_line = fh.trunk_line_diameter
    #Update with new values
    fh.category_id = cat_id
    fh.description = description
    fh.latitude = lat
    fh.longitude = long
    fh.trunk_line_diameter = trunk_line
    errs = fh.validate()
    if errs:
        #Restore previuous values
        fh.category_id = _cat_id
        fh.latitude = _lat
        fh.longitude = _long
        fh.description = _desc
        fh.trunk_line_diameter = _trunk_line
        return setHTTPResponse(status=406, body=errs)
    else:
        try:
            db.commit()
            response.content_type = "application/json"
            return fh.to_json()
        except:
            db.rollback()
            traceback.print_exc()
            return setHTTPResponse(status=500)
Beispiel #4
0
def new_fire_hydrant():
    if request.method == "OPTIONS":
        return setHTTPResponse(status=200)
    req_flag = False
    for req in required_values:
        if not req in request.forms:
            req_flag = True
    if req_flag:
        return setHTTPResponse(status=400)
    cat_id = request.forms.get('category_id')
    lat = request.forms.get('latitude')
    long = request.forms.get('longitude')
    description = ""
    try:
        description = request.forms.get('description')
    except:
        pass
    trunk_line = ""
    try:
        trunk_line = request.forms.get('trunk_line_diameter')
    except:
        pass
    fh = FireHydrant(description, trunk_line, cat_id, lat, long)
    errs = fh.validate()
    if errs:
        return setHTTPResponse(status=406, body=errs)
    try:
        db.add(fh)
        db.commit()
        response.content_type = "application/json"
        return fh.to_json()
    except:
        db.rollback()
        traceback.print_exc()
        return setHTTPResponse(status=500)
Beispiel #5
0
def update_category(id):
    if request.method == 'OPTIONS':
        return setHTTPResponse(status=200)
    id = convert_to_integer(id)
    if id == ErrorMessages.NOT_NUMBER:
        return setHTTPResponse(status=406,
                               body=json.dumps({'id': 'NOT_NUMBER'}))
    if not "name" in request.forms:
        return setHTTPResponse(status=400)
    name = request.forms.get("name")
    _name = db.query(Category).filter_by(name=name).first()
    if _name:
        return setHTTPResponse(status=409,
                               body=json.dumps({'name': 'DUBLICATE'}))
    c = db.query(Category).get(id)
    c.name = name
    if not c.validate():
        try:
            db.commit()
            response.content_type = "application/json"
            return c.to_json()
        except Exception as err:
            db.rollback()
            traceback.print_exc()
            return setHTTPResponse(status=500)
    return setHTTPResponse(status=406, body=json.dumps(c.validate()))
Beispiel #6
0
def delete_category(id):
    if request.method == 'OPTIONS':
        return setHTTPResponse(status=200)
    id = convert_to_integer(id)
    if id == ErrorMessages.NOT_NUMBER:
        return setHTTPResponse(status=406)
    c = db.query(Category).get(id)
    _fire_hydrants = db.query(FireHydrant).filter_by(category_id=id)
    if _fire_hydrants:
        for fh in _fire_hydrants:
            try:
                db.delete(fh)
                db.commit()
            except:
                db.rollback()
                traceback.print_exc()
                return setHTTPResponse(500, body={"msg": "CHILD_FAILED"})

    if not c:
        return setHTTPResponse(status=404)
    try:
        db.delete(c)
        db.commit()
        return setHTTPResponse(status=200)
    except:
        db.rollback()
        traceback.print_exc()
        return setHTTPResponse(status=500)
Beispiel #7
0
def update_fire_hydrant(id):
    if request.method == "OPTIONS":
        return setHTTPResponse(status=200)
    id = convert_to_integer(id)
    if id == ErrorMessages.NOT_NUMBER:
        return setHTTPResponse(status=406, body=json.dumps({'id': 'NOT_NUMBER'}))
    req_flag = False
    for req in required_values:
        if not req in request.forms:
            req_flag = True
    if req_flag:
        return setHTTPResponse(status=400, body=json.dumps({'msg': 'REQUIRED_VALUES'}))
    cat_id = request.forms.get('category_id')
    lat = request.forms.get('latitude')
    long = request.forms.get('longitude')
    description = ""
    try:
        description = request.forms.get('description')
    except:
        pass
    trunk_line = ""
    try:
        trunk_line = request.forms.get('trunk_line_diameter')
    except:
        pass
    fh = db.query(FireHydrant).get(id)
    if not fh:
        return setHTTPResponse(status=404)
    #Store previous values
    _cat_id= fh.category_id
    _lat = fh.latitude
    _long = fh.longitude
    _desc = fh.description
    _trunk_line = fh.trunk_line_diameter
    #Update with new values
    fh.category_id = cat_id
    fh.description = description
    fh.latitude = lat
    fh.longitude = long
    fh.trunk_line_diameter = trunk_line
    errs = fh.validate()
    if errs:
        #Restore previuous values
        fh.category_id = _cat_id
        fh.latitude = _lat
        fh.longitude = _long
        fh.description = _desc
        fh.trunk_line_diameter = _trunk_line
        return setHTTPResponse(status=406, body=errs)
    else:
        try:
            db.commit()
            response.content_type = "application/json"
            return fh.to_json()
        except:
            db.rollback()
            traceback.print_exc()
            return setHTTPResponse(status=500)
Beispiel #8
0
def write_to_db(location_bool, coordinates, ip_addr): 
    if location_bool is None or coordinates is None or ip_addr is None:
        return
    elif not re.search("true|false|NULL", location_bool, re.I|re.M):
        logger.log("ERROR", str(location_bool) + " is not a known mode.")
    elif not re.search("\A\((\d|\-\d)+\.\d+,\s(\d|\-\d)+\.\d+\)|NULL", coordinates, re.M | re.I): 
        logger.log("ERROR", "Improper coordinate format -> " + str(coordinates) + ".")
    elif not re.search("\A\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$|NULL", ip_addr, re.M|re.I):
        logger.log("ERROR", "Improper ip address format -> " + str(ip_addr) + ".") 
    else:
        coor = re.sub("[\(\)]", "", str(coordinates))
        db.execute("insert into connected (location_bool, coordinates, ip_addr) values(\"" + str(location_bool) + "\", \"" + str(coor) + "\", \"" + str(ip_addr) + "\")")
        db.commit()
Beispiel #9
0
def delete_fire_hydrant(id):
    if request.method == "OPTIONS":
        return setHTTPResponse(status=200)
    id = convert_to_integer(id)
    if id == ErrorMessages.NOT_NUMBER:
        return setHTTPResponse(status=406)
    fh = db.query(FireHydrant).get(id)
    if not fh:
        return setHTTPResponse(status=404)
    try:
        db.delete(fh)
        db.commit()
        return setHTTPResponse(status=200)
    except Exception:
        db.rollback()
        traceback.print_exc()
        return setHTTPResponse(status=500)
Beispiel #10
0
def delete_fire_hydrant(id):
    if request.method == "OPTIONS":
        return setHTTPResponse(status=200)
    id = convert_to_integer(id)
    if id == ErrorMessages.NOT_NUMBER:
        return setHTTPResponse(status=406)
    fh = db.query(FireHydrant).get(id)
    if not fh:
        return setHTTPResponse(status=404)
    try:
        db.delete(fh)
        db.commit()
        return setHTTPResponse(status=200)
    except Exception:
        db.rollback()
        traceback.print_exc()
        return setHTTPResponse(status=500)
Beispiel #11
0
    def update_db(self):
        db.multiexec("INSERT OR IGNORE INTO guilds (GuildID) VALUES (?)",
                     ((guild.id, ) for guild in self.guilds))

        db.multiexec("INSERT OR IGNORE INTO exp (USerID) VALUES (?)",
                     ((member.id, ) for guild in self.guilds
                      for member in guild.members if not member.bot))

        to_remove = []
        stored_members = db.column("SELECT UserID FROM exp")
        for id_ in stored_members:
            if not self.guild.get_member(id_):
                to_remove.append(id_)

        db.multiexec("DELETE FROM exp WHERE UserID = ?",
                     ((id_, ) for id_ in to_remove))

        db.commit()
Beispiel #12
0
def new_category():
    if "name" in request.forms:
        name = request.forms.getunicode('name')
        print type(name)
        _name = db.query(Category).filter_by(name=name).first()
        if _name:
            return setHTTPResponse(status=409, body=json.dumps({"name": "DUBLICATE"}))
        c = Category(name)
        if not c.validate():
            try:
                db.add(c)
                db.commit()
                response.content_type = "application/json"
                return c.to_json()

            except Exception as err:
                db.rollback()
                traceback.print_exc()
                return setHTTPResponse(status=500)
        return setHTTPResponse(status=406, body=json.dumps(c.validate()))
    return setHTTPResponse(status=400)
Beispiel #13
0
    async def update_db(self):
        print("Updating db...")
        
        db.multiexec("INSERT OR IGNORE INTO guilds (guild_id, name) VALUES (?, ?);",
            ((guild.id, guild.name) for guild in self.guilds))

        # had to change the old guild.members to use an async alternative 
        for guild in self.guilds:
            async for member in guild.fetch_members(): 
                if not member.bot:
                    db.execute("""INSERT OR IGNORE INTO members 
                    (guild_id, member_id, username, nickname, discriminator, joined_date) 
                    VALUES (?, ?, ?, ?, ?, ?);""",
                    member.guild.id,  member.id, member.name, member.nick, member.discriminator, 
                    member.joined_at)
                    db.execute("INSERT OR IGNORE INTO member_exp (guild_id, member_id) VALUES (?, ?)", member.guild.id, member.id)
                    db.execute("INSERT OR IGNORE INTO member_points (guild_id, member_id) VALUES (?, ?)", member.guild.id, member.id)
                    db.execute("INSERT OR IGNORE INTO osu (member_id) VALUES (?)", member.id)
        
        db.commit()
        print("Done.")
Beispiel #14
0
def new_category():
    if "name" in request.forms:
        name = request.forms.getunicode('name')
        print type(name)
        _name = db.query(Category).filter_by(name=name).first()
        if _name:
            return setHTTPResponse(status=409,
                                   body=json.dumps({"name": "DUBLICATE"}))
        c = Category(name)
        if not c.validate():
            try:
                db.add(c)
                db.commit()
                response.content_type = "application/json"
                return c.to_json()

            except Exception as err:
                db.rollback()
                traceback.print_exc()
                return setHTTPResponse(status=500)
        return setHTTPResponse(status=406, body=json.dumps(c.validate()))
    return setHTTPResponse(status=400)
Beispiel #15
0
def update_category(id):
    if request.method == 'OPTIONS':
        return setHTTPResponse(status=200)
    id = convert_to_integer(id)
    if id == ErrorMessages.NOT_NUMBER:
        return setHTTPResponse(status=406, body=json.dumps({'id': 'NOT_NUMBER'}))
    if not "name" in request.forms:
        return setHTTPResponse(status=400)
    name = request.forms.get("name")
    _name = db.query(Category).filter_by(name=name).first()
    if _name:
        return setHTTPResponse(status=409, body=json.dumps({'name': 'DUBLICATE'}))
    c = db.query(Category).get(id)
    c.name = name
    if not c.validate():
        try:
            db.commit()
            response.content_type = "application/json"
            return c.to_json()
        except Exception as err:
            db.rollback()
            traceback.print_exc()
            return setHTTPResponse(status=500)
    return setHTTPResponse(status=406, body=json.dumps(c.validate()))
Beispiel #16
0
def update_db(column,value):
    if column is None or value is None:
        return
    try:
        if read_from_db('location_bool') is None or read_from_db('coordinates') is None or read_from_db('ip_addr') is None:
            logger.log("ERROR", "You must write to the database first before updating!")
            return
        elif re.search("true|false", value, re.I|re.M) and column == 'location_bool':
            db.execute("update connected set location_bool = \"" + value + "\"")
            db.commit()
        elif re.search("\A(\d|\-\d)+\.\d+,\s(\d|\-\d)+\.\d+", value, re.M | re.I) and column == 'coordinates':    
            db.execute("update connected set coordinates = \"" + value + "\"")
            db.commit()
        elif re.search("\A\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$", value, re.M|re.I) and column == 'ip_addr':
            db.execute("update connected set ip_addr = \"" + value + "\"")
            db.commit()
        else:
            logger.log("ERROR", str(column) + " is not a known column for the connected table in the imagecapture db.")
            return
    except sqlite3.OperationalError:
      logger.log("ERROR", "The database is lock, could not add coordinates to DB.")