示例#1
0
class TestCaseContextIndependent(unittest.TestCase):
    def setUp(self):
        self.app = create_app()
        self.db = MongoKit(self.app)

    def tearDown(self):
        pass
    
    def test_register_document(self):
        self.db.register([BlogPost])
        
        assert len(self.db.registered_documents) > 0
        assert self.db.registered_documents[0] == BlogPost
    
    def test_bson_object_id_converter(self):
        converter = BSONObjectIdConverter("/")
    
        self.assertRaises(BadRequest, converter.to_python, ("132"))
        assert converter.to_python("4e4ac5cfffc84958fa1f45fb") == \
               ObjectId("4e4ac5cfffc84958fa1f45fb")
        assert converter.to_url(ObjectId("4e4ac5cfffc84958fa1f45fb")) == \
               "4e4ac5cfffc84958fa1f45fb"

    def test_is_extension_registerd(self):
        assert hasattr(self.app, 'extensions')
        assert 'mongokit' in self.app.extensions
        assert self.app.extensions['mongokit'] == self.db
示例#2
0
class TestCaseContextIndependent(unittest.TestCase):
    def setUp(self):
        self.app = create_app()
        self.db = MongoKit(self.app)

    def tearDown(self):
        pass

    def test_register_document(self):
        self.db.register([BlogPost])

        assert len(self.db.registered_documents) > 0
        assert self.db.registered_documents[0] == BlogPost

    def test_bson_object_id_converter(self):
        converter = BSONObjectIdConverter("/")

        self.assertRaises(BadRequest, converter.to_python, ("132"))
        assert converter.to_python("4e4ac5cfffc84958fa1f45fb") == \
               ObjectId("4e4ac5cfffc84958fa1f45fb")
        assert converter.to_url(ObjectId("4e4ac5cfffc84958fa1f45fb")) == \
               "4e4ac5cfffc84958fa1f45fb"

    def test_is_extension_registerd(self):
        assert hasattr(self.app, 'extensions')
        assert 'mongokit' in self.app.extensions
        assert self.app.extensions['mongokit'] == self.db
示例#3
0
    def test_init_later(self):
        self.db = MongoKit()
        self.assertRaises(RuntimeError, self.db.connect)

        self.db.init_app(self.app)
        self.db.connect()
        assert self.db.connected
示例#4
0
    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)
示例#5
0
    def setUp(self):
        self.app_1 = create_app()
        self.app_1.config['MONGODB_DATABASE'] = 'app_1'

        self.app_2 = create_app()
        self.app_2.config['MONGODB_DATABASE'] = 'app_2'

        assert self.app_1 != self.app_2

        self.db = MongoKit()
        self.db.init_app(self.app_1)
        self.db.init_app(self.app_2)
示例#6
0
    def test_init_later(self):
        self.db = MongoKit()
        self.assertRaises(RuntimeError, self.db.connect)

        self.db.init_app(self.app)
        self.db.connect()
        assert self.db.connected
示例#7
0
 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)
示例#8
0
 def setUp(self):
     self.app_1 = create_app()
     self.app_1.config['MONGODB_DATABASE'] = 'app_1'
     
     self.app_2 = create_app()
     self.app_2.config['MONGODB_DATABASE'] = 'app_2'
     
     assert self.app_1 != self.app_2
     
     self.db = MongoKit()
     self.db.init_app(self.app_1)
     self.db.init_app(self.app_2)
示例#9
0
class BaseTestCaseWithAuth():
    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 test_correct_login(self):
        self.app.config['MONGODB_USERNAME'] = '******'
        self.app.config['MONGODB_PASSWORD'] = '******'
        
        self.db.connect()
    
    def test_incorrect_login(self):
        self.app.config['MONGODB_USERNAME'] = '******'
        self.app.config['MONGODB_PASSWORD'] = '******'
        
        self.assertRaises(AuthenticationIncorrect, self.db.connect)
示例#10
0
class BaseTestCaseWithAuth():
    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 test_correct_login(self):
        self.app.config['MONGODB_USERNAME'] = '******'
        self.app.config['MONGODB_PASSWORD'] = '******'

        self.db.connect()

    def test_incorrect_login(self):
        self.app.config['MONGODB_USERNAME'] = '******'
        self.app.config['MONGODB_PASSWORD'] = '******'

        self.assertRaises(AuthenticationIncorrect, self.db.connect)
示例#11
0
class BaseTestCaseInitAppWithContext():
    def setUp(self):
        self.app = create_app()

    def test_init_later(self):
        self.db = MongoKit()
        self.assertRaises(RuntimeError, self.db.connect)

        self.db.init_app(self.app)
        self.db.connect()
        assert self.db.connected

    def test_init_immediately(self):
        self.db = MongoKit(self.app)
        self.db.connect()
        assert self.db.connected
示例#12
0
class BaseTestCaseInitAppWithContext():
    def setUp(self):
        self.app = create_app()

    def test_init_later(self):
        self.db = MongoKit()
        self.assertRaises(RuntimeError, self.db.connect)

        self.db.init_app(self.app)
        self.db.connect()
        assert self.db.connected

    def test_init_immediately(self):
        self.db = MongoKit(self.app)
        self.db.connect()
        assert self.db.connected
示例#13
0
class BaseTestCaseMultipleApps():

    def setUp(self):
        self.app_1 = create_app()
        self.app_1.config['MONGODB_DATABASE'] = 'app_1'
        
        self.app_2 = create_app()
        self.app_2.config['MONGODB_DATABASE'] = 'app_2'
        
        assert self.app_1 != self.app_2
        
        self.db = MongoKit()
        self.db.init_app(self.app_1)
        self.db.init_app(self.app_2)

    def tearDown(self):
        self.pop_ctx()

    def push_ctx(self):
        raise NotImplementedError
    
    def pop_ctx(self):
        raise NotImplementedError

    def test_app_1(self):
        self.push_ctx(self.app_1)
        
        self.db.connect()
        assert self.db.connected
        assert self.db.name == 'app_1'
        assert self.db.name != 'app_2'
        
    def test_app_2(self):
        self.push_ctx(self.app_2)
        
        self.db.connect()
        assert self.db.connected
        assert self.db.name != 'app_1'
        assert self.db.name == 'app_2'
示例#14
0
class BaseTestCaseMultipleApps():
    def setUp(self):
        self.app_1 = create_app()
        self.app_1.config['MONGODB_DATABASE'] = 'app_1'

        self.app_2 = create_app()
        self.app_2.config['MONGODB_DATABASE'] = 'app_2'

        assert self.app_1 != self.app_2

        self.db = MongoKit()
        self.db.init_app(self.app_1)
        self.db.init_app(self.app_2)

    def tearDown(self):
        self.pop_ctx()

    def push_ctx(self):
        raise NotImplementedError

    def pop_ctx(self):
        raise NotImplementedError

    def test_app_1(self):
        self.push_ctx(self.app_1)

        self.db.connect()
        assert self.db.connected
        assert self.db.name == 'app_1'
        assert self.db.name != 'app_2'

    def test_app_2(self):
        self.push_ctx(self.app_2)

        self.db.connect()
        assert self.db.connected
        assert self.db.name != 'app_1'
        assert self.db.name == 'app_2'
示例#15
0
        def setUp(self):
            self.app = create_app()
            self.db = MongoKit(self.app)

            self.ctx = self.app.app_context()
            self.ctx.push()
示例#16
0
    def setUp(self):
        self.app = create_app()
        self.db = MongoKit(self.app)

        self.ctx = self.app.test_request_context('/')
        self.ctx.push()
示例#17
0
import re 
from helpers import * 

# MongoDB imports 
from flask_mongokit import MongoKit, Document


# User class (will not be placed here permanently)
class User(Document):
	# Collection name
	__collection__ = 'users'

	# Structure
	structure = {
		'name' : unicode, 
		'email' : unicode, 
		'image_url' : unicode, 
		'likes' : list,
		'dislikes' : list,
		'created_at': datetime,
		'updated_at': datetime
	}

	required_fields = ['name', 'email']
	default_values = { 'created_at': datetime.utcnow, 'updated_at': datetime.utcnow }
	use_dot_notation = True


# Init db + register documents
db = MongoKit(app)
db.register([User])
示例#18
0
def str2bool(v):
    return v.lower() in ("yes", "true", "t", "1")
#
app = Flask(__name__)

app.config['MONGODB_HOST'] = os.environ.get('MONGODB_HOST','194.177.192.227')
app.config['MONGODB_USERNAME'] = os.environ.get('MONGODB_USERNAME',None)
app.config['MONGODB_PASSWORD'] = os.environ.get('MONGODB_PASSWORD',None)
app.config['MONGODB_PORT'] = int(os.environ.get('MONGODB_PORT','27017'))
app.config['MONGODB_DATABASE'] = os.environ.get('MONGODB_DATABASE','faqs')
# app.config['SERVER_NAME'] = os.environ.get('SERVER_NAME','localhost:5000')
app_port = int(os.environ.get('APP_PORT','5000'))
app.config['DEBUG'] = str2bool(os.environ.get('DEBUG','True'))
CORS(app)
mongo = MongoKit(app)

mongo.register([Topic,Question,Page,PageHelpContent])

#===========
#  TOPICS
#===========

def delete_topic_questions(topic):
    # pprint(topic)
    questions = mongo.Question.find({"topics" : topic._id})

    for question in questions:
        question.topics.remove(topic._id)
        if len(question.topics)==0 :
            question.delete()
示例#19
0
 def test_init_immediately(self):
     self.db = MongoKit(self.app)
     self.db.connect()
     assert self.db.connected
示例#20
0
 def setUp(self):
     self.app = create_app()
     self.db = MongoKit(self.app)
示例#21
0
文件: app.py 项目: dwright213/site
# mongo stuff
class Blag(Document):
    __collection__ = 'blags'

    structure = {
        'title': unicode,
        'text': unicode,
        'creation': datetime,
    }
    required_fields = ['title', 'creation']
    default_values = {'creation': datetime.utcnow}
    use_dot_notation = True


db = MongoKit(app)
db.register([Blag])
# end mongo stuff


@app.before_request
def set_up_nav():
    g.nav = ['home', 'infos', 'fotos', 'etc']


def tumblr_keys():
    g.client = pytumblr.TumblrRestClient(app.config['CONSUMER_KEY'],
                                         app.config['CONSUMER_SECRET'],
                                         app.config['OAUTH_TOKEN'],
                                         app.config['OAUTH_SECRET'])
示例#22
0
 def setUp(self):
     self.app = create_app()
     self.db = MongoKit(self.app)
示例#23
0
    }
    required_fields = ['name', 'college_id', 'email']
    default_values = {'time': datetime.now()}
    use_dot_notation = True

class Attendance(Document):
    __collection__ = 'pypals'
    structure = {
        'college_id': unicode,
        'talks_attended': list
    }
    required_fields = ['college_id', 'talks_attended']
    default_values = {'talks_attended': []}
    use_dot_notation = True

conn = MongoKit(app)
conn.register(User)
conn.register(Attendance)


@app.route("/")
def main():
    return redirect(u'/\u03BCpy')

@app.route("/repo")
def repo():
    return redirect('https://github.com/PyPals')

@app.route(u'/\u03BCpy')
def mu_py():
    return render_template('index.html', title="MUPy 2018", subtitle="MUPy")
示例#24
0
 def test_init_immediately(self):
     self.db = MongoKit(self.app)
     self.db.connect()
     assert self.db.connected
示例#25
0
#!/usr/bin/env python
#-*- coding: utf-8 -*-
__author__ = 'cybersg'

from flask import Flask, g
from flask_mongokit import MongoKit

app = Flask(__name__, template_folder='static')
app.config.from_object('scrumteam.conf.Config')

import models
db = MongoKit(app)
app.config['DB'] = db
db.register(models.documents())

def get_db():
    global db
    cn = getattr(g, 'db', None)
    if cn is None:
        cn = db
    return cn


import views



示例#26
0
redis_connection = redis.Redis(connection_pool=redis_pool)
strict_redis = redis.StrictRedis(connection_pool=redis_pool)

store = RedisStore(strict_redis)

KVSessionExtension(store, app)

from rq import Queue

queue = Queue('default', connection=redis_connection)

import sys

from flask_mongokit import MongoKit

db = MongoKit(app)

# Mailer
from flask_mail import Mail

mail = Mail(app)

# Login manager for frontend
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login"

# User Auth DB file - create if not existing
if not os.path.exists(app.config.get('USER_DB_FILE')):
    from glider_util.bdb import UserDB
    UserDB.init_db(app.config.get('USER_DB_FILE'))
示例#27
0
    __collection__ = 'tuits'
    structure = {
        'type': str,
        'features': [EmbedDoc],
    }
    required_fields = ['type']
    default_values = {'type': 'FeatureCollection'}


arr = list()
a = list()
x = list()
w = {}

app = Flask(__name__)
db = MongoKit(app)

db.register([EmbedDoc, MyDocument])
lock = threading.Lock()


@app.route('/')
def index():
    return render_template('flot.html')


@app.route('/mongo', methods=["POST"])
def conecta_mongo():
    app.config['MONGODB_DATABASE'] = 'Twitter_Xml'
    app.config['MONGODB_HOST'] = 'localhost'
示例#28
0
文件: blog.py 项目: adilansari/blogit
    __collection__ = 'posts'
    structure = {
        'title': basestring,
        'body': basestring,
        'date_creation': datetime,
        'rank': int,
    }

    required_fields = ['title', 'date_creation']
    default_values = {
        'rank': 0,
        'date_creation': datetime.utcnow
    }
    use_dot_notation = True

db = MongoKit(app)
db.register([BlogPost])

def get_rank():
    u = int(random.random()*100000)
    return u

@app.route('/')
def home():
    posts = db.posts.find()
    return render_template('home.html', posts=posts)

@app.route('/add', methods=['GET','POST'])
def add():
    if request.method == 'POST':
        post = db.BlogPost()
示例#29
0
from flask import Flask
from flask_cors import Cors
from flask_mongokit import MongoKit

from home.mongo_rest.home.documents import Home

app = Flask(__name__)

app.config['SECRET_KEY'] = 'SECRET_KEY'
app.config['MONGODB_DATABASE'] = "/test"

CORS(app, resources={r"/api/*": {"origins": "*"}})
db = MongoKit(app)

db.register([Home])

from home.mongo_rest.home.api import home_api

app.register_blueprint(home_api, url_prefix="/api/v1")