示例#1
0
def register():
    """ basic registration page """
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        error = None

        if not username:
            error = 'Username is required.'
        elif not password:
            error = 'Password is required.'

        # ensure the username is not taken
        else:
            db = get_mongoDB()
            result = db.users.find_one({"username": username})

            if result is not None:
                error = 'User {} is already registered.'.format(username)

        # if error checks pass, insert 'name' and hashed 'password' into database
        if error is None:
            addOne = Users(username=username, password=generate_password_hash(password))
            addOne.save()
            return redirect(url_for('auth.login'))

        flash(error)

    return render_template('auth/register.html')
示例#2
0
def init_db():
    """ populate database with admin user and two posts """
    try:
        version = current_app.config["MONGODB_SETTINGS"]["db"]
        print('using {} database'.format(version))
    except:
        print(
            "ERROR: $export APP_SETTINGS=flaskr.config.DevelopmentConfig OR $export APP_SETTINGS=flaskr.config.TestingConfig' to configure database"
        )

    if (version is None):
        print('raise error: dataBase == None')

    # if the database version is not 'testDB' and not 'devDB' don't proceed
    elif ((version != 'testDB') & (version != 'devDB')):
        print('raise error: not testDB or devDB')

    else:
        print('\nrefresh database ...')
        with current_app.app_context():
            db = get_mongoDB()

            # find all collections in current database
            collections = db.list_collection_names()
            for item in collections:
                db.drop_collection(item)

            # adding administrative user to setup schema
            adminUser = Users(username="******",
                              password=generate_password_hash("blackOps"))
            adminUser.save()

            # get the admin user objectId
            admin = db.users.find_one({"username": '******'})

            # add two posts to the database
            post = Posts(post_id=1,
                         username=admin["username"],
                         author_id=ObjectId(str(admin["_id"])),
                         title='first post',
                         body='a first time posting')
            post2 = Posts(post_id=2,
                          username=admin["username"],
                          author_id=ObjectId(str(admin["_id"])),
                          title='second post',
                          body='second go at it')
            post.save()
            post2.save()
            print('Added admin user and two posts\n')