# Path: tilde/core/orm_tools.py
# class UniqueMixin(object):
#     @classmethod
#     def unique_filter(cls, query, *arg, **kw):
#         raise NotImplementedError()
# 
#     @classmethod
#     def as_unique(cls, session, *arg, **kw):
#         return _unique(session, cls, cls.unique_filter, cls, arg, kw)
# 
#     @classmethod
#     def as_unique_todict(cls, session, *arg, **kw):
#         return _unique_todict(session, cls, cls.unique_filter, arg, kw)
# 
# def get_or_create(cls, session, defaults=None, **kwds):
#     result = session.query(cls).filter_by(**kwds).first()
#     if result:
#         return result, False
#     new_vals = defaults
#     if defaults is None:
#         new_vals = {}
#     new_vals.update(kwds)
#     result = cls(**new_vals)
#     session.add(result)
#     session.flush()
#     return result, True
# 
# def correct_topics(session, model, calc_id, cid, new_topics, mode, topics_hierarchy):
#     assert model.Calculation.__tablename__
# 
#     found_entity = None
#     for e in topics_hierarchy:
#         if e['cid'] == cid:
#             found_entity = e
#             break
#     assert found_entity, "Wrong topic identifier!"
# 
#     if isinstance(calc_id, str):
#         calc_id = [calc_id]
#     assert isinstance(calc_id, list)
#     if isinstance(new_topics, str):
#         new_topics = [new_topics]
#     assert isinstance(new_topics, list)
# 
#     if mode == 'REPLACE':
#         _replace_topics(session, model, calc_id, cid, new_topics)
#     elif mode == 'APPEND':
#         assert found_entity.get('multiple', False)
#         _append_topics(session, model, calc_id, cid, new_topics)


import logging
import datetime
import six
import ujson as json
from collections import namedtuple
from tilde.core.orm_tools import UniqueMixin, get_or_create, correct_topics
from sqlalchemy import and_, or_, Index, UniqueConstraint, MetaData, String, UnicodeText, Table, Column, Boolean, Float, Integer, BigInteger, Enum, Text, Date, DateTime, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
from sqlalchemy.sql.expression import insert, delete
    from sqlalchemy import UnicodeText as JSONString
    from sqlalchemy import LargeBinary as JSONString
    # mimic bool
    multiple = Column(Integer, default=0)
    optional = Column(Integer, default=0)
    has_summary_contrb = Column(Integer, default=0)
    has_column = Column(Integer, default=0)
    has_facet = Column(Integer, default=0)
    has_topic = Column(Integer, default=0)
    chem_formula = Column(Integer, default=0)
    plottable = Column(Integer, default=0)

    values = relationship("Hierarchy_value")
    hgroup_id = Column(Integer, ForeignKey('hierarchy_groups.hgroup_id'), default=None)

class Hierarchy_group(Base):
    __tablename__ = 'hierarchy_groups'
    hgroup_id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)

    # mimic bool
    landing_group = Column(Integer, default=0)
    settings_group = Column(Integer, default=0)

    includes = relationship("Hierarchy")

class Hierarchy_value(Base):
    __tablename__ = 'hierarchy_values'
    cid = Column(Integer, ForeignKey('hierarchy.cid'), primary_key=True)
    num = Column(Integer, nullable=False, primary_key=True)
    name = Column(String, nullable=False)


# Path: tilde/core/orm_tools.py
# class UniqueMixin(object):
#     @classmethod
#     def unique_filter(cls, query, *arg, **kw):
#         raise NotImplementedError()
# 
#     @classmethod
#     def as_unique(cls, session, *arg, **kw):
#         return _unique(session, cls, cls.unique_filter, cls, arg, kw)
# 
#     @classmethod
#     def as_unique_todict(cls, session, *arg, **kw):
#         return _unique_todict(session, cls, cls.unique_filter, arg, kw)
# 
# def get_or_create(cls, session, defaults=None, **kwds):
#     result = session.query(cls).filter_by(**kwds).first()
#     if result:
#         return result, False
#     new_vals = defaults
#     if defaults is None:
#         new_vals = {}
#     new_vals.update(kwds)
#     result = cls(**new_vals)
#     session.add(result)
#     session.flush()
#     return result, True
# 
# def correct_topics(session, model, calc_id, cid, new_topics, mode, topics_hierarchy):
#     assert model.Calculation.__tablename__
# 
#     found_entity = None
#     for e in topics_hierarchy:
#         if e['cid'] == cid:
#             found_entity = e
#             break
#     assert found_entity, "Wrong topic identifier!"
# 
#     if isinstance(calc_id, str):
#         calc_id = [calc_id]
#     assert isinstance(calc_id, list)
#     if isinstance(new_topics, str):
#         new_topics = [new_topics]
#     assert isinstance(new_topics, list)
# 
#     if mode == 'REPLACE':
#         _replace_topics(session, model, calc_id, cid, new_topics)
#     elif mode == 'APPEND':
#         assert found_entity.get('multiple', False)
#         _append_topics(session, model, calc_id, cid, new_topics)


# Path: tilde/core/model.py
import logging
import datetime
import six
import ujson as json
from collections import namedtuple
from tilde.core.orm_tools import UniqueMixin, get_or_create, correct_topics
from sqlalchemy import and_, or_, Index, UniqueConstraint, MetaData, String, UnicodeText, Table, Column, Boolean, Float, Integer, BigInteger, Enum, Text, Date, DateTime, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
from sqlalchemy.sql.expression import insert, delete
    from sqlalchemy import UnicodeText as JSONString
    from sqlalchemy import LargeBinary as JSONString

    # mimic bool
    multiple = Column(Integer, default=0)
    optional = Column(Integer, default=0)
    has_summary_contrb = Column(Integer, default=0)
    has_column = Column(Integer, default=0)
    has_facet = Column(Integer, default=0)
    has_topic = Column(Integer, default=0)
    chem_formula = Column(Integer, default=0)
    plottable = Column(Integer, default=0)

    values = relationship("Hierarchy_value")
    hgroup_id = Column(Integer, ForeignKey('hierarchy_groups.hgroup_id'), default=None)

class Hierarchy_group(Base):
    __tablename__ = 'hierarchy_groups'
    hgroup_id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)

    # mimic bool
    landing_group = Column(Integer, default=0)
    settings_group = Column(Integer, default=0)

    includes = relationship("Hierarchy")

class Hierarchy_value(Base):
    __tablename__ = 'hierarchy_values'
    cid = Column(Integer, ForeignKey('hierarchy.cid'), primary_key=True)
    num = Column(Integer, nullable=False, primary_key=True)
    name = Column(String, nullable=False)


class Topic(UniqueMixin, Base):