def update_floorplan_images(file, floorplan_id, project_id): filename = file.filename name = filename[:filename.rfind(".")] extension = filename[filename.rfind("."):] thumb_filename = "{}_thumb{}".format(name, extension) large_filename = "{}_large{}".format(name, extension) store_temporary_images(file, filename) original = open(filename, "rb") thumb = open("{}_{}".format("thumb", filename), "rb") large = open("{}_{}".format("large", filename), "rb") image_cursor = sql_conn.cursor() image_cursor.execute(update_image, (original.read(), thumb.read(), large.read(), floorplan_id, project_id)) original.close() thumb.close() large.close() image_cursor.close() delete_temporary_images(filename) resource_cursor = sql_conn.cursor() resource_cursor.execute( update_floorplan_resources, (image_resource_path(project_id, floorplan_id, filename), image_resource_path(project_id, floorplan_id, thumb_filename), image_resource_path(project_id, floorplan_id, large_filename), floorplan_id, project_id)) resource_cursor.close()
def get_last_floorplan_id(project_id): cursor = sql_conn.cursor() cursor.execute(select_project_floorplan_counter, (project_id,)) last_id = cursor.fetchone() cursor.close() return last_id[0]
def drop_floorplan(project_id, floorplan_id): floorplan = load_floorplan(project_id, floorplan_id) cursor = sql_conn.cursor() cursor.execute(delete_floorplan, (floorplan.id, floorplan.project_id)) sql_conn.commit() cursor.close() return True
def drop_project(project_id): project = load_project(project_id) cursor = sql_conn.cursor() cursor.execute(delete_project, (project.id, )) sql_conn.commit() cursor.close() return True
def load_floorplans_ids(project_id): floorplans = [] cursor = sql_conn.cursor() cursor.execute(select_project_floorplans_ids, (project_id,)) for floorplan_id in cursor: floorplans.append(floorplan_id) cursor.close() return floorplans
def insert_new_floorplan(floorplan): floorplan_cursor = sql_conn.cursor() floorplan_cursor.execute(insert_floorplan, (floorplan.id, floorplan.project_id, floorplan.name, floorplan.original_resource_url, floorplan.thumb_resource_url, floorplan.large_resource_url)) floorplan_cursor.close()
def add_project(request_data): data = loads(request_data) if not all(field in data for field in project_fields): raise AttributeError("Missing project attributes in request") cursor = sql_conn.cursor() cursor.execute(insert_project, (data["name"],)) sql_conn.commit() cursor.close() return new_project_resource(cursor.lastrowid)
def load_all_projects(): projects = [] cursor = sql_conn.cursor() cursor.execute(select_all_projects) resultset = cursor.fetchall() cursor.close() for result in resultset: floorplans_ids = load_floorplans_ids(result[0]) projects.append(Project(result[0], result[1], result[3], result[4], floorplans_ids)) return projects
def load_project(project_id): cursor = sql_conn.cursor() cursor.execute(select_project, (project_id,)) resultset = cursor.fetchone() cursor.close() if not resultset: raise ValueError("No project associated to id: {}".format(project_id)) floorplans_ids = load_floorplans_ids(resultset[0]) return Project(resultset[0], resultset[1], resultset[3], resultset[4], floorplans_ids)
def update_project(project_id, request_data): data = loads(request_data) project = load_project(project_id) if not all(field in project_fields for field in data): raise AttributeError("Unknown project attributes in request") cursor = sql_conn.cursor() cursor.execute(update_project_name, (data["name"], project.id)) sql_conn.commit() cursor.close() return True
def insert_floorplan_images(file, filename, floorplan_id, project_id): store_temporary_images(file, filename) image_cursor = sql_conn.cursor() original = open(filename, "rb") thumb = open("{}_{}".format("thumb", filename), "rb") large = open("{}_{}".format("large", filename), "rb") image_cursor.execute(insert_image, (floorplan_id, project_id, original.read(), thumb.read(), large.read())) original.close() thumb.close() large.close() image_cursor.close() delete_temporary_images(filename)
def load_floorplan_details(project_id, floorplan_id): cursor = sql_conn.cursor() cursor.execute(select_floorplan, (floorplan_id, project_id)) resultset = cursor.fetchone() cursor.close() if not resultset: raise ValueError("Project {} has no floorplan with id: {}".format(project_id, floorplan_id)) return Floorplan( resultset[0], resultset[1], resultset[2], resultset[3], resultset[4], resultset[5], resultset[6], resultset[7])
def fetch_image(project_id, floorplan_id, name, filepath): floorplan = load_floorplan(project_id, floorplan_id) if name not in floorplan.original_resource_url \ and name not in floorplan.thumb_resource_url \ and name not in floorplan.large_resource_url: print(name, floorplan.original_resource_url, floorplan.thumb_resource_url, floorplan.large_resource_url) raise ValueError("A file with the given name doesn't exists for the given project and floorplan") query = select_original_image if "thumb" in name: query = select_thumb_image elif "large"in name: query = select_large_image cursor = sql_conn.cursor() cursor.execute(query, (floorplan.id, floorplan.project_id)) image_data = cursor.fetchone() cursor.close() with open(filepath, "wb+") as f: f.write(image_data[0]) return True
def update_floorplan_name_field(name, floorplan_id, project_id): cursor = sql_conn.cursor() cursor.execute(update_floorplan_name, (name, floorplan_id, project_id)) cursor.close()
def add_floorplan_to_project(floorplan_id, project_id): project_cursor = sql_conn.cursor() project_cursor.execute(update_project_floorplan_counter, (floorplan_id, project_id)) project_cursor.close()