def register_event(): ''' Function parameter (JSON): event - type of the event :return: user's browser has cookies and session ID ''' user_session = UserSession(db.session, request) # create user session result = False try: # Try to get name from request #param = request.json jsonObject = request.json event = jsonObject['event'] except: # set name to none if can't event = None print "Event is None, user session: %s" % (user_session.getUserID()) else: if (event and event != User_action.REGISTER and event != User_action.CONNECT): try: eventData = jsonObject['eventData'] except KeyError as e: eventData = None user_session.registerEvent(db.session, event, request, data=eventData) print "registering event %s, with data %s" % (event, eventData) result = True resp = make_response(jsonify({'result': result}), 200) user_session.setCookies(resp) return resp
def index(): print "App static folder: ", app.static_folder user_session = UserSession(db.session, request) # create user session resp = make_response("Hello from server", 200) user_session.setCookies(resp) return resp
def register_event(): ''' Function parameter (JSON): event - type of the event :return: user's browser has cookies and session ID ''' user_session = UserSession(db.session, request) # create user session result = False try: # Try to get name from request #param = request.json jsonObject = request.json event = jsonObject['event'] except: # set name to none if can't event = None print "Event is None, user session: %s"%(user_session.getUserID()) else: if (event and event != User_action.REGISTER and event != User_action.CONNECT ): try: eventData=jsonObject['eventData'] except KeyError as e: eventData = None user_session.registerEvent(db.session,event,request,data=eventData) print "registering event %s, with data %s"%(event, eventData) result = True resp = make_response(jsonify({'result' : result}), 200) user_session.setCookies(resp) return resp
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 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