Exemple #1
0
    def connect(self):
        """Connect to the MongoDB server and register the documents from
        :attr:`registered_documents`. If you set ``MONGODB_USERNAME`` and
        ``MONGODB_PASSWORD`` then you will be authenticated at the
        ``MONGODB_DATABASE``.
        """
        if self.app is None:
            raise RuntimeError('The flask-mongokit extension was not init to '
                               'the current application.  Please make sure '
                               'to call init_app() first.')

        ctx = ctx_stack.top
        mongokit_connection = getattr(ctx, 'mongokit_connection', None)
        if mongokit_connection is None:
            ctx.mongokit_connection = Connection(
                host=ctx.app.config.get('MONGODB_HOST'),
                port=ctx.app.config.get('MONGODB_PORT'),
                slave_okay=ctx.app.config.get('MONGODB_SLAVE_OKAY'))

            ctx.mongokit_connection.register(self.registered_documents)

        mongokit_database = getattr(ctx, 'mongokit_database', None)
        if mongokit_database is None:
            ctx.mongokit_database = Database(
                ctx.mongokit_connection,
                ctx.app.config.get('MONGODB_DATABASE'))

        if ctx.app.config.get('MONGODB_USERNAME') is not None:
            auth_success = ctx.mongokit_database.authenticate(
                ctx.app.config.get('MONGODB_USERNAME'),
                ctx.app.config.get('MONGODB_PASSWORD'))
            if not auth_success:
                raise AuthenticationIncorrect
Exemple #2
0
def init_db():
    config = DefaultConfig()
    con = Connection(config.MONGODB_HOST, config.MONGODB_PORT)
    #con.drop_database(config.MONGODB_DATABASE)
    con.register(User)
    db = Database(con, config.MONGODB_DATABASE)

    index = 0
    for email in config.ADMIN:
        user = db.User()
        user['email'] = email
        user['password'] = db.User.encode_pwd(u'123456')
        user['create_time'] = datetime.datetime.now()
        user['nickname'] = u''
        if db.User.find({'email': email}).count() == 0:
            user.save()
            index += 1

    print '%s done.' % index
Exemple #3
0
def init_db(host=None, port=None, database=None):
    con = Connection(host, port)
    con.drop_database(database)
    con.register([Admin, AdminRole])
    db = Database(con, database)

    generate_index(host, port, database)

    role = db.AdminRole()
    role['name'] = u'管理员'
    role['auth_list'] = get_auth_list()
    role.save()

    user = db.Admin()
    user['email'] = u'*****@*****.**'
    user['name'] = u'admin'
    user['password'] = db.Admin.encode_pwd('123456')
    user['login_time'] = datetime.now()
    user['status'] = True
    user['role'] = role
    user.save()

    return 'success'
Exemple #4
0
def generate_index(host=None, port=None, database=None):
    con = Connection(host, port)
    con.register([Article])
    db = Database(con, database)

    db.Article.generate_index(db.article)
Exemple #5
0
def get_db():
    config = DefaultConfig()
    con = Connection(config.MONGODB_HOST, config.MONGODB_PORT)
    con.register(Dictionary)
    db = Database(con, config.MONGODB_DATABASE)
    return db