def test_should_be_able_to_create_two_decoupled_mongoalchemy_instances(
         self):
     app = Flask(__name__)
     app.config['MONGOALCHEMY_DATABASE'] = 'my_database'
     db1 = MongoAlchemy(app)
     db2 = MongoAlchemy(app)
     assert db1.Document is not db2.Document, "two documents should not be the same object"
    def test_should_contain_a_not_none_query(self):
        "Document.query should never be None"
        db = MongoAlchemy()
        db.init_app(self.app)

        class Person(db.Document):
            name = db.StringField()

        p = Person()
        assert p.query is not None
    def test_should_contain_a_not_none_query(self):
        "Document.query should never be None"
        db = MongoAlchemy()
        db.init_app(self.app)

        class Person(db.Document):
            name = db.StringField()

        p = Person()
        assert p.query is not None
    def test_should_provide_a_query_object_for_queries_on_a_document(self):
        db = MongoAlchemy(self.app)

        class Todo(db.Document):
            description = db.StringField()

        self.assertIsInstance(Todo.query, BaseQuery)
 def test_loads_without_database_connection_data(self):
     app = Flask(__name__)
     app.config['MONGOALCHEMY_DATABASE'] = 'my_database'
     MongoAlchemy(app)
     self.assertEqual(app.config['MONGOALCHEMY_SERVER'], 'localhost')
     self.assertEqual(app.config['MONGOALCHEMY_PORT'], '27017')
     self.assertEqual(app.config['MONGOALCHEMY_USER'], None)
     self.assertEqual(app.config['MONGOALCHEMY_PASSWORD'], None)
     self.assertEqual(app.config['MONGOALCHEMY_REPLICA_SET'], '')
    def test_invalid_query_is_none(self):
        db = MongoAlchemy()

        class Query(object):
            pass

        class Todo(db.Document):
            description = db.StringField()
            query_class = Query

        assert Todo.query is None
    def test_should_be_possible_to_create_a_customized_query_class(self):
        db = MongoAlchemy(self.app)

        class Query(BaseQuery):
            pass

        class Todo(db.Document):
            description = db.StringField()
            query_class = Query

        self.assertIsInstance(Todo.query, Query)
 def test_should_be_able_to_instantiate_without_passing_the_app_and_set_it_later(
         self):
     db = MongoAlchemy()
     assert db.session is None
     db.init_app(self.app)
     assert db.session is not None
 def test_should_be_able_to_instantiate_passing_the_app(self):
     db = MongoAlchemy(self.app)
     assert db.session is not None
 def test_should_include_all_mongo_alchemy_fields_objects(self):
     db = MongoAlchemy()
     for key in dir(fields):
         assert hasattr(db, key), "should have the %s attribute" % key
     assert hasattr(
         db, 'DocumentField'), "should have the DocumentField attribute"
 def test_should_be_able_to_instantiate_without_passing_the_app_and_set_it_later(self):
     db = MongoAlchemy()
     assert db.session is None
     db.init_app(self.app)
     assert db.session is not None
 def test_should_provide_a_Document_class_to_be_extended_inside_the_MongoAlchemy_object(
         self):
     db = MongoAlchemy()
     assert db.Document is not None
Beispiel #13
0
from flask import Flask
from flask_cors import CORS
from flask.ext.mongoalchemy import MongoAlchemy

app = Flask(__name__)
app.secret_key = 'matcha'
app.config.from_pyfile('config.py')
CORS(app)
mongo = MongoAlchemy(app)

# keep here to avoid circular dep
from views import *

if __name__ == '__main__':
    import nltk
    from fixtures import *
    import pymongo
    from config import MONGOALCHEMY_CONNECTION_STRING

    nltk.download('punkt')
    nltk.download('stopwords')
    db = pymongo.MongoClient(MONGOALCHEMY_CONNECTION_STRING).matcha2

    try:
        has_listing = db.Listing.find()[0]
    except IndexError:
        populate_listings()

    try:
        has_skill = db.Skill.find()[0]
    except IndexError:
from flask.ext.pymongo import PyMongo
from time import gmtime, strftime

from flask.ext.mongoalchemy import MongoAlchemy

# Object creation
app = Flask(__name__)
app.config.from_object(__name__)
app.secret_key = '<some secret key>'
CORS(app)

app.config['MONGOALCHEMY_DATABASE'] = 'app'
app.config['MONGOALCHEMY_CONNECTION_STRING'] = 'mongodb://localhost:27017/'
connection = MongoClient("mongodb://localhost:27017/")

db = MongoAlchemy()

mongo = PyMongo(app)


# Initialize Database
def create_mongodatabase():
    try:
        dbnames = connection.database_names()
        if 'app' not in dbnames:
            db_api = connection.app.apirelease
            db_api.insert({
                "buildtime": "2017-01-01 10:00:00",
                "links": "/api/v1/users",
                "methods": "get, post, put, delete",
                "version": "v1"
 def test_should_not_be_able_to_work_without_providing_a_database_name(
         self):
     with self.assertRaises(ImproperlyConfiguredError):
         app = Flask(__name__)
         MongoAlchemy(app)
 def test_should_provide_a_session_object_on_mongoalchemy_instance(self):
     db = MongoAlchemy(self.app)
     self.assertIsInstance(db.session, Session)
Beispiel #17
0
app = Flask(__name__)
app.debug = True
# Configuracion de app.
app.config['SECRET_KEY'] = 'super-secret'
app.config['JWT_REQUIRED_CLAIMS'] = []
app.config['JWT_EXPIRATION_DELTA'] = timedelta(hours=12)
app.config['BUNDLE_ERRORS'] = True
app.config['SEND_EMAILS'] = True

# Configuracion de MongoDB.
url = os.environ.get('MONGOLAB_URI', 'mongodb://localhost/enjoy-events')
app.config['MONGOALCHEMY_CONNECTION_STRING'] = url
parsed = urlsplit(url)
app.config['MONGOALCHEMY_DATABASE'] = parsed.path[1:]
db = MongoAlchemy(app)

CORS(app)

errors = {
    'SpecsError': {
        'message': "This field can't be empty.",
        'status': 401,
    },
    'ValidationError': {
        'message': "This field can't be empty.",
        'status': 402,
    },
    'RestException': {
        'message': "This field can't be empty.",
        'status': 400,
Beispiel #18
0
from mongoalchemy.document import Index
from flask.ext.mongoalchemy import MongoAlchemy
from pymongo import Connection
from pymongo.errors import BulkWriteError
from server.constants import EXPLANATION_REGIONS

# For debugging:
# from flask import flash

mongo = MongoAlchemy()

# TODO: down the road, maybe more generic demographics class or something?
# TODO: modify manual edit method to incorporate regions


class DBError(Exception):
    """ Exception generated when database access classes are misused. """
    def __init__(self, msg):
        self.msg = msg

    def __str__(self):
        return 'Database Access Error -- \"{}\"!'.format(self.msg)


class DocumentBase(mongo.Document):
    '''  Common code for my own mongo documents.    
    '''
    name = mongo.StringField()
    # This should just ensure that names stay unique:
    name_index = Index().ascending('name').unique()