def main():
    parser = ArgumentParser(
        description='''
        Setup indexes and constraints on labels in Neo4j for your neomodel schema.
        Database credentials can be set by the environment variable NEO4J_BOLT_URL.
        ''')

    parser.add_argument(
        'apps',  metavar='<someapp.models/app.py>', type=str, nargs='+',
        help='python modules or files to load schema from.')

    parser.add_argument(
        '--db', metavar='bolt://*****:*****@localhost:7687', dest='neo4j_bolt_url', type=str, default='',
        help='address of your neo4j database'
    )

    args = parser.parse_args()

    bolt_url = args.neo4j_bolt_url
    if len(bolt_url) == 0:
        bolt_url = environ.get('NEO4J_BOLT_URL', 'bolt://*****:*****@localhost:7687')

    for app in args.apps:
        load_python_module_or_file(app)

    # Connect after to override any code in the module that may set the connection
    print('Connecting to {}\n'.format(bolt_url))
    db.set_connection(bolt_url)

    install_all_labels()
Exemple #2
0
def seed_db():
    print("Seeding DB...")
    db.cypher_query("MATCH (n) DETACH DELETE n")

    remove_all_labels()
    print("Installing Labels...")
    install_all_labels()

    with db.transaction:
        print("Seeding Nodes...")
        hotels = seed_hotel()
        packages = seed_package()
        seed_attraction()
        cities = seed_city()
        agencies = seed_agency()
        seed_hotel_owner()
        seed_shop_owner()
        travellers = seed_traveller()
        blogs = seed_blog()
        topics = seed_topic()
        seed_package_day(packages, cities)

        print("Seeding Relations...")
        seed_hotel_city_relations(hotels, cities)
        seed_blog_relations(travellers, blogs)
        seed_package_review_relation(travellers, packages)
        seed_city_review_relation(travellers, cities)
        seed_hotel_review_relation(travellers, hotels)
        seed_blog_topic_relation(topics, blogs)
        seed_blog_location_relation(
            cities, blogs)  # EXTRA: locations=cities+attractions
        seed_blog_comment_relation(travellers, blogs)
        seed_hotel_like_relation(travellers, hotels)
        seed_package_agency(packages, agencies)
        print("Done.")
Exemple #3
0
    def custom_init(self, pinit=False, pdestroy=False, abackend=None, **kwargs):
        """ Note: we ignore args here """

        # recover instance with the parent method
        graph = super().custom_init()

        # db.init_app(self.app)

        with self.app.app_context():

            if pdestroy:
                log.critical("Destroy current Neo4j data")
                from neomodel import clear_neo4j_database

                clear_neo4j_database(graph.db)

            if pinit:

                auto_index = self.variables.get("autoindexing", 'True') == 'True'

                if auto_index:
                    try:
                        from neomodel import remove_all_labels, install_all_labels
                        remove_all_labels()
                        install_all_labels()
                    except BaseException as e:
                        log.exit(str(e))

        return graph
def test_install_all():
    install_labels(TestAbstractNode)
    # run install all labels
    install_all_labels()
    assert True
    # remove constraint for above test
    db.cypher_query("DROP CONSTRAINT on (n:NoConstraintsSetup) ASSERT n.name IS UNIQUE")
Exemple #5
0
def set_node_config():
    from neomodel import (config, StringProperty, DateTimeProperty,
                          IntegerProperty, UniqueIdProperty, remove_all_labels,
                          install_all_labels)
    from neomodel.contrib import SemiStructuredNode
    from neomodel.util import clear_neo4j_database
    from neomodel import db

    class SomeNode(SemiStructuredNode):
        nodeId = UniqueIdProperty()
        someDate = DateTimeProperty()

    config.DATABASE_URL = NEOMODEL_DATABASE_URI

    clear_neo4j_database(db)
    remove_all_labels()

    install_all_labels()

    node_array = []
    with db.transaction:
        for i in range(1, 32):
            for j in range(0, 24):
                dt = datetime(2019, 5, i, j)
                sm = SomeNode(someDate=dt)
                sm.save()
                node_array.append(SomeNode(someDate=dt))

    random.shuffle(node_array)
    return node_array
Exemple #6
0
def test_install_all():
    # run install all labels
    install_all_labels()
    assert True
    # remove constraint for above test
    db.cypher_query(
        "DROP CONSTRAINT on (n:NoConstraintsSetup) ASSERT n.name IS UNIQUE")
Exemple #7
0
    def setUpClass(cls):
        super().setUpClass()
        logger.info("Clearing Graph...")
        neomodel.clear_neo4j_database(graphdb)
        neomodel.install_all_labels()

        logger.info("Creating a user...")
        User.objects.create_user(username=cls.username, password=cls.password)
Exemple #8
0
def install():
    bolt_url = environ.get("DB_URL")

    load_python_module_or_file("main.py")

    # Connect after to override any code in the module that may set the connection
    print('Connecting to {}\n'.format(bolt_url))
    db.set_connection(bolt_url)

    install_all_labels()
Exemple #9
0
def reformat_database() -> bool:
    """
    Completely erase and reinstall indexes and constraints. Remove all nodes.
    """
    result = False
    try:
        remove_all_labels()
        results, meta = db.cypher_query(DROP_ALL_QUERY)
        install_all_labels()
        result = True
    except:
        raise Exception(
            "An error occurred while resetting the database. Try again manually"
        )
    return result
Exemple #10
0
def init_db(app):

    from neomodel import install_all_labels
    install_all_labels()

    from .utils.detect_loaders import detect
    import importlib
    try:
        loaders = importlib.import_module('src.database.loaders')
        mods = detect(loaders)
        for mod in mods:
            mod_obj = getattr(loaders, mod)
            run_method = getattr(mod_obj, 'run')
            print(run_method)
            run_method(app)
    except ImportError:
        pass
Exemple #11
0
def main(path, remove_label):
    df = pd.read_csv(path)
    df.drop_duplicates(subset='id', inplace=True)
    df.fillna('', inplace=True)

    install_all_labels()

    if remove_label:
        remove_all_labels()

    for index, row in tqdm(df.iterrows(), total=df.shape[0]):
        try:
            Movie.nodes.get(uid=row['id'])
        except DoesNotExist:
            movie = Movie(
                uid=row['id'],
                title=row['title'],
                poster=row['poster'],
                overview=row['overview'],
                vote=row['vote'],
                date=row['date'],
                language=row['language'],
            ).save()
        else:
            continue

        director = get_or_create(Director, name=row['director'])
        compositor = get_or_create(Compositor, name=row['compositor'])
        producer = get_or_create(Producer, name=row['producer'])
        movie.producer.connect(producer)
        movie.director.connect(director)
        movie.compositor.connect(compositor)

        for name in row['genres'].split('|'):
            genre = get_or_create(Genre, name=name)
            movie.genres.connect(genre)

        for name in row['keywords'].split('|'):
            keyword = get_or_create(Keyword, name=name)
            movie.keywords.connect(keyword)

        for name in row['actors'].split('|'):
            actor = get_or_create(Actor, name=name)
            movie.actors.connect(actor)
Exemple #12
0
def create_app(config_filename=None):
    """
    Creates the app using the specific config file of that environment.
    See instance directory.
    """
    app = Flask(__name__, instance_relative_config=True)
    CORS(app, supports_credentials=True)
    create_routes(app)
    app.config.from_pyfile(config_filename)
    app.es = Elasticsearch(app.config["ELASTICSEARCH_URL"]
                           ) if app.config["ELASTICSEARCH_URL"] else None
    config.DATABASE_URL = app.config["NEO4J_URL"]
    install_all_labels()
    JWTManager(app)
    register_extensions(app)
    register_error_handlers(app)
    add_background_jobs(app)
    add_debug_scripts(app)

    if not os.path.isdir(app.config["UPLOAD_FOLDER"]):
        os.mkdir(app.config["UPLOAD_FOLDER"])

    return app
 def handle(self, *args, **options):
     setup_django()
     install_all_labels(stdout=self.stdout)
Exemple #14
0
 def login(self):
     db.set_connection(self.url)
     install_all_labels()
Exemple #15
0
def create_labels():
    print('Creating constrains...')
    install_all_labels()
    print('Created constrains in database:')
    results, columns = db.cypher_query("CALL db.constraints")
    [print(x) for x in results]
Exemple #16
0
from neo4j.v1 import GraphDatabase
import json
import eth_account
from eth_utils import keccak, to_normalized_address
from models import *
from neomodel import install_all_labels
from dynaconf import settings
import neomodel

driver = GraphDatabase.driver(
    settings['NEO4J']['URL'],
    auth=(settings['NEO4J']['USERNAME'], settings['NEO4J']['PASSWORD']),
    encrypted=settings['NEO4J']['ENCRYPTED_CONNECTION'])


def create_test_members(tx):
    u1 = settings['NEO']
    return tx.run(f"""
    merge (u1:User {{uuid: '{u1['uuid']}', email: '{u1['email']}', name: '{u1['name']}'}})
    """)


if __name__ == '__main__':
    with driver.session() as session:
        install_all_labels()
        session.write_transaction(create_test_members)
Exemple #17
0
 def login(self):
     db.set_connection(self.url)
     install_all_labels()
Exemple #18
0
 def handle(self, *args, **options):
     install_all_labels(stdout=self.stdout)