Commit 8fdc4870 authored by sim's avatar sim

[REFACT] Move Base and models imports where they belongs

parent 2e8958d7
...@@ -18,8 +18,8 @@ if __name__ == "__main__": ...@@ -18,8 +18,8 @@ if __name__ == "__main__":
django_models_names = set(model._meta.db_table for model in django_models) django_models_names = set(model._meta.db_table for model in django_models)
# migrate SQLAlchemy models # migrate SQLAlchemy models
from gargantext import models from gargantext.models import Base
from gargantext.util.db import Base, engine from gargantext.util.db import engine
sqla_models_names = ( sqla_models_names = (
model for model in Base.metadata.tables.keys() model for model in Base.metadata.tables.keys()
if model not in django_models_names if model not in django_models_names
......
from .base import Base
from .nodes import * from .nodes import *
from .hyperdata import * from .hyperdata import *
from .users import * from .users import *
......
from sqlalchemy.schema import Column, ForeignKey, UniqueConstraint
from sqlalchemy.orm import relationship
from sqlalchemy.types import TypeDecorator, \
Integer, Float, Boolean, DateTime, String, Text
from sqlalchemy.dialects.postgresql import JSONB, DOUBLE_PRECISION as Double
from sqlalchemy.ext.mutable import MutableDict, MutableList
from sqlalchemy.ext.declarative import declarative_base
__all__ = ["Column", "ForeignKey", "UniqueConstraint", "relationship",
"Integer", "Float", "Boolean", "DateTime", "String", "Text",
"TypeDecorator",
"JSONB", "Double",
"MutableDict", "MutableList",
"Base"]
Base = declarative_base()
from gargantext.util.db import *
from gargantext.constants import INDEXED_HYPERDATA from gargantext.constants import INDEXED_HYPERDATA
from .base import Base, Column, ForeignKey, TypeDecorator, \
Integer, Double, DateTime, String, Text
from .nodes import Node from .nodes import Node
import datetime import datetime
......
from gargantext.util.db import * from .base import Base, Column, ForeignKey, relationship, \
Integer, Float, String
from .nodes import Node from .nodes import Node
__all__ = ['Ngram', 'NodeNgram', 'NodeNodeNgram', 'NodeNgramNgram'] __all__ = ['Ngram', 'NodeNgram', 'NodeNodeNgram', 'NodeNgramNgram']
......
from gargantext.util.db import * from gargantext.util.db import session
from gargantext.util.files import upload from gargantext.util.files import upload
from gargantext.constants import * from gargantext.constants import *
from datetime import datetime from datetime import datetime
from .base import Base, Column, ForeignKey, relationship, TypeDecorator, \
Integer, Float, String, DateTime, JSONB, \
MutableList, MutableDict
from .users import User from .users import User
__all__ = ['Node', 'NodeNode', 'CorpusNode'] __all__ = ['Node', 'NodeNode', 'CorpusNode']
...@@ -22,7 +25,7 @@ class NodeType(TypeDecorator): ...@@ -22,7 +25,7 @@ class NodeType(TypeDecorator):
class Node(Base): class Node(Base):
"""This model can fit many purposes: """This model can fit many purposes:
myFirstCorpus = session.query(CorpusNode).first() myFirstCorpus = session.query(CorpusNode).first()
It intends to provide a generic model, allowing hierarchical structure It intends to provide a generic model, allowing hierarchical structure
......
from django.contrib.auth import models from django.contrib.auth import models
from gargantext.util.db import * from gargantext.util.db import session, aliased
from datetime import datetime from datetime import datetime
from .base import Base, Column, ForeignKey, UniqueConstraint, \
Integer, Boolean, DateTime, String
__all__ = ['User', 'Contact'] __all__ = ['User', 'Contact']
class User(Base): class User(Base):
...@@ -60,7 +63,7 @@ class User(Base): ...@@ -60,7 +63,7 @@ class User(Base):
"""check if a given node is owned by the user""" """check if a given node is owned by the user"""
return (node.user_id == self.id) or \ return (node.user_id == self.id) or \
node.id in (contact.id for contact in self.contacts()) node.id in (contact.id for contact in self.contacts())
def get_params(self, username=None): def get_params(self, username=None):
print(self.__dict__.items()) print(self.__dict__.items())
return self.hyperdata return self.hyperdata
......
...@@ -6,7 +6,6 @@ from gargantext.util.json import json_dumps ...@@ -6,7 +6,6 @@ from gargantext.util.json import json_dumps
# get engine, session, etc. # get engine, session, etc.
######################################################################## ########################################################################
from sqlalchemy.orm import sessionmaker, scoped_session from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import delete from sqlalchemy import delete
def get_engine(): def get_engine():
...@@ -22,24 +21,13 @@ def get_engine(): ...@@ -22,24 +21,13 @@ def get_engine():
engine = get_engine() engine = get_engine()
Base = declarative_base()
session = scoped_session(sessionmaker(bind=engine)) session = scoped_session(sessionmaker(bind=engine))
########################################################################
# tools to build models
########################################################################
from sqlalchemy.types import *
from sqlalchemy.schema import Column, ForeignKey, UniqueConstraint
from sqlalchemy.dialects.postgresql import JSONB, DOUBLE_PRECISION
from sqlalchemy.ext.mutable import MutableDict, MutableList
Double = DOUBLE_PRECISION
######################################################################## ########################################################################
# useful for queries # useful for queries
######################################################################## ########################################################################
from sqlalchemy.orm import aliased, relationship from sqlalchemy.orm import aliased
from sqlalchemy import func, desc from sqlalchemy import func, desc
######################################################################## ########################################################################
......
...@@ -10,7 +10,7 @@ __all__ = ['json_encoder', 'json_dumps'] ...@@ -10,7 +10,7 @@ __all__ = ['json_encoder', 'json_dumps']
class JSONEncoder(json.JSONEncoder): class JSONEncoder(json.JSONEncoder):
def default(self, obj): def default(self, obj):
from gargantext.util.db import Base from gargantext.models import Base
if isinstance(obj, Base): if isinstance(obj, Base):
return { return {
key: value key: value
......
...@@ -26,8 +26,7 @@ environ.setdefault("DJANGO_SETTINGS_MODULE", "gargantext.settings") ...@@ -26,8 +26,7 @@ environ.setdefault("DJANGO_SETTINGS_MODULE", "gargantext.settings")
DATABASES['default']['NAME'] = DATABASES['default']['TEST']['NAME'] DATABASES['default']['NAME'] = DATABASES['default']['TEST']['NAME']
setup() # models can now be imported setup() # models can now be imported
from gargantext import models # Base is now filled from gargantext.models import Base # contains metadata.tables
from gargantext.util.db import Base # contains metadata.tables
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# thanks to our hack, util.db.engine and util.db.session already use the test DB # thanks to our hack, util.db.engine and util.db.session already use the test DB
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment