def user_files_list(dir_name=""): ''' HTML list of user-uploaded files. ''' user = user_session.get_user() full_path = pathjoin(g.site_vars['user_dir'], dir_name) if not isdir(full_path): makedirs(full_path) if request.method == 'POST' and user.is_admin: if request.form.get('action') == 'upload': f = request.files['image_file'] if f and allow_filetype(f.filename): filename = secure_filename(f.filename) f.save(pathjoin(full_path, filename)) flash('Uploaded file:' + filename) else: flash('Sorry. Invalid Filetype') elif request.form.get('action') == 'delete': filename = secure_filename(request.form.get('filename')) full_filename = pathjoin(full_path, filename) remove(full_filename) flash('Deleted ' + filename) files = make_dirlist(dir_name) return render_template('user_files.html', full_path=full_path, file_list=files, dirname=dir_name)
def app_upload_file(simulation_type, simulation_id, file_type): f = flask.request.files['file'] lib = simulation_db.simulation_lib_dir(simulation_type) template = sirepo.template.import_module(simulation_type) filename = werkzeug.secure_filename(f.filename) if simulation_type == 'srw': p = lib.join(filename) else: p = lib.join(werkzeug.secure_filename('{}.{}'.format(file_type, filename))) err = None if p.check(): err = 'file exists: {}'.format(filename) if not err: f.save(str(p)) err = template.validate_file(file_type, str(p)) if err: pkio.unchecked_remove(p) if err: return _json_response({ 'error': err, 'filename': filename, 'fileType': file_type, 'simulationId': simulation_id, }) return _json_response({ 'filename': filename, 'fileType': file_type, 'simulationId': simulation_id, })
def upload_file(): if request.method == "POST": f = request.files["the_file"] f.save("./" + secure_filename(f.filename)) return secure_filename(f.filename) else: return render_template("upload_file.html")
def upload(): if request.method == 'POST': #Get the file audiofile = request.files['audio'] fname = 'tmp/' + str(long(time.time())) + secure_filename(audiofile.filename) audiofile.save(fname) remixfile = audio.LocalAudioFile(fname) beats = remixfile.analysis.beats #https://github.com/echonest/remix/blob/master/examples/sorting/sorting.py def sorting_function(chunk): return chunk.mean_loudness() sortedbeats = sorted(beats, key=sorting_function) out = audio.getpieces(remixfile, sortedbeats) audioname = str(long(time.time())) + 'sorted' + secure_filename(audiofile.filename) + '.mp3' outfname = 'tmp/' + audioname out.encode(outfname, mp3=True) #Upload to rackspace chksum = pyrax.utils.get_checksum(outfname) cf.upload_file("beatsorter", outfname, etag=chksum) #os.remove(fname) #os.remove(outfname) return redirect(url_for('getaudiofile', filename=audioname))
def upload_models(): """ Uploads the user's robot and environment files and saves them to the server. The URL to these files are then returned for use by ColladaLoader to visualize. """ robot = flask.request.files['robot'] env = flask.request.files['env'] session_id = flask.request.form['session_id'] session_dir = join(ompl_sessions_dir, session_id) file_locs = {} # Check that the uploaded files are valid if robot and env: if allowed_file(robot.filename) and allowed_file(env.filename): # If valid files, save them to the server robot_file = join(session_dir, secure_filename(robot.filename)) robot.save(robot_file) file_locs['robot_loc'] = join("static/sessions", session_id, basename(robot_file)) env_file = join(session_dir, secure_filename(env.filename)) env.save(env_file) file_locs['env_loc'] = join("static/sessions", session_id, basename(env_file)) else: return "Error: Wrong file format. Robot and environment files must be .dae" else: return "Error: Didn't upload any files! Please choose both a robot and environment file in the .dae format." return json.dumps(file_locs)
def new_release(repo, username=None): """ Upload a new release. """ if not APP.config.get("UPLOAD_FOLDER_PATH") and not APP.config.get("UPLOAD_FOLDER"): flask.abort(404) repo = pagure.lib.get_project(SESSION, repo, user=username) if not repo: flask.abort(404, "Project not found") if not is_repo_admin(repo): flask.abort(403, "You are not allowed to change the settings for this project") form = pagure.forms.UploadFileForm() if form.validate_on_submit(): for filestream in flask.request.files.getlist("filestream"): filename = werkzeug.secure_filename(filestream.filename) try: folder = os.path.join(APP.config["UPLOAD_FOLDER_PATH"], werkzeug.secure_filename(repo.fullname)) if not os.path.exists(folder): os.mkdir(folder) filestream.save(os.path.join(folder, filename)) flask.flash('File "%s" uploaded' % filename) except Exception as err: # pragma: no cover APP.logger.exception(err) flask.flash("Upload failed", "error") return flask.redirect(flask.url_for("view_tags", repo=repo.name, username=username)) return flask.render_template("new_release.html", select="tags", username=username, repo=repo, form=form)
def upload(): error = None #This tells where the 'images' and 'documents' folders are located imupfol = os.listdir(app.config['UPLOAD_FOLDER'] + '/images/') docupfol = os.listdir(app.config['UPLOAD_FOLDER'] + '/documents/') if request.method == 'POST': # The File That is Being Uploaded file = request.files['file'] #The name of the file being uploaded (to be used later) name = request.form['name'] category = request.form['category'] #This makes sure is's a legit file and it has a name if file and name !="" and category !="": # This checks to see if the uploaded file was an image if check_file(file.filename) == "image": filename = fileRename(secure_filename(file.filename), imupfol, name) addToDb(filename, name, category, "images") file.save(os.path.join(app.config['UPLOAD_FOLDER'], "images", filename)) # This checks to see if the uploaded file was a document elif check_file(file.filename) == "document": filename = fileRename(secure_filename(file.filename), docupfol, name) addToDb(filename, name, category, "documents") file.save(os.path.join(app.config['UPLOAD_FOLDER'], "documents", filename)) else: #This error is thrown if for whatever reason it is neither an image nor a document error = "Unacceptable File Type" else: #This error is thrown when the file isn't good or there is no name given for the file error = "Please Fill In All of the Required Info" #This is around the 'return' function because the webpage requires a connection to the database with sqlite3.connect("database.db") as con: return render_template('upload.html', error=error) con.close()
def home(): if request.method == "POST": text = request.form['message'] if text != "": messageFile = open("Message.txt","w+") messageFile.write(text) messageFile.close() file = request.files['musicfile'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) hide("Message.txt", filename, '1', "MixedTape.wav") else: print 'Add the right file you dumb f**k' else: file = request.files['encryptedfile'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) text = extract(filename, '1') messageFile = open("DecodedMessage.txt","w+") messageFile.write(text) messageFile.close() else: print 'Add the right file you dumb f**k' return render_template('index.html')
def make_file(self, foldername, filename, challenge_id, file_contents): try: created = False fi = self.app.config['SERVER_ROOT']+'sand/'+str(challenge_id)+'/base/'+foldername if fi[-1]=='/': fi=fi[:-1] if(not path.normpath(fi) == fi and foldername != ''): return('Stop hacking me! ahum, I mean. There was an error making that file') if(not secure_filename(filename) == filename and not '.'+secure_filename(filename) == filename): return('Stop hacking me! ahum, I mean. There was an error making that file') if( path.exists( path.normpath(fi)+'/'+filename )): return('The file already exists') if(foldername != '' and not path.exists(fi)): mkdir(fi) created = True fi = path.normpath(fi)+'/'+filename if(filename != '' and not path.exists( fi ) ): open(fi,'w',encoding='utf-8').close() file_contents.save(fi) created = True if created: chmod(fi, S.S_IRWXU | S.S_IRWXG | S.S_IRWXO) return('The file has been made') except Exception, e: self.app.logger.warning('make_file '+str(e)) return('There was an error making that file')
def update(self, d): """ Update both the Post object and the database using a dictionary of attributes. The dictionary should only have the attributes that should be changed, and should only have keys from the SQL schema. """ # no changing the ID! if 'ID' in d: del d['ID'] print d # save uploaded image files if 'thumbnail' in d and allowed_file(d['thumbnail'].filename): ext = d['thumbnail'].filename.split('.')[-1] filename = secure_filename(d['id'] + '_thumbnail.' + ext) d['thumbnail'].save(path.join(UPLOAD_FOLDER, filename)) d['thumbnail'] = filename if 'image' in d and allowed_file(d['image'].filename): ext = d['image'].filename.split('.')[-1] filename = secure_filename(d['id'] + '_main.' + ext) d['image'].save(path.join(UPLOAD_FOLDER, filename)) d['image'] = filename values = [ col + "='" + val.replace("'", '’') + "'" for col, val in zip(d.keys(), d.values()) ] formatted_values = ", ".join(values) cur = g.db.cursor() query = 'UPDATE posts SET ' + formatted_values + ' WHERE id=%s' cur.execute(query, self.ID) g.db.commit()
def add(cls, d): """ Create a Post object and adds the object to the database from a dictionary with keys matching the SQL schema. :raises ValueError: if this ID is already in use """ if cls.get(d['id']) is not None: raise ValueError('Post ID already in use') # save uploaded image files if type(d['thumbnail']) is not unicode and allowed_file(d['thumbnail'].filename): ext = d['thumbnail'].filename.split('.')[-1] filename = secure_filename(d['id'] + '_thumbnail.' + ext) d['thumbnail'].save(path.join(UPLOAD_FOLDER, filename)) d['thumbnail'] = filename if type(d['image']) is not unicode and allowed_file(d['image'].filename): ext = d['image'].filename.split('.')[-1] filename = secure_filename(d['id'] + '_main.' + ext) d['image'].save(path.join(UPLOAD_FOLDER, filename)) d['image'] = filename columns = '(`' + '`, `'.join(d.keys()) + '`)' values = '(' + ', '.join([ '%s' for val in d.values() ]) + ')' cur = g.db.cursor() query = 'INSERT INTO posts ' + columns + ' VALUES ' + values cur.execute(query, d.values()) g.db.commit() return cls.get(d['id'])
def generate_dax(result): from pegasus.gtfar.dax.dax import GTFAR path = os.path.join(app.config['GTFAR_STORAGE_DIR'], str(result['name'])) filesize = os.path.getsize(os.path.join(path, 'input', secure_filename(result['filename']))) splits = max(1, int(math.floor(filesize / app.config['SPLIT_DIVISOR']))) gtfar = GTFAR(result['species'], result['name'], secure_filename(result['filename']), base_dir=path, bin_dir=app.config['GTFAR_BIN_DIR'], read_length=result['readLength'], mismatches=result['mismatches'], is_trim_unmapped=result['trimUnmapped'], is_map_filtered=result['mapFiltered'], clip_reads=result['genSplice'], splice=True, strand_rule=result['strandRule'], dax=os.path.join(path, '%s' % result['name']), url='%s#/runs/details/%s' % (url_for('index', _external=True), result['name']), email=result['email'], splits=splits) validation_results = gtfar.validate() if validation_results is True: gtfar.annotate() gtfar.option_filter() gtfar.clip_and_parse() gtfar.iterative_map() gtfar.analyze() gtfar.write_dax()
def register_user(user_data): """Registers a user in the database, returns False and flashes an error if an error occurs""" disallowed_characters = [" ", "/", "'"] if username_exists(user_data[0]): flash("That username is already taken!") if user_data[1] != user_data[2]: flash("Your passwords don't match!") if not user_data[2] or not user_data[1]: flash("You didn't enter a password") if any(c in disallowed_characters for c in user_data[0]): flash("The following characters are disallowed in your username (spaces,/,')") if not (user_data[3] and user_data[4]): flash("It looks like you didn't enter your name or age") else: db = get_db() db.execute("INSERT INTO user_data\ (password,username,name,age,bio)\ VALUES (?,?,?,?,?)", [generate_password_hash(user_data[1]), werkzeug.secure_filename(user_data[0]), user_data[3], user_data[4], user_data[5]]) db.commit() os.mkdir(os.path.join('static/uploads', werkzeug.secure_filename(user_data[0]))) return True return False
def dirpath(self): return os.path.join(app.config['SUBMISSIONS_DIR'], secure_filename(self.assignment.course.name), "submitted", str(self.user_id), secure_filename(self.assignment.name) )
def handleUpload(f, js=True): """ Handles the main file upload behavior. """ value = "" if secure_filename(f.filename): # get variables dirname, extension = getDirnameExtension(f) sfilename = secure_filename(f.filename) # do the file saving if not os.path.exists("static/files/%s" % dirname): # if it it's not there, make the directory and save the file os.mkdir('static/files/%s' % dirname) f.save('static/files/%s/%s' % (dirname, sfilename)) print('Uploaded file "%s" to %s' % (sfilename, dirname)) # value is used with /js route, otherwise it's ignored url = url_for('getFile', dirname=dirname) value = 'success:' + url + ':' + dirname # if not js, then flash # used to prevent flashes from showing up upon refresh if not js: message = 'Uploaded file %s to <a href="%s">%s</a>' flash(Markup(message) % (sfilename, url, dirname)) else: url = url_for('getFile', dirname=dirname) value = 'exists:' + url + ':' + dirname if not js: message = 'File %s already exists at <a href="%s">%s</a>' flash(Markup(message) % (sfilename, url, dirname)) else: value = 'error:filenameinvalid' if not js: flash('Invalid filename.', url_for('getIndex')) return value
def home(): if request.method == "POST": text = request.form["message"] if text != "": messageFile = open("Message.txt", "w+") messageFile.write(text) messageFile.close() file = request.files["musicfile"] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename)) hide("Message.txt", filename, "1", "MixedTape.mp3") else: print "Add the right file you dumb f**k" else: file = request.files["encryptedfile"] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename)) text = extract(filename, "1") messageFile = open("DecodedMessage.txt", "w+") messageFile.write(text) messageFile.close() else: print "Add the right file you dumb f**k" return render_template("index.html")
def project_new_version(id): form = NewProjectVersionForm(request.form) if request.method == 'POST' and form.validate(): version_number_formatted_without_the_dot = request.form['version'].replace('.', '') project = Project.query.get(id) new_project_folder = os.path.join(app.config['UPLOAD_FOLDER'], str(id), 'v{0}'.format(version_number_formatted_without_the_dot)) os.makedirs(new_project_folder, exist_ok=True) excel_file = request.files['excel_file'] excel_file_path = os.path.join(new_project_folder, secure_filename(excel_file.filename)) excel_file.save(excel_file_path) template_file = request.files['template_file'] template_file_path = os.path.join(new_project_folder, secure_filename(template_file.filename)) template_file.save(template_file_path) versioning_data = {'to_fill': 0, 'filled': 0} for _file, _path in [('excel_file', excel_file.filename), ('template_file', template_file.filename)]: versioning_data[_file] = _path try: equipments, wb, hostnames = nsg_processing(excel_file_path, template_file_path) except: exit() #TODO: Add a return page that handles this error data_of_equipments = list() for equipment in equipments: equipment.save_script_as(new_project_folder, equipment.hostname) versioning_data['to_fill'] += equipment.get_nbr_of_var_to_fill_in() versioning_data['filled'] += equipment.get_resolved_var() _data_of_equipment = dict() for _item in ('Hostname', 'Equipment', 'Type'): _data_of_equipment[_item] = equipment.get_value_of_var(_item, wb) _data_of_equipment['filling_ratio'] = equipment.get_filling_ratio() _data_of_equipment['filling_ratio_in_percentage'] = equipment.get_filling_ratio_in_percentage() _data_of_equipment['tb'] = equipment.tb data_of_equipments.append(_data_of_equipment) with open(os.path.join(new_project_folder, 'data.pickle'), 'wb') as f: pickle.dump(data_of_equipments, f, 0) versioning_data['fillingRatio'] = '{:.0%}'.format(versioning_data['filled']/versioning_data['to_fill']) versioning_data['project_folder'] = new_project_folder versioning_data['description'] = request.form['description'] versioning_data['version'] = request.form['version'] session['versioning_data'] = versioning_data return render_template('project_upgrade_preview.html', id=project.id, equipments=data_of_equipments, iterator=Integer(1)) return redirect(url_for('project_display', project_id=id))
def upload_file(): if request.method == 'POST': f = request.files['the_file'] f.save('./'+secure_filename(f.filename)) return secure_filename(f.filename) else: return render_template('upload_file.html')
def upload_file(): """ upload file to temporary, thend send to ming to process it and save features to database """ f = request.files['file'] if not (f and allowed_file(f.filename)): return redirect(url_for('hello')) if UPLOAD_FOLDER: filepath = os.path.join(UPLOAD_FOLDER, secure_filename(f.filename)) else: extension = secure_filename(f.filename).split('.')[-1] temp = tempfile.NamedTemporaryFile(suffix = '.'+extension) filepath = temp.name print filepath f.save(filepath) result = process_file(filepath) try: f.close() temp.close() except: print 'cant delete' return result
def upload(): # Get the name of the uploaded files uploaded_files1 = request.files.getlist("survey") uploaded_files2 = request.files.getlist("timetable") uploaded_files3 = request.files.getlist("log") filenames = [] #Check if file type is acceptable and saves file to appropriate directory for file in uploaded_files1: if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['SURVEY'], filename)) filenames.append(filename) flash('Files Uploaded Successfully') #Check if file type is acceptable and saves file to appropriate directory for file in uploaded_files2: if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['TIMETABLE'], filename)) filenames.append(filename) flash('Files Uploaded Successfully') #Check if file type is acceptable and saves file to appropriate directory for file in uploaded_files3: if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['CSILogs'], filename)) filenames.append(filename) flash('Files Uploaded Successfully') return render_template('fileupload.html')
def handle_request(config): # We are getting the url to generate from a form parameter options = {} options = request.values.getlist('options', type=float) print(options) # Converting post options group to dictionary listname = 'options' options = dict() for key, value in request.form.items(): if key[:len(listname)] == listname: options[key[len(listname)+1:-1]] = value if ('url' in request.form): print("URL provided: " + request.form['url']) pdf = pdfkit.from_url(str(request.form['url']), output_path=False, configuration=config, options=options) if ('html' in request.form): print("Html provided") pdf = pdfkit.from_string(str(request.form['html']), output_path=False, configuration=config, options=options) # If we are receiving the html contents from a uploaded file elif ('content' in request.files): print("File provided: " + str(request.files['content'])) f = request.files['content'] f.save(tmpfolder + secure_filename(f.filename)) pdf = pdfkit.from_file(tmpfolder + secure_filename(f.filename), output_path=False, configuration=config, options=options) return pdf
def upload_waptsetup(): logger.debug("Entering upload_waptsetup") tmp_target = None try: if request.method == "POST": file = request.files["file"] if file and "waptsetup.exe" in file.filename: filename = secure_filename(file.filename) tmp_target = os.path.join(wapt_folder, secure_filename("." + filename)) target = os.path.join(wapt_folder, secure_filename(filename)) file.save(tmp_target) if not os.path.isfile(tmp_target): result = dict(status="ERROR", message="Problem during upload") else: os.rename(tmp_target, target) result = dict(status="OK", message="%s uploaded" % (filename,)) else: result = dict(status="ERROR", message="Wrong file name") else: result = dict(status="ERROR", message="Unsupported method") except: e = sys.exc_info() if tmp_target and os.path.isfile(tmp_target): os.unlink(tmp_target) result = dict(status="ERROR", message="unexpected: %s" % (e,)) return Response(response=json.dumps(result), status=200, mimetype="application/json")
def upload_file(): if request.method=='POST': file=request.files['file'] if (file and allowed_file(file.filename) and notblue(file.filename)): filename=secure_filename(file.filename) uploadFilePath=os.path.join(app.config['UPLOAD_FOLDER'],filename) file.save(uploadFilePath) img=Image.open(uploadFilePath) width,height=img.size size=width,height block_blob_service.create_blob_from_path('normalpicture', filename, uploadFilePath +'/'+filename, content_settings=ContentSettings(content_type='image/jpg')) return render_template('render.html') elif (file and allowed_file(file.filename) and isblue(file.filename)): filename=secure_filename(file.filename) uploadFilePath=os.path.join(app.config['UPLOAD_FOLDER'],filename) file.save(uploadFilePath) img=Image.open(uploadFilePath) width,height=img.size size=width,height ndviFilePath=os.path.join(app.config['UPLOAD_FOLDER'],'ndvi_'+filename) nirFilePath=os.path.join(app.config['UPLOAD_FOLDER'],'nir_'+filename) #blueFilePath=os.path.join(app.config['UPLOAD_FOLDER'],'blue_'+filename) ndvi(uploadFilePath,ndviFilePath) nir(uploadFilePath,nirFilePath) #only_blue(uploadFilePath,blueFilePath) block_blob_service.create_blob_from_path('infrabluepicture', filename, uploadFilePath + '/' + filename, content_settings=ContentSettings(content_type='image/jpg')) block_blob_service.create_blob_from_path('ndvipicture', 'ndvi_'+filename, uploadFilePath + 'ndvi_' + filename, content_settings=ContentSettings(content_type='image/jpg')) return render_template('render.html') return render_template('index.html')
def upload(): form = UploadForm() if form.validate_on_submit(): _uuid = str(uuid.uuid4()) dirpath = osp.abspath(osp.join(app.config['TEMP_FOLDER'], _uuid)) app.logger.debug(dirpath) os.makedirs(dirpath, exist_ok=True) parsed_results = request.files['parsed_results'] parsed_results_file = osp.join(dirpath, secure_filename(parsed_results.filename)) parsed_results.save(parsed_results_file) pkg_src_archive = request.files['pkg_src_archive'] pkg_src_archive_file = osp.join(dirpath, secure_filename(pkg_src_archive.filename)) pkg_src_archive.save(pkg_src_archive_file) try: _id = db_util.store_in_db(parsed_results_file, pkg_src_archive_file, {'package-short-name': form.pkg_short_name.data, 'package-version': form.pkg_version.data, 'platform': form.platform.data, 'assessment-date': datetime.datetime.today()}) return render_template('results.html', report_id=_id) except Exception as err: app.logger.debug(err) return abort(400) else: return render_template('view.html') else: return render_template('upload.html', form=form)
def add_user(): """ Add or update user information """ if request.method == 'POST': print('In Adduser post method') data = request.form pics=[] try: #save in database here, use userid when actually saving for name in request.files: f = request.files[name] filename = secure_filename(f.filename) pics.append(filename) userid=data.get('userid',None) resp = AddUpdateUser(data['name'], data['dob'], data['email'], data['mentorfor'], \ data['learn'], data['about'], str(pics), userid) #error, return message if resp == -1: print('In Adduser post method: returning -1') return json.dumps({'msg':'error', 'code':-1}) #else return userid else: #del pics[:] print('In Adduser post method: returning 200') for name in request.files: f = request.files[name] filename = str(resp) + '_' + secure_filename(f.filename) #pics.append("http://tushki1405.pythonanywhere.com/pics/" + filename) f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) return json.dumps({'msg':'success', 'code':200, 'userid':resp, 'name':data['name']}) except Exception as e: return json.dumps({'msg':str(e), 'code':-1}) return 'add user'
def home(): msg1 = "" msg2 = "" if request.method == "POST": text = request.form['message'] if text != "": messageFile = open("Message.txt","w+") messageFile.write(text) messageFile.close() file = request.files['musicfile'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) if filename.rsplit('.', 1)[1] == "wav": os.system("./wavstego/wavstego -e 1234 -m Message.txt -a "+filename+" -o MixedTape.wav") else: os.system("./mp3stego/mp3stego -b 320 -e Message.txt -p 1234 "+filename+" MixedTape.mp3") msg1 = "Success!" else: msg1 = "Error: Please enter a proper music file" else: file = request.files['encryptedfile'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) if filename.rsplit('.', 1)[1] == "wav": os.system("./wavstego/wavstego -d 1234 -a MixedTape.wav") else: os.system("./mp3stego/mp3stego -p 1234 MixedTape.mp3") msg2 = "Success!" else: msg2 = "Error: Please enter a proper music file" return render_template('index.html',message1 = msg1, message2 = msg2)
def upload_file(): if 'nickname' not in session: return json.dumps({'success': False}) if 'encrypted_file' in request.files: file = request.files['encrypted_file'] is_allowed_file_type = allowed_enc_file_type(file.filename) else: file = request.files['file'] is_allowed_file_type = allowed_file_type(file.filename) path = request.form['path'] if file and is_allowed_file_type: filename = secure_filename(file.filename) user_path = os.path.join(UPLOAD_DIR, secure_filename(session['nickname'])) directory_path = os.path.join(user_path, path) if not is_path_safe_and_valid(directory_path, user_path): directory_path = user_path file.save(os.path.join(directory_path, filename)) flash(u'Plik został pomyślnie zapisany.', 'success') response = { 'success': True } else: response = { 'success': False, 'message': u'Plik posiada niedozwolone rozszerzenie.' } return json.dumps(response)
def addAssignment(): assignmentName = request.form['assignmentName'] try: input_file = request.files['input_file'] except Exception as e: message.append("The following errors occurred:\n") message.append("- input file error") print e comp_file = None try: comp_file = request.files['comp_file'] except Exception as e: if len(message) == 0: message.append("The following errors occurred:\n") message.append("- comp file error") if input_file and comp_file and len(assignmentName) > 0 and allowed_input_file(input_file.filename) and allowed_comp_file(comp_file.filename): conn = connectToDB() cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) insert_assignment_query = "INSERT INTO assignments VALUES (default, %s, %s) RETURNING id" cur.execute(insert_assignment_query, (assignmentName , session['id'] )) assignmentId = cur.fetchone()[0] filename = secure_filename(input_file.filename) the_input_file_path = os.path.join(app.config['INPUT_FOLDER'], filename) input_file.save(the_input_file_path) filename = secure_filename(comp_file.filename) the_comp_file_path = os.path.join(app.config['COMPARISON_FOLDER'], filename) comp_file.save(the_comp_file_path) insert_comp_file_query = "INSERT INTO test_cases VALUES (default, %s, %s, %s)" cur.execute(insert_comp_file_query, (the_input_file_path,the_comp_file_path, assignmentId)) conn.commit() return redirect(url_for('admin_panel'))
def build_msg(self, recipient): try: current_app.logger.info( 'EMAILTRY | Sending message:\nTo: {}\n:From: {}\nSubject: {}'.format( recipient, self.from_email, self.subject ) ) msg = Message( subject='[Pittsburgh Purchasing] {}'.format(self.subject), html=self.html_body, body=self.txt_body, sender=self.from_email, reply_to=self.reply_to, recipients=self.handle_recipients(recipient), cc=self.cc_email ) for attachment in self.attachments: if ( isinstance(attachment, FileStorage) and secure_filename(attachment.filename) != '' ): msg.attach( filename=secure_filename(attachment.filename), content_type=attachment.content_type, data=attachment.stream.read() ) return msg except Exception, e: current_app.logger.info( 'EMAILFAIL | Error: {}\nTo: {}\n:From: {}\nSubject: {}'.format( e, self.to_email, self.from_email, self.subject ) ) return False
def edit_food(foodid) : food_form = FoodForm(request.form) food = Food.query.get(foodid) food_form.name.data = food.name food_form.description.data = food.description food_form.price.data = food.price food_form.is_available.data = food.is_available food_form.image.data = food.image food_form.image_present = (food.image not in ('', None)) stall = Hawker.query.get(food.hawker_id) if food_form.image_present: name, ext = food.image.rsplit('.', 1) food_form.image_thumb = '%s_thumb.%s'%(name, ext) if request.method == 'POST' and food_form.validate() : food.name = food_form.name.data food.description = food_form.description.data food.price = food_form.price.data food.is_available = food_form.is_available.data food.hawker_id = stall.id image_file = request.files['image'] if image_file and allowed_file(image_file.filename) : filename = secure_filename(image_file.filename) image_file.save(os.path.join(vendor_page.config['UPLOADED_FILES_DEST'], filename)) thumb = Image.open(os.path.join(vendor_page.config['UPLOADED_FILES_DEST'], filename)).resize((40, 40), Image.ANTIALIAS) name,ext = filename.rsplit('.', 1) thumb_filename = '%s_thumb.%s'%(name, ext) thumb.save(os.path.join(vendor_page.config['UPLOADED_FILES_DEST'], thumb_filename)) food.image = secure_filename(image_file.filename) db.session.commit() return redirect(url_for('vendor_page.index')) return render_template('editfood.html', form=food_form, food=food, stall=stall)
def upload_file(): if request.method == 'POST': f = request.files['file'] #저장할 경로 + 파일명 f.save(secure_filename(f.filename)) return 'uploads 디렉토리 -> 파일 업로드 성공!'
def tracking(): # print(type(video)) # here I need to check the authenticates user and track their movement. # It includes the following sub-functionality # encoding # Retrieval of known person encoding from database # Recongnize the people video = request.files["video"] videoname = request.form.get('video_name') filename = secure_filename(video.filename) # TODO check path before save path = os.path.join(app.config['videos']) print("path", path) if not os.path.exists(path): os.makedirs(path) path_filename = os.path.join(path, filename) print("path_filename", path_filename) video.save(path_filename) inp_file = "videos/" + video.filename out_file = videoname + "_output" + ".avi" display = 2 # # load the known faces and embeddings # print("[INFO] loading encodings...") # data = pickle.loads(open("encodings.pickle", "rb").read()) # # # initialize the pointer to the video file and the video writer # print("[INFO] processing video...") # stream = cv2.VideoCapture(inp_file) # writer = None # # # loop over frames from the video file stream # while True: # ## grab the next frame # (grabbed, frame) = stream.read() # if not grabbed: # break ## convert the input frame from BGR to RGB then resize it to have ## a width of 750px (to speedup processing) # rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # rgb = imutils.resize(frame, width=750) # r = frame.shape[1] / float(rgb.shape[1]) # # # detect the (x, y)-coordinates of the bounding boxes # # corresponding to each face in the input frame, then compute # # the facial embeddings for each face # boxes = face_recognition.face_locations(rgb,model='cnn') # encodings = face_recognition.face_encodings(rgb, boxes) # names = [] ## loop over the facial embeddings # for encoding in encodings: # matches = face_recognition.compare_faces(data["encodings"],encoding) # name = "Unknown" # # if True in matches: # ## find the indexes of all matched faces then initialize a # # dictionary to count the total number of times each face # # was matched # matchedIdxs = [i for (i, b) in enumerate(matches) if b] # counts = {} ## loop over the matched indexes and maintain a count for # # each recognized face face # for i in matchedIdxs: # name = data["names"][i] # counts[name] = counts.get(name, 0) + 1 # # ## determine the recognized face with the largest number # # of votes (note: in the event of an unlikely tie Python # # will select first entry in the dictionary) # name = max(counts, key=counts.get) # # update the list of names # names.append(name) # # loop over the recognized faces # for ((top, right, bottom, left), name) in zip(boxes, names): # # rescale the face coordinates # top = int(top * r) # right = int(right * r) # bottom = int(bottom * r) # left = int(left * r) # # draw the predicted face name on the image # cv2.rectangle(frame, (left, top), (right, bottom),(0, 255, 0), 2) # y = top - 15 if top - 15 > 15 else top + 15 # cv2.putText(frame, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,0.75, (0, 255, 0), 2) # # if the video writer is None *AND* we are supposed to write # # the output video to disk initialize the writer # if writer is None and out_file is not None: # fourcc = cv2.VideoWriter_fourcc(*"MJPG") # writer = cv2.VideoWriter(out_file, fourcc, 24,(frame.shape[1], frame.shape[0]), True) # # if the writer is not None, write the frame with recognized # # faces t odisk # if writer is not None: # writer.write(frame) # # check to see if we are supposed to display the output frame to # # the screen # if display > 0: # cv2.imshow("Frame", frame) # key = cv2.waitKey(1) & 0xFF # ## if the `q` key was pressed, break from the loop # if key == ord("q"): # break # # close the video file pointers # stream.release() ## check to see if the video writer point needs to be released # if writer is not None: # writer.release() # client = boto3.client('s3') data = [videoname, inp_file, out_file] vid_status = insert_video_data(data) return vid_status
def result(): # Get sender info sender_id = request.form["sender_id"][:-1] sender_privatekeyname = "./sender/privatekey_{0}".format(sender_id) sender_publickeyname = "./sender/publickey_{0}".format(sender_id) # Save sender file sender_file = request.files["file"] sender_filename = "./sender/file_{0}".format( secure_filename(sender_file.filename)) sender_file.save(sender_filename) mimetype = sender_file.content_type # Generate receiver key receiver_id = str(int(binascii.hexlify(os.urandom(24)), 24)) receiver_key = RSA.generate(2048) receiver_privatekey = receiver_key.exportKey("PEM") receiver_publickey = receiver_key.publickey().exportKey("PEM") receiver_privatekeyname = "./receiver/privatekey_{0}".format(receiver_id) f = open(receiver_privatekeyname, "wb") f.write(receiver_privatekey) f.close() receiver_publickeyname = "./sender/publickey_{0}".format(receiver_id) f = open(receiver_publickeyname, "wb") f.write(receiver_publickey) f.close() # Hashing sender_hashedfilename = "./sender/hashed_{0}".format(sender_id) Generate_DigSig_On_Hashed_File(sender_filename, sender_privatekeyname, sender_hashedfilename) sender_outputname = "./sender/output_{0}".format(sender_id) Generate_AES_Enc_On_DigSig_Plus_Key(sender_hashedfilename, receiver_publickeyname, sender_outputname) sender_outputb64name = "./sender/outputb64_{0}".format(sender_id) B64Encoding(sender_outputname, sender_outputb64name) # Verify receiver_outputname = "./receiver/output_{0}".format(receiver_id) B64Decoding(sender_outputb64name, receiver_outputname) f = open(receiver_outputname, "rb") received_data = f.read() f.close() receiver_hashedfilename = "./receiver/hashed_{0}".format(receiver_id) Generate_AES_Dec_For_DigSig_Plus_Key(sender_outputname, receiver_privatekeyname, receiver_hashedfilename) decrypted_data, valid = Verify_DigSig_On_Hashed_File( receiver_hashedfilename, sender_publickeyname) # Text decoding if mimetype == "text/plain": decrypted_data = decrypted_data.decode("cp949") return render_template("result.html", filename=sender_filename, valid=valid, mimetype=mimetype, received_data=received_data, decrypted_data=decrypted_data)
def upload(): if not os.path.exists(app.config['UPLOAD_FOLDER']): os.makedirs(app.config['UPLOAD_FOLDER']) if request.files['input-file'].filename == '' or request.files[ 'hru-file'].filename == '' or request.form[ 'row'] == '' or request.form['column'] == '' or request.form[ 'epsg'] == '': return render_template( 'modeling/prms.html', InputErrorMessage= "Please upload required files and/or fill in all the fields") numberOfRows = int(request.form['row']) numberOfColumns = int(request.form['column']) epsgValue = int(request.form['epsg']) inputFile = request.files['input-file'] hruFile = request.files['hru-file'] new_mr_uuid = str(request.form['uuid']) if inputFile: inputFileName = secure_filename(inputFile.filename) inputFile.save(os.path.join(app.config['UPLOAD_FOLDER'], inputFileName)) if hruFile: hruFileName = secure_filename(hruFile.filename) hruFile.save(os.path.join(app.config['UPLOAD_FOLDER'], hruFileName)) with open(os.path.join(app.config['UPLOAD_FOLDER'], hruFileName)) as f: numberOfLines = len(f.readlines()) product = numberOfRows * numberOfColumns if product == numberOfLines: hruFileHandle = open( os.path.join(app.config['UPLOAD_FOLDER'], hruFileName), 'r') values = util.findAverageResolution(hruFileHandle, numberOfRows, numberOfColumns) inputFileHandle = open( os.path.join(app.config['UPLOAD_FOLDER'], inputFileName), 'r') copyParameterSectionFromInputFile(inputFileHandle) readncFile(numberOfRows, numberOfColumns, epsgValue, values[0], values[1], values[2], values[3]) readtifFile(numberOfRows, numberOfColumns, epsgValue, values[0], values[1], values[2], values[3]) util.generateMetaData() util.moveFilesToANewDirectory() dirList = os.listdir(app.config['DOWNLOAD_FOLDER']) for fname in dirList: print "uploading " + fname res = VW_CLIENT.upload( new_mr_uuid, os.path.join(app.config['DOWNLOAD_FOLDER'], fname)) input_file = fname parent_uuid = new_mr_uuid description = 'Lehman Creek PRMS Data' watershed_name = 'Lehman Creek' state = 'Nevada' start_datetime = '2010-01-01 00:00:00' end_datetime = '2010-01-01 01:00:00' model_name = 'prms' # create XML FGDC-standard metadata that gets included in VW metadata fgdc_metadata = make_fgdc_metadata(input_file, None, new_mr_uuid, start_datetime, end_datetime, model=model_name) # create VW metadata watershed_metadata = metadata_from_file( input_file, parent_uuid, new_mr_uuid, description, watershed_name, state, start_datetime=start_datetime, end_datetime=end_datetime, model_name=model_name, fgdc_metadata=fgdc_metadata) response = VW_CLIENT.insert_metadata(watershed_metadata) return render_template( "modeling/prms.html", Success_Message= "Successfully inserted NetCDF and GeoTIFF files in Virtual Watershed" ) else: return render_template( "modeling/prms.html", Error_Message= "The product of the number of rows and columns do not match the number of parameter values" )
def getfile(): if request.method == 'POST': # for secure filenames. Read the documentation. file = request.files['myfile'] filename = secure_filename(file.filename) #return filename # os.path.join is used so that paths work in every operating system file.save(os.path.join("test_pdf/", filename)) # You should use os.path.join here too. parsed = parser.from_file("test_pdf/" + filename) text = parsed["content"] os.remove(os.path.join('test_pdf/', filename)) cachedStopWords = stopwords.words("english") #convert to lowercase text = text.lower() #remove all stopwords text = ' '.join( [word for word in text.split() if word not in cachedStopWords]) #remove special characters punctuations = '''!()“-[]{};:”'"’\,<>./?@#$%^&*_~''' punctuations4 = '''!()“-[]{};:”'"’\,<>/?@#$%^&*_~''' no_punct_text = "" no_punct_text4 = "" for char in text: if char not in punctuations: no_punct_text = no_punct_text + char if char not in punctuations4: no_punct_text4 = no_punct_text4 + char text = re.sub('[flfi]', '', no_punct_text) text = re.sub('maximum', '', text) #remove multiple whitespaces text = ' '.join(text.split()) text2 = ''.join([i for i in text if not i.isdigit()]) text2 = ' '.join(text2.split()) text3 = text.replace('accounts', 'account') text4 = ' '.join( no_punct_text4.split()) #Use this text for keypoint 4 & 6 text5 = ' '.join(no_punct_text.split()) text6 = text4 if re.search("average life test weighted average life", text): text = text.split("average life test weighted average life", 1)[1] text = text.split()[:35] text = "".join([ " " + i if not i.startswith("'") and i not in string.punctuation else i for i in text ]).strip() text = ''.join([i for i in text if not i.isdigit()]) text = re.sub(r'\b\w{1,1}\b', '', text) text = ' '.join(text.split()) test = os.listdir() for item in test: if item.endswith(".xlsx"): os.remove(os.path.join('', item)) text1 = cl.classify(text) text2 = point2(text2) text3 = point3(text3) text4 = point4(text4) text5 = point5(text5) text6 = point6(text6) return jsonify(point1=text1, point2=text2, point3=text3, point4=text4, point5=text5, point6=text6) # return send_file(file_name, as_attachment=True) else: return 'Not Found' else: result = request.args.get['myfile'] return result
def upload(): f=request.files['file'] f.save(secure_filename(f.filename)) return 'File is uploaded succesfully!!'
def post(self, _id=None): user_id = get_jwt_identity() if 'file' not in request.files: return {'message':'Please upload a file!'}, 400 file = request.files['file'] if file.filename == '': return {'message':'Please name your file.'}, 400 if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join('./uploads',filename)) final_list = [] with open('./uploads/' + filename) as csvfile: csv_reader = csv.reader(csvfile) for row in csv_reader: final_list.append(row) if final_list: last_item = final_list[-1] player_id = int(last_item[0]) if player_id == -1: player_id = 3000 total_wrong = int(last_item[1]) total_right = int(last_item[2]) date = last_item[3] response_score = int(last_item[4]) player_score = int(last_item[5]) time = last_item[6] query = "SELECT MAX(sessionID) FROM session" result = get_one_from_db(query) maxID = check_max_id(result) query = "INSERT INTO session VALUES (%s,%s,%s,%s,%s,%s,%s,%s)" post_to_db(query, (maxID, player_id, total_wrong, total_right, date, response_score, player_score, time)) del final_list[-2:] del final_list[0] round_count = 0 new_round = False query = "SELECT MAX(roundID) FROM round" result = get_one_from_db(query) roundID = result[0][0] + 1 query_items = [] try: for item in final_list: if new_round: start_time = item[0] end_time = item[1] total_time = item[2] query = "INSERT INTO round VALUES (%s,%s,%s,%s,%s,%s)" post_to_db(query, (roundID, maxID, round_count, start_time, end_time, total_time)) for blah in query_items: query = "INSERT INTO loggedanswers VALUES (%s,%s,%s,%s,%s,%s)" post_to_db(query, (blah[1], blah[0], roundID, blah[2], blah[3], blah[4])) round_count += 1 roundID += 1 new_round = False query_items = [] elif item[0] == "ROUND DATA": new_round = True else: baby_list = [] baby_list.append(item[0]) baby_list.append(item[2]) baby_list.append(item[3]) baby_list.append(item[4]) baby_list.append(item[5]) query_items.append(baby_list) except: return {'message':'Failed to upload to DB'}, 500 try: os.system('rm ./uploads/' + filename) except: pass return {'message':'File uploaded and removed'} else: return {'message':'Failed to upload file.'}, 400
def save_pic(f): file_name = str(timeit.default_timer()) + '_' + secure_filename(f.filename) file_path = os.path.join(os.path.dirname(__file__), '..', 'upload_pic', file_name) f.save(file_path)
def check_rank(rank): if current_user.rank >= rank: return True return False def get_filename(directory, f): filenames = os.listdir(directory) b = True try: ext = f[f.rfind('.'):] except Exception, e: ext = '' while b: t = True s = string.digits + string.ascii_letters f = secure_filename(''.join(random.choice(s) for x in range(30))+ext) print f for filename in filenames: if filename == f: t = False if t: b = False return f def remove_img(url): os.remove(app.config['UPLOADS_FOLDER'] + '/' + url) filenames = os.listdir(app.config['MEDIA_THUMBNAIL_FOLDER']) for filename in filenames: name = url[:url.rfind('.')] name2 = filename[:filename.index('_')] if name == name2:
def post_add_sample(): """Handle an uploaded sample file Returns ------- Response """ sample_name = request.values['sample-name'] if sample_name == "": sample_name = request.files['observed-ions-file'].filename # If no sample name could be constructed at this point # and we are not running a native client, stop now. if sample_name == "" and not g.has_native_client: current_app.logger.info("No sample name could be extracted. %r", request.values) return abort(400) # If we are running in the native client, then the program # have different information about where to read file information # from. Normal browsers cannot access the full path of files being # uploaded, but Electron can. It will intercept the file upload and # instead send its native path. Since the native client is running # on the local file system, we can directly read from that path # without needing to first copy the sample file to application server's # file system. if g.has_native_client: native_path = request.values.get("observed-ions-file-path") if sample_name == "": sample_name = os.path.splitext(os.path.basename(native_path))[0] if sample_name == "": current_app.logger.info("No sample name could be extracted. %r", request.values) abort(400) path = native_path sample_name = g.manager.make_unique_sample_name( sample_name) secure_name = secure_filename(sample_name) current_app.logger.info( "Preparing to run with native path: %r, %r, %r", path, sample_name, secure_name) else: file_name = request.files['observed-ions-file'].filename sample_name = g.manager.make_unique_sample_name( sample_name) secure_name = secure_filename(file_name) path = g.manager.get_temp_path(secure_name) request.files['observed-ions-file'].save(path) storage_path = g.manager.get_sample_path( re.sub(r"[\s\(\)]", "_", secure_name) + '-%s.mzML') storage_path = make_unique_name(storage_path) touch_file(storage_path) # Construct the task with a callback to add the processed sample # to the set of project samples start_time = float(request.values['start-time']) end_time = float(request.values['end-time']) extract_only_tandem_envelopes = bool(request.values.get("msms-features-only", False)) prefab_averagine = request.values['ms1-averagine'] prefab_msn_averagine = request.values['msn-averagine'] custom_ms1_averagine_formula = request.values['ms1-averagine-custom'] custom_msn_averagine_formula = request.values['msn-averagine-custom'] if custom_ms1_averagine_formula: averagine = custom_ms1_averagine_formula else: averagine = prefab_averagine if custom_msn_averagine_formula: msn_averagine = custom_msn_averagine_formula else: msn_averagine = prefab_msn_averagine ms1_score_threshold = float(request.values['ms1-minimum-isotopic-score']) msn_score_threshold = float(request.values['msn-minimum-isotopic-score']) missed_peaks = int(request.values['missed-peaks']) msn_missed_peaks = int(request.values['msn-missed-peaks']) maximum_charge_state = int(request.values['maximum-charge-state']) ms1_background_reduction = float(request.values.get( 'ms1-background-reduction', 5.)) msn_background_reduction = float(request.values.get( 'msn-background-reduction', 0.)) ms1_averaging = int(request.values.get("ms1-averaging", 0)) n_workers = g.manager.configuration.get("preprocessor_worker_count", 6) if cpu_count() < n_workers: n_workers = cpu_count() task = PreprocessMSTask( path, g.manager.connection_bridge, averagine, start_time, end_time, maximum_charge_state, sample_name, msn_averagine, ms1_score_threshold, msn_score_threshold, missed_peaks, msn_missed_peaks, n_processes=n_workers, storage_path=storage_path, extract_only_tandem_envelopes=extract_only_tandem_envelopes, ms1_background_reduction=ms1_background_reduction, msn_background_reduction=msn_background_reduction, ms1_averaging=ms1_averaging, callback=lambda: 0) g.add_task(task) return Response("Task Scheduled")
def uploader(): if request.method == 'POST': f = request.files['file'] f.save(UPLOAD_FOLDER + secure_filename(f.filename)) return 'file uploaded successfully'
def upload_file(): if request.method == 'POST': f = request.files['file'] f.save(secure_filename(f.filename)) return 'file uploaded successfully'
def photo_uploader(): #haar cascade file faceDetect = cv2.CascadeClassifier( 'cascadeFiles/haarcascade_frontalface_alt.xml') smileDetect = cv2.CascadeClassifier( 'CascadeClassifier/haarcascade_smile.xml') palmDetect = cv2.CascadeClassifier('CascadeClassifier/palm.xml') # Get the name of the uploaded files uploaded_files = request.files.getlist("file[]") filenames = [] Ids = [] errors = [] Faces = [] size = (50, 50) recognizerEigen = cv2.createEigenFaceRecognizer() recognizerFisher = cv2.createFisherFaceRecognizer() recognizerLBPH = cv2.createLBPHFaceRecognizer() for file in uploaded_files: # Check if the file is one of the allowed types/extensions if file and photo_allowed_file(file.filename): # Make the filename safe, remove unsupported chars filename = secure_filename(file.filename) # Move the file form the temporal folder to the upload # folder we setup filename1 = filename.split(".")[0] print 'filename1 is ', filename1 retval, errors = validateName(filename1, errors) if retval == False: continue file.save(os.path.join(app.config['PHOTO_UPLOAD_FOLDER'], filename)) # Save the filename into a list, we'll use it later imagePath = 'trainSet/' + filename img = cv2.imread(imagePath) Id = os.path.split(imagePath)[-1].split('.') Id = '.'.join(Id[0:2]) Id = Id[1:] print Id #convert image into greyscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #detect faces from the grayscale image #list those faces faces = faceDetect.detectMultiScale(gray, 1.3, 5) faceFound = False for (x, y, w, h) in faces: faceFound = True #cutting the image new = gray[y:y + h, x:x + h] w_rm = int(0.2 * w / 2) gray = gray[y:y + h, x + w_rm:x + w - w_rm] #normalizing the pixel density of the image new = cv2.equalizeHist(new) gray = cv2.equalizeHist(gray) #resizing the image if gray.shape < size: gray = cv2.resize(gray, size, interpolation=cv2.INTER_AREA) else: gray = cv2.resize(gray, size, interpolation=cv2.INTER_CUBIC) if faceFound == False: errors.append('No face found in {}'.format(filename)) os.remove(imagePath) print "removed ", imagePath continue else: cv2.imwrite('dataSet/' + str(Id), gray) filenames.append(filename) if not filenames: errors.append('No file provided for training') print errors return render_template("upload.html", filenames=filenames, errors=errors) Ids = [] Faces = [] path = 'dataSet/' count = 0 for file in os.listdir(path): try: img = cv2.imread(path + file) except: print "{} is not a valid file".format(path + file) continue count += 1 Id = file[0:6] print count, path + file, Id try: Ids.append(int(Id)) except Exception, e: if str(e) == "invalid literal for int() with base 10: '.DS_St'": continue errors.append(str(e)) continue try: gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) except: print "{} is not a valid file".format(path + file) continue faceNp = np.array(gray, 'uint8') Faces.append(faceNp)
import re sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf8', buffering=1) form = cgi.FieldStorage() # Get filename here. try: fileitem = form['filename'] fileitem2 = form['filename2'] except KeyError: # if no file, just print first page. printwoscop() print("<p>No files loaded...</p>") sys.exit() # Test if the file was uploaded if fileitem.filename: securefn = secure_filename(fileitem.filename) fn = os.path.basename(securefn) open('upload/' + fn, 'wb').write(fileitem.file.read()) message = '<p>The file "' + fn + '" was uploaded successfully</p>' # print(fn) else: message = 'No file was uploaded' print("content-type:text/html; charset=utf-8\r\n\r\n") print() print('Please load files...') if fileitem2.filename: securefn2 = secure_filename(fileitem2.filename) fn2 = os.path.basename(securefn2) open('upload/' + fn2, 'wb').write(fileitem2.file.read())
def upload(): """ Assumes that a chunked upload is provided A unique gridfs id is created upon first chunk and later """ if request.method == 'POST': # TODO: Get the new method in paramters # Verfiy that the uploader has access id = ObjectId() form = request.form # No need to return conventional file list jsonresponse = {} jsonresponse["_id"] = id return jsonify(jsonresponse) if request.method == 'PUT': # we are expected to save the uploaded file and return some info about it: # this is the name for input type=file names = [] # Make sure to read the form before sending the reply # Parse headers #Get filename from content disposition fnameheader = request.headers["Content-Disposition"] disposition = re.search(r'filename="(.+?)"', fnameheader) filename = disposition.group(0)[10:-1] # Get the actual chunk position from Content-Range range = request.headers["Content-Range"] match = re.findall(r'\d+', range) start = int(match[0]) end = int(match[1]) total = int(match[2]) # No need to return conventional file list jsonresponse = {} # Expect _id in the form try: id = request.form['_id'] except: return Response( "{\"error\" : \" each put request must include _id requested from server \"}", status=400) # Craft the response json jsonresponse["done"] = end + 1 jsonresponse["_id"] = id return jsonify(jsonresponse) # print request.headers # Now create the file being uploaded or append etc # TODO: Decide the format to return # obj = {} # obj["boundary"] = request.headers["Content-Type"] # obj["size"] = False # obj["delete_type"] = "DELETE" # obj["delete_url"] = "?file=" + filename # obj["name"] = filename # print obj return jsonify(obj) for file in request.files.getlist('files[]'): filename = secure_filename(file.filename) print filename file.save( os.path.join(current_app.config['UPLOAD_FOLDER'], filename)) names.append({"name": filename}) return render_template("list.html", names=names) return render_template("upload.html")
def do_upload(request, dataPath): if request.method == 'POST': file = request.files['fileUploader'] if file: filename = secure_filename(file.filename) file.save(dataPath + '/raw/' + filename)
def removeItem(cmd, id): if current_user.admin: if cmd == 'course': scheduled = False allSchedules = models.Schedule.query.all() course = models.Course.query.get(id) for sch in allSchedules: if sch.course.title == course.title: scheduled = True if scheduled: course.enabled = False db.session.commit() else: db.session.delete(course) db.session.commit() return redirect(url_for('adminCourse')) elif cmd == 'trainer': scheduled = False allSchedules = models.Schedule.query.all() trainer = models.Trainer.query.get(id) for sch in allSchedules: if sch.trainer.email == trainer.email: scheduled = True if scheduled: trainer.enabled = False db.session.commit() else: db.session.delete(trainer) db.session.commit() return redirect(url_for('adminTrainer')) elif cmd == 'room': scheduled = False allSchedules = models.Schedule.query.all() room = models.Room.query.get(id) for sch in allSchedules: if sch.room.name == room.name: scheduled = True if scheduled: room.enabled = False db.session.commit() else: deleteItem = models.Room.query.get(id) originalImgPath = deleteItem.roomImage tempImgPath = "" if originalImgPath != "": tempImgPath = secure_filename(originalImgPath) if "_" in tempImgPath: imgFiles = tempImgPath.split("_") for imgFile in imgFiles: os.remove( os.path.join(app.config['UPLOAD_FOLDER'], imgFile)) else: os.remove( os.path.join(app.config['UPLOAD_FOLDER'], tempImgPath)) db.session.delete(deleteItem) db.session.commit() return redirect(url_for('adminRoom')) elif cmd == 'schedule': deleteItem = models.Schedule.query.get(id) db.session.delete(deleteItem) db.session.commit() return redirect(url_for('adminSchedule')) else: flash("You don't have any permission to delete.") return redirect(url_for('administrator'))
def settings(): if current_user.is_anonymous: return redirect(url_for('login')) if request.method == 'POST': name = request.form['name'] status = request.form['status'] handleCFORCE = request.form['handleCFORCE'] handleTIMUS = request.form['handleTIMUS'] handleINFORM = request.form['handleINFORM'] vk_url = request.form['vk_url'] about = request.form['about'] password = request.form['password'] confirm_password = request.form['confirm_password'] if 'new_icon' in request.files: new_icon = request.files['new_icon'] if allowed_file(new_icon.filename): filename = secure_filename(new_icon.filename) new_icon.save( f'app/static/images/icons/{current_user.login}.jpg') if 'new_font' in request.files: new_font = request.files['new_font'] if allowed_file(new_font.filename): filename = secure_filename(new_font.filename) new_font.save( f'app/static/images/fonts/{current_user.login}.jpg') params = { 'name': name, 'handleCFORCE': handleCFORCE, 'handleTIMUS': handleTIMUS, 'handleINFORM': handleINFORM, 'status': status, 'vk_url': vk_url, 'about': about } update = Users.query.filter_by(login=current_user.login).first() update.set_params(params) if password != '' or confirm_password != '': if len(password) < 3 or len(confirm_password) < 3: flash('Пароль слишком короткий', 'danger') return render_template('settings.html') elif password != confirm_password: flash('Пароли не совпадают', 'danger') return render_template('settings.html') else: update.set_password( hashlib.sha224(password.encode('utf-8')).hexdigest()) flash('Пароль обновлен', 'success') db.session.add(update) db.session.commit() flash('Успешно', 'success') return redirect(url_for('profile', login=current_user.login)) return render_template('settings.html', get_rating=get_rating)
def upload(): # Get priority priority = int(request.form.get('priority', PRIORITY.medium)) if priority not in PRIORITY.get_values(): priority = PRIORITY.medium # Get output formats output_formats = request.form.get('output-formats', '') output_formats = list(set( filter( lambda format: format in app.config['ALLOWED_EXTENSIONS'], output_formats.split(';') ) )) if not output_formats: return jsonify({'Error': 'Must provide valid output formats'}), 400 # Get file (either directly or via URL) file = request.files.get('file') allowed_extensions = app.config['ALLOWED_EXTENSIONS'] if file: if allowed_filename(file.filename, allowed_extensions): filename = secure_filename(file.filename).strip()[-FILE_NAME_LIMIT] local_path = os.path.join(app.config['UPLOAD_FOLDER'], timestamp_filename(filename)) file.save(local_path) else: return jsonify({'Error': 'File format not allowed'}), 400 else: fileURL = request.form.get('fileURL') if fileURL: filename = get_filename_from_url(fileURL) try: local_path = download_url( fileURL, app.config['UPLOAD_FOLDER'], timestamp=True) except FileAccessDenied as fad: return jsonify({ 'status': 'error', 'code': fad.status_code, 'message': fad.message }), 500 else: return jsonify({'status': 'error', 'message': 'Unable to decode uploaded file'}), 500 # Upload to remote and remove file from local remote_destination = os.path.join(app.config['REMOTE_INPUT_FOLDER'], get_uuid(), filename) upload_to_remote(remote_destination, local_path) os.remove(local_path) # Register the file for conversions and return docIds docIds = Conversion.register_file(filename, remote_destination, g.user, output_formats, priority) # Call request fetcher request_fetcher.delay() return jsonify({'status': STATUS.introduced, 'doc_ids': docIds})
def editItem(cmd, id): if current_user.admin: if cmd == 'course': courseItem = models.Course.query.get(id) form = CourseForm(obj=courseItem) if courseItem.preRequisiteCourses != None: checkedPreRequisiteCourse = courseItem.preRequisiteCourses.split( ", ") else: checkedPreRequisiteCourse = "" if form.validate_on_submit(): courseItem.title = form.title.data courseItem.description = form.description.data courseItem.duration = form.duration.data courseItem.capacity = form.capacity.data listOfPreRequisiteCourses = ', '.join( request.form.getlist('preRequisite')) courseItem.preRequisiteCourses = listOfPreRequisiteCourses db.session.add(courseItem) db.session.commit() return redirect(url_for('adminCourse')) return render_template( 'adminCourseEdit.html', courses=models.Course.query.all(), checkedPreRequisiteCourse=checkedPreRequisiteCourse, course=models.Course.query.get(id), form=form) elif cmd == 'trainer': oneTrainer = models.Trainer.query.get(id) form = TrainerForm(obj=oneTrainer) if form.validate_on_submit(): oneTrainer.name = form.name.data oneTrainer.address = form.address.data oneTrainer.email = form.email.data oneTrainer.mobile = form.mobile.data db.session.add(oneTrainer) db.session.commit() return redirect(url_for('adminTrainer')) return render_template('adminTrainerEdit.html', trainer=models.Trainer.query.get(id), form=form) elif cmd == 'room': roomItem = models.Room.query.get(id) form = RoomForm(obj=roomItem) image = roomItem.roomImage imageList = image.split() numOfImages = len(imageList) imagePaths = [] removeImgPath = "" filenames = [] facilities = [ 'Microphone', 'DVD Player', 'Voting System', 'Interactive Monitor', 'Slide Projector', 'Laptop Connection', 'Split Screen' ] if models.Course.query.get(id).preRequisiteCourses != None: currentFacilities = roomItem.facilities.split(", ") else: currentFacilities = [] print(currentFacilities) if form.validate_on_submit(): if imagePaths != []: for i in range(numOfImages): removeImgPath = secure_filename(imageList[i]) os.remove( os.path.join(app.config['UPLOAD_FOLDER'], removeImgPath)) uploaded_files = request.files.getlist("file[]") for file in uploaded_files: filename = secure_filename(file.filename) if filename != '': file.save( os.path.join(app.config['UPLOAD_FOLDER'], filename)) filenames.append(filename) else: for i in range(0, numOfImages): filenames.append(secure_filename(imageList[i])) listOfImages = ' '.join(filenames) roomItem.name = form.name.data roomItem.city = form.city.data roomItem.address = form.address.data roomItem.roomType = form.roomType.data roomItem.capacity = form.capacity.data listOfFaclilties = ', '.join( request.form.getlist('facilities')) roomItem.facilities = listOfFaclilties roomItem.accessibility = form.accessibility.data roomItem.roomImage = listOfImages db.session.add(roomItem) db.session.commit() return redirect(url_for('adminRoom')) return render_template('adminRoomEdit.html', room=models.Room.query.get(id), form=form, items=len(currentFacilities), currentFacilities=currentFacilities, facilities=facilities) elif cmd == 'schedule': form = ScheduleForm() scheduleItem = models.Schedule.query.get(id) form.course.choices = [ (course.id, course.title) for course in models.Course.query.order_by(models.Course.title) ] form.trainer.choices = [ (trainer.id, trainer.name) for trainer in models.Trainer.query.order_by( models.Trainer.name) ] form.room.choices = [ (room.id, room.name) for room in models.Room.query.order_by(models.Room.name) ] form.startTime.choices = [(time, str(time) + ':00') for time in range(8, 18)] # form = ScheduleForm(obj=scheduleItem) if request.method == 'GET': form.course.data = scheduleItem.course.id form.trainer.data = scheduleItem.trainer.id form.room.data = scheduleItem.room.id form.startDate.data = scheduleItem.startDate form.startTime.data = scheduleItem.startTime if form.validate_on_submit(): scheduleItem.courseId = form.course.data scheduleItem.trainerId = form.trainer.data scheduleItem.roomId = form.room.data scheduleItem.startDate = form.startDate.data scheduleItem.startTime = form.startTime.data db.session.commit() return redirect(url_for('adminSchedule')) return render_template('adminScheduleAdd.html', schedule=models.Schedule.query.get(id), form=form, pageHeader="Edit a scheduled course") else: flash("You don't have any permission to edit.") return redirect(url_for('administrator')) return redirect(url_for('administrator'))
def upload_file(): if request.method == 'POST': f = request.files['file'] f.save(secure_filename(f.filename)) #f.save(os.path.join['UPLOAD_FOLDER'], filename) return 'File successfully uploaded!'
def upload_file(): if request.method == 'POST': try: f = request.files['file'] f.save( os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename))) numberlayer = int(request.form['numberlayer']) sizelayer = int(request.form['sizelayer']) split = request.form['split'] gate = request.form['gate'] enabledropout = ast.literal_eval(request.form['enabledropout']) dropout = request.form['dropout'] learningrate = float(request.form['learningrate']) timestamp = int(request.form['timestamp']) selectpenalty = ast.literal_eval(request.form['selectpenalty']) penalty = request.form['penalty'] activationfunction = request.form['activationfunction'] optimizer = request.form['optimizer'] selectloss = request.form['selectloss'] epoch = int(request.form['epoch']) columntoselect = request.form['columntoselect'] future = int(request.form['future']) idsocket = request.form['id'] net_namespace.emit('privatesocket', {'id': idsocket}) if dropout == '': dropout = 0 else: try: dropout = float(dropout) print dropout except: return json.dumps({'error': 'Fail to cast dropout rate'}) if penalty == '': penalty = 0 else: penalty = float(penalty) try: split = (100 - int(split)) / 100.0 except: return json.dumps( {'error': 'Split must be positive integer only'}) try: dataset = pd.read_csv(UPLOAD_FOLDER + str(f.filename)) dataset = dataset.iloc[:(dataset.shape[0] // timestamp) * timestamp + 1, :] except: return json.dumps({'error': 'Fail to read CSV'}) try: counter_nan = dataset.isnull().sum() counter_without_nan = counter_nan[counter_nan == 0] dataset = dataset[counter_without_nan.keys()] except: return json.dumps({'error': 'Fail to remove NaN'}) try: columntoselect = columntoselect.split(',') for i in range(len(columntoselect)): columntoselect[i] = columntoselect[i].strip() except: return json.dumps({'error': 'Fail to parse selected columns'}) X = dataset[columntoselect] try: minmax = MinMaxScaler().fit(X.astype('float32')) X_log_temp = pd.DataFrame(minmax.transform( X.astype('float32'))) except: return json.dumps( {'error': 'Select columns that contain numbers only'}) X_log_train = X_log_temp.iloc[:int(split * X_log_temp.shape[0]) + 1, :] X_log_test = X_log_temp.iloc[int(split * X_log_temp.shape[0]):, :] color_array = ( np.array(sns.color_palette("Paired", len(columntoselect) * 2)) * 255).astype('int').astype('str').tolist() color_array = ['rgb(' + ', '.join(i) + ')' for i in color_array] EPOCH, LOSS, VALID_LOSS = [], [], [] tf.reset_default_graph() try: sess = tf.InteractiveSession() modelnet = model.Model(numberlayer, sizelayer, enabledropout, dropout, selectpenalty, penalty, activationfunction, selectloss, learningrate, X_log_temp.shape[1], X_log_temp.shape[1], optimizer, gate) sess.run(tf.global_variables_initializer()) for i in range(epoch): X_log = X_log_temp.copy() EPOCH.append(i) total_loss = 0 if gate == 'lstm': init_value = np.zeros((1, numberlayer * 2 * sizelayer)) else: init_value = np.zeros((1, numberlayer * sizelayer)) for n in range(0, (X_log.shape[0] // timestamp) * timestamp, timestamp): batch_x = np.expand_dims(X_log.iloc[n:n + timestamp, :], axis=0) batch_y = X_log.iloc[n + 1:n + timestamp + 1, :] last_state, _, loss = sess.run( [ modelnet.last_state, modelnet.optimizer, modelnet.cost ], feed_dict={ modelnet.X: batch_x, modelnet.Y: batch_y, modelnet.hidden_layer: init_value }) total_loss += loss init_value = last_state save_shape = X_log.shape[0] + future total_loss /= (X_log.shape[0] // timestamp) LOSS.append(total_loss) output_predict = np.zeros((save_shape, X_log.shape[1])) output_predict[0, :] = X_log.iloc[0, :] upper_b = (X_log.shape[0] // timestamp) * timestamp if gate == 'lstm': init_value = np.zeros((1, numberlayer * 2 * sizelayer)) else: init_value = np.zeros((1, numberlayer * sizelayer)) for n in range(0, (X_log.shape[0] // timestamp) * timestamp, timestamp): out_logits, last_state = sess.run( [modelnet.logits, modelnet.last_state], feed_dict={ modelnet.X: np.expand_dims(X_log.iloc[n:n + timestamp, :], axis=0), modelnet.hidden_layer: init_value }) init_value = last_state output_predict[n + 1:n + timestamp + 1, :] = out_logits out_logits, last_state = sess.run( [modelnet.logits, modelnet.last_state], feed_dict={ modelnet.X: np.expand_dims(X_log.iloc[upper_b:, :], axis=0), modelnet.hidden_layer: init_value }) init_value = last_state output_predict[upper_b + 1:X_log.shape[0] + 1, :] = out_logits X_log.loc[X_log.shape[0]] = out_logits[-1, :] for n in range(future - 1): out_logits, last_state = sess.run( [modelnet.logits, modelnet.last_state], feed_dict={ modelnet.X: np.expand_dims(X_log.iloc[-timestamp:, :], axis=0), modelnet.hidden_layer: init_value }) init_value = last_state output_predict[X_log.shape[0], :] = out_logits[-1, :] X_log.loc[X_log.shape[0]] = out_logits[-1, :] valid_loss = np.mean( np.square( X_log_temp.iloc[ int(split * X_log_temp.shape[0]):, :].values - output_predict[int(split * X_log_temp.shape[0] ):X_log_temp.shape[0], :])) X_out = minmax.inverse_transform(output_predict).T.tolist() VALID_LOSS.append(valid_loss) status = 'epoch: %d, loss: %f, validation loss: %f' % ( i + 1, total_loss, valid_loss) x_range_original = np.arange(X.shape[0]).tolist() x_range_future = np.arange(save_shape).tolist() X_T = X.values.T.tolist() X_T_arr = X.values.T X_out_arr = np.array(X_out) quantile_real, linear_real, quantile_predict, linear_predict = [], [], [], [] kde_real, bar_real, kde_predict, bar_predict = [], [], [], [] for n in range(len(columntoselect)): # REAL DATA # quantile quan, _ = stats.probplot(X_T_arr[n, :]) quan_x = quan[0] quan_y = quan[1] regr.fit(quan_x.reshape([-1, 1]), quan_y.reshape([-1, 1])) pred = regr.predict(quan_x.reshape([-1, 1])) quantile_real.append( [quan_x.tolist(), quan_y.tolist()]) linear_real.append(pred[:, 0].tolist()) # histogram z = sns.distplot( X_T_arr[n, :]).get_lines()[0].get_data() plt.cla() kde_real.append([z[0].tolist(), z[1].tolist()]) hist, bins = np.histogram( X_T_arr[n, :], bins=min(freedman_diaconis_bins(X_T_arr[n, :]), 50), normed=True) center = (bins[:-1] + bins[1:]) / 2 bar_real.append([center.tolist(), hist.tolist()]) # PREDICT DATA # quantile quan, _ = stats.probplot(X_out_arr[n, :]) quan_x = quan[0] quan_y = quan[1] regr.fit(quan_x.reshape([-1, 1]), quan_y.reshape([-1, 1])) pred = regr.predict(quan_x.reshape([-1, 1])) quantile_predict.append( [quan_x.tolist(), quan_y.tolist()]) linear_predict.append(pred[:, 0].tolist()) # histogram z = sns.distplot( X_out_arr[n, :]).get_lines()[0].get_data() plt.cla() kde_predict.append([z[0].tolist(), z[1].tolist()]) hist, bins = np.histogram( X_out_arr[n, :], bins=min(freedman_diaconis_bins(X_out_arr[n, :]), 50), normed=True) center = (bins[:-1] + bins[1:]) / 2 bar_predict.append([center.tolist(), hist.tolist()]) del X_T_arr del X_out_arr net_namespace.emit( 'senddata', json.dumps({ 'id': idsocket, 'data': { 'color': color_array, 'ori': X_T, 'ori-range': x_range_original, 'predict-range': x_range_future, 'epoch': EPOCH, 'loss': LOSS, 'valid': VALID_LOSS, 'columns': columntoselect, 'predict': X_out, 'status': status, 'quantile-real': quantile_real, 'linear_real': linear_real, 'quantile_predict': quantile_predict, 'linear_predict': linear_predict, 'kde_real': kde_real, 'bar_real': bar_real, 'kde_predict': kde_predict, 'bar_predict': bar_predict } })) time.sleep(1) return json.dumps({'status': 'done'}) except Exception as e: print e tf.reset_default_graph() return json.dumps({ 'error': 'Incompatible shapes, please change timestamp value' }) except Exception as e: return json.dumps({'error': str(e)}) else: return json.dumps({'error': 'accept POST request only'})
def edit_item(item_name, category_name=None): """Edit the details of the specified item. Args: item_name (str): Name of item to be edited. category_name (str): Optionally, can also specify the category to which the item belongs to. """ if 'username' not in login_session: flash("You need to login to edit any animals.") return redirect('/login') session = connect_to_database() try: item = session.query(Item).filter_by(name=item_name).one() except NoResultFound: flash("Error: The item '%s' does not exist." % item_name) return redirect(url_for('show_homepage')) if login_session['user_id'] != item.user_id: flash("You didn't add this animal, so you can't edit it. Sorry :-(") category = session.query(Category).filter_by(id=item.category_id).one() category_name = category.name item_name = item.name session.close() return redirect( url_for('show_item', category_name=category_name, item_name=item_name)) if request.method == 'POST': if request.form['name'] != item.name: # Enforce rule that item names are unique qry = session.query(Item).filter(Item.name == request.form['name']) already_exists = (session.query(literal(True)).filter( qry.exists()).scalar()) if already_exists is True: original_category = (session.query(Category).filter_by( id=item.category_id).one()) flash("Error: There is already an animal with the name '%s'" % request.form['name']) session.close() return redirect( url_for('show_items', category_name=original_category.name)) item.name = request.form['name'] form_category = (session.query(Category).filter_by( name=request.form['category']).one()) if form_category != item.category: item.category = form_category item.description = request.form['description'] item.quantity = request.form['quantity'] # Process optional item image image_file = request.files['file'] if image_file and allowed_file(image_file.filename): if item.image_filename: delete_image(item.image_filename) filename = secure_filename(image_file.filename) if os.path.isdir(app.config['UPLOAD_FOLDER']) is False: os.mkdir(app.config['UPLOAD_FOLDER']) image_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) item.image_filename = filename item.image_url = None elif ('delete_image' in request.form and request.form['delete_image'] == 'delete'): if item.image_filename: delete_image(item.image_filename) item.image_filename = None if not image_file and request.form['image_url']: item.image_url = request.form['image_url'] if item.image_filename: delete_image(item.image_filename) item.image_filename = None session.add(item) session.commit() flash("Animal successfully edited!") category_name = form_category.name item_name = item.name session.close() return redirect( url_for('show_item', category_name=category_name, item_name=item_name)) else: categories = session.query(Category).all() session.close() return render_template('edit_item.html', categories=categories, item=item)
# print(i,val) return redirect(url_for('login')) else: return 'error' @app.route('/uploader', methods = ['GET', 'POST']) def upload(): if request.method == 'POST': f = request.files['file'] info = os.stat(f.filename) print info if(info.st_size < 1048576 ): f.save(os.path.join("SampleInputFiles/",f.filename)) flash('File uploaded sucessfully...') return render_template('summery.html') elif(info.st_size > 1048576 ): flash('File is to large..! Please login to proceed...') return render_template('login.html') @app.route('/summery') def summery(): return render_template('summery.html') if __name__ == '__main__': app.run(debug = True) f.save(secure_filename(f.filename)) return 'file uploaded successfully'
def create_key(study_id, file): return "s" + study_id + "/" + secure_filename(file.filename)
def update_transaction(id): if request.method == 'POST': print(request.form) payload = json.loads(request.form['data']) print(payload) file = request.files if payload: try: trans = Transaction.query.filter_by(id=int(id)).first() if len(file) != 0: file = request.files['image'] try: if file and allowed_file(file.filename): filename = secure_filename(file.filename) foldertemp = os.path.join( UPLOAD_FOLDER, 'transaction_report') if os.path.exists(foldertemp): filetemp = os.path.join( foldertemp, filename) file.save(filetemp) setattr(trans, 'image', filetemp) else: os.makedirs(foldertemp) filetemp = os.path.join( foldertemp, filename) file.save(filetemp) setattr(trans, 'image', filetemp) else: return jsonify({'message': 'Image file not supported.'}) except KeyError as e: print(str(e)) pass except Exception as e: print(str(e)) temp_date = payload['date'].split('-') start_date = datetime( int(temp_date[0]), int(temp_date[1]), int(temp_date[2])) trans.flag = payload['status'] trans.finished_goods_code = payload['finished_goods_code'] trans.quantity = payload['quantity'] trans.report = payload['report'] trans.date = start_date db.session.commit() return jsonify({'success': 'Data Added'}) except sqlalchemy.exc.IntegrityError as e: print('Here' + str(e)) db.session.rollback() db.session.close() return jsonify({'message': 'Duplicate entry for values.'}) except Exception as e: print('Here' + str(e)) db.session.rollback() db.session.close() return jsonify({'message': 'Something unexpected happened. Check logs', 'log': str(e)}) else: return jsonify({'message': 'Empty Data.'}) else: return jsonify({'message': 'Invalid HTTP method . Use POST instead.'})
def final(): if 'email' in session: user = mongo.db.users app = mongo.db.apps img = session['img'] if request.method == 'POST': check = app.find_one({'img': img}) if check: ver = request.form['version'] plat = request.form['platform'] pric = request.form['pricing'] app.update({'img': check['img']}, { '$set': { 'version': ver, 'platform': plat, 'pricing': pric, 'icon': img } }) message = mongo.db.messages message.insert({ 'email': session['email'], 'text': 'Your app is being created and will be uploaded to the app stores shortly' }) f = request.files['icon'] filena = secure_filename(img) f.save(os.path.join(upload_folder, filena)) #os.path.join(app.config['UPLOAD_FOLDER'], from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText fromaddr = "*****@*****.**" toaddr = session['email'] msg = MIMEMultipart() msg['From'] = fromaddr msg['To'] = toaddr msg['Subject'] = "Applee support" username = session['email'] body = "Hello {} ".format(username) html = """\ <html> <head></head> <body> <h2>Hello from the Applee team</h2> <p>Thank you for deciding to use App-Lee </br> Your app is being created and will be uploaded to the app store,</br> kindly sit back and enjoy our services.</br> </p> <style> h2{ color:red; } </style> </body> </html> """ part1 = MIMEText(body, 'plain') part2 = MIMEText(html, 'html') msg.attach(part1) msg.attach(part2) #msg.attach(MIMEText(body, 'plain')) import smtplib s = smtplib.SMTP('smtp.gmail.com', 587) s.ehlo() s.starttls() s.login("*****@*****.**", "@123Applee") text = msg.as_string() s.sendmail(fromaddr, toaddr, text) s.quit() return redirect(url_for('dashboard')) return render_template('final.html') return redirect(url_for('login'))
def create_item(): """Allow users to create a new item in the catalog.""" if 'username' not in login_session: return redirect('/login') session = connect_to_database() if request.method == 'POST': if not request.form['name']: flash("New animal not created: No name provided.") return redirect(url_for('show_homepage')) if request.form['name'] == "items": # Can't have an item called "items" as this is a route. flash("Error: Can't have an animal called 'items'.") return redirect(url_for('show_homepage')) # Enforce rule that item names are unique qry = session.query(Item).filter(Item.name == request.form['name']) already_exists = (session.query(literal(True)).filter( qry.exists()).scalar()) if already_exists is True: flash("Error: There is already an animal with the name '%s'" % request.form['name']) session.close() return redirect(url_for('show_homepage')) category = (session.query(Category).filter_by( name=request.form['category']).one()) new_item = Item(category=category, name=request.form['name'], description=request.form['description'], quantity=request.form['quantity'], user_id=login_session['user_id']) # Process optional item image image_file = request.files['file'] if image_file and allowed_file(image_file.filename): filename = secure_filename(image_file.filename) if os.path.isdir(app.config['UPLOAD_FOLDER']) is False: os.mkdir(app.config['UPLOAD_FOLDER']) image_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) new_item.image_filename = filename elif request.form['image_url']: new_item.image_url = request.form['image_url'] session.add(new_item) session.commit() flash("New animal successfully created!") category_name = category.name item_name = new_item.name session.close() return redirect( url_for('show_item', category_name=category_name, item_name=item_name)) else: categories = session.query(Category).all() # See, if any, which category page new item was click on. ref_category = None if request.referrer and 'catalog' in request.referrer: ref_url_elements = request.referrer.split('/') if len(ref_url_elements) > 5: ref_category = ref_url_elements[4] session.close() return render_template('new_item.html', categories=categories, ref_category=ref_category)
def upload(): debug('Uploading files') if request.method == 'POST': trainingFile = request.files['training'] testingFile = request.files['testing'] name = request.form['name'] separate = request.form.getlist('separatetraining') delimiter = request.form['delimiters'] global error_initialization if not name or not allowedName(name): message = "Error: the name of the network must be different than the others" debug(message) error_initialization = message return redirect('/') if not trainingFile: message = "Error: the training file must be attached" debug(message) error_initialization = message return redirect('/') if not testingFile and separate: message = "Error: the testing file must be attached" debug(message) error_initialization = message return redirect('/') if not allowed_file(trainingFile.filename): message = "Error: the training file must have the " + str( ALLOWED_EXTENSIONS) + " format" debug(message) error_initialization = message return redirect('/') if not allowed_file(testingFile.filename) and separate: message = "Error: the testing file must have the " + str( ALLOWED_EXTENSIONS) + " format" debug(message) error_initialization = message return redirect('/') trainingFilename = secure_filename(trainingFile.filename) testingFilename = secure_filename(testingFile.filename) trainingFile.save( os.path.join(app.config['UPLOAD_FOLDER'], trainingFilename)) global new_model if separate: testingFile.save( os.path.join(app.config['UPLOAD_FOLDER'], testingFilename)) new_model = network.Network(name, UPLOAD_FOLDER + '/' + trainingFilename, testing_path=UPLOAD_FOLDER + '/' + testingFilename) else: new_model = network.Network(name, UPLOAD_FOLDER + '/' + trainingFilename, None) debug("Model created : " + str(new_model)) new_model.delimitation = delimiter new_model.separate = separate error_initialization = None global summary_initialization, summary_training, summary_output summary_initialization = True summary_training = False summary_output = False return redirect('/')
def add_image(): """ Add a new image """ form = forms.NewPostForm() if form.validate_on_submit(): filename = secure_filename(form.upload.data.filename) fileext = os.path.splitext(filename)[1] filedata = form.upload.data.stream.read() # Calculate SHA1 checksum h = hashlib.new('sha1') h.update(filedata) filehash = h.hexdigest() # Validate file uniqueness dupe = dbmodel.Image.query.filter_by(sha1sum=filehash).first() if dupe: flash("Image already exists: %s" % (dupe)) return redirect(url_for('entries_index')) else: # File is unique, proceed to create post and image. # Save file to filesystem # Rewind file, it was read() by the SHA1 checksum # routine. By the way this is gross and I'm sorry. form.upload.data.seek(0) # Proceed with storage try: uploaded_images.save(storage=form.upload.data, name=''.join([filehash, '.']), ) # FIXME: generate thumbnail in a safer way. # This is fairly horrible and I'm sorry. imagepath = uploaded_images.path(''.join([filehash, fileext])) utils.mkthumb(imagepath) except IOError: flash("Oh god a terrible error occured while saving %s" % (filename)) else: dbimage = dbmodel.Image(filename, filehash) db.session.add(dbimage) user = None if "uid" in session: user = dbmodel.User.query.filter_by( id=session['uid'] ).first() note = form.note.data #TODO: Implement tags. # Create a new post with the image post = dbmodel.Post(image=dbimage, title=filename,\ note=note, user=user) db.session.add(post) # Commit database transaction db.session.commit() flash("Image successfully posted!") return redirect(url_for('entries_index')) else: flash("Your form has terrible errors in it.") return(redirect(url_for("entries_index")))