def account_info(admin_key):
    payload = {}
    if request.method == 'GET':
        if not userManager.verify_key(admin_key):
            resp = jsonify(payload)
            resp.status_code = 401
            return resp

        mydb = mysql.connector.connect(
            host=os.environ.get('MYSQL_HOST'),
            user=os.environ.get('MYSQL_USER'),
            password=os.environ.get('MYSQL_PASSWORD'),
            database=os.environ.get('MYSQL_DATABASE'))

        mycursor = mydb.cursor()
        sql = "SELECT * FROM user"
        mycursor.execute(sql)
        result = mycursor.fetchall()
        user = result[0]

        payload["username"] = user[1]
        payload["email"] = user[2]

        resp = jsonify(payload)
        resp.status_code = 200
        return resp
    else:
        resp = jsonify(payload)
        resp.status_code = 405
        return resp
Beispiel #2
0
def remove_images(id, removals, admin_key):
	payload = {}

	if request.method == 'DELETE':
		if not userManager.verify_key(admin_key):
			resp = jsonify(payload)
			resp.status_code = 401
			return resp

		path = os.path.join(current_app.root_path, 'static', 'picture_files', str(id))
		re_path = os.path.join(current_app.root_path, 'static', 'picture_files', str(id), 'resized')
		if os.path.isdir(path):

			# Parse removals
			rem_list = removals.split(",")
			# fix index and put in a set
			rem_set = set()
			for index, value in enumerate(rem_list):
				rem_set.add(str(int(rem_list[index])-1))

			# Remove the image files in there
			all_files = [f for f in listdir(path) if isfile(join(path, f))]
			for file in all_files:
				f_name, f_ext = os.path.splitext(file)
				if f_name in rem_set:
					file_path = os.path.join(path, file)
					re_file_path = os.path.join(re_path, file) # Resized names are the same as the original
					os.remove(file_path)
					os.remove(re_file_path)

			# readjust the file names
			all_files = [f for f in listdir(path) if isfile(join(path, f))]
			count = 0
			for file in all_files:
				f_name, f_ext = os.path.splitext(file)
				# Original
				src_path = os.path.join(path, file)
				dst_path = os.path.join(path, str(count) + f_ext)
				os.rename(src_path, dst_path)

				# Resized
				src_path = os.path.join(re_path, file)
				dst_path = os.path.join(re_path, str(count) + f_ext)
				os.rename(src_path, dst_path)
				count += 1

			resp = jsonify(payload)
			resp.status_code = 204
			return resp
		else:
			resp = jsonify(payload)
			resp.status_code = 400
			return resp
	else:
		resp = jsonify(payload)
		resp.status_code = 405
		return resp
Beispiel #3
0
def device_location_img_count(id, admin_key):
	payload = {}
	if request.method == 'GET':
		if not userManager.verify_key(admin_key):
			resp = jsonify(payload)
			resp.status_code = 401
			return resp

		mydb = mysql.connector.connect(
			host=os.environ.get('MYSQL_HOST'),
			user=os.environ.get('MYSQL_USER'),
			password=os.environ.get('MYSQL_PASSWORD'),
			database=os.environ.get('MYSQL_DATABASE')
		)
		mycursor = mydb.cursor()

		# Grab device
		sql = "SELECT * FROM device WHERE id = %s"
		val = (str(id),)
		mycursor.execute(sql,val)
		result = mycursor.fetchall()

		if len(result) > 0:
			# Grab the settings
			sql = "SELECT * FROM settings WHERE id = " + str(id)
			mycursor.execute(sql)
			result = mycursor.fetchall()

			# Put the location from settings
			if len(result) > 0:
				payload["location"] = result[0][5]
			else:
				payload["location"] = "No Settings"

			# # Grab image count
			path = os.path.join(current_app.root_path, 'static', 'picture_files', str(id))
			count = 0
			if os.path.isdir(path): # If no directory, send nothing
				all_files = [f for f in listdir(path) if isfile(join(path, f))]
				for file in all_files:
					count += 1

			payload["image_count"] = count

			resp = jsonify(payload)
			resp.status_code = 200
			return resp
		else:
			resp = jsonify(payload)
			resp.status_code = 400
			return resp
	else:
		resp = jsonify(payload)
		resp.status_code = 405
		return resp
def device_settings(id, admin_key):
    payload = {}

    if request.method == 'GET':
        if not userManager.verify_key(admin_key):
            resp = jsonify(payload)
            resp.status_code = 401
            return resp

        mydb = mysql.connector.connect(
            host=os.environ.get('MYSQL_HOST'),
            user=os.environ.get('MYSQL_USER'),
            password=os.environ.get('MYSQL_PASSWORD'),
            database=os.environ.get('MYSQL_DATABASE'))
        mycursor = mydb.cursor()

        sql = "SELECT * FROM device WHERE id = " + str(id)
        mycursor.execute(sql)
        result = mycursor.fetchall()

        if len(result) > 0:
            # Grab the settings
            sql = "SELECT * FROM settings WHERE id = " + str(id)
            mycursor.execute(sql)
            result = mycursor.fetchall()

            if len(result) > 0:
                settings = result[0]
                payload["toggle_pay"] = settings[1]
                payload["price"] = settings[2]
                payload["charge_time"] = settings[3]
                payload["time_offset"] = settings[4]
                payload["location"] = settings[5]
                payload["aspect_ratio_width"] = settings[6]
                payload["aspect_ratio_height"] = settings[7]

                resp = jsonify(payload)
                resp.status_code = 200
                return resp

            else:
                resp = jsonify(payload)
                resp.status_code = 500
                return resp

        else:
            resp = jsonify(payload)
            resp.status_code = 400
            return resp

    else:
        resp = jsonify(payload)
        resp.status_code = 405
        return resp
Beispiel #5
0
def all_devices(admin_key):
    payload = {}
    if request.method == 'GET':
        if not userManager.verify_key(admin_key):
            resp = jsonify(payload)
            resp.status_code = 401
            return resp

        mydb = mysql.connector.connect(
            host=os.environ.get('MYSQL_HOST'),
            user=os.environ.get('MYSQL_USER'),
            password=os.environ.get('MYSQL_PASSWORD'),
            database=os.environ.get('MYSQL_DATABASE'))
        mycursor = mydb.cursor()

        # Grab all devices from database
        sql = "SELECT * FROM device"
        mycursor.execute(sql)
        all_devices = mycursor.fetchall()

        list_id = []
        list_location = []
        count = 0
        for devi in all_devices:
            list_id.append(devi[0])

            # Grab the device settings
            sql = "SELECT * FROM settings WHERE id = " + str(devi[0])
            mycursor.execute(sql)
            result = mycursor.fetchall()
            if len(result) > 0:
                settings = result[0]
                list_location.append(settings[5])
            else:
                list_location.append("No Settings")

            count += 1

        payload["device_id"] = list_id
        payload["location"] = list_location
        payload["count"] = count

        resp = jsonify(payload)
        resp.status_code = 200
        return resp
    else:
        resp = jsonify(payload)
        resp.status_code = 405
        return resp
def update_account(admin_key):
    payload = {}
    if request.method == 'PUT':
        if not userManager.verify_key(admin_key):
            resp = jsonify(payload)
            resp.status_code = 401
            return resp

        # Verify Json
        # Json validation
        schema = {
            "type": "object",
            "properties": {
                "username": {
                    "type": "string"
                },
                "email": {
                    "type": "string"
                }
            }
        }
        try:
            validate(instance=request.json, schema=schema)
        except:
            resp = jsonify(payload)
            resp.status_code = 400
            return resp

        mydb = mysql.connector.connect(
            host=os.environ.get('MYSQL_HOST'),
            user=os.environ.get('MYSQL_USER'),
            password=os.environ.get('MYSQL_PASSWORD'),
            database=os.environ.get('MYSQL_DATABASE'))
        mycursor = mydb.cursor()
        sql = "UPDATE user SET username = %s, email = %s WHERE id = 1"
        val = (request.json["username"], request.json["email"])

        mycursor.execute(sql, val)

        mydb.commit()

        resp = jsonify(payload)
        resp.status_code = 200
        return resp

    else:
        resp = jsonify(payload)
        resp.status_code = 405
        return resp
Beispiel #7
0
def device_location(id, admin_key):
    payload = {}
    if request.method == 'GET':
        if not userManager.verify_key(admin_key):
            resp = jsonify(payload)
            resp.status_code = 401
            return resp

        mydb = mysql.connector.connect(
            host=os.environ.get('MYSQL_HOST'),
            user=os.environ.get('MYSQL_USER'),
            password=os.environ.get('MYSQL_PASSWORD'),
            database=os.environ.get('MYSQL_DATABASE'))
        mycursor = mydb.cursor()

        sql = "SELECT * FROM device WHERE id = " + str(id)
        mycursor.execute(sql)
        result = mycursor.fetchall()

        if len(result) > 0:
            # Grab the settings
            sql = "SELECT * FROM settings WHERE id = " + str(id)
            mycursor.execute(sql)
            result = mycursor.fetchall()

            if len(result) > 0:
                payload["location"] = result[0][5]
            else:
                payload["location"] = "No Settings"

            resp = jsonify(payload)
            resp.status_code = 200
            return resp
        else:
            resp = jsonify(payload)
            resp.status_code = 400
            return resp
    else:
        resp = jsonify(payload)
        resp.status_code = 405
        return resp
def update_device_settings(id_number, admin_key):
    payload = {}
    if request.method == 'PUT':
        if not userManager.verify_key(admin_key):
            resp = jsonify(payload)
            resp.status_code = 401
            return resp

        # Json validation
        schema = {
            "type": "object",
            "properties": {
                "toggle_pay": {
                    "type": "boolean"
                },
                "price": {
                    "type": "number"
                },
                "charge_time": {
                    "type": "number"
                },
                "time_offset": {
                    "type": "string"
                },
                "location": {
                    "type": "string"
                },
                "aspect_ratio_width": {
                    "type": "number"
                },
                "aspect_ratio_height": {
                    "type": "number"
                }
            }
        }
        try:
            validate(instance=request.json, schema=schema)
        except:
            resp = jsonify(payload)
            resp.status_code = 400
            return resp

        mydb = mysql.connector.connect(
            host=os.environ.get('MYSQL_HOST'),
            user=os.environ.get('MYSQL_USER'),
            password=os.environ.get('MYSQL_PASSWORD'),
            database=os.environ.get('MYSQL_DATABASE'))
        mycursor = mydb.cursor()

        sql = "SELECT * FROM device WHERE id_number = %s"
        val = (id_number, )
        mycursor.execute(sql, val)
        result = mycursor.fetchall()

        if len(result) > 0:
            devi = result[0]
            id = devi[0]

            # Grab the settings
            sql = "SELECT * FROM settings WHERE id = " + str(id)
            mycursor.execute(sql)
            result = mycursor.fetchall()
            settings = result[0]

            # Check if aspect ration is different so that it can resize all images
            resize = False
            if settings[6] != float(request.json["aspect_ratio_width"]) or \
             settings[7] != float(request.json["aspect_ratio_height"]):
                resize = True

            # Update settings
            sql = "UPDATE settings SET toggle_pay = %s, price = %s, charge_time = %s, time_offset = %s, location = %s, aspect_ratio_width = %s, aspect_ratio_height = %s WHERE id = %s"
            val = (request.json["toggle_pay"], request.json["price"],
                   request.json["charge_time"], request.json["time_offset"],
                   request.json["location"],
                   request.json["aspect_ratio_width"],
                   request.json["aspect_ratio_height"], id)
            mycursor.execute(sql, val)

            if resize:
                device_id = result[0][0]
                resize_all_images(request.json["aspect_ratio_width"],
                                  request.json["aspect_ratio_height"],
                                  device_id)

            mydb.commit()

            resp = jsonify(payload)
            resp.status_code = 200
            return resp
        else:
            resp = jsonify(payload)
            resp.status_code = 400
            return resp
    else:
        resp = jsonify(payload)
        resp.status_code = 405
        return resp
Beispiel #9
0
def all_sessions(id, admin_key):
    payload = {}
    if request.method == 'GET':
        if not userManager.verify_key(admin_key):
            resp = jsonify(payload)
            resp.status_code = 401
            return resp

        mydb = mysql.connector.connect(
            host=os.environ.get('MYSQL_HOST'),
            user=os.environ.get('MYSQL_USER'),
            password=os.environ.get('MYSQL_PASSWORD'),
            database=os.environ.get('MYSQL_DATABASE'))
        mycursor = mydb.cursor()

        sql = "SELECT * FROM device WHERE id = %s"
        val = (id, )
        mycursor.execute(sql, val)
        result = mycursor.fetchall()

        if len(result) > 0:
            device_id = result[0][0]

            # Grab the sessions
            sql = "SELECT * FROM session WHERE host_id = " + str(device_id)
            mycursor.execute(sql)
            all_sessions = mycursor.fetchall()

            payload["sessions"] = []

            for sess in all_sessions:
                sess_items = {}
                sess_items["id"] = sess[0]

                sess_items["duration"] = sess[1]
                sess_items["power_used"] = sess[2]
                sess_items["amount_paid"] = sess[3]

                sess_items["date_initiated_year"] = sess[4].year
                sess_items["date_initiated_month"] = sess[4].month
                sess_items["date_initiated_day"] = sess[4].day
                sess_items["date_initiated_hour"] = sess[4].hour
                sess_items["date_initiated_minute"] = sess[4].minute
                sess_items["date_initiated_second"] = sess[4].second

                sess_items["location"] = sess[5]
                sess_items["port"] = sess[6]
                sess_items["increment_size"] = sess[7]
                sess_items["increments"] = sess[8]

                payload["sessions"].append(sess_items)

            # Grab the settings
            sql = "SELECT * FROM settings WHERE id = " + str(device_id)
            mycursor.execute(sql)
            result = mycursor.fetchall()
            device_settings = result[0]

            # Add the settings to the payload as well
            settings = {}
            settings["toggle_pay"] = device_settings[1]
            settings["price"] = device_settings[2]
            settings["charge_time"] = device_settings[3]
            settings["time_offset"] = device_settings[4]
            settings["location"] = device_settings[5]
            settings["aspect_ratio_width"] = device_settings[6]
            settings["aspect_ratio_height"] = device_settings[7]

            payload["settings"] = settings

            resp = jsonify(payload)
            resp.status_code = 200
            return resp
        else:
            resp = jsonify(payload)
            resp.status_code = 400
            return resp
    else:
        resp = jsonify(payload)
        resp.status_code = 405
        return resp
Beispiel #10
0
def get_sessions(id, page, admin_key):
    items_per_page = 25

    payload = {}
    if request.method == 'GET':
        if not userManager.verify_key(admin_key):
            resp = jsonify(payload)
            resp.status_code = 401
            return resp

        mydb = mysql.connector.connect(
            host=os.environ.get('MYSQL_HOST'),
            user=os.environ.get('MYSQL_USER'),
            password=os.environ.get('MYSQL_PASSWORD'),
            database=os.environ.get('MYSQL_DATABASE'))
        mycursor = mydb.cursor()

        sql = "SELECT * FROM device WHERE id = %s"
        val = (id, )
        mycursor.execute(sql, val)
        result = mycursor.fetchall()

        if len(result) > 0:
            device_id = result[0][0]

            # Get the number of rows in sessions
            sql = "SELECT COUNT(*) FROM session"
            mycursor.execute(sql)
            result = mycursor.fetchall()
            num_of_sessions = result[0][0]
            num_pages = ceildiv(num_of_sessions, items_per_page)

            # Grab the sessions
            offset = (page - 1) * items_per_page
            sql = "SELECT * FROM session WHERE host_id = %s ORDER BY date_initiated DESC LIMIT %s OFFSET %s"
            val = (device_id, items_per_page, offset)
            mycursor.execute(sql, val)
            result = mycursor.fetchall()

            payload["sessions"] = []
            payload["iter_pages"] = []

            for sess in result:
                sess_items = {}
                sess_items["id"] = sess[0]

                sess_items["duration"] = sess[1]
                sess_items["power_used"] = sess[2]
                sess_items["amount_paid"] = sess[3]

                sess_items["date_initiated_year"] = sess[4].year
                sess_items["date_initiated_month"] = sess[4].month
                sess_items["date_initiated_day"] = sess[4].day
                sess_items["date_initiated_hour"] = sess[4].hour
                sess_items["date_initiated_minute"] = sess[4].minute
                sess_items["date_initiated_second"] = sess[4].second

                sess_items["location"] = sess[5]
                sess_items["port"] = sess[6]
                sess_items["increment_size"] = sess[7]
                sess_items["increments"] = sess[8]

                payload["sessions"].append(sess_items)

            for page_num in get_iter_pages(left_edge=2,
                                           right_edge=2,
                                           left_current=2,
                                           right_current=5,
                                           pages=num_pages,
                                           page=page):
                if page_num:
                    payload["iter_pages"].append(page_num)
                else:
                    payload["iter_pages"].append(0)

            # Grab the settings
            sql = "SELECT * FROM settings WHERE id = " + str(device_id)
            mycursor.execute(sql)
            result = mycursor.fetchall()
            device_settings = result[0]

            # Add the settings to the payload as well
            settings = {}
            settings["toggle_pay"] = device_settings[1]
            settings["price"] = device_settings[2]
            settings["charge_time"] = device_settings[3]
            settings["time_offset"] = device_settings[4]
            settings["location"] = device_settings[5]
            settings["aspect_ratio_width"] = device_settings[6]
            settings["aspect_ratio_height"] = device_settings[7]

            payload["settings"] = settings

            resp = jsonify(payload)
            resp.status_code = 200
            return resp
        else:
            resp = jsonify(payload)
            resp.status_code = 400
            return resp
    else:
        resp = jsonify(payload)
        resp.status_code = 405
        return resp
Beispiel #11
0
def remove_device(id, admin_key):
    payload = {}

    if request.method == 'DELETE':
        if not userManager.verify_key(admin_key):
            resp = jsonify(payload)
            resp.status_code = 401
            return resp

        mydb = mysql.connector.connect(
            host=os.environ.get('MYSQL_HOST'),
            user=os.environ.get('MYSQL_USER'),
            password=os.environ.get('MYSQL_PASSWORD'),
            database=os.environ.get('MYSQL_DATABASE'))
        mycursor = mydb.cursor()

        # Grab the device
        sql = "SELECT * FROM device WHERE id = " + str(id)
        mycursor.execute(sql)
        result = mycursor.fetchall()

        if len(result) > 0:
            devi = result[0]

            payload["deleted_id"] = devi[0]
            payload["deleted_num"] = devi[1]

            # Remove all the images that go along with it
            # -----
            # Check if the directory exists
            path = os.path.join(current_app.root_path, 'static',
                                'picture_files', str(id))
            if os.path.isdir(path):
                # Check if the resized image directory exists
                re_path = os.path.join(current_app.root_path, 'static',
                                       'picture_files', str(id), 'resized')
                if os.path.isdir(re_path):
                    # Remove all files here
                    all_files = [
                        f for f in listdir(re_path) if isfile(join(re_path, f))
                    ]
                    for file in all_files:
                        file_path = os.path.join(re_path, file)
                        os.remove(file_path)

                    # Remove the resized directory
                    os.rmdir(re_path)

                # Remove all files in the image file directory
                all_files = [f for f in listdir(path) if isfile(join(path, f))]
                for file in all_files:
                    file_path = os.path.join(path, file)
                    os.remove(file_path)

                # Remove the image file directory
                os.rmdir(path)

            # Remove the sessions of the device
            sql = "DELETE FROM session WHERE host_id = " + str(id)
            mycursor.execute(sql)

            # Remove the settings of the device
            sql = "DELETE FROM settings WHERE id = " + str(id)
            mycursor.execute(sql)

            # Remove the device
            sql = "DELETE FROM device WHERE id = " + str(id)
            mycursor.execute(sql)

            # Commit
            mydb.commit()

            resp = jsonify(payload)
            resp.status_code = 204
            return resp

        else:
            resp = jsonify(payload)
            resp.status_code = 400
            return resp

    else:
        resp = jsonify(payload)
        resp.status_code = 405
        return resp
Beispiel #12
0
def upload_images(id, admin_key):
	payload = {}

	if request.method == 'POST':
		if not userManager.verify_key(admin_key):
			resp = jsonify(payload)
			resp.status_code = 401
			return resp
			
		mydb = mysql.connector.connect(
			host=os.environ.get('MYSQL_HOST'),
			user=os.environ.get('MYSQL_USER'),
			password=os.environ.get('MYSQL_PASSWORD'),
			database=os.environ.get('MYSQL_DATABASE')
		)
		mycursor = mydb.cursor()

		# Grab device
		sql = "SELECT * FROM device WHERE id = %s"
		val = (str(id),)
		mycursor.execute(sql,val)
		result = mycursor.fetchall()

		if len(result) > 0:
			# Grab settings
			sql = "SELECT * FROM settings WHERE id = " + str(id)
			mycursor.execute(sql)
			result = mycursor.fetchall()
			devi_settings = result[0]

			# From settings get ration width and height
			ratio_width = devi_settings[6]
			ratio_height = devi_settings[7]

			# Check if directory exists for device images
			path = os.path.join(current_app.root_path, 'static', 'picture_files', str(id))
			if not os.path.isdir(path):
				os.mkdir(path)

			# Check if resized image directory exists
			re_path = os.path.join(current_app.root_path, 'static', 'picture_files', str(id), 'resized')
			if not os.path.isdir(re_path):
				os.mkdir(re_path)

			# Count how many images are in the directory
			all_files = [f for f in listdir(path) if isfile(join(path, f))]
			count = 0
			for file in all_files:
				count += 1

			# Save the incomming images
			files = request.files.to_dict(flat=False)
			for image_file in files['image']:
				# Save the original file
				_, f_ext = os.path.splitext(image_file.filename)
				file_path = os.path.join(path, str(count) + f_ext)
				image_file.save(file_path)

				# Get a resized image
				background_color = 'black'
				re_img = resize_image(image_file, background_color, ratio_width, ratio_height)

				# Save the resized image
				resized_file_path = os.path.join(current_app.root_path, 'static', 'picture_files', str(id), 'resized', str(count) + f_ext)
				re_img.save(resized_file_path)

				count += 1

			resp = jsonify(payload)
			resp.status_code = 200
			return resp
		else:
			resp = jsonify(payload)
			resp.status_code = 400
			return resp
	else:
		resp = jsonify(payload)
		resp.status_code = 405
		return resp
Beispiel #13
0
def remove_device_images(id_number, removals, admin_key):
	payload = {}

	if request.method == 'DELETE':
		if not userManager.verify_key(admin_key):
			resp = jsonify(payload)
			resp.status_code = 401
			return resp

		mydb = mysql.connector.connect(
			host=os.environ.get('MYSQL_HOST'),
			user=os.environ.get('MYSQL_USER'),
			password=os.environ.get('MYSQL_PASSWORD'),
			database=os.environ.get('MYSQL_DATABASE')
		)
		mycursor = mydb.cursor()

		# Grab device
		sql = "SELECT * FROM device WHERE id_number = %s"
		val = (id_number,)
		mycursor.execute(sql,val)
		result = mycursor.fetchall()

		if len(result) > 0:
			devi = result[0]
			id = devi[0]

			path = os.path.join(current_app.root_path, 'static', 'picture_files', str(id))
			re_path = os.path.join(current_app.root_path, 'static', 'picture_files', str(id), 'resized')
			if os.path.isdir(path):

				# Parse removals
				rem_list = removals.split(",")
				# fix index and put in a set
				rem_set = set()
				for index, value in enumerate(rem_list):
					rem_set.add(str(int(rem_list[index])-1))

				# Remove the image files in there
				all_files = [f for f in listdir(path) if isfile(join(path, f))]
				for file in all_files:
					f_name, f_ext = os.path.splitext(file)
					if f_name in rem_set:
						file_path = os.path.join(path, file)
						re_file_path = os.path.join(re_path, file) # Resized names are the same as the original
						os.remove(file_path)
						os.remove(re_file_path)

				# readjust the file names
				all_files = [f for f in listdir(path) if isfile(join(path, f))]
				count = 0
				for file in all_files:
					f_name, f_ext = os.path.splitext(file)
					# Original
					src_path = os.path.join(path, file)
					dst_path = os.path.join(path, str(count) + f_ext)
					os.rename(src_path, dst_path)

					# Resized
					src_path = os.path.join(re_path, file)
					dst_path = os.path.join(re_path, str(count) + f_ext)
					os.rename(src_path, dst_path)
					count += 1

				resp = jsonify(payload)
				resp.status_code = 204
				return resp
			else:
				resp = jsonify(payload)
				resp.status_code = 400
				return resp

		else:
			resp = jsonify(payload)
			resp.status_code = 400
			return resp

	else:
		resp = jsonify(payload)
		resp.status_code = 405
		return resp