コード例 #1
0
def main(*args):
    con = Connection()
    con.register([Vote, GistPoints, UserPoints, User, Gist, Comment])
    db = con[settings.DEFAULT_DATABASE_NAME]

    for user in db.User.find():

        up = db.UserPoints.one({'user.$id': user._id})
        total_up = 0
        if not up:
            up = db.UserPoints()
            up.points = 0
            up.user = user

        for gist in db.Gist.find({'user.$id': user._id}):
            gp = db.GistPoints.one({'gist.$id': gist._id})
            if not gp:
                gp = db.GistPoints()
                gp.gist = gist
                gp.points = 0
            gp.points = sum(
                x['points']
                for x in db.Vote.collection.find({'gist.$id': gist._id}))
            gp.save()
            total_up += gp.points
        up.points = total_up
        up.save()

    return 0
コード例 #2
0
ファイル: structure.py プロジェクト: iceout/group_talk_bot
def init():
    global connection
    logger.info('connecting to database')
    #conn_args = getattr(config,'conn_args',{})
    connection = Connection()
    logger.info('database connected ')
    connection.register([User])
コード例 #3
0
ファイル: connect.py プロジェクト: rockybars/grimlock
def get_connection():
    Item.__database__ = settings.MONGO_DB

    connection = Connection(host=settings.MONGO_HOST)
    connection.register([Item])

    return connection
コード例 #4
0
class DBConnection(object):

    def __init__(self):
        #mongodb_uri = "mongodb://13.76.244.22:27017"
        #mongolab_uri = "mongodb://*****:*****@ds062818.mlab.com:62818/MongoLab-25"
        #connectionString = 'mongodb://*****:*****@ds062818.mlab.com:62818/MongoLab-25'
        #self.client = MongoClient(mongolab_uri,
        #             connectTimeoutMS=30000,
        #             socketTimeoutMS=None,
        #             socketKeepAlive=True)
        #self.db = self.client.get_default_database()
        #self.con = Connection(mongodb_uri)
        self.con = Connection()

        self.con.register([Route])
        self.con.register([Stop])

        self.routes = self.con.Route
        self.stops = self.con.Stop
        
    def __del__(self):
        self.con.close()
        self.con.disconnect()

    def connect():        
        con = Connection()
        routes = con.amdoraft.routes
        stops = con.amdoraft.stops
        return

    def disconnect():
        return
コード例 #5
0
def run(max_updates=5):
    from apps.main.models import User
    from mongokit import Connection
    con = Connection()
    con.register([User])
    import settings
    db = con[settings.DEFAULT_DATABASE_NAME]
    print db.User.find().count(), "users in total"
    today = datetime.date.today()
    today = datetime.datetime(*(today.timetuple()[:6]))
    search = {'modify_date': {'$lt': today}}
    print db.User.find(search).count(), "left to update today"
    for user in db.User.find(search).sort('modify_date', 1).limit(max_updates):
        print repr(user.login), user.modify_date
        details = pull_details(user.login)
        if details is None:
            print "FAIL!"
            print "??? http://github.com/api/v2/json/user/show/%s" % user.login
            continue
        for key in 'name company email gravatar_id'.split():
            if key in details:
                if getattr(user, key, '') != details[key]:
                    print "\t", key, repr(getattr(user, key, '*blank*')),
                    print '-->', repr(details[key])
                setattr(user, key, details[key])
        user.modify_date = datetime.datetime.now()
        user.save()
コード例 #6
0
def main(*args):
    con = Connection()
    con.register([Vote, GistPoints, UserPoints,
                  User, Gist, Comment])
    db = con[settings.DEFAULT_DATABASE_NAME]

    for user in db.User.find():

        up = db.UserPoints.one({'user.$id': user._id})
        total_up = 0
        if not up:
            up = db.UserPoints()
            up.points = 0
            up.user = user

        for gist in db.Gist.find({'user.$id': user._id}):
            gp = db.GistPoints.one({'gist.$id': gist._id})
            if not gp:
                gp = db.GistPoints()
                gp.gist = gist
                gp.points = 0
            gp.points = sum(x['points'] for x in
                            db.Vote.collection.find({'gist.$id': gist._id}))
            gp.save()
            total_up += gp.points
        up.points = total_up
        up.save()

    return 0
コード例 #7
0
ファイル: connect.py プロジェクト: sarutobi/suckapy
def get_connection():
    Item.__database__ = settings.MONGO_DB
    Source.__database__ = settings.MONGO_DB

    connection = Connection(host=settings.MONGO_HOST)
    connection.register([Item, Source])

    return connection
コード例 #8
0
ファイル: db.py プロジェクト: dkislyuk/synced.fm
def get_connection(models_to_register):
    ctx = _app_ctx_stack.top
    con = getattr(ctx, 'synced_database', None)
    if con is None:
        con = Connection(os.environ['MONGOHQ_CONN'])
        con.register(models_to_register)
        ctx.synced_database = con
    return con
コード例 #9
0
ファイル: models.py プロジェクト: dkislyuk/synced.fm
 def get_tracks(self):
     connection = Connection() if conn_str is None else Connection(conn_str)
     connection.register([TrackData])
     
     for track_id in self.data:
         track = connection.TrackData.one({"_id": ObjectId(track_id)})
         
         yield (track.label, track.data)
コード例 #10
0
ファイル: base.py プロジェクト: amitdash/worklog
 def setUp(self):
     if not self._once:
         self._once = True
         from mongokit import Connection
         con = Connection()
         con.register([User, Event, UserSettings, Share,
                       FeatureRequest, FeatureRequestComment])
         self.db = con.test
         self._emptyCollections()
コード例 #11
0
class Application(tornado.web.Application):
    def __init__(self, handlers, database_name=None, **kwargs):
        tornado.web.Application.__init__(self, handlers, **kwargs)
        self.database_name = database_name and database_name or options.database_name
        self.con = Connection()
        self.con.register([User, ChatMessage])

    @property
    def db(self):
        return self.con[self.database_name]
コード例 #12
0
ファイル: models.py プロジェクト: dkislyuk/synced.fm
 def delete_data(self):
     connection = Connection() if conn_str is None else Connection(conn_str)
     connection.register([TrackData])
     
     for track_id in self.data:
         track = connection.TrackData.one({"_id": ObjectId(track_id)})
         track.delete()
         
     self.data = []
     self.save()
コード例 #13
0
 def setUp(self):
     if not self._once:
         self._once = True
         from mongokit import Connection
         con = Connection()
         con.register([
             User, Event, UserSettings, Share, FeatureRequest,
             FeatureRequestComment
         ])
         self.db = con.test
         self._emptyCollections()
コード例 #14
0
class NotablyTestCase(unittest.TestCase):
	'''Test case for notably--MONGO DB MUST BE RUNNING ON localhost:27107'''
	def setUp(self):
		app.config.update({
			'DATABASE': 'test',
			'MONGODB_HOST': 'localhost',
			'MONGODB_PORT': 27017,
			'TESTING': True,
			'SECRET_KEY': 'testing key',
		})
		
		self.generic_entry = {'content': 'test', 'rows': '1', 'date': datetime.datetime.now()}
		self.generic_user = {'name': 'test_user', 'pw': 'default'}	
	
		self.app = app.test_client()
		self.conn = Connection(app.config['MONGODB_HOST'], app.config['MONGODB_PORT'])
		#self.conn.register([Entry, User])
		self.conn.register([Entry])
		#reset the collections
		self.conn[app.config['DATABASE']].entries.drop()
		self.conn[app.config['DATABASE']].users.drop()
	
	#we don't actually need to tear anything down--but we'll leave this in case that changes
	def tearDown(self):
		pass

	def test_entries_view(self):
		'''tests that the database is queried, index.html is rendered and returned.
		there are no entries in the test db so the index page will have nothing but and empty textarea'''
		rv = self.app.get('/')
		assert '<!DOCTYPE html>' in rv.data
		
	def test_add_entry(self):
		'''tests adding a new entry'''
		rv = self.app.post('/update/', data=self.generic_entry, follow_redirects=True)
		assert 'Bad Request' not in rv.data
		
	def test_update_entry(self):
		'''test modifying an existing entry'''
		#generate a new entry
		entries = self.conn[app.config['DATABASE']].entries
		entry = entries.Entry()
		entry.content.append(u'test')
		entry.rows.append(1)
		entry.date.append(datetime.datetime.now())	
		entry.save()
		new_entry = self.generic_entry
		new_entry.update({'id': entry._id})
		#post an entry update using the id of the entry we just created
		rv = self.app.post('/update/', data=new_entry, follow_redirects=True)
		import pymongo
		entry = entries.Entry.one({'_id': pymongo.objectid.ObjectId(entry._id)})
		assert len(entry.content) == 2
		assert 'Bad Request' not in rv.data
コード例 #15
0
ファイル: database.py プロジェクト: twil/catalog-backend
def init_connection(app):
    """
    Init DB connection and register models (documents)
    
    """
    
    config = get_config(app)
    conn = Connection(**config['connection'])
    conn.register([Product, Category])
    
    return conn
コード例 #16
0
ファイル: dbmodel.py プロジェクト: breynisson/AutobenchmarkUI
def get_mongo_objs(host, port, dbname,
                   username=None, passwd=None):
    """
    :return: Database connection, database instance and collection instance.
    """
    connection = Connection(host=host, port=port,
                            read_preference=ReadPreference.NEAREST)
    connection.register([BenchmarkResult])
    benchresult = getattr(connection, dbname).benchresults.BenchmarkResult
    db = Database(connection, dbname)
    if username is not None:
        db.authenticate(username, password=passwd)
    return connection, db, benchresult
コード例 #17
0
ファイル: mongo.py プロジェクト: ASCIIDuck/SysFest
class MongoFest(BaseDB):
	def __init__(self,app):
		self.conn = Connection(app.config['MONGODB_HOST'],app.config['MONGODB_PORT'])	
		self.conn.register(Host)
		self.logger = app.logger
	def find(self,hostname='',tags=''):
		if hostname == '':
			return [ self._clean_oid(h) for h in self.conn.sysfest.Host.find().sort("hostname", pymongo.ASCENDING) ]
		elif hostname != '':
			regx = re.compile(hostname)
			return [ self._clean_oid(h) for h in self.conn.sysfest.Host.find({"$or":[{'hostname':regx},{'homes.hostnames.val':regx}]}).sort("hostname", pymongo.ASCENDING) ]
	def search(self,query):
		tag_pattern = re.compile('tag:(\w+)')
		host_pattern = re.compile('host:([\w\.-]+)')
		tags = tag_pattern.findall(query)
		hosts = host_pattern.findall(query)
		query = tag_pattern.sub('',query)
		query = host_pattern.sub('',query)
		key_words = query.split(' ')

		terms = [ {"description":re.compile(x,flags=re.I)} for x in key_words]

		if hosts:
			terms = terms + [{"$or":[{'hostname':re.compile(x)},{'homes.hostnames.val':re.compile(x)}]} for x in hosts]
		
		if tags: #pythonic (moronic) way to check if an array is empty
			terms = terms + [{"tags":{"$all":tags}}]

		return [ self._clean_oid(h) for h in self.conn.sysfest.Host.find({"$and":terms}).sort("hostname", pymongo.ASCENDING) ]

	def find_one(self,host_id):
		if isinstance(host_id, basestring):
			host_id=bson.objectid.ObjectId(host_id)
		host = self._clean_oid(self.conn.sysfest.Host.find_one({'_id':host_id}))
		return host
	def update(self,host_id,values):
		self.conn.sysfest.hosts.update({"_id":bson.objectid.ObjectId(host_id)},{"$set":values})
		return self.find_one(host_id)
	def create(self,values):
		oid = self.conn.sysfest.hosts.insert(values)
		return self.find_one(host_id=oid)
	def delete(self,host_id):
		return self.conn.sysfest.hosts.remove({'_id':bson.objectid.ObjectId(host_id)})

	def close(self):
		self.conn.disconnect()

	def _clean_oid(self,host):
		if host is not None and isinstance(host["_id"], bson.objectid.ObjectId):
			host["_id"]=str(host["_id"])
		return host
コード例 #18
0
ファイル: db_operations.py プロジェクト: yedi/Avalon
def init(host, port, db='test'):
    global connection
    global initialized
    global cur_db
    global root
    global dbcon
    connection = Connection(host, port)
    initialized = True
    cur_db = db
    dbcon = connection[cur_db]

    # register the User document with our current connection
    connection.register([models.Item, models.Relation, models.User, models.Comment])

    root = getRootItem()
コード例 #19
0
ファイル: models.py プロジェクト: Jack1007/xmpptalk
def init():
  global connection
  logger.info('connecting to database...')
  conn_args = getattr(config, 'connection', {})
  connection = Connection(**conn_args)
  logger.info('database connected')

  if getattr(config, 'database_auth', None):
    logger.info('authenticating...')
    connection[config.database].authenticate(*config.database_auth)
  try:
    connection[config.database].collection_names()
  except OperationFailure:
    logger.error('database authentication failed')
    raise
  connection.register([User, Log, Group])
コード例 #20
0
def init(host, port, db='test'):
    global connection
    global initialized
    global cur_db
    global root
    global dbcon
    connection = Connection(host, port)
    initialized = True
    cur_db = db
    dbcon = connection[cur_db]

    # register the User document with our current connection
    connection.register(
        [models.Item, models.Relation, models.User, models.Comment])

    root = getRootItem()
コード例 #21
0
def init():
    global connection
    logger.info('connecting to database...')
    conn_args = getattr(config, 'connection', {})
    connection = Connection(**conn_args)
    logger.info('database connected')

    if getattr(config, 'database_auth', None):
        logger.info('authenticating...')
        connection[config.database].authenticate(*config.database_auth)
    try:
        connection[config.database].collection_names()
    except OperationFailure:
        logger.error('database authentication failed')
        raise
    connection.register([User, Log, Group])
コード例 #22
0
def connect_mongodb(cfg):
    mongodb_conn = MongodbConn(
        host=cfg.MONGODB_HOST,
        port=cfg.MONGODB_PORT,
        max_pool_size=cfg.MONGODB_MAX_POOL_SIZE,
    )

    mongodb_conn.register(models)

    mongodb = mongodb_conn[cfg.MONGODB_DATABASE]
    if hasattr(cfg, 'MONGODB_USER') and \
       hasattr(cfg, 'MONGODB_PASSWORD') and \
       cfg.MONGODB_USER and cfg.MONGODB_PASSWORD:
        mongodb.authenticate(cfg.MONGODB_USER, cfg.MONGODB_PASSWORD)

    return mongodb_conn, mongodb
コード例 #23
0
ファイル: base.py プロジェクト: MechanisM/tornado_gists
class BaseModelsTestCase(unittest.TestCase):
    _once = False
    def setUp(self):
        if not self._once:
            self._once = True
            from mongokit import Connection
            self.con = Connection()
            self.con.register([User, UserSettings])
            self.db = self.con.test
            self._emptyCollections()

    def _emptyCollections(self):
        [self.db.drop_collection(x) for x
         in self.db.collection_names()
         if x not in ('system.indexes',)]

    def tearDown(self):
        self._emptyCollections()
コード例 #24
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
コード例 #25
0
ファイル: __init__.py プロジェクト: Soopro/flow
def create_app():
    app = Flask(__name__)
    app.config.from_object('config')

    mongodb_database = MongoDBConn(host=app.config.get("MONGODB_HOST"),
                                   port=app.config.get("MONGODB_PORT"))

    mongodb_conn = mongodb_database[app.config.get("MONGODB_DATABASE")]

    from .models import User, Task, Project

    mongodb_database.register([User, Task, Project])

    app.mongodb_database = mongodb_database
    app.mongodb_conn = mongodb_conn

    from .api_v1 import api as api_blueprint
    from .open_api import open_api as open_api_blueprint

    app.register_blueprint(api_blueprint, url_prefix="/api/v1")
    app.register_blueprint(open_api_blueprint, url_prefix="/open_api")

    @app.before_request
    def app_before_request():
        if request.method == "OPTIONS":
            resp = current_app.make_default_options_response()
            cors_headers = {
                "Access-Control-Allow-Headers": "Origin, Accept, Content-Type, Authorization",
                "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS, HEAD",
                "Access-Control-Allow-Origin": "*"
            }
            resp.headers.extend(cors_headers)
            return resp
        return

    @app.after_request
    def after_request(rv):
        headers = getattr(g, "headers", {})
        rv.headers.extend(headers)
        return rv


    return app
コード例 #26
0
ファイル: init_db.py プロジェクト: Lobos/wordstore
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
コード例 #27
0
ファイル: base.py プロジェクト: peterbe/tornado_gists
class BaseModelsTestCase(unittest.TestCase):
    _once = False

    def setUp(self):
        if not self._once:
            self._once = True
            from mongokit import Connection
            self.con = Connection()
            self.con.register([User])
            self.db = self.con.test
            self._emptyCollections()

    def _emptyCollections(self):
        [
            self.db.drop_collection(x) for x in self.db.collection_names()
            if x not in ('system.indexes', )
        ]

    def tearDown(self):
        self._emptyCollections()
コード例 #28
0
ファイル: chat.py プロジェクト: darrenmoore/chat
class Chat():

	def __init__(self):
		self.sessions = {}
		return

	def init(self):
		print('Chat started')

		#Database connection
		self.database()

		#Setup
		self.protocols()
		self.email()
		self.controllers()

		#Server
		self.server()

	#Protocols
	def protocols(self):
		print('Loading Protocols')
		self.JsonProtocol = JsonProtocol()
		self.TextProtocol = TextProtocol()

	#Database connection
	def database(self):
		print('Loading Database')
		self.connection = Connection()
		self.connection.register([User])
		self.connection.register([Channel])
		self.connection.register([Post])
		self.connection.register([Email])

	def email(self):
		print('Loading Email')
		self.Emailer = Emailer(self.connection)

	def controllers(self):
		print('Loading Controllers')
		self.UsersController = UsersController(self)
		self.ChannelsController = ChannelsController(self)
		self.PostsController = PostsController(self)
		self.SystemsController = SystemsController(self)

	#Server connection
	def server(self):
		print('Loading Server')
		self.srv = Server()
		self.srv.start(self)
コード例 #29
0
ファイル: test_email.py プロジェクト: darrenmoore/chat
class TestEmail(unittest.TestCase):
    def setUp(self):
        self.db = Connection()
        self.db.register([Email])
        self.emailer = Emailer(self.db)

    def test_send(self):
        to_email = '*****@*****.**'
        subject = 'Test email %s' % random_word()
        template = 'test'
        data = {}
        self.emailer.send(to_email, subject, template, data)
        self.emailer.send_queue()
        email = self.db.Email.find_one({'subject': subject, 'status': 'sent'})
        self.assertTrue(email)

    def test_queued(self):
        to_email = '*****@*****.**'
        subject = 'Test email %s' % random_word()
        template = 'test'
        data = {}
        self.emailer.send(to_email, subject, template, data)
        email = self.db.Email.find_one({
            'subject': subject,
            'status': 'pending'
        })
        self.emailer.cancel_queue()
        self.assertTrue(email)

    def test_cancelled(self):
        to_email = '*****@*****.**'
        subject = 'Test email %s' % random_word()
        template = 'test'
        data = {}
        self.emailer.send(to_email, subject, template, data)
        self.emailer.cancel_queue()
        email = self.db.Email.find_one({
            'subject': subject,
            'status': 'cancelled'
        })
        self.assertTrue(email)
コード例 #30
0
ファイル: app_globals.py プロジェクト: s3thi/QuoteHI
class Globals(object):
    """Globals acts as a container for objects available throughout the
    life of the application

    """

    def __init__(self, config):
        """One instance of Globals is created during application
        initialization and is available during requests via the
        'app_globals' variable

        """
        self.cache = CacheManager(**parse_cache_config_options(config))
        
        mongo_host = config['mongo.host']
        mongo_port = int(config['mongo.port'])
        mongo_db = config['mongo.db']
    
        self.connection = Connection(mongo_host, mongo_port)
        self.db = self.connection[mongo_db]
        self.connection.register(register_models)
コード例 #31
0
ファイル: di.py プロジェクト: Sunzhifeng/redmine
def execute():
    """ this function is used to execute dependency inject (DI).
    """
    # provider db connection
    connection = Connection(configs.db.mongo)
    connection.register([Bug, Feature, Improvement, Ticketing, User])
    features.provide(Connection.__name__, connection)  # signletion

    # provider handler
    features.provide(UserHandler.__name__, UserHandler(User))
    features.provide('bugHandler', TicketHandler(Bug))
    features.provide('featureHandler', TicketHandler(Feature))
    features.provide('imporvementHandler', TicketHandler(Improvement))
    features.provide('ticketingHandler', TicketingHandler(Ticketing))

    # provider service
    features.provide(UserService.__name__, UserService())
    features.provide('bugService', BugService())
    features.provide('featureService', FeatureService())
    features.provide('improvementService', ImprovementService())
    features.provide('ticketingService', TicketingService())
コード例 #32
0
class Chat():
    def __init__(self):
        self.sessions = {}
        return

    def init(self):
        print('Chat started')

        #Database connection
        self.database()

        #Setup
        self.protocols()
        self.email()
        self.controllers()

        #Server
        self.server()

    #Protocols
    def protocols(self):
        print('Loading Protocols')
        self.JsonProtocol = JsonProtocol()
        self.TextProtocol = TextProtocol()

    #Database connection
    def database(self):
        print('Loading Database')
        self.connection = Connection()
        self.connection.register([User])
        self.connection.register([Channel])
        self.connection.register([Post])
        self.connection.register([Email])

    def email(self):
        print('Loading Email')
        self.Emailer = Emailer(self.connection)

    def controllers(self):
        print('Loading Controllers')
        self.UsersController = UsersController(self)
        self.ChannelsController = ChannelsController(self)
        self.PostsController = PostsController(self)
        self.SystemsController = SystemsController(self)

    #Server connection
    def server(self):
        print('Loading Server')
        self.srv = Server()
        self.srv.start(self)
コード例 #33
0
class Client(): 

	def __init__(self):
		self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		self.connection.connect(('localhost',2020))
		#result = self.connection.recv(1024).rstrip()

	def connect_db(self):
		self.db = Connection()
		self.db.register([User])
		self.db.register([Channel])
		self.db.register([Post])
		self.db.register([Email])
		return self.db

	def reply(self, type, data = None):
		reply = getattr(Replies, type)
		message = reply['message']
		if data:
			message = message % data
		return message

	def send(self, msg):
		self.connection.send(msg+"\r\n")
		result = self.connection.recv(1024).rstrip()
		return result

	def register_login(self, username = None, password = None):
		if username is None:
			username = random_word()

		if password is None:
			password = random_word()
			
		email = random_email()

		result = self.send(b'register %s %s %s' % (username, password, email))
		#result = self.send(b'login %s %s' % (username, password))
		return username

	def create_channel(self):
		self.register_login()
		channel = random_channel()
	 	result = self.send(b'create %s' % channel)
		return channel

	def tearDown(self):
		self.connection.close()
		self.connection = None
コード例 #34
0
ファイル: datum.py プロジェクト: Lobos/wordstore
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'
コード例 #35
0
ファイル: datum.py プロジェクト: Lobos/wordstore
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'
コード例 #36
0
ファイル: views.py プロジェクト: rohanbadlani/Django-projects
def details(request):
	if request.method == 'POST':
		if request.POST.get("computer_details"):
			print "You have successfully submitted the foem"
			conn = Connection()
			conn.register([Computer])
			database = conn.mydb
			collection = database.mycollection
			computer = collection.Computer()
			computer.make = unicode(request.POST['make'])
			computer.model = unicode(request.POST['model'])
			computer.purchase_date = unicode(request.POST['pur_date'])
			computer.cpu_ghz = unicode(request.POST['cpu_speed'])
			computer.save()
			print "Computer is:::" + str(computer.model)
			computer_list = collection.Computer.find()
			context = {'computer_list': computer_list}
			return render(request, 'computers/details.html', context)
			#return HttpResponse("Your response has been recorded")
		else:
			print "You havent clicked submit as yet"

	return HttpResponse("You response was not successfully recd")
コード例 #37
0
ファイル: views.py プロジェクト: rohanbadlani/Django-projects
def details(request):
    if request.method == 'POST':
        if request.POST.get("computer_details"):
            print "You have successfully submitted the foem"
            conn = Connection()
            conn.register([Computer])
            database = conn.mydb
            collection = database.mycollection
            computer = collection.Computer()
            computer.make = unicode(request.POST['make'])
            computer.model = unicode(request.POST['model'])
            computer.purchase_date = unicode(request.POST['pur_date'])
            computer.cpu_ghz = unicode(request.POST['cpu_speed'])
            computer.save()
            print "Computer is:::" + str(computer.model)
            computer_list = collection.Computer.find()
            context = {'computer_list': computer_list}
            return render(request, 'computers/details.html', context)
            #return HttpResponse("Your response has been recorded")
        else:
            print "You havent clicked submit as yet"

    return HttpResponse("You response was not successfully recd")
コード例 #38
0
ファイル: db.py プロジェクト: nathanjordan/ncr
class Controller(object):
    def __init__(self):
        self.conn = Connection()
        self.conn.register([User, Session, Neuron])
        # 12 hours?
        self.conn["ncr"]["session"].ensure_index("created", expireAfterSeconds=43200)

    def login(self, username, password):
        u = User.find({"username": username})
        hashed_pass = Crypt.hash_pw(password, u["salt"])
        if hashed_pass != u["password"]:
            return None
        ses = Session.find({"username": username})
        if ses:
            return ses["token"]
        token = Crypt.gen_token()
        created = datetime.datetime.now()
        ses = Session({"username": username, "token": token, "created": created})
        ses.save()
        return token

    def verify_token(self, token):
        ses = Session.find({"token": token})
        return True if ses else False
コード例 #39
0
ファイル: server.py プロジェクト: findhappyme/happycat
        "id": unicode,
        "start": float,
        "correct": int,
        "time": float,
        "round": int,
        "best": float,
        "last": float,
        "count": int,
    }
    indexes = [{"fields": "id", "unique": True}]
    default_values = {"time": float("Inf"), "best": 0, "last": 0, "round": 1, "count": 0}
    use_dot_notation = True


# Register the document with connection
connection.register([Player])
collection = connection["happycat"].players


@app.route("/")
def main_page():
    return render_template("index.html")


@app.route("/prizes")
def prizes():
    return render_template("prizes.html")


@app.route("/contact")
def contact():
コード例 #40
0
ファイル: __init__.py プロジェクト: h10gforks/lib_service
# -*- coding: utf-8 -*-
import os
from .mongodoc import Attention
from mongokit import Connection

# config
MONGODB_HOST = os.getenv("REST_MONGO_HOST")
MONGODB_PORT = int(os.getenv("REST_MONGO_PORT"))

connection = Connection(MONGODB_HOST, MONGODB_PORT)
connection.register([Attention])
コード例 #41
0
from apps.questions.models import Question
from mongokit import Connection
con = Connection()
con.register([Question])


collection = con.gkc[Question.__collection__]
print "Fixing", collection.Question.find({'difficulty':{'$exists': False}}).count(), "objects"
for each in collection.Question.find({'difficulty':{'$exists': False}}):
    each['difficulty'] = u"MEDIUM"
    each['language'] = u'en-gb'
    each.save()
コード例 #42
0
from models import UserSettings
from mongokit import Connection
con = Connection()
con.register([UserSettings])

collection = con.worklog.user_settings
print "Fixing", collection.UserSettings.find({
    'disable_sound': {
        '$exists': False
    }
}).count(), "objects"
for each in collection.UserSettings.find({'disable_sound': {
        '$exists': False
}}):
    each['disable_sound'] = False
    each.save()
コード例 #43
0
ファイル: test_index.py プロジェクト: zavatskiy/mongokit
class IndexTestCase(unittest.TestCase):
    def setUp(self):
        self.connection = Connection()
        self.col = self.connection['test']['mongokit']

    def tearDown(self):
        self.connection['test'].drop_collection('mongokit')
        self.connection = None

    def test_index_basic(self):
        class Movie(Document):
            structure = {
                'standard': unicode,
                'other': {
                    'deep': unicode,
                },
                'notindexed': unicode,
            }

            indexes = [
                {
                    'fields': ['standard', 'other.deep'],
                    'unique': True,
                },
            ]

        self.connection.register([Movie])
        movie = self.col.Movie()
        self.col.Movie.generate_index(self.col.Movie.collection)
        movie['standard'] = u'test'
        movie['other']['deep'] = u'testdeep'
        movie['notindexed'] = u'notthere'
        movie.save()

        db = self.connection.test
        item = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'standard_1_other.deep_1',
            'unique': True
        })
        assert item is not None, 'No Index Found'

        movie = self.col.Movie()
        movie['standard'] = u'test'
        movie['other']['deep'] = u'testdeep'
        self.assertRaises(OperationFailure, movie.save)

    def test_index_single_without_generation(self):
        class Movie(Document):
            structure = {
                'standard': unicode,
            }

            indexes = [
                {
                    'fields': 'standard',
                    'unique': True,
                },
            ]

        self.connection.register([Movie])
        movie = self.col.Movie()
        movie['standard'] = u'test'
        movie.save()

        db = self.connection.test
        item = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'standard_1',
            'unique': True,
            'key': {
                'standard': 1
            }
        })

        assert item is None, 'Index is found'

    def test_index_single(self):
        class Movie(Document):
            structure = {
                'standard': unicode,
            }

            indexes = [
                {
                    'fields': 'standard',
                    'unique': True,
                },
            ]

        self.connection.register([Movie])
        self.col.Movie.generate_index(self.col.Movie.collection)
        movie = self.col.Movie()
        movie['standard'] = u'test'
        movie.save()

        db = self.connection.test
        item = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'standard_1',
            'unique': True,
            'key': {
                'standard': 1
            }
        })

        assert item is not None, 'No Index Found'

    def test_index_multi(self):
        class Movie(Document):
            structure = {
                'standard': unicode,
                'other': {
                    'deep': unicode,
                },
                'notindexed': unicode,
                'alsoindexed': unicode,
            }

            indexes = [
                {
                    'fields': 'standard',
                    'unique': True,
                },
                {
                    'fields': ['alsoindexed', 'other.deep'],
                    'unique': True,
                },
            ]

        self.connection.register([Movie])
        self.col.Movie.generate_index(self.col.Movie.collection)
        movie = self.col.Movie()
        movie['standard'] = u'test'
        movie.save()

        db = self.connection.test
        item = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'standard_1',
            'unique': True,
            'key': {
                'standard': 1
            }
        })
        index2 = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'alsoindexed_1_other.deep_1',
            'unique': True
        })

        assert item is not None, 'No Index Found'
        assert index2 is not None, 'Index not found'

        movie = self.col.Movie()
        movie['standard'] = u'test'
        self.assertRaises(OperationFailure, movie.save)

    def test_index_multi2(self):
        class Movie(Document):
            structure = {
                'standard': unicode,
                'other': {
                    'deep': unicode,
                },
                'notindexed': unicode,
                'alsoindexed': unicode,
            }

            indexes = [
                {
                    'fields': 'standard',
                    'unique': True,
                },
                {
                    'fields': ['other.deep'],
                    'unique': True,
                },
            ]

        self.connection.register([Movie])
        self.col.Movie.generate_index(self.col.Movie.collection)
        movie = self.col.Movie()
        movie['standard'] = u'test'
        movie['other']['deep'] = u'foo'
        movie.save()

        db = self.connection.test
        item = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'standard_1',
            'unique': True,
            'key': {
                'standard': 1
            }
        })
        index2 = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'other.deep_1',
            'unique': True
        })

        assert item is not None, 'No Index Found'
        assert index2 is not None, 'Index not found'

        movie = self.col.Movie()
        movie['standard'] = u'test'
        self.assertRaises(OperationFailure, movie.save)

        movie = self.col.Movie()
        movie['other']['deep'] = u'foo'
        self.assertRaises(OperationFailure, movie.save)

    def test_index_direction(self):
        class Movie(Document):
            structure = {
                'standard': unicode,
                'other': {
                    'deep': unicode,
                },
                'notindexed': unicode,
                'alsoindexed': unicode,
            }

            indexes = [
                {
                    'fields': ('standard', INDEX_DESCENDING),
                    'unique': True,
                },
                {
                    'fields': [('alsoindexed', INDEX_ASCENDING),
                               ('other.deep', INDEX_DESCENDING)],
                    'unique':
                    True,
                },
            ]

        self.connection.register([Movie])
        self.col.Movie.generate_index(self.col.Movie.collection)
        movie = self.col.Movie()
        movie['standard'] = u'test'
        movie.save()

        db = self.connection.test
        index1 = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'standard_-1',
            'unique': True
        })
        index2 = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'alsoindexed_1_other.deep_-1',
            'unique': True
        })

        assert index1 is not None, 'No Index Found'
        assert index2 is not None, 'Index not found'

    def test_index_direction_GEO2D(self):
        class Movie(Document):
            structure = {
                'standard': unicode,
                'other': {
                    'deep': unicode,
                },
                'notindexed': unicode,
                'alsoindexed': unicode,
            }

            indexes = [
                {
                    'fields': ('standard', INDEX_GEO2D),
                    'unique': True,
                },
                {
                    'fields': [('alsoindexed', INDEX_GEO2D),
                               ('other.deep', INDEX_DESCENDING)],
                    'unique':
                    True,
                },
            ]

        self.connection.register([Movie])
        self.col.Movie.generate_index(self.col.Movie.collection)
        movie = self.col.Movie()
        movie['standard'] = u'test'
        movie.save()

        db = self.connection.test
        index1 = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'standard_2d',
            'unique': True
        })
        index2 = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'alsoindexed_2d_other.deep_-1',
            'unique': True
        })

        assert index1 is not None, 'No Index Found'
        assert index2 is not None, 'Index not found'

    def test_bad_index_descriptor(self):
        failed = False
        try:

            class Movie(Document):
                structure = {'standard': unicode}
                indexes = [{'unique': True}]
        except BadIndexError, e:
            self.assertEqual(str(e), "'fields' key must be specify in indexes")
            failed = True
        self.assertEqual(failed, True)

        failed = False
        try:

            class Movie(Document):
                structure = {
                    'standard': unicode,
                }
                indexes = [
                    {
                        'fields': ('standard', INDEX_DESCENDING),
                        'uniq': True,
                    },
                ]
        except BadIndexError, e:
            self.assertEqual(str(e), "uniq is unknown key for indexes")
            failed = True
コード例 #44
0
ファイル: indexes.py プロジェクト: peterbe/tornado_gists
from pymongo import ASCENDING, DESCENDING
from models import Vote, GistPoints, UserPoints
from mongokit import Connection
import settings
con = Connection()
con.register([Vote, GistPoints, UserPoints])
db = con[settings.DEFAULT_DATABASE_NAME]


def run():
    collection = db.Vote.collection
    collection.ensure_index('user.$id')
    yield 'user.$id'
    collection.ensure_index('gist.$id')
    yield 'user.$id'

    collection = db.GistPoints.collection
    collection.ensure_index('gist.$id')
    yield 'user.$id'
    collection.ensure_index([('points', DESCENDING)])
    yield 'points'

    collection = db.UserPoints.collection
    collection.ensure_index('user.$id')
    yield 'user.$id'
    collection.ensure_index([('points', DESCENDING)])
    yield 'points'

    test()

コード例 #45
0
ファイル: app.py プロジェクト: rodneyslafuente/squeak
        'created_at': datetime.datetime,
        'phone_number': basestring,
        'address': basestring,
        'categories': [basestring]
    }

    default_values = {'created_at': datetime.datetime.utcnow}

    def id(self):
        return self._id

    def __repr__(self):
        return '<Entry %s>' % self['name']


connection.register([Entry])
collection = connection['squeak'].entries


@app.route('/')
def index():
    entries = list(collection.Entry.find())
    return render_template('index.html', saved_entries=entries)


@app.route('/save', methods=['POST'])
def save_entry():
    new_entry = collection.Entry()
    new_entry.name = request.form['name']
    new_entry.url = request.form['url']
    new_entry.phone_number = request.form['phone_number']
コード例 #46
0
from apps.main.models import FeatureRequest
from mongokit import Connection
con = Connection()
con.register([FeatureRequest])

db = con.worklog
print "Fixing", db.FeatureRequest.find({
    'implemented': {
        '$exists': False
    }
}).count(), "objects"
for each in db.FeatureRequest.find({'implemented': {'$exists': False}}):
    each['implemented'] = False
    each.save()
コード例 #47
0
ファイル: mongo.py プロジェクト: angstwad/textmeplz
def get_mongoconn():
    global _mongo_conn
    if _mongo_conn is None:
        _mongo_conn = Connection(config.MONGO_URI, **config.MONGO_KWARGS)
        _mongo_conn.register([User, Log])
    return _mongo_conn
コード例 #48
0
ファイル: indexes.py プロジェクト: peterbe/tornado_gists
from pymongo import ASCENDING, DESCENDING
from models import Gist, Comment
from mongokit import Connection
import settings

con = Connection()
con.register([Gist, Comment])
db = con[settings.DEFAULT_DATABASE_NAME]


def run():
    collection = db.Gist.collection
    collection.ensure_index([('add_date', DESCENDING)])
    yield 'add_date'
    collection.ensure_index('gist_id')
    yield 'gist_id'
    collection.ensure_index('tags')
    yield 'tags'
    collection.ensure_index('user.$id')  # default ttl=300
    yield 'user.$id'

    collection = db.Comment.collection
    collection.ensure_index('user.$id')
    yield 'user.$id'
    collection.ensure_index('gist.$id')
    yield 'gist.$id'
    collection.ensure_index([('add_date', DESCENDING)])
    yield 'add_date'

    test()
コード例 #49
0
ファイル: test_types.py プロジェクト: WuXianglong/mongokit
class TypesTestCase(unittest.TestCase):
    def setUp(self):
        self.connection = Connection()
        self.col = self.connection['test']['mongokit']

    def tearDown(self):
        self.connection.drop_database('test')

    def test_authorized_type(self):
        for auth_type in SchemaDocument.authorized_types:
            if auth_type is dict:
                auth_type = {}

            class MyDoc(SchemaDocument):
                structure = {"foo": auth_type}

            if type(auth_type) is dict:
                assert MyDoc() == {"foo": {}}, MyDoc()
            elif auth_type is list:
                assert MyDoc() == {"foo": []}
            else:
                assert MyDoc() == {"foo": None}, auth_type

    def test_not_authorized_type(self):
        for unauth_type in [set]:
            failed = False
            try:

                class MyDoc(SchemaDocument):
                    structure = {"foo": [unauth_type]}
            except StructureError as e:
                self.assertEqual(
                    str(e),
                    "MyDoc: %s is not an authorized type" % unauth_type)
                failed = True
            self.assertEqual(failed, True)
            failed = False
            try:

                class MyDoc(SchemaDocument):
                    structure = {"foo": (unauth_type)}
            except StructureError as e:
                self.assertEqual(
                    str(e),
                    "MyDoc: %s is not an authorized type" % unauth_type)
                failed = True
            self.assertEqual(failed, True)
            failed = False
            try:

                class MyDoc2(SchemaDocument):
                    structure = {'foo': [{int: unauth_type}]}
            except StructureError as e:
                self.assertEqual(
                    str(e),
                    "MyDoc2: %s is not an authorized type" % unauth_type)
                failed = True
            self.assertEqual(failed, True)
            failed = False
            try:

                class MyDoc3(SchemaDocument):
                    structure = {'foo': [{unauth_type: int}]}
            except AuthorizedTypeError as e:
                self.assertEqual(
                    str(e),
                    "MyDoc3: %s is not an authorized type" % unauth_type)
                failed = True
            self.assertEqual(failed, True)

        failed = False
        try:

            class MyDoc4(SchemaDocument):
                structure = {1: six.text_type}
        except StructureError as e:
            self.assertEqual(str(e), "MyDoc4: 1 must be a string or a type")
            failed = True
        self.assertEqual(failed, True)

    def test_type_from_functions(self):
        from datetime import datetime

        class MyDoc(SchemaDocument):
            structure = {
                "foo": datetime,
            }

        assert MyDoc() == {"foo": None}, MyDoc()
        mydoc = MyDoc()
        mydoc['foo'] = datetime.now()
        mydoc.validate()

    def test_non_typed_list(self):
        class MyDoc(SchemaDocument):
            structure = {"foo": []}

        mydoc = MyDoc()
        mydoc.validate()
        assert mydoc['foo'] == []
        mydoc['foo'] = [u"bla", 23]
        mydoc.validate()
        mydoc['foo'] = [set([1, 2]), "bla"]
        self.assertRaises(AuthorizedTypeError, mydoc.validate)
        mydoc['foo'] = u"bla"
        self.assertRaises(SchemaTypeError, mydoc.validate)

#        class MyDoc(SchemaDocument):
#            structure = {
#                "foo":list
#            }
#        mydoc = MyDoc()
#        mydoc.validate()
#        assert mydoc['foo'] == []
#        mydoc['foo'] = [u"bla", 23]
#        mydoc.validate()
#        mydoc['foo'] = [set([1,2]), "bla"]
#        self.assertRaises(AuthorizedTypeError, mydoc.validate)

    def test_typed_list(self):
        class MyDoc(SchemaDocument):
            structure = {"foo": [int]}

        mydoc = MyDoc()
        mydoc.validate()
        assert mydoc['foo'] == []
        mydoc['foo'] = [1, 2, 3]
        mydoc.validate()
        mydoc['foo'] = [u"bla"]
        self.assertRaises(SchemaTypeError, mydoc.validate)

    def test_typed_list_with_dict(self):
        class MyDoc(SchemaDocument):
            structure = {"foo": [{six.text_type: int}]}

        mydoc = MyDoc()
        mydoc['foo'] = [{u"bla": 1}, {u"ble": 2}]
        mydoc.validate()
        mydoc['foo'] = [{u"bla": u"bar"}]
        self.assertRaises(SchemaTypeError, mydoc.validate)

    def test_typed_list_with_list(self):
        class MyDoc(SchemaDocument):
            structure = {"foo": [[six.text_type]]}

        mydoc = MyDoc()
        mydoc['foo'] = [[u"bla", u"blu"], [u"ble", u"bli"]]
        mydoc.validate()
        mydoc['foo'] = [[u"bla", 1]]
        self.assertRaises(SchemaTypeError, mydoc.validate)

    def test_typed_tuple(self):
        class MyDoc(SchemaDocument):
            structure = {"foo": (int, six.text_type, float)}

        mydoc = MyDoc()
        mydoc.validate()
        assert mydoc['foo'] == [None, None, None]
        mydoc['foo'] = [u"bla", 1, 4.0]
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['foo'] = [1, u"bla"]
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['foo'] = u"bla"
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['foo'] = [1, u'bar', 3.2]
        mydoc.validate()
        mydoc['foo'] = [None, u"bla", 3.1]
        mydoc.validate()
        mydoc['foo'][0] = 50
        mydoc.validate()

    def test_nested_typed_tuple(self):
        class MyDoc(SchemaDocument):
            structure = {"foo": {'bar': (int, six.text_type, float)}}

        mydoc = MyDoc()
        mydoc.validate()
        assert mydoc['foo']['bar'] == [None, None, None]
        mydoc['foo']['bar'] = [u"bla", 1, 4.0]
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['foo']['bar'] = [1, u"bla"]
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['foo']['bar'] = [1, u'bar', 3.2]
        mydoc.validate()
        mydoc['foo']['bar'] = [None, u"bla", 3.1]
        mydoc.validate()
        mydoc['foo']['bar'][0] = 50
        mydoc.validate()

    def test_saving_tuple(self):
        class MyDoc(Document):
            structure = {'foo': (int, six.text_type, float)}

        self.connection.register([MyDoc])

        mydoc = self.col.MyDoc()
        assert mydoc == {'foo': [None, None, None]}, mydoc
        mydoc['foo'] = (1, u'a', 1.1
                        )  # note that this will be converted to list
        assert mydoc == {'foo': (1, u'a', 1.1000000000000001)}, mydoc
        mydoc.save()
        mydoc = self.col.find_one()

        class MyDoc(Document):
            structure = {'foo': [six.text_type]}

        self.connection.register([])
        self.connection.register([MyDoc])
        mydoc = self.col.MyDoc()
        mydoc['foo'] = (u'bla', u'bli', u'blu', u'bly')
        mydoc.save()
        mydoc = self.col.get_from_id(mydoc['_id'])

    def test_nested_typed_tuple_in_list(self):
        class MyDoc(SchemaDocument):
            structure = {"foo": {'bar': [(int, six.text_type, float)]}}

        mydoc = MyDoc()
        mydoc.validate()
        assert mydoc == {'foo': {'bar': []}}
        mydoc['foo']['bar'].append([u"bla", 1, 4.0])
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['foo']['bar'] = []
        mydoc['foo']['bar'].append([1, u"bla"])
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['foo']['bar'] = []
        mydoc['foo']['bar'].append([1, u'bar', 3.2])
        mydoc.validate()
        mydoc['foo']['bar'].append([None, u"bla", 3.1])
        mydoc.validate()
        mydoc['foo']['bar'][1][0] = 50
        mydoc.validate()

    def test_dict_unicode_typed_list(self):
        class MyDoc(SchemaDocument):
            structure = {"foo": {six.text_type: [int]}}

        mydoc = MyDoc()
        mydoc['foo'] = {u"bar": [1, 2, 3]}
        mydoc.validate()
        mydoc['foo'] = {u"bar": [u"bla"]}
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['foo'] = {3: [1, 2, 3]}
        self.assertRaises(SchemaTypeError, mydoc.validate)

    def test_with_custom_object(self):
        class MyDict(dict):
            pass

        class MyDoc(SchemaDocument):
            structure = {"foo": {six.text_type: int}}

        mydoc = MyDoc()
        mydict = MyDict()
        mydict[u"foo"] = 3
        mydoc["foo"] = mydict
        mydoc.validate()

    def test_custom_object_as_type(self):
        class MyDict(dict):
            pass

        class MyDoc(SchemaDocument):
            structure = {"foo": MyDict({six.text_type: int})}

        mydoc = MyDoc()
        mydict = MyDict()
        mydict[u"foo"] = 3
        mydoc["foo"] = mydict
        mydoc.validate()
        mydoc['foo'] = {u"foo": "7"}
        self.assertRaises(SchemaTypeError, mydoc.validate)

        class MyInt(int):
            pass

        class MyDoc(SchemaDocument):
            structure = {
                "foo": MyInt,
            }

        mydoc = MyDoc()
        mydoc["foo"] = MyInt(3)
        mydoc.validate()
        mydoc['foo'] = 3
        self.assertRaises(SchemaTypeError, mydoc.validate)

    def test_list_instead_of_dict(self):
        class MyDoc(SchemaDocument):
            structure = {"foo": {six.text_type: [six.text_type]}}

        mydoc = MyDoc()
        mydoc['foo'] = [u'bla']
        self.assertRaises(SchemaTypeError, mydoc.validate)

    def _test_big_nested_example(self):
        # XXX TODO
        class MyDoc(SchemaDocument):
            structure = {
                "foo": {
                    six.text_type: [int],
                    u"bar": {
                        "spam": {
                            int: [six.text_type]
                        }
                    }
                },
                "bla": {
                    "blo": {
                        "bli": [{
                            "arf": six.text_type
                        }]
                    }
                },
            }

        mydoc = MyDoc()
        mydoc['foo'].update({u"bir": [1, 2, 3]})
        mydoc['foo'][u'bar'][u'spam'] = {
            1: [u'bla', u'ble'],
            3: [u'foo', u'bar']
        }
        mydoc.validate()
        mydoc['bla']['blo']['bli'] = [{u"bar": [u"bla"]}]
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['bla']['blo']['bli'] = [{u"arf": [1]}]
        self.assertRaises(SchemaTypeError, mydoc.validate)

    def test_adding_custom_type(self):
        class MyDoc(SchemaDocument):
            structure = {
                "foo": str,
            }
            authorized_types = SchemaDocument.authorized_types + [str]

        mydoc = MyDoc()

    def test_schema_operator(self):
        from mongokit.operators import SchemaOperator

        class OP(SchemaOperator):
            repr = "op"

        op = OP()
        self.assertRaises(NotImplementedError, op.validate, "bla")

    def test_or_operator(self):
        from mongokit import OR
        assert repr(OR(int, str)) == "<int or str>"

        failed = False
        try:

            class BadMyDoc(SchemaDocument):
                structure = {"bla": OR(int, tuple)}
        except StructureError as e:
            self.assertEqual(
                str(e),
                "BadMyDoc: <%s 'tuple'> in <int or tuple> is not an authorized type (type found)"
                % ('type' if six.PY2 else 'class'))
            failed = True
        self.assertEqual(failed, True)

        from datetime import datetime
        if six.PY2:
            string_type = basestring
        else:
            string_type = str

        class MyDoc(SchemaDocument):
            structure = {
                "foo": OR(six.text_type, int),
                "bar": OR(six.text_type, datetime),
                "foobar": OR(string_type, int),
            }

        mydoc = MyDoc()
        assert str(
            mydoc.structure['foo']) == '<%s or int>' % six.text_type.__name__
        assert str(mydoc.structure['bar']
                   ) == '<%s or datetime>' % six.text_type.__name__
        assert str(
            mydoc.structure['foobar']) == '<%s or int>' % string_type.__name__
        assert mydoc == {'foo': None, 'bar': None, 'foobar': None}
        mydoc['foo'] = 3.0
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['foo'] = six.u("foo")
        mydoc.validate()
        mydoc['foo'] = 3
        mydoc.validate()
        mydoc['foo'] = 'bar'
        if six.PY2:
            self.assertRaises(SchemaTypeError, mydoc.validate)
        else:
            mydoc.validate()
        mydoc['foo'] = datetime.now()
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['foo'] = six.u("foo")
        mydoc['bar'] = datetime.now()
        mydoc.validate()
        mydoc['bar'] = six.u("today")
        mydoc.validate()
        mydoc['bar'] = 25
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['bar'] = six.u("bar")
        mydoc["foo"] = six.u("foo")
        mydoc["foobar"] = "foobar"
        mydoc.validate()
        mydoc["foobar"] = datetime.now()
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc["foobar"] = 3
        mydoc.validate()

    def test_not_operator(self):
        from mongokit import NOT
        failed = False
        try:

            class BadMyDoc(SchemaDocument):
                structure = {"bla": NOT(int, tuple)}
        except StructureError as e:
            self.assertEqual(
                str(e),
                "BadMyDoc: <%s 'tuple'> in <not int, not tuple> is not an authorized type (type found)"
                % ('type' if six.PY2 else 'class'))
            failed = True
        self.assertEqual(failed, True)

        from datetime import datetime
        if six.PY2:
            string_type = basestring
        else:
            string_type = str

        class MyDoc(SchemaDocument):
            structure = {
                "foo": NOT(six.text_type, int),
                "bar": NOT(datetime),
                "foobar": NOT(string_type)
            }

        mydoc = MyDoc()
        assert str(mydoc.structure['foo']
                   ) == '<not %s, not int>' % six.text_type.__name__, str(
                       mydoc.structure['foo'])
        assert str(mydoc.structure['bar']) == '<not datetime>'
        assert str(
            mydoc.structure['foobar']) == '<not %s>' % string_type.__name__
        assert mydoc == {'foo': None, 'bar': None, 'foobar': None}
        assert mydoc['foo'] is None
        assert mydoc['bar'] is None
        assert mydoc['foobar'] is None
        mydoc['foo'] = 3
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['foo'] = u"foo"
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['foo'] = 3.0
        mydoc.validate()
        mydoc['foo'] = datetime.now()
        mydoc.validate()

        mydoc['bar'] = datetime.now()
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['bar'] = u"today"
        mydoc.validate()
        mydoc['bar'] = 25
        mydoc.validate()
        mydoc['foobar'] = 'abc'
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['foobar'] = 1
        mydoc.validate()

    def test_is_operator(self):
        from mongokit import IS
        failed = False
        try:

            class BadMyDoc(SchemaDocument):
                structure = {"bla": IS(('bla', ), 3)}
        except StructureError as e:
            self.assertEqual(
                str(e),
                "BadMyDoc: ('bla',) in <is ('bla',) or is 3> is not an authorized type (tuple found)"
            )
            failed = True
        self.assertEqual(failed, True)

        from datetime import datetime

        class MyDoc(SchemaDocument):
            structure = {"foo": IS(u'spam', u'eggs'), "bar": IS(u'3', 3)}

        mydoc = MyDoc()
        if six.PY2:
            assert str(mydoc.structure['foo']) == "<is u'spam' or is u'eggs'>"
            assert str(mydoc.structure['bar']) == "<is u'3' or is 3>"
        else:
            assert str(mydoc.structure['foo']) == "<is 'spam' or is 'eggs'>"
            assert str(mydoc.structure['bar']) == "<is '3' or is 3>"
        assert mydoc == {'foo': None, 'bar': None}
        assert mydoc['foo'] is None
        assert mydoc['bar'] is None
        mydoc['foo'] = 3
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['foo'] = u"bla"
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['foo'] = datetime.now()
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['foo'] = u"spam"
        mydoc.validate()
        mydoc['foo'] = u"eggs"
        mydoc.validate()

        mydoc['bar'] = datetime.now()
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['bar'] = u"today"
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['bar'] = 'foo'
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['bar'] = 3
        mydoc.validate()
        mydoc['bar'] = u"3"
        mydoc.validate()

    def test_subclassed_type(self):
        """
        accept all subclass of supported type
        """
        class CustomFloat(float):
            def __init__(self, float):
                self = float + 2

        class MyDoc(SchemaDocument):
            structure = {
                "foo": float,
            }

        mydoc = MyDoc()
        mydoc['foo'] = CustomFloat(4)
        mydoc.validate()

    def test_set_type(self):
        from mongokit import Set

        class MyDoc(Document):
            structure = {
                "tags": Set(int),
            }

        self.connection.register([MyDoc])
        mydoc = self.col.MyDoc()
        mydoc['tags'] = set(["1", "1", "2", "3", "4"])
        self.assertRaises(ValueError, mydoc.validate)
        mydoc['tags'] = set([1, 1, 2, 3, 4])
        mydoc.save()

        doc = self.col.MyDoc.find_one()
        assert doc['tags'] == set([1, 2, 3, 4]), doc['tags']

    def test_set_type2(self):
        class MyDoc(Document):
            structure = {
                'title': six.text_type,
                'category': Set(six.text_type)
            }
            required_fields = ['title']

        self.connection.register([MyDoc])
        doc = self.col.MyDoc()
        print(doc)  # {'category': set([]), 'title': None}
        assert isinstance(doc['category'], set)
        try:
            doc.validate()
        except RequireFieldError as e:
            print(e)  # title is required

        print(doc)  # {'category': [], 'title': None}
        assert isinstance(doc['category'], set)
        doc['title'] = u'hello'
        doc.validate()

    def test_int_type(self):
        @self.connection.register
        class MyDoc(Document):
            structure = {
                "foo": int,
            }

        mydoc = self.col.MyDoc()
        mydoc['foo'] = ''
        self.assertRaises(SchemaTypeError, mydoc.validate)
        mydoc['foo'] = 10
        mydoc.save()

    def test_uuid_type(self):
        import uuid

        @self.connection.register
        class MyDoc(Document):
            structure = {
                'uuid': uuid.UUID,
            }

        uid = uuid.uuid4()
        obj = self.col.MyDoc()
        obj['uuid'] = uid
        obj.save()

        assert isinstance(self.col.MyDoc.find_one()['uuid'], uuid.UUID)

    if six.PY2:

        def test_binary_with_str_type(self):
            import bson
            six.text_type

            @self.connection.register
            class MyDoc(Document):
                structure = {
                    'my_binary': basestring,
                }

            obj = self.col.MyDoc()
            # non-utf8 string
            non_utf8 = "\xFF\xFE\xFF"
            obj['my_binary'] = non_utf8

            self.assertRaises(bson.errors.InvalidStringData, obj.validate)

        def test_binary_with_unicode_type(self):
            import bson

            @self.connection.register
            class MyDoc(Document):
                structure = {
                    'my_binary': unicode,
                }

            obj = self.col.MyDoc()
            # non-utf8 string
            non_utf8 = "\xFF\xFE\xFF"
            obj['my_binary'] = non_utf8

            self.assertRaises(bson.errors.InvalidStringData, obj.validate)

    def test_binary_with_binary_type(self):
        import bson

        @self.connection.register
        class MyDoc(Document):
            structure = {
                'my_binary': bson.binary.Binary,
            }

        obj = self.col.MyDoc()
        # non-utf8 string
        string = "\xFF\xFE\xFF"

        if six.PY3:
            bin_obj = bson.binary.Binary(bytes(string, 'utf-8'))
        else:
            bin_obj = bson.binary.Binary(string)

        obj['my_binary'] = bin_obj
        obj.save()
        db_obj = bson.binary.Binary(self.col.MyDoc.find_one()['my_binary'])
        self.assertEquals(db_obj, bin_obj)
コード例 #50
0
from apps.main.models import FeatureRequest
from mongokit import Connection
con = Connection()
con.register([FeatureRequest])


db = con.worklog
print "Fixing", db.FeatureRequest.find({'implemented':{'$exists': False}}).count(), "objects"
for each in db.FeatureRequest.find({'implemented':{'$exists': False}}):
    each['implemented'] = False
    each.save()
コード例 #51
0
ファイル: app.py プロジェクト: amitdash/worklog
class Application(tornado.web.Application):
    def __init__(self,
                 database_name=None,
                 xsrf_cookies=True,
                 optimize_static_content=None):
        ui_modules_map = {}
        for app_name in settings.APPS:
            # XXX consider replacing this with use of tornado.util.import_object
            _ui_modules = __import__('apps.%s' % app_name, globals(), locals(),
                                     ['ui_modules'], -1)
            try:
                ui_modules = _ui_modules.ui_modules
            except AttributeError:
                # this app simply doesn't have a ui_modules.py file
                continue

            for name in [x for x in dir(ui_modules) if re.findall('[A-Z]\w+', x)]:
                thing = getattr(ui_modules, name)
                try:
                    if issubclass(thing, tornado.web.UIModule):
                        ui_modules_map[name] = thing
                except TypeError:
                    # most likely a builtin class or something
                    pass

        if options.dont_combine:
            ui_modules_map['Static'] = ui_modules_map['PlainStatic']
            ui_modules_map['StaticURL'] = ui_modules_map['PlainStaticURL']

        try:
            cdn_prefix = [x.strip() for x in file('cdn_prefix.conf')
                             if x.strip() and not x.strip().startswith('#')][0]
            #logging.info("Using %r as static URL prefix" % cdn_prefix)
        except (IOError, IndexError):
            cdn_prefix = None

        # unless explicitly set, then if in debug mode, disable optimization
        # of static content
        if optimize_static_content is None:
            optimize_static_content = not options.debug

        handlers = route.get_routes()
        app_settings = dict(
            title=settings.TITLE,
            template_path=os.path.join(os.path.dirname(__file__), "apps", "templates"),
            static_path=os.path.join(os.path.dirname(__file__), "static"),
            embed_static_url_timestamp=True and not options.dont_embed_static_url,
            ui_modules=ui_modules_map,
            xsrf_cookies=xsrf_cookies,
            cookie_secret=settings.COOKIE_SECRET,
            login_url=settings.LOGIN_URL,
            debug=options.debug,
            optimize_static_content=optimize_static_content,
            git_revision=get_git_revision(),
            email_backend=options.debug and \
                 'utils.send_mail.backends.console.EmailBackend' \
              or 'utils.send_mail.backends.smtp.EmailBackend',
            webmaster=settings.WEBMASTER,
            admin_emails=settings.ADMIN_EMAILS,
            CLOSURE_LOCATION=os.path.join(os.path.dirname(__file__),
                                      "static", "compiler.jar"),
            YUI_LOCATION=os.path.join(os.path.dirname(__file__),
                                      "static", "yuicompressor-2.4.2.jar"),
            UNDOER_GUID=u'UNDOER', # must be a unicode string
            cdn_prefix=cdn_prefix,
        )
        tornado.web.Application.__init__(self, handlers, **app_settings)

        # Have one global connection to the blog DB across all handlers
        self.database_name = database_name and database_name or options.database_name
        self.con = Connection()
        self.redis = redis.client.Redis(settings.REDIS_HOST,
                                        settings.REDIS_PORT)

        model_classes = []
        for app_name in settings.APPS:
            _models = __import__('apps.%s' % app_name, globals(), locals(),
                                     ['models'], -1)
            try:
                models = _models.models
            except AttributeError:
                # this app simply doesn't have a models.py file
                continue
            for name in [x for x in dir(models) if re.findall('[A-Z]\w+', x)]:
                thing = getattr(models, name)
                if issubclass(thing, mongokit_Document):
                    model_classes.append(thing)

        self.con.register(model_classes)
コード例 #52
0
ファイル: datum.py プロジェクト: Lobos/wordstore
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)
コード例 #53
0
ファイル: application.py プロジェクト: novking/Flask_AWS

def max_length(length):
    def validate(value):
        if len(value) <= length:
            return True
        raise ValidationError(
            '%s must be at most {} characters long'.format(length))

    return validate


class User(Document):
    structure = {
        'name': unicode,
        'email': unicode,
    }
    validators = {'name': max_length(50), 'email': max_length(120)}
    use_dot_notation = True

    def __repr__(self):
        return '<User %r>' % (self.name)


if __name__ == "__main__":
    # Setting debug to True enables debug output. This line should be
    # removed before deploying a production app.
    application.debug = True
    application.run()
    connection.register([User])
コード例 #54
0
class IndexTestCase(unittest.TestCase):
    def setUp(self):
        self.connection = Connection()
        self.col = self.connection['test']['mongokit']

    def tearDown(self):
        self.connection['test'].drop_collection('mongokit')
        self.connection = None

    def test_index_basic(self):
        class Movie(Document):
            structure = {
                'standard': six.text_type,
                'other': {
                    'deep': six.text_type,
                },
                'notindexed': six.text_type,
            }

            indexes = [
                {
                    'fields': ['standard', 'other.deep'],
                    'unique': True,
                },
            ]

        self.connection.register([Movie])
        movie = self.col.Movie()
        self.col.Movie.generate_index(self.col.Movie.collection)
        movie['standard'] = u'test'
        movie['other']['deep'] = u'testdeep'
        movie['notindexed'] = u'notthere'
        movie.save()

        db = self.connection.test
        item = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'standard_1_other.deep_1',
            'unique': True
        })
        assert item is not None, 'No Index Found'

        movie = self.col.Movie()
        movie['standard'] = u'test'
        movie['other']['deep'] = u'testdeep'
        self.assertRaises(OperationFailure, movie.save)

    def test_index_single_without_generation(self):
        class Movie(Document):
            structure = {
                'standard': six.text_type,
            }

            indexes = [
                {
                    'fields': 'standard',
                    'unique': True,
                },
            ]

        self.connection.register([Movie])
        movie = self.col.Movie()
        movie['standard'] = u'test'
        movie.save()

        db = self.connection.test
        item = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'standard_1',
            'unique': True,
            'key': {
                'standard': 1
            }
        })

        assert item is None, 'Index is found'

    def test_index_single(self):
        class Movie(Document):
            structure = {
                'standard': six.text_type,
            }

            indexes = [
                {
                    'fields': 'standard',
                    'unique': True,
                },
            ]

        self.connection.register([Movie])
        self.col.Movie.generate_index(self.col.Movie.collection)
        movie = self.col.Movie()
        movie['standard'] = u'test'
        movie.save()

        db = self.connection.test
        item = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'standard_1',
            'unique': True,
            'key': {
                'standard': 1
            }
        })

        assert item is not None, 'No Index Found'

    def test_index_multi(self):
        class Movie(Document):
            structure = {
                'standard': six.text_type,
                'other': {
                    'deep': six.text_type,
                },
                'notindexed': six.text_type,
                'alsoindexed': six.text_type,
            }

            indexes = [
                {
                    'fields': 'standard',
                    'unique': True,
                },
                {
                    'fields': ['alsoindexed', 'other.deep'],
                    'unique': True,
                },
            ]

        self.connection.register([Movie])
        self.col.Movie.generate_index(self.col.Movie.collection)
        movie = self.col.Movie()
        movie['standard'] = u'test'
        movie.save()

        db = self.connection.test
        item = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'standard_1',
            'unique': True,
            'key': {
                'standard': 1
            }
        })
        index2 = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'alsoindexed_1_other.deep_1',
            'unique': True
        })

        assert item is not None, 'No Index Found'
        assert index2 is not None, 'Index not found'

        movie = self.col.Movie()
        movie['standard'] = u'test'
        self.assertRaises(OperationFailure, movie.save)

    def test_index_multi2(self):
        class Movie(Document):
            structure = {
                'standard': six.text_type,
                'other': {
                    'deep': six.text_type,
                },
                'notindexed': six.text_type,
                'alsoindexed': six.text_type,
            }

            indexes = [
                {
                    'fields': 'standard',
                    'unique': True,
                },
                {
                    'fields': ['other.deep'],
                    'unique': True,
                },
            ]

        self.connection.register([Movie])
        self.col.Movie.generate_index(self.col.Movie.collection)
        movie = self.col.Movie()
        movie['standard'] = u'test'
        movie['other']['deep'] = u'foo'
        movie.save()

        db = self.connection.test
        item = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'standard_1',
            'unique': True,
            'key': {
                'standard': 1
            }
        })
        index2 = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'other.deep_1',
            'unique': True
        })

        assert item is not None, 'No Index Found'
        assert index2 is not None, 'Index not found'

        movie = self.col.Movie()
        movie['standard'] = u'test'
        self.assertRaises(OperationFailure, movie.save)

        movie = self.col.Movie()
        movie['other']['deep'] = u'foo'
        self.assertRaises(OperationFailure, movie.save)

    def test_index_direction(self):
        class Movie(Document):
            structure = {
                'standard': six.text_type,
                'other': {
                    'deep': six.text_type,
                },
                'notindexed': six.text_type,
                'alsoindexed': six.text_type,
            }

            indexes = [
                {
                    'fields': ('standard', INDEX_DESCENDING),
                    'unique': True,
                },
                {
                    'fields': [('alsoindexed', INDEX_ASCENDING),
                               ('other.deep', INDEX_DESCENDING)],
                    'unique':
                    True,
                },
            ]

        self.connection.register([Movie])
        self.col.Movie.generate_index(self.col.Movie.collection)
        movie = self.col.Movie()
        movie['standard'] = u'test'
        movie.save()

        db = self.connection.test
        index1 = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'standard_-1',
            'unique': True
        })
        index2 = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'alsoindexed_1_other.deep_-1',
            'unique': True
        })

        assert index1 is not None, 'No Index Found'
        assert index2 is not None, 'Index not found'

    def test_index_direction_GEO2D(self):
        class Movie(Document):
            structure = {
                'standard': six.text_type,
                'other': {
                    'deep': six.text_type,
                },
                'notindexed': six.text_type,
                'alsoindexed': six.text_type,
            }

            indexes = [
                {
                    'fields': ('standard', INDEX_GEO2D),
                    'unique': True,
                },
                {
                    'fields': [('alsoindexed', INDEX_GEO2D),
                               ('other.deep', INDEX_DESCENDING)],
                    'unique':
                    True,
                },
            ]

        self.connection.register([Movie])
        self.col.Movie.generate_index(self.col.Movie.collection)
        movie = self.col.Movie()
        movie['standard'] = u'test'
        movie.save()

        db = self.connection.test
        index1 = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'standard_2d',
            'unique': True
        })
        index2 = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'alsoindexed_2d_other.deep_-1',
            'unique': True
        })

        assert index1 is not None, 'No Index Found'
        assert index2 is not None, 'Index not found'

    def test_bad_index_descriptor(self):
        failed = False
        try:

            class Movie(Document):
                structure = {'standard': six.text_type}
                indexes = [{'unique': True}]
        except BadIndexError as e:
            self.assertEqual(str(e), "'fields' key must be specify in indexes")
            failed = True
        self.assertEqual(failed, True)

        failed = False
        try:

            class Movie(Document):
                structure = {
                    'standard': six.text_type,
                }
                indexes = [
                    {
                        'fields': ('standard', INDEX_DESCENDING),
                        'uniq': True,
                    },
                ]
        except BadIndexError as e:
            self.assertEqual(str(e), "uniq is unknown key for indexes")
            failed = True
        #self.assertEqual(failed, True)

        failed = False
        try:

            class Movie(Document):
                structure = {
                    'standard': six.text_type,
                }
                indexes = [
                    {
                        'fields': 'std',
                    },
                ]
        except ValueError as e:
            self.assertEqual(str(e),
                             "Error in indexes: can't find std in structure")
            failed = True
        self.assertEqual(failed, True)

        failed = False
        try:

            class Movie(Document):
                structure = {
                    'standard': six.text_type,
                }
                indexes = [
                    {
                        'fields': {
                            'standard': 1
                        },
                    },
                ]
        except BadIndexError as e:
            self.assertEqual(
                str(e),
                "fields must be a string, a tuple or a list of tuple (got <%s 'dict'> instead)"
                % ('type' if six.PY2 else 'class'))
            failed = True
        self.assertEqual(failed, True)

        failed = False
        try:

            class Movie(Document):
                structure = {
                    'standard': six.text_type,
                }
                indexes = [
                    {
                        'fields': ('standard', 1, "blah"),
                    },
                ]
        except BadIndexError as e:
            self.assertEqual(
                str(e),
                "Error in indexes: a tuple must contain only two value : the field name and the direction"
            )
            failed = True
        self.assertEqual(failed, True)

        failed = False
        try:

            class Movie(Document):
                structure = {
                    'standard': six.text_type,
                }
                indexes = [
                    {
                        'fields': ('standard', "2"),
                    },
                ]
        except BadIndexError as e:
            self.assertEqual(
                str(e),
                "index direction must be INDEX_DESCENDING, INDEX_ASCENDING, INDEX_OFF, INDEX_ALL, INDEX_GEO2D, INDEX_GEOHAYSTACK, or INDEX_GEOSPHERE. Got 2"
            )
            failed = True
        self.assertEqual(failed, True)

        failed = False
        try:

            class Movie(Document):
                structure = {
                    'standard': six.text_type,
                }
                indexes = [
                    {
                        'fields': (3, 1),
                    },
                ]
        except BadIndexError as e:
            self.assertEqual(
                str(e),
                "Error in 3, the field name must be string (got <%s 'int'> instead)"
                % ('type' if six.PY2 else 'class'))
            failed = True
        self.assertEqual(failed, True)

        failed = False
        try:

            class Movie(Document):
                structure = {
                    'standard': six.text_type,
                }
                indexes = [
                    {
                        'fields': ("blah", 1),
                    },
                ]
        except ValueError as e:
            self.assertEqual(str(e),
                             "Error in indexes: can't find blah in structure")
            failed = True
        self.assertEqual(failed, True)

        failed = False
        try:

            class Movie(Document):
                structure = {
                    'standard': six.text_type,
                }
                indexes = [
                    {
                        'fields': [('standard', 1), ('bla', 1)],
                    },
                ]
        except ValueError as e:
            self.assertEqual(str(e),
                             "Error in indexes: can't find bla in structure")
            failed = True
        self.assertEqual(failed, True)

        failed = False
        try:

            class Movie(Document):
                structure = {
                    'standard': six.text_type,
                }
                indexes = [
                    {
                        'fields': [('standard', 3)],
                    },
                ]
        except BadIndexError as e:
            self.assertEqual(
                str(e),
                "index direction must be INDEX_DESCENDING, INDEX_ASCENDING, INDEX_OFF, INDEX_ALL, INDEX_GEO2D, INDEX_GEOHAYSTACK, or INDEX_GEOSPHERE. Got 3"
            )
            failed = True
        self.assertEqual(failed, True)

        failed = False
        try:

            class Movie(Document):
                structure = {
                    'standard': six.text_type,
                }
                indexes = [
                    {
                        'fields': ['std'],
                    },
                ]
        except ValueError as e:
            self.assertEqual(str(e),
                             "Error in indexes: can't find std in structure")
            failed = True
        self.assertEqual(failed, True)

    def test_index_ttl(self):
        class Movie(Document):
            structure = {
                'standard': six.text_type,
            }

            indexes = [
                {
                    'fields': 'standard',
                    'unique': True,
                    'ttl': 86400
                },
                # If indexes are still broken validation will choke on the ttl
            ]

        self.connection.register([Movie])
        self.col.Movie.generate_index(self.col)
        movie = self.col.Movie()
        movie['standard'] = u'test'
        movie.save()

        db = self.connection.test
        item = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'standard_1',
            'unique': True,
            'key': {
                'standard': 1
            }
        })

        assert item is not None, 'No Index Found'

    def test_index_simple_inheritance(self):
        class DocA(Document):
            structure = {
                'standard': six.text_type,
            }

            indexes = [
                {
                    'fields': 'standard',
                    'unique': True,
                },
            ]

        class DocB(DocA):
            structure = {
                'docb': six.text_type,
            }

        self.connection.register([DocA, DocB])
        self.col.DocB.generate_index(self.col)
        docb = self.col.DocB()
        docb['standard'] = u'test'
        docb['docb'] = u'foo'
        docb.save()

        db = self.connection.test
        item = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'standard_1',
            'unique': True,
            'key': {
                'standard': 1
            }
        })

        assert item is not None, 'No Index Found'

    def test_index_inheritance(self):
        class DocA(Document):
            structure = {
                'standard': six.text_type,
            }

            indexes = [
                {
                    'fields': 'standard',
                    'unique': True,
                },
            ]

        class DocB(DocA):
            structure = {
                'docb': six.text_type,
            }
            indexes = [
                {
                    'fields': 'docb',
                    'unique': True,
                },
            ]

        self.connection.register([DocA, DocB])
        self.col.DocB.generate_index(self.col.DocB.collection)

        docb = self.col.DocB()
        docb['standard'] = u'test'
        docb['docb'] = u'foo'
        docb.save()

        db = self.connection.test
        item = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'standard_1',
            'unique': True,
            'key': {
                'standard': 1
            }
        })
        item = db['system.indexes'].find_one({
            'ns': 'test.mongokit',
            'name': 'docb_1',
            'unique': True,
            'key': {
                'docb': 1
            }
        })

        assert item is not None, 'No Index Found'

    def test_index_real_world(self):
        import datetime

        class MyDoc(Document):
            structure = {
                "mydoc": {
                    "creation_date": datetime.datetime,
                }
            }
            indexes = [{'fields': [('mydoc.creation_date', -1), ('_id', 1)]}]

        self.connection.register([MyDoc])

        date = datetime.datetime.utcnow()

        mydoc = self.col.MyDoc()
        mydoc['mydoc']['creation_date'] = date
        mydoc['_id'] = u'aaa'
        mydoc.save()

        mydoc3 = self.col.MyDoc()
        mydoc3['mydoc']['creation_date'] = date
        mydoc3['_id'] = u'bbb'
        mydoc3.save()

        import time
        time.sleep(1)
        date2 = datetime.datetime.utcnow()

        mydoc2 = self.col.MyDoc()
        mydoc2['mydoc']['creation_date'] = date2
        mydoc2['_id'] = u'aa'
        mydoc2.save()

        time.sleep(1)
        date3 = datetime.datetime.utcnow()

        mydoc4 = self.col.MyDoc()
        mydoc4['mydoc']['creation_date'] = date3
        mydoc4['_id'] = u'ccc'
        mydoc4.save()

        self.col.ensure_index([('mydoc.creation_date', -1), ('_id', 1)])
        results = [
            i['_id']
            for i in self.col.MyDoc.fetch().sort([('mydoc.creation_date',
                                                   -1), ('_id', 1)])
        ]
        assert results == ['ccc', 'aa', 'aaa', 'bbb'], results

    def test_index_pymongo(self):
        import datetime
        date = datetime.datetime.utcnow()
        import pymongo
        collection = pymongo.Connection()['test']['test_index']

        mydoc = {'mydoc': {'creation_date': date}, '_id': u'aaa'}
        collection.insert(mydoc)

        mydoc2 = {'mydoc': {'creation_date': date}, '_id': u'bbb'}
        collection.insert(mydoc2)

        import time
        time.sleep(1)
        date2 = datetime.datetime.utcnow()

        mydoc3 = {'mydoc': {'creation_date': date2}, '_id': u'aa'}
        collection.insert(mydoc3)

        time.sleep(1)
        date3 = datetime.datetime.utcnow()

        mydoc4 = {'mydoc': {'creation_date': date3}, '_id': u'ccc'}
        collection.insert(mydoc4)

        collection.ensure_index([('mydoc.creation_date', -1), ('_id', 1)])
        #print list(collection.database.system.indexes.find())

        results = [
            i['_id'] for i in collection.find().sort([('mydoc.creation_date',
                                                       -1), ('_id', 1)])
        ]
        print(results)
        assert results == [u'ccc', u'aa', u'aaa', u'bbb'], results

    def test_index_inheritance2(self):
        class A(Document):
            structure = {
                'a': {
                    'title': six.text_type,
                }
            }
            indexes = [{'fields': 'a.title'}]

        class B(A):
            structure = {
                'b': {
                    'title': six.text_type,
                }
            }
            indexes = [{'fields': 'b.title'}]

        class C(Document):
            structure = {
                'c': {
                    'title': six.text_type,
                }
            }
            indexes = [{'fields': 'c.title'}]

        class D(B, C):
            structure = {
                'd': {
                    'title': six.text_type,
                }
            }

        self.connection.register([D])
        doc = self.col.D()
        assert doc.indexes == [{
            'fields': 'b.title'
        }, {
            'fields': 'a.title'
        }, {
            'fields': 'c.title'
        }]

    def test_index_with_default_direction(self):
        class MyDoc(Document):
            structure = {'foo': six.text_type, 'bar': int}
            indexes = [
                {
                    'fields': ['foo', ('bar', -1)]
                },
            ]

        self.connection.register([MyDoc])
        self.col.MyDoc.generate_index(self.col)
        for i in range(10):
            doc = self.col.MyDoc()
            doc['foo'] = six.text_type(i)
            doc['bar'] = i
            doc.save()
        assert self.col.database.system.indexes.find_one(
            {'name': 'foo_1_bar_-1'})

    def test_index_with_check(self):
        @self.connection.register
        class MyDoc(Document):
            structure = {'foo': dict, 'bar': int}
            indexes = [
                {
                    'fields': ['foo.title'],
                    'check': False
                },
            ]

        self.col.MyDoc.generate_index(self.col)
        for i in range(10):
            doc = self.col.MyDoc()
            doc['foo']['title'] = six.text_type(i)
            doc['bar'] = i
            doc.save()
        assert self.col.database.system.indexes.find_one(
            {'name': 'foo.title_1'})

    def test_index_with_check_is_true(self):
        @self.connection.register
        class MyDoc(Document):
            structure = {'foo': six.text_type, 'bar': int}
            indexes = [
                {
                    'fields': ['foo'],
                    'check': True
                },
            ]

        self.col.MyDoc.generate_index(self.col)
        for i in range(10):
            doc = self.col.MyDoc()
            doc['foo'] = six.text_type(i)
            doc['bar'] = i
            doc.save()
        assert self.col.database.system.indexes.find_one({'name': 'foo_1'})

    def test_index_with_additional_keywords(self):
        @self.connection.register
        class KWDoc(Document):
            structure = {
                'foo': six.text_type,
            }
            indexes = [{
                'fields': ["foo"],
                'dropDups': True,
                'name': 'additional_kws',
            }]

        self.col.KWDoc.generate_index(self.col)
        index = self.col.database.system.indexes.find_one(
            {'name': 'additional_kws'})
        assert index["name"] == u'additional_kws'
        assert index["dropDups"] is True
コード例 #55
0
ファイル: __init__.py プロジェクト: restccnu/restccnu
    :MAINTAINER: neo1218
    :OWNER: muxistudio
"""

import os
from .mongodoc import User, Dormitory, Table, Attention, Week, Feedback
from mongokit import Connection

# config
MONGODB_HOST = os.getenv("REST_MONGO_HOST")
MONGODB_PORT = int(os.getenv("REST_MONGO_PORT"))

#  使用mongodb进行课表数据存储
connection = Connection(MONGODB_HOST, MONGODB_PORT)
connection.register([User])
connection.register([Dormitory])
connection.register([Table])
connection.register([Attention])
connection.register([Week])
connection.register([Feedback])  # [ios]用户反馈

# _zero: 占位课程, id=0
## mongodb😓 的特性, 只有数据写入的时候创建数据库
_zero = {
    "id": "0",
    "course": "re:从零开始的异世界生活",
    "teacher": "neo1218",
    "weeks": "1",
    "day": "2",
    "start": "3",
コード例 #56
0
    con = Connection()

    import settings
    model_classes = []
    for app_name in settings.APPS:
        _models = __import__('apps.%s' % app_name, globals(), locals(),
                             ['models'], -1)
        try:
            models = _models.models
        except AttributeError:
            # this app simply doesn't have a models.py file
            continue
        for name in [x for x in dir(models) if re.findall('[A-Z]\w+', x)]:
            thing = getattr(models, name)
            try:
                if issubclass(thing, mongokit_Document):
                    model_classes.append(thing)
            except TypeError:
                pass

    con.register(model_classes)

    db = con.gkc
    print "AVAILABLE:"
    print '\n'.join([
        '\t%s' % x for x in locals().keys()
        if re.findall('[A-Z]\w+|db|con', x)
    ])
    print "Database available as 'db'"
    code.interact(local=locals())