def get_uploads(directory): return [(file, \ os.path.getmtime(os.path.join(directory, file)), \ time.ctime(os.path.getmtime(os.path.join(directory, file)))) \ for file in os.listdir(directory) if \ os.path.isfile(os.path.join(directory, file)) and \ allowed_file(file)]
def upload_file(): files = request.files.getlist('files[]') for file in files: if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) return redirect(url_for('admin_list_files'))
def get_uploads(directory): return [(file, \ os.path.getmtime(os.path.join(directory, file)), \ time.ctime(os.path.getmtime(os.path.join(directory, file)))) \ for file in os.listdir(directory) if \ os.path.isfile(os.path.join(directory, file)) and \ allowed_file(file)]
def upload_file(): files = request.files.getlist('files[]') for file in files: if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) return redirect(url_for('admin_list_files'))
def proposaldetails(id): proposal = Proposal.query.filter_by(id=id).first() if proposal is None: return redirect('/proposals') customers = Customer.query.order_by(Customer.id).all() # print(proposal.__dict__) if request.method == 'POST': is_valid = validate_proposal_form(request.form) print('is_valid', is_valid) if 'errors' not in is_valid: print(request.files) if 'sketchup_model' in request.files: print("masuk") file = request.files['sketchup_model'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file_path = app.config.get( 'UPLOAD_IMAGES_FOLDER') / filename file.save(file_path) is_valid.update({ 'sketchup_model': file_path.relative_to(Path('app')).__str__() }) proposal.update(**is_valid, project_name=request.form.get('project_name')) db.session.commit() flash('successfully updated', 'success') return redirect(f'/proposal-details/{id}') return render_template("proposal-details.html", proposal=proposal, customers=customers)
def addproposal(): if request.method == 'POST': is_valid = validate_proposal_form(request.form) if 'errors' not in is_valid: timestamp = datetime.now().replace(microsecond=0) date_of_proposals = timestamp.replace(hour=0, minute=0, second=0) if 'sketchup_model' in request.files: file = request.files['sketchup_model'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file_path = app.config.get( 'UPLOAD_IMAGES_FOLDER') / filename file.save(file_path) is_valid.update({ 'sketchup_model': file_path.relative_to(Path('app')).__str__() }) proposal = Proposal(**is_valid, date_of_proposals=date_of_proposals, project_name=request.form.get('project_name'), created_at=timestamp, updated_at=timestamp, status='Pending') db.session.add(proposal) db.session.commit() flash('successfully added', 'success') proposal_current = Proposal.query.filter_by( created_at=proposal.created_at).first() return redirect(f'/proposal-details/{proposal_current.id}') customers = Customer.query.order_by(Customer.id).all() return render_template("add-proposal.html", customers=customers)
def add_order(id): # print(request.files) data = {} roofs = [] timestamp = datetime.now().replace(microsecond=0) proposal = Proposal.query.filter_by(id=id).first() if request.files: filepaths = [] for index, gsa_report in enumerate(request.files): file = request.files[f'gsa_report_file{index+1}'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file_path = app.config.get('UPLOAD_FILES_FOLDER') / filename file.save(file_path) filepaths.append(file_path) s_data, proposal_data = add_gsa_report_to_db(filepaths) roofs_data = proposal_data.pop('roofs') for nums in range(1, proposal.num_of_roofs + 1): data['pv_panel'] = request.form.get(f"pv_panel{nums}") data['pv_panel_qty'] = request.form.get(f"pv_panel_qty{nums}") data['pv_cable'] = request.form.get(f"pv_cable{nums}") data['add_construction_qty'] = request.form.get( f"add_construction_qty{nums}") data['add_construction_price'] = request.form.get( f"add_construction_price{nums}") data['azimuth'] = roofs_data[nums - 1].get('azimuth') data['angle'] = roofs_data[nums - 1].get('angle') data['gsa_report_file'] = filepaths[nums - 1].relative_to( Path('app')).__str__() roof = Roof(**data, created_at=timestamp, updated_at=timestamp) roof.solar_data.extend(s_data[nums - 1]) roofs.append(roof) proposal.roofs.extend(roofs) print(request.form) proposal.geocoordinates = proposal_data.get('geocoordinates') proposal.location = proposal_data.get('location') proposal.pv_system_model = proposal_data.get('pv_system_model') proposal.inverter_stg3 = request.form.get('inverter_stg3') proposal.inverter_stg6 = request.form.get('inverter_stg6') proposal.inverter_stg20 = request.form.get('inverter_stg20') proposal.inverter_stg125 = request.form.get('inverter_stg125') proposal.inverter_stg250 = request.form.get('inverter_stg250') proposal.energy_accounting_system = request.form.get( 'energy_accounting_system') proposal.transport_price = request.form.get('transport_price') proposal.installation_price = request.form.get('installation_price') proposal.discount = request.form.get('discount') db.session.add(proposal) db.session.add_all(roofs) db.session.commit() flash('successfully add roofs', category='success') return redirect(f'/proposal-details/{id}')
def root(): key_folder = current_user.userkey BASE_PATH = "" form = NewFolder() upload_form = FileUpload() if request.method == 'POST' and 'files' in request.files: file_list = request.files.getlist('files') response = {} for file in file_list: file_length = request.content_length if not check_storage_limit(current_user, file_length): flash('Sorry you out of your storage, please upgrade account') response = {'status': 0} return jsonify(response) if file and functions.allowed_file(file.filename): current_path = os.path.join(functions.GetBasePath(key_folder), BASE_PATH) file_name = secure_filename(file.filename) try: file.save(os.path.join(current_path, file_name)) update_used_storage(current_user, file_length) response = {'status': 1} except Exception as e: current_app.logger.warning(e) response = {'status': 0} else: flash("Sorry, your file extension is not allowed at Ubox") response = {'status': 0} current_app.logger.warning( "Some one upload wrong extention: " + current_user.email + ':' + file.filename) return jsonify(response) elif form.validate_on_submit(): new_folder = form.folder.data functions.MkNewDir(new_folder, BASE_PATH, key_folder) return redirect(url_for('main.root')) folders, files = functions.ReadPath("", key_folder) foldersjson, filesjson = functions.osWalkJson(folders, files) return render_template('root.html', form=form, classmaker=classmaker, upload_form=upload_form, foldersjson=foldersjson, filesjson=filesjson, BASE_PATH=BASE_PATH)
def update_order(id): data = {} roofs = [] proposal = Proposal.query.filter_by(id=id).first() s_data = None if request.files: filepaths = [] for index, gsa_report in enumerate(request.files): file = request.files[f'gsa_report_file{index+1}'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file_path = app.config.get('UPLOAD_FILES_FOLDER') / filename file.save(file_path) filepaths.append(file_path) s_data, proposal_data = add_gsa_report_to_db(filepaths) for nums in range(1, proposal.num_of_roofs + 1): data['pv_panel'] = request.form.get(f"pv_panel{nums}") data['pv_panel_qty'] = request.form.get(f"pv_panel_qty{nums}") data['pv_cable'] = request.form.get(f"pv_cable{nums}") data['add_construction_qty'] = request.form.get( f"add_construction_qty{nums}") data['add_construction_price'] = request.form.get( f"add_construction_price{nums}") data['azimuth'] = request.form.get(f"azimuth{nums}") data['angle'] = request.form.get(f"angle{nums}") print('proposals', len(proposal.roofs)) print('sdata', s_data) proposal.roofs[nums - 1].update( **data, gsa_report_file=s_data[ nums - 1]) if s_data is not None else proposal.roofs[nums - 1].update( **data, gsa_report_file=None) proposal.inverter_stg3 = request.form.get('inverter_stg3') proposal.inverter_stg6 = request.form.get('inverter_stg6') proposal.inverter_stg20 = request.form.get('inverter_stg20') proposal.inverter_stg125 = request.form.get('inverter_stg125') proposal.inverter_stg250 = request.form.get('inverter_stg250') proposal.energy_accounting_system = request.form.get( 'energy_accounting_system') proposal.transport_price = request.form.get('transport_price') proposal.installation_price = request.form.get('installation_price') proposal.discount = request.form.get('discount') db.session.commit() flash('successfully update roofs', category='success') return redirect(f'/proposal-details/{id}')
def upload(): """文件上传""" file = request.files.get("Filedata") upload_path = os.path.join(current_app.static_folder, 'upload') if not os.path.exists(upload_path): os.makedirs(upload_path) # 如果上传的是文档格式 后缀 if file and allowed_file(file.filename, EXCEL_EXTENSIONS): file_name = file.filename file_path = os.path.join(upload_path, file_name) file.save(file_path) frame = pandas.read_excel(file_path) data_resource = [] frame = frame.fillna("") for index, row in frame.iterrows(): event_code = row.get("事件code") event_description = row.get("事件说明") category = row.get("分类") principal = row.get("负责人") remark = row.get("备注") sub_datas = get_datas(event_code=event_code, event_description=event_description, category=category, principal=principal, remark=remark, config_type_id=request.form.get( 'config_type_id', 0)) data_resource.extend(sub_datas) os.remove(file_path) return json.dumps(data_resource) return json.dumps({})
def save_photo_on_server(user_id): if request.method == 'POST': connection = functions.connect_to_db(constants.connect_db_hostname, constants.connect_db_user, constants.connect_db_password, constants.connect_db_dbname) with connection: with connection.cursor() as cursor: cursor.execute(constants.sql_get_user, user_id) result = cursor.fetchall() if len(result) == 0: result = { 'status_code': 400, 'description': constants.not_exist_user_in_db_error_description } return make_response(result, 400) else: cursor.execute(constants.sql_check_exist_photo, user_id) result = cursor.fetchall() if len(result) != 0: result = { 'status_code': 400, 'description': constants.exist_photo_error_description } return make_response(result, 400) else: if (request.files.get('image')): image = request.files.get('image') else: result = { 'status_code': 400, 'description': constants.not_sending_photo_in_route } return make_response(result, 400) path_to_image = os.path.join( app.config['UPLOAD_FOLDER'], image.filename) if os.path.exists(path_to_image): result = { 'status_code': 400, 'description': constants.exist_photo_error_description } return make_response(result, 400) else: if image and functions.allowed_file( image.filename): cursor.execute(constants.sql_insert_path_photo, (user_id, image.filename)) image.save(path_to_image) if os.path.exists(path_to_image): result = { 'status_code': 200, 'description': constants. success_save_photo_description } return make_response(result, 200) else: result = { 'status_code': 400, 'description': constants. not_save_photo_error_description } return make_response(result, 400) else: result = { 'status_code': 400, 'description': constants. allow_format_img_error_description } return make_response(result, 400) else: result = { 'status_code': 400, 'description': constants.allow_post_method_error_description } return make_response(result, 400)
def find_face_on_photo(): if request.method == 'GET': connection = functions.connect_to_db(constants.connect_db_hostname, constants.connect_db_user, constants.connect_db_password, constants.connect_db_dbname) with connection: with connection.cursor() as cursor: cursor.execute(constants.sql_get_all_users_photo) known_faces_user_id = [] known_faces_images_path = [] known_face_encodings_images = [] for row in cursor: known_faces_user_id.append(row["user_id"]) image_path = os.path.join(app.config['UPLOAD_FOLDER'], row["path"]) known_faces_images_path.append(image_path) image = face_recognition.load_image_file(image_path) image_face_encoding = face_recognition.face_encodings( image)[0] known_face_encodings_images.append(image_face_encoding) # connection.close() if (request.files.get('image')): image = request.files.get('image') else: result = { 'status_code': 400, 'description': constants.not_sending_photo_in_route } return make_response(result, 400) path_to_image = os.path.join(app.config['UPLOAD_FOLDER_FOR_FIND_FACE'], image.filename) if image and functions.allowed_file(image.filename): image.save(path_to_image) if not os.path.exists(path_to_image): result = { 'status_code': 400, 'description': constants.not_save_photo_error_description } return make_response(result, 400) unknown_image = face_recognition.load_image_file(path_to_image) os.remove(path_to_image) face_locations = face_recognition.face_locations(unknown_image) face_encodings = face_recognition.face_encodings( unknown_image, face_locations) str_user_id = "Unknown" for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): matches = face_recognition.compare_faces( known_face_encodings_images, face_encoding) user_id = "Unknown" face_distances = face_recognition.face_distance( known_face_encodings_images, face_encoding) best_match_index = np.argmin(face_distances) if matches[best_match_index]: user_id = known_faces_user_id[best_match_index] str_user_id = str(user_id) result = {'status_code': 200, 'user_id': str_user_id} return make_response(result, 200) else: result = { 'status_code': 400, 'description': constants.allow_get_method_error_description } return make_response(result, 400)
def update_photo(user_id): if request.method == 'PUT': connection = functions.connect_to_db(constants.connect_db_hostname, constants.connect_db_user, constants.connect_db_password, constants.connect_db_dbname) with connection: with connection.cursor() as cursor: cursor.execute(constants.sql_get_user, user_id) result = cursor.fetchall() if len(result) == 0: result = { 'status_code': 400, 'description': constants.not_exist_user_in_db_error_description } return make_response(result, 400) else: if (request.files.get('image')): image = request.files.get('image') else: result = { 'status_code': 400, 'description': constants.not_sending_photo_in_route } return make_response(result, 400) if image and functions.allowed_file(image.filename): path_to_image = os.path.join( app.config['UPLOAD_FOLDER'], image.filename) cursor.execute(constants.sql_get_path, user_id) result = cursor.fetchall() if len(result) != 0: cursor.execute(constants.sql_get_path, user_id) for row in cursor: sql_path_to_image = os.path.join( app.config['UPLOAD_FOLDER'], row["path"]) if os.path.exists(sql_path_to_image): os.remove(sql_path_to_image) image.save(path_to_image) # update запрос к БД else: result = { 'status_code': 400, 'description': constants.not_exist_photo_error_description } return make_response(result, 400) else: result = { 'status_code': 400, 'description': constants. not_exist_photo_in_db_error_description } return make_response(result, 400) else: result = { 'status_code': 400, 'description': constants.allow_format_img_error_description } return make_response(result, 400) else: result = { 'status_code': 400, 'description': constants.allow_put_method_error_description } return make_response(result, 400)