Example #1
0
def before_request():
    if request.method != 'OPTIONS':
        if request.endpoint == 'login' or request.endpoint == 'register':
            admin = usersCollection.find_one({ 'email': 'admin' })
            if admin is None :
                usersCollection.insert_one(adminObj)
        elif 'Authorization' not in request.headers:
            raise exceptionHandler.InvalidUsage('Un Authorized User', status_code=420)
        if 'Authorization' in request.headers and auth.isUserLoggedIn(request.headers['Authorization']) is None:
            raise exceptionHandler.InvalidUsage('Session Timed Out', status_code=420)
    else:
        return ''
Example #2
0
def upload_file():
    if request.method == 'POST':
        #  file = request.files['file']
        #     if file and allowed_file(file.filename):
        #         filename = secure_filename(file.filename)
        #         file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

        #         ## snippet to read code below
        #         file.stream.seek(0) # seek to the beginning of file
        #         myfile = file.file # will point to tempfile itself
        #         dataframe = pd.read_csv(myfile)
        #         ## end snippet
        user = auth.getLoggedInUser(request.headers['Authorization'])
        # check if the post request has the file part
        file = request.files.get('file')
        # if user does not select file, browser also
        # submit an empty part without filename
        if file.filename == '':
            flash('No file part')
            return exceptionHandler.InvalidUsage('Invalid Recording', status_code=420)
        if file and allowed_file(file.filename) and not mongoConnector.existInDatabase(file.filename):
            filename = uploader._safe_filename(file.filename)
            file.save('./' + filename)
            response = mongoConnector.pushToDatabase(filename, user['email'], user['permission'])
            return mongoConnector.prepareResponse(response)
    return '''
Example #3
0
def register():
    if request.method == 'POST':
        existing_user = usersCollection.find_one(
            {'email': request.json['email']})
        if existing_user is None:
            hashpass = bcrypt.hashpw(request.json['password'].encode('utf-8'),
                                     bcrypt.gensalt())
            usersCollection.insert({
                'firstName': request.json['firstName'],
                'lastNname': request.json['lastName'],
                'email': request.json['email'],
                'empId': request.json['empId'],
                'password': hashpass,
                'industry': request.json['industry'],
                'serviceLine': request.json['serviceLine'],
                'serviceArea': request.json['serviceArea'],
                'designation': request.json['designation'],
                'location': request.json['location'],
                'mobileNo': request.json['mobileNo'],
                'permission': 'guest'
            })
            newUser = usersCollection.find({'email': request.json['email']},
                                           {'password': 0})
            return mongo.prepareResponse(newUser)
        else:
            raise exceptionHandler.InvalidUsage('User already exists',
                                                status_code=420)
Example #4
0
def login():
    login_user = usersCollection.find_one({'email': request.json['email']})
    if login_user:
        if bcrypt.checkpw(request.json['password'].encode('utf-8'),
                          login_user['password']):
            token = secrets.token_hex(20)
            tokensCollection.delete_one({'email': request.json['email']})
            tokensCollection.insert({
                'token':
                token,
                'email':
                request.json['email'],
                'expiry':
                (datetime.now() +
                 timedelta(hours=1)).strftime("%Y-%m-%dT%H:%M:%S.000Z")
            })
            response = {
                'email': login_user['email'],
                'firstName': login_user['firstName'],
                'token': token,
                'permission': login_user['permission']
            }
            return mongo.prepareResponse(response)
    raise exceptionHandler.InvalidUsage('Invalid email/password combination',
                                        status_code=420)
Example #5
0
def register():
    if request.method == 'POST':
        existing_user = usersCollection.find_one(
            {'email': request.form['email']})
        if existing_user is None:
            hashpass = bcrypt.hashpw(request.form['password'].encode('utf-8'),
                                     bcrypt.gensalt())
            usersCollection.insert({
                'firstname': request.form['firstname'],
                'lastname': request.form['lastname'],
                'email': request.form['email'],
                'empid': request.form['empid'],
                'password': hashpass,
                'industry': request.form['industry'],
                'serviceline': request.form['serviceline'],
                'servicearea': request.form['servicearea'],
                'designation': request.form['designation'],
                'location': request.form['location'],
                'mobileno': request.form['mobileno'],
                'permission': 'guest'
            })
            session['email'] = request.form['email']
            return redirect('/login', code=200)
        else:
            raise exceptionHandler.InvalidUsage('User already exists',
                                                status_code=410)
Example #6
0
def users():
    if request.method == 'GET':
        existing_users = usersCollection.find({'permission': 'guest'},
                                              {'password': 0})
        if existing_users:
            return mongo.prepareResponse(existing_users)
        else:
            raise exceptionHandler.InvalidUsage('No Users', status_code=420)
Example #7
0
def login():
    login_user = usersCollection.find_one({'email': request.form['email']})
    if login_user:
        if bcrypt.hashpw(request.form['password'].encode('utf-8'),
                         login_user['password']) == login_user['password']:
            session['email'] = request.form['email']
            return redirect('/voiceAnalyzer', code=200)

    raise exceptionHandler.InvalidUsage('Invalid email/password combination',
                                        status_code=410)