def post(self):
        user_collection = app.db.users
        password = request.json["password"]
        pass_hash = hash_password(password, bcrypt.gensalt(app.bcrypt_rounds))
        request.json["password"] = pass_hash
        request.json["trips"] = []

        result = user_collection.insert_one(request.json)
        user = user_collection.find_one({"_id": ObjectId(result.inserted_id)})
        del user['password']  # DO NOT return password back to the client!!!
        return user
예제 #2
0
def new():
        form = NewAccount()
        if form.validate_on_submit():
            check_userdb()
            pwdHash = bcrypt.hashpw(form.pwd.data, bcrypt.gensalt())
            usersDB = bcrypt.hashpw(form.first_name.data + form.last_name.data + form.email.data, bcrypt.gensalt())
            dbase = './troops-to-task/user_db.sqlite'
            try:
                sql = t2t_sqlite.addUser(form.first_name.data, form.last_name.data, form.email.data, pwdHash, usersDB)
                debug(sql)
            except:
                flash('Error in generating SQL for new account.')
            try:
                theReturn = callSQL(dbase, sql)
                debug(theReturn)
            except:
                flash('Error Calling SQL: ' + sql)


            sql = t2t_sqlite.getUser(form.email.data)
            check = callSQL(dbase, sql)

            if bcrypt.hashpw(form.pwd.data, check[0][4]) == check[0][4]:
                usersdb = './troops-to-task/userdb/' + check[0][5] + '.sqlite'
                init_sql = '''
CREATE TABLE troops (
    troopid char(10) NOT NULL PRIMARY KEY,
    rank char(5) NOT NULL,
    first_name char(50),
    last_name char(50) NOT NULL,
    timestamp DATETIME DEFAULT CURRENT_TIMESTAMP);

CREATE TABLE tasks (
    taskid INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    troop_id INTEGER NOT NULL,
    event_id INTEGER NOt NULL,
    timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY(troop_id) REFERENCES troops(dodid),
    FOREIGN KEY(event_id) REFERENCES events(eventid));

CREATE TABLE log (
    logid INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    event text,
    timestamp DATETIME DEFAULT CURRENT_TIMESTAMP);

CREATE TABLE events (
    eventid INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    title char(50) NOT NULL,
    startDTG DATETIME NOT NULL,
    endDTG DATETIME NOT NULL,
    timestamp DATETIME DEFAULT CURRENT_TIMESTAMP);
'''

                try:
                    connect_db(usersdb)      # SQLite3 auto-creates on connect if it does not exists
                except:
                    flash('Error connecting to user''s database.')
                try:
                    cursor.executescript(init_sql)               # initialize
                except:
                    flash('Error creating user''s database.')
                try:
                    disconnect_db()
                except:
                    flash('Error disconnecting from user''s database.')

                flash('Account created successfully!')
                #account created, proceed to login the user
            else:
                flash('Sorry, there was an error creating your account. :(')
            return redirect('/index')
        return render_template('new.html',
                               title='Sign Up!',
                               form=form)