class BikeUsageAll(Resource):
    def __init__(self):
        self.sql = SQLConn()

    def get_bikes(self, bike_ids):
        #query bikes info
        BIKE_INFO = "SELECT * FROM `spin_bikes` WHERE sb_id =" + str(
            bike_ids[0])
        for bike_id in bike_ids[1:]:
            BIKE_INFO += (" OR sb_id =" + str(bike_id))
        bikes_info = self.sql.select_query(BIKE_INFO)
        return bikes_info

    def get_locations(self, location_ids):
        #query location info
        LOCATION_INFO = "SELECT * FROM `location` WHERE l_id = " + str(
            location_ids[0])
        for location_id in location_ids[1:]:
            LOCATION_INFO += (" OR l_id =" + str(location_id))
        location_info = self.sql.select_query(LOCATION_INFO)
        return location_info

    def get(self):
        #obtain total usage right away and goes backwards to locations
        ALL_BIKE_USAGE = "SELECT * FROM `bike_usage`"
        bike_usage_entries = self.sql.select_query(ALL_BIKE_USAGE)

        convert_dict_datetime_str(
            bike_usage_entries, 'start_time')  #cast date type to str for json

        #group usage entries by bike_id to know which bikes need to be queried
        bike_usage_dict = {}
        for b_id, g in groupby(bike_usage_entries, lambda x: x['sb_id']):
            bike_usage_dict[b_id] = list(g)
        bikes_ids = list(bike_usage_dict.keys())

        bikes_info = self.get_bikes(bikes_ids)  #get the needed bikes

        convert_dict_datetime_str(
            bikes_info, 'last_battery_change')  #cast date type to str for json

        #group bikes by location id to know which locations need to be queried
        location_dict = {}
        for l_id, g in groupby(bikes_info, lambda x: x['l_id']):
            location_dict[l_id] = list(g)
        location_ids = list(location_dict.keys())

        location_info = self.get_locations(
            location_ids)  #get the needed locations

        #Build the dictionnary object to dump to a json object
        for bike in bikes_info:
            bike['usage'] = bike_usage_dict[
                bike['sb_id']]  # add the bike usage to the bike dictionnary

        for location in location_info:
            location['bikes'] = location_dict[
                location['l_id']]  # add the bikes to the location dictionnary
        return location_info
예제 #2
0
class Schedule(Resource):
    def __init__(self):
        self.sql = SQLConn()

    def get(self, bm_id):
        SELECT_SCHEDULE = "SELECT * FROM `schedule` WHERE `bm_id`={bm_id}".format(
            bm_id=bm_id)
        schedule_of_manager = self.sql.select_query(SELECT_SCHEDULE)
        convert_and_trim(schedule_of_manager
                         )  #cast date type to str for json and remove None
        return schedule_of_manager
class AccountCreationInsertQuery(Resource):

    def __init__(self):
        self.sql = SQLConn()

    #Form data must be in the form:
    # {"key": "value", "key": "value", ...} 
    def put(self):
        INSERT_USER = "******"
        data = request.form['data']
        data = json.loads(data)
        #Do check to make sure not already in db
        try:
            self.sql.insert_query(INSERT_USER.format(
                email=data["email"],
                pwd=data["pwd"],
                bm_name=data["bm_name"],
                role=data["role"]
            ))
        except Exception as e:
            return False
        return True
예제 #4
0
class UsageByIDSelect(Resource):
    def __init__(self):
        self.sql = SQLConn()

    def to_serializable(self, val):
        if isinstance(val, datetime.datetime):
            return val.isoformat()

    def get(self, bike_id):
        SELECT = "SELECT * FROM `bike_usage` WHERE `sb_id`={bike_id}".format(
            bike_id=bike_id)
        result = self.sql.select_query(SELECT)
        result = json.dumps(result, default=self.to_serializable)
        return result
예제 #5
0
class ScheduleAll(Resource):
    def __init__(self):
        self.sql = SQLConn()

    def get(self):
        SELECT_ALL_SCHEDULE = "SELECT * FROM `schedule`"
        schedule_entries = self.sql.select_query(SELECT_ALL_SCHEDULE)
        convert_and_trim(
            schedule_entries)  #cast date type to str for json and remove None

        #group schedule by manager id
        managers_schedule_dict = {}
        schedule_entries.sort(key=lambda x: x['bm_id'])
        for bm_id, g in groupby(schedule_entries, lambda x: x['bm_id']):
            managers_schedule_dict[bm_id] = list(g)

        return managers_schedule_dict
예제 #6
0
class Managers(Resource):

    CARETAKER = "caretaker"
    ADMIN = "admin"

    def __init__(self):
        self.sql = SQLConn()

    def get(self):
        #obtain all managers from database
        ALL_MANAGERS = "SELECT * FROM `bike_manager`"
        all_managers_entries = self.sql.select_query(ALL_MANAGERS)

        #group managers by caretakers or admin
        managers_dict = {}
        for role, g in groupby(all_managers_entries, lambda x: x['role']):
            managers_dict[role] = list(g)

        return managers_dict
 def __init__(self):
     self.sql = SQLConn()