Ejemplo n.º 1
0
    def test_documented_initial_insert(self):
        from sqlalchemy_mptt import tree_manager

        engine = create_engine('sqlite:///:memory:')
        Session = mptt_sessionmaker(sessionmaker(bind=engine))
        session = Session()
        Base.metadata.create_all(engine)

        tree_manager.register_events(remove=True)  # Disable MPTT events

        _tree_id = 'tree1'

        for node_id, parent_id in [(1, None), (2, 1), (3, 1), (4, 2)]:
            item = Tree(
                id=node_id,
                parent_id=parent_id,
                left=0,
                right=0,
                tree_id=_tree_id
            )
            session.add(item)
        session.commit()

        tree_manager.register_events()  # enabled MPTT events back
        Tree.rebuild_tree(
            session,
            _tree_id
        )  # rebuild lft, rgt value automatically
Ejemplo n.º 2
0
    def setUp(self):
        self.engine = create_engine('sqlite:///:memory:')
        Session = mptt_sessionmaker(sessionmaker(bind=self.engine))
        self.session = Session()
        self.base.metadata.create_all(self.engine)
        self.fixture = Fixtures(self.session)
        self.fixture.add(
            self.model,
            os.path.join('fixtures', getattr(self, 'fixtures', 'tree.json'))
        )

        self.result = self.session.query(
            self.model.get_pk_column(), self.model.left, self.model.right,
            self.model.level, self.model.parent_id, self.model.tree_id)
Ejemplo n.º 3
0
    def test(self):
        engine = create_engine('sqlite:///:memory:')
        Session = mptt_sessionmaker(sessionmaker(bind=engine))
        session = Session()
        Base.metadata.create_all(engine)

        root = Tree(id=0)
        child = Tree(id=1, parent_id=0)

        session.add(root)
        session.add(child)
        session.commit()

        self.assertEqual(root.tree_id, 1)
        self.assertEqual(child.tree_id, 1)
Ejemplo n.º 4
0
    def setUp(self):
        self.engine = create_engine('sqlite:///:memory:')
        Session = mptt_sessionmaker(sessionmaker(bind=self.engine))
        self.session = Session()
        self.base.metadata.create_all(self.engine)
        self.fixture = Fixtures(self.session)
        self.fixture.add(
            self.model,
            os.path.join('fixtures', getattr(self, 'fixtures', 'tree.json')))

        self.result = self.session.query(self.model.get_pk_column(),
                                         self.model.left, self.model.right,
                                         self.model.level,
                                         self.model.parent_id,
                                         self.model.tree_id)
Ejemplo n.º 5
0
 def create_session(self, options):
     Session = super().create_session(options)
     return mptt_sessionmaker(Session)
Ejemplo n.º 6
0
 def setUpClass(cls):
     cls.engine = engine_from_config(settings, prefix='sqlalchemy.')
     cls.DBSession = mptt_sessionmaker(sessionmaker())
Ejemplo n.º 7
0
from datetime import datetime
import os

import bookmarks_parser
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy_mptt import mptt_sessionmaker

from SQL.sqlalchemy.mptt.model import Folder, Bookmark

engine = create_engine(os.environ.get('PG_DATABASE_URL'))
Session = mptt_sessionmaker(sessionmaker(bind=engine))
session = Session()


def save2db(child, prev_db_folder=None):
    for bookmark in child:
        title = bookmark['title']
        add_date = None
        if bookmark.get('add_date'):
            add_date = datetime.fromtimestamp(int(bookmark['add_date']))
        if bookmark.get('children'):
            last_modified = None
            if bookmark.get('last_modified'):
                last_modified = datetime.fromtimestamp(
                    int(bookmark['add_date']))
            if prev_db_folder:
                folder = Folder(title=title,
                                add_date=add_date,
                                last_modified=last_modified,
                                parent_id=prev_db_folder.id)
from pyramid.session import SignedCookieSessionFactory
from sqlalchemy_mptt import mptt_sessionmaker
from zope.sqlalchemy import ZopeTransactionExtension
from sqlalchemy.ext.declarative import declarative_base

from pyramid_pages.models import FlatPageMixin, MpttPageMixin, RedirectMixin
from pyramid_pages.resources import (
    BasePageResource,
    resource_of_node,
    resources_of_config
)

Base = declarative_base()
DBSession = scoped_session(
    mptt_sessionmaker(
        sessionmaker(extension=ZopeTransactionExtension())
    )
)

CONFIG_SQLALCHEMY_URL = 'sqlalchemy.url'
CONFIG_PYRAMID_PAGES_MODELS = 'pyramid_pages.models'
CONFIG_PYRAMID_PAGES_DBSESSION = 'pyramid_pages.dbsession'


class BasePage(Base, RedirectMixin):
    __tablename__ = 'base_pages'
    id = Column(Integer, primary_key=True)
    page_type = Column(String(50))

    __mapper_args__ = {
        'polymorphic_identity': 'base_page',
Ejemplo n.º 9
0
from pyramid.config import Configurator
from pyramid.events import BeforeRender
from sqlalchemy.orm import relationship, sessionmaker, scoped_session
from sqlalchemy.sql import func
from pyramid.session import SignedCookieSessionFactory
from sqlalchemy_mptt import mptt_sessionmaker
from zope.sqlalchemy import ZopeTransactionExtension
from sqlalchemy.ext.declarative import declarative_base

from pyramid_pages.models import FlatPageMixin, MpttPageMixin, RedirectMixin
from pyramid_pages.resources import (BasePageResource, resource_of_node,
                                     resources_of_config)

Base = declarative_base()
DBSession = scoped_session(
    mptt_sessionmaker(sessionmaker(extension=ZopeTransactionExtension())))

CONFIG_SQLALCHEMY_URL = 'sqlalchemy.url'
CONFIG_PYRAMID_PAGES_MODELS = 'pyramid_pages.models'
CONFIG_PYRAMID_PAGES_DBSESSION = 'pyramid_pages.dbsession'


class BasePage(Base, RedirectMixin):
    __tablename__ = 'base_pages'
    id = Column(Integer, primary_key=True)
    page_type = Column(String(50))

    __mapper_args__ = {
        'polymorphic_identity': 'base_page',
        'polymorphic_on': page_type,
        'with_polymorphic': '*'