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()
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
def __init__(self): #mongodb_uri = "mongodb://13.76.244.22:27017" #mongolab_uri = "mongodb://*****:*****@ds062818.mlab.com:62818/MongoLab-25" connectionString = 'insert mongodb uri here' #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([Project]) self.con.register([Files]) self.con.register([Job]) self.con.register([User]) self.con.register([Admin]) self.con.register([Log]) self.con.register([Server]) self.projects = self.con.Project self.files = self.con.Files self.jobs = self.con.Job self.users = self.con.User self.admins = self.con.Admin self.logs = self.con.Log self.server = self.con.Server
def test_create_test_database_by_specific_good_name(self): from django.conf import settings try: assert 'mongodb' in settings.DATABASES except AttributeError: # Django <1.2 return settings.DATABASES['mongodb']['TEST_NAME'] = "test_mustard" old_database_name = settings.DATABASES['mongodb']['NAME'] from django.db import connections connection = connections['mongodb'] connection.creation.create_test_db() test_database_name = settings.DATABASES['mongodb']['NAME'] self.assertTrue('test_' in test_database_name) from mongokit import Connection con = Connection() # the test database isn't created till it's needed self.assertTrue(test_database_name not in con.database_names()) # creates it db = con[settings.DATABASES['mongodb']['NAME']] coll = db.test_collection_name # do a query on the collection to force the database to be created list(coll.find()) test_database_name = settings.DATABASES['mongodb']['NAME'] self.assertTrue(test_database_name in con.database_names()) connection.creation.destroy_test_db(old_database_name) self.assertTrue('test_mustard' not in settings.DATABASES['mongodb']['NAME']) self.assertTrue(test_database_name not in con.database_names())
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 get_connection(): Item.__database__ = settings.MONGO_DB connection = Connection(host=settings.MONGO_HOST) connection.register([Item]) return connection
def init(): global connection logger.info('connecting to database') #conn_args = getattr(config,'conn_args',{}) connection = Connection() logger.info('database connected ') connection.register([User])
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
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 setUp(self): app.config['TESTING'] = True self.raw_app = app self.app = app.test_client() self.db = Connection( app.config['MONGODB_HOST'], app.config['MONGODB_PORT'])[app.config['MONGODB_DATABASE']]
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
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)
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 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
def connect(self, uri): """Connect to given uri :param uri: The URI to connect to, such as mongodb://LOGIN:PASSWORD@SERVER:PORT/DB_NAME """ logging.info('Connecting to uri %s', uri) self.connection = Connection(host=uri) self.connection.register([User, Play]) return self.connection
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()
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, str]: failed = False try: class MyDoc(SchemaDocument): structure = {"foo": [unauth_type]} except StructureError, 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, 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, e: self.assertEqual( str(e), "MyDoc2: %s is not an authorized type" % unauth_type) failed = True
class FlaskMongoTestCase(unittest.TestCase): def setUp(self): app.config['TESTING'] = True self.raw_app = app self.app = app.test_client() self.db = Connection(app.config['MONGODB_HOST'], app.config['MONGODB_PORT'])[app.config['MONGODB_DATABASE']] def tearDown(self): self.db.drop_collection("runs")
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()
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]
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
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()
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
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
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
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
class FlaskMongoTestCase(unittest.TestCase): def setUp(self): app.config['TESTING'] = True self.app = app.test_client() self.db = Connection(app.config['MONGODB_HOST'], app.config['MONGODB_PORT'])[app.config['MONGODB_DATABASE']] with self.app.session_transaction() as sess: sess['user_email'] = u'*****@*****.**' def tearDown(self): self.db.drop_collection("capability") self.db.drop_collection("diel") self.db.drop_collection("taxis") self.db.drop_collection("lifestage") self.db.drop_collection("libraries") self.db.drop_collection("users")
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()
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, 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, 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, e: self.assertEqual(str(e), "MyDoc2: %s is not an authorized type" % unauth_type) failed = True
def test_can_find_user_with_objectid(app, ACCOUNTS): ''' There's a fun an exciting bug where a ton of our users have ended up with ObjectIds instead of straight string IDs. This checks for backwards compatibility. ''' from mongokit import Document, Connection from hummedia import config from hummedia.models import User from bson.objectid import ObjectId connection = Connection(host=config.MONGODB_HOST, port=config.MONGODB_PORT) user = connection[User.__database__][User.__collection__] _id = ObjectId() pid = str(_id) a = {'_id': _id, 'pid': str(pid)} a.update(ACCOUNTS['STUDENT']) user.insert(a) app.login(ACCOUNTS['SUPERUSER']) patch = {"username": a['username'],"superuser": a['superuser'],"firstname":"George","preferredLanguage":"en","lastname":"Norris","userid":"555555560","role": a['role'],"oauth":{"twitter":{},"google":{"access_token":[],"id":None,"email":None},"facebook":{}},"fullname":"George Norris","_id":str(pid),"email":"","isSaving":True} r = app.patch('/account/' + pid, data=json.dumps(patch), headers={'Content-Type': 'application/json'}) print r.data assert r.status_code is 200
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])
def connect(self): """Connect to the MongoDB server and register the documents from :attr:`registered_documents`. If you set ``MONGODB_USERNAME`` and ``MONGODB_PASSWORD`` then you will be authenticated at the ``MONGODB_DATABASE``. """ if self.app is None: raise RuntimeError('The flask-mongokit extension was not init to ' 'the current application. Please make sure ' 'to call init_app() first.') ctx = ctx_stack.top mongokit_connection = getattr(ctx, 'mongokit_connection', None) if mongokit_connection is None: ctx.mongokit_connection = Connection( host=ctx.app.config.get('MONGODB_HOST'), port=ctx.app.config.get('MONGODB_PORT'), slave_okay=ctx.app.config.get('MONGODB_SLAVE_OKAY')) ctx.mongokit_connection.register(self.registered_documents) mongokit_database = getattr(ctx, 'mongokit_database', None) if mongokit_database is None: ctx.mongokit_database = Database( ctx.mongokit_connection, ctx.app.config.get('MONGODB_DATABASE')) if ctx.app.config.get('MONGODB_USERNAME') is not None: auth_success = ctx.mongokit_database.authenticate( ctx.app.config.get('MONGODB_USERNAME'), ctx.app.config.get('MONGODB_PASSWORD')) if not auth_success: raise AuthenticationIncorrect
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()
def get_src_db(conn=None): uri = "mongodb://{}:{}@{}:{}/{}".format(DATA_SERVER_USERNAME, DATA_SERVER_PASSWORD, DATA_SRC_SERVER, DATA_SRC_PORT, DATA_SRC_DATABASE) conn = Connection(uri) return conn[DATA_SRC_DATABASE]
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
def connect(self): mongo_connection = Connection(self._server["hostname"], self._server["port"], replicaset=self._server["replicaset"]) self._select_db(self._server['database']) self._authenticate(mongo_connection) return mongo_connection
def database(self): print('Loading database') self.connection = Connection() self.connection.register([World]) self.connection.register([Island]) self.connection.register([Event]) self.connection.register([Player])
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()
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()
def init_app(self, app): """This method connect your ``app`` with this extension. Flask- MongoKit will now take care about to open and close the connection to your MongoDB. Also it registers the :class:`flask.ext.mongokit.BSONObjectIdConverter` as a converter with the key word **ObjectId**. :param app: The Flask application will be bound to this MongoKit instance. """ app.config.setdefault('MONGODB_HOST', '127.0.0.1') app.config.setdefault('MONGODB_PORT', 27017) app.config.setdefault('MONGODB_DATABASE', 'flask') app.config.setdefault('MONGODB_USERNAME', None) app.config.setdefault('MONGODB_PASSWORD', None) app.config.setdefault('MONGODB_URL', 'mongodb://127.0.0.1:27071/') app.config.setdefault('MONGODB_CONNECTION_OPTIONS', { 'auto_start_request': False }) app.before_first_request(self._before_first_request) # 0.9 and later # no coverage check because there is everytime only one if hasattr(app, 'teardown_appcontext'): # pragma: no cover app.teardown_appcontext(self._teardown_request) # 0.7 to 0.8 elif hasattr(app, 'teardown_request'): # pragma: no cover app.teardown_request(self._teardown_request) # Older Flask versions else: # pragma: no cover app.after_request(self._teardown_request) # register extension with app only to say "I'm here" app.extensions = getattr(app, 'extensions', {}) app.extensions['mongokit'] = self app.url_map.converters['ObjectId'] = BSONObjectIdConverter self.app = app self.mongokit_connection = Connection( host=app.config.get('MONGODB_HOST'), port=app.config.get('MONGODB_PORT'), **app.config.get('MONGODB_CONNECTION_OPTIONS', {}) ) self.mongokit_database = Database(self.mongokit_connection, app.config.get('MONGODB_DATABASE')) if app.config.get('MONGODB_USERNAME') is not None: auth_success = self.mongokit_database.authenticate( app.config.get('MONGODB_USERNAME'), app.config.get('MONGODB_PASSWORD') ) if not auth_success: raise AuthenticationIncorrect('Server authentication failed')
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
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
def get_connection(): """Get connection handler""" models = [] connection = getattr(g, 'db_connection', None) if connection is None: g.db_connection = Connection(app.config['MONGODB_HOST'], app.config['MONGODB_PORT']) g.db_connection.register(models) return g.db_connection return connection
def setUp(self): db = 'flask_testing_auth' conn = Connection() conn[db].add_user('test', 'test') self.app = create_app() self.app.config['TESTING'] = True self.app.config['MONGODB_DATABASE'] = db self.db = MongoKit(self.app)
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
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()
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)
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)
def start_instance(settings): settings.connection = Connection() settings.connection.register(register_models) app = tornado.web.Application(routes, **settings) http_server = tornado.httpserver.HTTPServer(app) http_server.listen(settings.instance_port, address='127.0.0.1') try: tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: pass
def MongoDB_Connection(app): global connection if not 'config' in dir(app): connection = False if 'MONGODB_URL' in app.config: connection = MongoClient(app.config['MONGODB_URL']) elif 'MONGODB_HOST' and 'MONGODB_PORT' in app.config: connection = Connection(app.config['MONGODB_HOST'], app.config['MONGODB_PORT']) else: connection = False return connection
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