def api_upload_file(): file = request.files['file'] user_session = UserSession(db.session, request) # create user session try: # Try to get name from request param = request.form['data'] jsonObject = json.loads(param) name = jsonObject['name'] except: # set name to none if can't name = None if file and allowed_file(file.filename): print ('--------->>>>>> Upload file through API call:') # get user's credentials and save him #ip_addr = request.remote_addr #user = User(None, User.USER_ROLE_USER,{'ip': ip_addr}) #db.session.add(user) #db.session.commit() # add to filename user's cookies filename = secure_filename(str(user_session.getUserID()) + '_'+file.filename) # new secure filename with cookie string dest_filename = os.path.join(app.config['UPLOAD_FOLDER'], filename) # save specification to disk file.save(dest_filename) # calculate the hash print "Internal path on server: ", filename, app.config['UPLOAD_FOLDER'] # Log upload log = open(os.path.join(app.config['UPLOAD_FOLDER'], '_uploads.log'), 'a+') log.write("%s : upload file: %s, from user: %s, IP: %s\n"%( datetime.datetime.now(), filename, user_session.getUserID(), request.remote_addr )) # Load specification to database try: print "Spec name: ", name spec_factory = SpecFactory(dest_filename, specName=name) print "specification factory created" hash = spec_factory.uploadSpecToDatabase(db.session, user_session.getUserID()) print "Spec hash: '%s', return success"%hash user_session.registerEvent(db.session,User_action.UPLOAD_SPEC, request, data={'filename' : dest_filename, 'hash': hash}) # register spec upload resp = make_response(jsonify({'result' : True, 'hash' : hash}), 200) except Exception as e: print "Exceptions happens: ", e resp = make_response({'result' : False, 'message' : 'Upload failed'}, 404) else: resp = make_response(jsonify({'result' : False, 'message' : 'File is not allowed'}), 404) user_session.setCookies(resp) return resp
def test_load_specification(self): user = User(name='User', role=User.USER_ROLE_USER) self.session.add(user) self.session.commit() self.basedir = os.path.abspath(os.path.dirname(__file__)) print "Dir: ", self.basedir filename = self.basedir + '/data/BillOfMaterials_14714923.xls' spec_factory = SpecFactory(filename) self.assertTrue(spec_factory.file_hash != None) hash = spec_factory.uploadSpecToDatabase(self.session, user) result = spec_factory.calculateSpecResources(self.session, hash) self.assertTrue(result.has_key('stat')) print 'Result', result['stat'] self.assertTrue(rtresult.has_key('spec'))
def test_load_specification(self): user=User(name='User', role = User.USER_ROLE_USER) self.session.add(user) self.session.commit() self.basedir = os.path.abspath(os.path.dirname(__file__)) print "Dir: ", self.basedir filename = self.basedir+'/data/BillOfMaterials_14714923.xls' spec_factory = SpecFactory(filename) self.assertTrue(spec_factory.file_hash !=None) hash = spec_factory.uploadSpecToDatabase(self.session,user) result = spec_factory.calculateSpecResources(self.session, hash) self.assertTrue(result.has_key('stat')) print 'Result', result['stat'] self.assertTrue(rtresult.has_key('spec'))
def upload_file(): file = request.files['file'] user_session = UserSession(db.session, request) # create user session if file and allowed_file(file.filename): # get user's credentials and save him #ip_addr = request.remote_addr #user = User(None, User.USER_ROLE_USER,{'ip': ip_addr}) #db.session.add(user) #db.session.commit() # user added - this part should be changed to registration # add to filename user's cookies filename = secure_filename(user_session.getCookie() + '_'+file.filename) # new secure filename with cookie string dest_filename = os.path.join(app.config['UPLOAD_FOLDER'], filename) # save specification to disk file.save(dest_filename) # calculate the hash print "Internal path on server: ", filename, app.config['UPLOAD_FOLDER'] # Log upload log = open(os.path.join(app.config['UPLOAD_FOLDER'], '_uploads.log'), 'a+') log.write("%s : upload file: %s, from user: %s, IP: %s\n"%( datetime.datetime.now(), filename, user_session.getUserID(), request.remote_addr )) # Load specification to database try: spec_factory = SpecFactory(dest_filename) print "specification factory created" hash = spec_factory.uploadSpecToDatabase(db.session, user_session.getUserID()) print "Spec hash: '%s', redirecting"%hash resp = make_response(redirect('#/specification/' + hash)) except Exception as e: print "Exceptions happens: ", e resp = make_response(redirect('/'), 404) # Add exception handler - redirect to bug report page. /#/bugreport/ par:{filename :} else: resp = make_response(redirect('/'), 404) user_session.setCookies(resp) return resp
def get_specification(hash): print "Got parameter: ", hash user_session = UserSession(db.session, request) # create user session user_session.registerEvent(db.session, User_action.FIND_SPEC,request,data={'hash' : hash}) result = SpecFactory.calculateSpecResources(db.session, hash) return jsonify({'result': True, 'data': result}), 200