sqlalchemy.orm.undefer

Here are the examples of the python api sqlalchemy.orm.undefer taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

74 Examples 7

3 Source : test_mapper.py
with Apache License 2.0
from gethue

    def test_no_previous_query(self):
        Thing = self.classes.Thing

        session = create_session()
        thing = session.query(Thing).options(sa.orm.undefer("name")).first()
        self._test(thing)

    def test_query_twice_with_clear(self):

3 Source : test_mapper.py
with Apache License 2.0
from gethue

    def test_query_twice_with_clear(self):
        Thing = self.classes.Thing

        session = create_session()
        result = session.query(Thing).first()  # noqa
        session.expunge_all()
        thing = session.query(Thing).options(sa.orm.undefer("name")).first()
        self._test(thing)

    def test_query_twice_no_clear(self):

3 Source : test_mapper.py
with Apache License 2.0
from gethue

    def test_query_twice_no_clear(self):
        Thing = self.classes.Thing

        session = create_session()
        result = session.query(Thing).first()  # noqa
        thing = session.query(Thing).options(sa.orm.undefer("name")).first()
        self._test(thing)

    def test_joinedload_with_clear(self):

3 Source : test_mapper.py
with Apache License 2.0
from gethue

    def test_join_with_clear(self):
        Thing, Human = self.classes.Thing, self.classes.Human

        session = create_session()
        result = (  # noqa
            session.query(Human).add_entity(Thing).join("thing").first()
        )
        session.expunge_all()
        thing = session.query(Thing).options(sa.orm.undefer("name")).first()
        self._test(thing)

    def test_join_no_clear(self):

3 Source : test_mapper.py
with Apache License 2.0
from gethue

    def test_join_no_clear(self):
        Thing, Human = self.classes.Thing, self.classes.Human

        session = create_session()
        result = (  # noqa
            session.query(Human).add_entity(Thing).join("thing").first()
        )
        thing = session.query(Thing).options(sa.orm.undefer("name")).first()
        self._test(thing)


class NoLoadTest(_fixtures.FixtureTest):

3 Source : migration.py
with MIT License
from OneGov

    def entries(self):
        session = object_session(self.directory)

        if not session:
            return self.directory.entries

        e = session.query(DirectoryEntry)
        e = e.filter_by(directory_id=self.directory.id)
        e = e.options(joinedload(DirectoryEntry.files))
        e = e.options(undefer(DirectoryEntry.content))

        return e

    def execute(self):

3 Source : booking.py
with MIT License
from OneGov

    def run(self, form, session):
        self.users = {
            user.username: user for user in
            session.query(User).options(undefer('*'))
        }

        return self.rows(session, form.selected_period)

    def query(self, session, period):

3 Source : occasion.py
with MIT License
from OneGov

    def query(self, session, period):
        q = session.query(Occasion)
        q = q.filter(Occasion.period_id == period.id)
        q = q.options(joinedload(Occasion.activity).joinedload(Activity.user))
        q = q.options(joinedload(Occasion.period))
        q = q.options(undefer('*'))
        q = q.order_by(Occasion.order)

        return q

    def rows(self, session, period):

3 Source : resource.py
with MIT License
from OneGov

    def bound_reservations(self, request, status='pending'):
        """ The reservations associated with this resource and user. """

        session = self.bound_session_id(request)
        scheduler = self.get_scheduler(request.app.libres_context)

        res = scheduler.queries.reservations_by_session(session)
        res = res.filter(Reservation.resource == self.id)
        res = res.filter(Reservation.status == status)
        res = res.order_by(None)  # clear existing order
        res = res.order_by(Reservation.start)

        # used by ReservationInfo
        res = res.options(undefer(Reservation.created))

        return res

    def bound_session_id(self, request):

3 Source : upgrade.py
with MIT License
from OneGov

def rename_guideline_to_submissions_guideline(context):
    directories = context.session.query(ExtendedDirectory)\
        .options(undefer(ExtendedDirectory.content))

    for directory in directories:
        directory.content['submissions_guideline'] \
            = directory.content.pop('guideline', None)


@upgrade_task('Add meta access property')

3 Source : newsletter.py
with MIT License
from OneGov

def news_by_newsletter(newsletter, request):
    news_ids = newsletter.content.get('news')

    if not news_ids:
        return None

    query = request.session.query(News)
    query = query.order_by(desc(News.created))
    query = query.options(undefer('created'))
    query = query.options(undefer('content'))
    query = query.filter(News.id.in_(news_ids))

    return request.exclude_invisible(query.all())


def occurrences_by_newsletter(newsletter, request):

3 Source : payment.py
with MIT License
from OneGov

    def subset(self):
        q = self.query().order_by(desc(Payment.created))

        if self.start:
            q = q.filter(self.start   <  = Payment.created)

        if self.end:
            q = q.filter(Payment.created  < = self.end)

        q = q.options(joinedload(Payment.provider))
        q = q.options(undefer(Payment.created))
        return q

    @property

3 Source : expression_profile.py
with MIT License
from sepro

def expression_profile_download_plot(profile_id):
    """
    Generates a tab-delimited table for off-line use

    :param profile_id: ID of the profile to render
    """
    current_profile = ExpressionProfile.query.options(undefer('profile')).get_or_404(profile_id)

    return Response(current_profile.table, mimetype='text/plain')


@expression_profile.route('/download/plot/  <  profile_id>/ < condition_tissue_id>')

3 Source : expression_profile.py
with MIT License
from sepro

def expression_profile_download_tissue_plot(profile_id, condition_tissue_id):
    """
    Generates a tab-delimited table for off-line use

    :param profile_id: ID of the profile to render
    :param condition_tissue_id: ID of conversion table
    """
    current_profile = ExpressionProfile.query.options(undefer('profile')).get_or_404(profile_id)

    return Response(current_profile.tissue_table(condition_tissue_id))


@expression_profile.route('/json/plot/  <  profile_id>')

3 Source : expression_profile.py
with MIT License
from sepro

def expression_profile_plot_json(profile_id):
    """
    Generates a JSON object that can be rendered using Chart.js line plots

    :param profile_id: ID of the profile to render
    """
    current_profile = ExpressionProfile.query.options(undefer('profile')).get_or_404(profile_id)
    data = json.loads(current_profile.profile)

    plot = prepare_expression_profile(data, show_sample_count=True, ylabel='TPM')

    return Response(json.dumps(plot), mimetype='application/json')


@expression_profile.route('/json/plot/  <  profile_id>/ < condition_tissue_id>')

3 Source : expression_profile.py
with MIT License
from sepro

def expression_profile_plot_tissue_json(profile_id, condition_tissue_id):
    """
    Generates a JSON object that can be rendered using Chart.js line plots

    :param profile_id: ID of the profile to render
    :param condition_tissue_id: ID of the condition to tissue conversion to be used
    """
    current_profile = ExpressionProfile.query.options(undefer('profile')).get_or_404(profile_id)
    data = current_profile.tissue_profile(condition_tissue_id)

    plot = prepare_expression_profile(data, ylabel='TPM')

    return Response(json.dumps(plot), mimetype='application/json')


@expression_profile.route('/json/compare_plot/  <  first_profile_id>/ < second_profile_id>')

3 Source : sequence.py
with MIT License
from sepro

def sequence_modal_coding(sequence_id):
    """
    Returns the coding sequence in a modal

    :param sequence_id: ID of the sequence
    :return: Response with the fasta file
    """
    current_sequence = Sequence.query\
        .options(undefer('coding_sequence'))\
        .options(noload('xrefs'))\
        .get_or_404(sequence_id)

    return render_template('modals/sequence.html', sequence=current_sequence, coding=True)


@sequence.route('/modal/protein/  <  sequence_id>')

3 Source : sequence.py
with MIT License
from sepro

def sequence_modal_protein(sequence_id):
    """
    Returns the protein sequence in a modal

    :param sequence_id: ID of the sequence
    :return: Response with the fasta file
    """
    current_sequence = Sequence.query\
        .options(undefer('coding_sequence'))\
        .options(noload('xrefs'))\
        .get_or_404(sequence_id)

    return render_template('modals/sequence.html', sequence=current_sequence, coding=False)


@sequence.route('/fasta/coding/  <  sequence_id>')

3 Source : sequence.py
with MIT License
from sepro

def sequence_fasta_protein(sequence_id):
    """
    Returns the protein sequence as a downloadable fasta file

    :param sequence_id: ID of the sequence
    :return: Response with the fasta file
    """
    current_sequence = Sequence.query\
        .options(undefer('coding_sequence'))\
        .options(noload('xrefs'))\
        .get_or_404(sequence_id)

    fasta = ">" + current_sequence.name + "\n" + current_sequence.protein_sequence + "\n"
    response = make_response(fasta)
    response.headers["Content-Disposition"] = "attachment; filename=" + current_sequence.name + ".protein.fasta"
    response.headers['Content-type'] = 'text/plain'

    return response

3 Source : coexpression_clusters.py
with MIT License
from sepro

    def profiles(self):
        """
        Returns a list with all expression profiles of cluster members
        :return: list of all profiles
        """

        sequence_subquery = self.sequences.subquery()

        profiles = ExpressionProfile.query.\
            options(undefer('profile')).\
            join(sequence_subquery, ExpressionProfile.sequence_id == sequence_subquery.c.id).all()

        return profiles



    @property

3 Source : sequences.py
with MIT License
from sepro

    def export_cds(filename):
        sequences = Sequence.query.options(undefer('coding_sequence')).all()

        with open(filename, "w") as f_out:
            for s in sequences:
                print(">%s\n%s" % (s.name, s.coding_sequence), file=f_out)

    @staticmethod

3 Source : sequences.py
with MIT License
from sepro

    def export_protein(filename):
        sequences = Sequence.query.options(undefer('coding_sequence')).all()

        with open(filename, "w") as f_out:
            for s in sequences:
                print(">%s\n%s" % (s.name, s.protein_sequence), file=f_out)

3 Source : model_query.py
with GNU Affero General Public License v3.0
from spectria

    def undefer_all_columns(self) -> ModelQuery:
        """Undefer all columns (generative)."""
        self = self.options(undefer("*"))

        return self

    @staticmethod

3 Source : test_typed_mapping.py
with MIT License
from sqlalchemy

    def test_construct_works_in_deferred(self, decl_base):
        class User(decl_base):
            __tablename__ = "users"

            id: Mapped[int] = mapped_column(primary_key=True)
            data: Mapped[str] = deferred(mapped_column())

        self.assert_compile(select(User), "SELECT users.id FROM users")
        self.assert_compile(
            select(User).options(undefer(User.data)),
            "SELECT users.data, users.id FROM users",
        )

    def test_deferred_kw(self, decl_base):

3 Source : test_typed_mapping.py
with MIT License
from sqlalchemy

    def test_deferred_kw(self, decl_base):
        class User(decl_base):
            __tablename__ = "users"

            id: Mapped[int] = mapped_column(primary_key=True)
            data: Mapped[str] = mapped_column(deferred=True)

        self.assert_compile(select(User), "SELECT users.id FROM users")
        self.assert_compile(
            select(User).options(undefer(User.data)),
            "SELECT users.data, users.id FROM users",
        )


class MixinTest(fixtures.TestBase, testing.AssertsCompiledSQL):

3 Source : test_deferred.py
with MIT License
from sqlalchemy

    def test_mapper_undefer_unraise(self):
        A = self.classes.A
        s = fixture_session()
        a1 = s.query(A).options(undefer(A.z)).first()
        assert "z" in a1.__dict__
        eq_(a1.z, 4)
        eq_(a1.x, 2)

    def test_deferred_raise_option_raise_column_plain(self):

3 Source : test_deferred.py
with MIT License
from sqlalchemy

    def test_no_previous_query(self):
        Thing = self.classes.Thing

        session = fixture_session()
        thing = (
            session.query(Thing).options(sa.orm.undefer(Thing.name)).first()
        )
        self._test(thing)

    def test_query_twice_with_clear(self):

3 Source : test_deferred.py
with MIT License
from sqlalchemy

    def test_query_twice_with_clear(self):
        Thing = self.classes.Thing

        session = fixture_session()
        result = session.query(Thing).first()  # noqa
        session.expunge_all()
        thing = (
            session.query(Thing).options(sa.orm.undefer(Thing.name)).first()
        )
        self._test(thing)

    def test_query_twice_no_clear(self):

3 Source : test_deferred.py
with MIT License
from sqlalchemy

    def test_query_twice_no_clear(self):
        Thing = self.classes.Thing

        session = fixture_session()
        result = session.query(Thing).first()  # noqa
        thing = (
            session.query(Thing).options(sa.orm.undefer(Thing.name)).first()
        )
        self._test(thing)

    def test_joinedload_with_clear(self):

3 Source : test_deferred.py
with MIT License
from sqlalchemy

    def test_join_with_clear(self):
        Thing, Human = self.classes.Thing, self.classes.Human

        session = fixture_session()
        result = (  # noqa
            session.query(Human).add_entity(Thing).join(Human.thing).first()
        )
        session.expunge_all()
        thing = (
            session.query(Thing).options(sa.orm.undefer(Thing.name)).first()
        )
        self._test(thing)

    def test_join_no_clear(self):

3 Source : test_deferred.py
with MIT License
from sqlalchemy

    def test_join_no_clear(self):
        Thing, Human = self.classes.Thing, self.classes.Human

        session = fixture_session()
        result = (  # noqa
            session.query(Human).add_entity(Thing).join(Human.thing).first()
        )
        thing = (
            session.query(Thing).options(sa.orm.undefer(Thing.name)).first()
        )
        self._test(thing)

3 Source : model_query.py
with GNU Affero General Public License v3.0
from themotte

    def undefer_all_columns(self) -> "ModelQuery":
        """Undefer all columns (generative)."""
        self = self.options(undefer("*"))

        return self

    @staticmethod

0 Source : test_deferred.py
with Apache License 2.0
from gethue

    def test_options(self):
        """Options on a mapper to create deferred and undeferred columns"""

        orders, Order = self.tables.orders, self.classes.Order

        mapper(Order, orders)

        sess = create_session()
        q = sess.query(Order).order_by(Order.id).options(defer("user_id"))

        def go():
            q.all()[0].user_id

        self.sql_eq_(
            go,
            [
                (
                    "SELECT orders.id AS orders_id, "
                    "orders.address_id AS orders_address_id, "
                    "orders.description AS orders_description, "
                    "orders.isopen AS orders_isopen "
                    "FROM orders ORDER BY orders.id",
                    {},
                ),
                (
                    "SELECT orders.user_id AS orders_user_id "
                    "FROM orders WHERE orders.id = :param_1",
                    {"param_1": 1},
                ),
            ],
        )
        sess.expunge_all()

        q2 = q.options(undefer("user_id"))
        self.sql_eq_(
            q2.all,
            [
                (
                    "SELECT orders.id AS orders_id, "
                    "orders.user_id AS orders_user_id, "
                    "orders.address_id AS orders_address_id, "
                    "orders.description AS orders_description, "
                    "orders.isopen AS orders_isopen "
                    "FROM orders ORDER BY orders.id",
                    {},
                )
            ],
        )

    def test_undefer_group(self):

0 Source : test_deferred.py
with Apache License 2.0
from gethue

    def test_deep_options(self):
        users, items, order_items, Order, Item, User, orders = (
            self.tables.users,
            self.tables.items,
            self.tables.order_items,
            self.classes.Order,
            self.classes.Item,
            self.classes.User,
            self.tables.orders,
        )

        mapper(
            Item,
            items,
            properties=dict(description=deferred(items.c.description)),
        )
        mapper(
            Order,
            orders,
            properties=dict(items=relationship(Item, secondary=order_items)),
        )
        mapper(
            User,
            users,
            properties=dict(orders=relationship(Order, order_by=orders.c.id)),
        )

        sess = create_session()
        q = sess.query(User).order_by(User.id)
        result = q.all()
        item = result[0].orders[1].items[1]

        def go():
            eq_(item.description, "item 4")

        self.sql_count_(1, go)
        eq_(item.description, "item 4")

        sess.expunge_all()
        result = q.options(undefer("orders.items.description")).all()
        item = result[0].orders[1].items[1]

        def go():
            eq_(item.description, "item 4")

        self.sql_count_(0, go)
        eq_(item.description, "item 4")

    def test_path_entity(self):

0 Source : test_deferred.py
with Apache License 2.0
from gethue

    def test_load_only_w_deferred(self):
        orders, Order = self.tables.orders, self.classes.Order

        mapper(
            Order,
            orders,
            properties={"description": deferred(orders.c.description)},
        )

        sess = create_session()
        q = sess.query(Order).options(
            load_only("isopen", "description"), undefer("user_id")
        )
        self.assert_compile(
            q,
            "SELECT orders.description AS orders_description, "
            "orders.id AS orders_id, "
            "orders.user_id AS orders_user_id, "
            "orders.isopen AS orders_isopen FROM orders",
        )

    def test_load_only_propagate_unbound(self):

0 Source : test_deferred.py
with Apache License 2.0
from gethue

    def test_defer_on_wildcard_subclass(self):
        # pretty much the same as load_only except doesn't
        # exclude the primary key

        # TODO: what is ".*"?  this is not documented anywhere, how did this
        # get implemented without docs ?  see #4390
        s = Session()
        q = (
            s.query(Manager)
            .order_by(Person.person_id)
            .options(defer(".*"), undefer("status"))
        )
        self.assert_compile(
            q,
            "SELECT managers.status AS managers_status "
            "FROM people JOIN managers ON "
            "people.person_id = managers.person_id ORDER BY people.person_id",
        )

        # note this doesn't apply to "bound" loaders since they don't seem
        # to have this ".*" featue.

    def test_load_only_subclass_of_type(self):

0 Source : test_deprecations.py
with Apache License 2.0
from gethue

    def test_defer_addtl_attrs(self):
        users, User, Address, addresses = (
            self.tables.users,
            self.classes.User,
            self.classes.Address,
            self.tables.addresses,
        )

        mapper(Address, addresses)
        mapper(
            User,
            users,
            properties={
                "addresses": relationship(
                    Address, lazy="selectin", order_by=addresses.c.id
                )
            },
        )

        sess = create_session()

        with testing.expect_deprecated(
            r"The \*addl_attrs on orm.defer is deprecated.  "
            "Please use method chaining"
        ):
            sess.query(User).options(defer("addresses", "email_address"))

        with testing.expect_deprecated(
            r"The \*addl_attrs on orm.undefer is deprecated.  "
            "Please use method chaining"
        ):
            sess.query(User).options(undefer("addresses", "email_address"))


class LegacyLockModeTest(_fixtures.FixtureTest):

0 Source : test_eager_relations.py
with Apache License 2.0
from gethue

    def test_with_deferred(self):
        nodes = self.tables.nodes

        class Node(fixtures.ComparableEntity):
            def append(self, node):
                self.children.append(node)

        mapper(
            Node,
            nodes,
            properties={
                "children": relationship(
                    Node, lazy="joined", join_depth=3, order_by=nodes.c.id
                ),
                "data": deferred(nodes.c.data),
            },
        )
        sess = create_session()
        n1 = Node(data="n1")
        n1.append(Node(data="n11"))
        n1.append(Node(data="n12"))
        sess.add(n1)
        sess.flush()
        sess.expunge_all()

        def go():
            eq_(
                Node(data="n1", children=[Node(data="n11"), Node(data="n12")]),
                sess.query(Node).order_by(Node.id).first(),
            )

        self.assert_sql_count(testing.db, go, 4)

        sess.expunge_all()

        def go():
            eq_(
                Node(data="n1", children=[Node(data="n11"), Node(data="n12")]),
                sess.query(Node)
                .options(undefer("data"))
                .order_by(Node.id)
                .first(),
            )

        self.assert_sql_count(testing.db, go, 3)

        sess.expunge_all()

        def go():
            eq_(
                Node(data="n1", children=[Node(data="n11"), Node(data="n12")]),
                sess.query(Node)
                .options(undefer("data"), undefer("children.data"))
                .first(),
            )

        self.assert_sql_count(testing.db, go, 1)

    def test_options(self):

0 Source : test_expire.py
with Apache License 2.0
from gethue

    def test_state_deferred_to_col(self):
        """Behavioral test to verify the current activity of loader callables
        """

        users, User = self.tables.users, self.classes.User

        mapper(User, users, properties={"name": deferred(users.c.name)})

        sess = create_session()
        u1 = sess.query(User).options(undefer(User.name)).first()
        assert "name" not in attributes.instance_state(u1).callables

        # mass expire, the attribute was loaded,
        # the attribute gets the callable
        sess.expire(u1)
        assert "name" in attributes.instance_state(u1).expired_attributes
        assert "name" not in attributes.instance_state(u1).callables

        # load it
        u1.name
        assert "name" not in attributes.instance_state(u1).expired_attributes
        assert "name" not in attributes.instance_state(u1).callables

        # mass expire, attribute was loaded but then deleted,
        # the callable goes away - the state wants to flip
        # it back to its "deferred" loader.
        sess.expunge_all()
        u1 = sess.query(User).options(undefer(User.name)).first()
        del u1.name
        sess.expire(u1)
        assert "name" not in attributes.instance_state(u1).expired_attributes
        assert "name" not in attributes.instance_state(u1).callables

        # single attribute expire, the attribute gets the callable
        sess.expunge_all()
        u1 = sess.query(User).options(undefer(User.name)).first()
        sess.expire(u1, ["name"])
        assert "name" in attributes.instance_state(u1).expired_attributes
        assert "name" not in attributes.instance_state(u1).callables

    def test_state_noload_to_lazy(self):

0 Source : test_expire.py
with Apache License 2.0
from gethue

    def test_deferred_cols_missing_in_load_state_reset(self):
        Data = self.classes.DataDefer

        sess = create_session()

        d1 = Data(data="d1")
        sess.add(d1)
        sess.flush()
        sess.close()

        sess = create_session()
        d1 = (
            sess.query(Data)
            .from_statement(select([Data.id]))
            .options(undefer(Data.data))
            .first()
        )
        d1.data = "d2"

        # the deferred loader has to clear out any state
        # on the col, including that 'd2' here
        d1 = sess.query(Data).populate_existing().first()

        def go():
            eq_(d1.data, "d1")

        self.assert_sql_count(testing.db, go, 1)


class RefreshTest(_fixtures.FixtureTest):

0 Source : test_selectin_relations.py
with Apache License 2.0
from gethue

    def test_with_deferred(self):
        nodes = self.tables.nodes

        class Node(fixtures.ComparableEntity):
            def append(self, node):
                self.children.append(node)

        mapper(
            Node,
            nodes,
            properties={
                "children": relationship(
                    Node, lazy="selectin", join_depth=3, order_by=nodes.c.id
                ),
                "data": deferred(nodes.c.data),
            },
        )
        sess = create_session()
        n1 = Node(data="n1")
        n1.append(Node(data="n11"))
        n1.append(Node(data="n12"))
        sess.add(n1)
        sess.flush()
        sess.expunge_all()

        def go():
            eq_(
                Node(data="n1", children=[Node(data="n11"), Node(data="n12")]),
                sess.query(Node).order_by(Node.id).first(),
            )

        self.assert_sql_count(testing.db, go, 6)

        sess.expunge_all()

        def go():
            eq_(
                Node(data="n1", children=[Node(data="n11"), Node(data="n12")]),
                sess.query(Node)
                .options(undefer("data"))
                .order_by(Node.id)
                .first(),
            )

        self.assert_sql_count(testing.db, go, 5)

        sess.expunge_all()

        def go():
            eq_(
                Node(data="n1", children=[Node(data="n11"), Node(data="n12")]),
                sess.query(Node)
                .options(undefer("data"), undefer("children.data"))
                .first(),
            )

        self.assert_sql_count(testing.db, go, 3)

    def test_options(self):

0 Source : test_subquery_relations.py
with Apache License 2.0
from gethue

    def test_with_deferred(self):
        nodes = self.tables.nodes

        class Node(fixtures.ComparableEntity):
            def append(self, node):
                self.children.append(node)

        mapper(
            Node,
            nodes,
            properties={
                "children": relationship(
                    Node, lazy="subquery", join_depth=3, order_by=nodes.c.id
                ),
                "data": deferred(nodes.c.data),
            },
        )
        sess = create_session()
        n1 = Node(data="n1")
        n1.append(Node(data="n11"))
        n1.append(Node(data="n12"))
        sess.add(n1)
        sess.flush()
        sess.expunge_all()

        def go():
            eq_(
                Node(data="n1", children=[Node(data="n11"), Node(data="n12")]),
                sess.query(Node).order_by(Node.id).first(),
            )

        self.assert_sql_count(testing.db, go, 6)

        sess.expunge_all()

        def go():
            eq_(
                Node(data="n1", children=[Node(data="n11"), Node(data="n12")]),
                sess.query(Node)
                .options(undefer("data"))
                .order_by(Node.id)
                .first(),
            )

        self.assert_sql_count(testing.db, go, 5)

        sess.expunge_all()

        def go():
            eq_(
                Node(data="n1", children=[Node(data="n11"), Node(data="n12")]),
                sess.query(Node)
                .options(undefer("data"), undefer("children.data"))
                .first(),
            )

        self.assert_sql_count(testing.db, go, 3)

    def test_options(self):

0 Source : booking.py
with MIT License
from OneGov

    def query(self, session, period):
        q = session.query(Booking)
        q = q.filter(Booking.period_id == period.id)

        q = q.join(Occasion)
        q = q.join(Activity)
        q = q.join(Attendee)
        q = q.join(User)
        q = q.options(undefer('*'))

        q = q.options(
            contains_eager(Booking.attendee).contains_eager(Attendee.user))
        q = q.options(
            contains_eager(Booking.occasion).contains_eager(Occasion.activity))

        q = q.order_by(
            Activity.order,
            Occasion.order,
            Attendee.name,
            User.username,
            Booking.priority
        )

        return q

    def rows(self, session, period):

0 Source : occasion.py
with MIT License
from OneGov

    def query(self, session, period):
        q = session.query(OccasionNeed)
        q = q.filter(OccasionNeed.occasion_id.in_(
            session.query(Occasion.id)
            .filter(Occasion.period_id == period.id)
            .subquery()
        ))
        q = q.join(Occasion)
        q = q.options(
            joinedload(OccasionNeed.occasion)
            .joinedload(Occasion.activity)
        )
        q = q.options(
            joinedload(OccasionNeed.occasion)
            .joinedload(Occasion.period)
        )
        q = q.options(undefer('*'))
        q = q.order_by(Occasion.order, OccasionNeed.name)

        return q

    def rows(self, session, period):

0 Source : cronjobs.py
with MIT License
from OneGov

def send_daily_ticket_statistics(request):

    today = replace_timezone(datetime.utcnow(), 'UTC')
    today = to_timezone(today, 'Europe/Zurich')

    if today.weekday() in (SAT, SUN):
        return

    if not request.app.send_daily_ticket_statistics:
        return

    args = {}
    app = request.app
    layout = DefaultMailLayout(object(), request)

    # get the current ticket count
    collection = TicketCollection(app.session())
    count = collection.get_count()
    args['currently_open'] = count.open
    args['currently_pending'] = count.pending
    args['currently_closed'] = count.closed

    # get tickets created yesterday or on the weekend
    end = datetime(today.year, today.month, today.day, tzinfo=today.tzinfo)
    if today.weekday() == MON:
        start = end - timedelta(days=2)
    else:
        start = end - timedelta(days=1)

    query = collection.query()
    query = query.filter(Ticket.created >= start)
    query = query.filter(Ticket.created   <  = end)
    args['opened'] = query.count()

    query = collection.query()
    query = query.filter(Ticket.modified >= start)
    query = query.filter(Ticket.modified  < = end)
    query = query.filter(Ticket.state == 'pending')
    args['pending'] = query.count()

    query = collection.query()
    query = query.filter(Ticket.modified >= start)
    query = query.filter(Ticket.modified  < = end)
    query = query.filter(Ticket.state == 'closed')
    args['closed'] = query.count()

    # render the email
    args['title'] = request.translate(
        _("${org} OneGov Cloud Status", mapping={
            'org': app.org.title
        })
    )
    args['layout'] = layout
    args['is_monday'] = today.weekday() == MON
    args['org'] = app.org.title

    # send one e-mail per user
    users = UserCollection(app.session()).query()
    users = users.filter(User.active == True)
    users = users.filter(User.role.in_(app.settings.org.status_mail_roles))
    users = users.options(undefer('data'))
    users = users.all()

    for user in users:

        if user.data and not user.data.get('daily_ticket_statistics'):
            continue

        unsubscribe = layout.unsubscribe_link(user.username)

        args['username'] = user.username
        args['unsubscribe'] = unsubscribe
        content = render_template(
            'mail_daily_ticket_statistics.pt', request, args
        )

        app.send_marketing_email(
            subject=args['title'],
            receivers=(user.username, ),
            content=content,
            headers={
                'List-Unsubscribe': f' < {unsubscribe}>',
                'List-Unsubscribe-Post': 'List-Unsubscribe=One-Click'
            }
        )


@OrgApp.cronjob(hour=6, minute=5, timezone='Europe/Zurich')

0 Source : page.py
with MIT License
from OneGov

    def news_query(self, limit=2, published_only=True):
        news = object_session(self).query(News)
        news = news.filter(Page.parent == self)
        if published_only:
            news = news.filter(
                News.publication_started == True,
                News.publication_ended == False
            )

        filter = []
        for year in self.filter_years:
            start = replace_timezone(datetime(year, 1, 1), 'UTC')
            filter.append(
                and_(
                    News.created >= start,
                    News.created   <   start.replace(year=year + 1)
                )
            )
        if filter:
            news = news.filter(or_(*filter))

        if self.filter_tags:
            news = news.filter(
                News.meta['hashtags'].has_any(array(self.filter_tags))
            )

        news = news.order_by(desc(News.published_or_created))
        news = news.options(undefer('created'))
        news = news.options(undefer('content'))
        news = news.limit(limit)

        sticky = func.json_extract_path_text(
            func.cast(News.meta, JSON), 'is_visible_on_homepage') == 'true'

        sticky_news = news.limit(None)
        sticky_news = sticky_news.filter(sticky)

        return news.union(sticky_news).order_by(
            desc(News.published_or_created))

    @property

0 Source : newsletter.py
with MIT License
from OneGov

def get_newsletter_form(model, request):

    form = NewsletterForm

    def news():
        q = request.session.query(News)
        q = q.filter(News.parent != None)
        q = q.order_by(desc(News.created))
        q = q.options(undefer('created'))

        return q

    form = form.with_news(request, news())

    def publications():
        q = PublicationCollection(request.session).query()
        q = q.order_by(desc(File.created))

        return q

    form = form.with_publications(request, publications())

    def occurrences():
        q = OccurrenceCollection(request.session).query()

        s = q.with_entities(Occurrence.id)
        s = s.order_by(None)
        s = s.order_by(
            Occurrence.event_id,
            Occurrence.start)

        s = s.distinct(Occurrence.event_id).subquery()

        return q.filter(Occurrence.id.in_(s))

    form = form.with_occurrences(request, occurrences())

    return form


def news_by_newsletter(newsletter, request):

0 Source : newsletter.py
with MIT License
from OneGov

def handle_newsletters(self, request, form, layout=None, mail_layout=None):

    if not (request.is_manager or request.app.org.show_newsletter):
        raise HTTPNotFound()

    if form.submitted(request):
        recipients = RecipientCollection(request.session)
        recipient = recipients.by_address(form.address.data)

        # do not show a specific error message if the user already signed up,
        # just pretend like everything worked correctly - if someone signed up
        # or not is private

        if not recipient:
            recipient = recipients.add(address=form.address.data)
            unsubscribe = request.link(recipient.subscription, 'unsubscribe')

            title = request.translate(
                _("Welcome to the ${org} Newsletter", mapping={
                    'org': request.app.org.title
                })
            )

            confirm_mail = render_template('mail_confirm.pt', request, {
                'layout': mail_layout or DefaultMailLayout(self, request),
                'newsletters': self,
                'subscription': recipient.subscription,
                'title': title,
                'unsubscribe': unsubscribe
            })

            request.app.send_marketing_email(
                subject=title,
                receivers=(recipient.address, ),
                content=confirm_mail,
                headers={
                    'List-Unsubscribe': f'  <  {unsubscribe}>',
                    'List-Unsubscribe-Post': 'List-Unsubscribe=One-Click'
                },
            )

        request.success(_((
            "Success! We have sent a confirmation link to "
            "${address}, if we didn't send you one already."
        ), mapping={'address': form.address.data}))

    query = self.query()
    query = query.options(undefer(Newsletter.created))
    query = query.order_by(desc(Newsletter.created))

    # newsletters which were not sent yet are private
    if not request.is_manager:
        query = query.filter(Newsletter.sent != None)

    # the recipients count is only shown to logged in users
    if request.is_manager:
        recipients_count = RecipientCollection(self.session).query()\
            .filter(Recipient.confirmed == True)\
            .count()
    else:
        recipients_count = 0

    return {
        'form': form,
        'layout': layout or NewsletterLayout(self, request),
        'newsletters': query.all(),
        'title': _("Newsletter"),
        'recipients_count': recipients_count
    }


@OrgApp.html(model=Newsletter, template='newsletter.pt', permission=Public)

0 Source : resource_recipient.py
with MIT License
from OneGov

def view_resource_recipients(self, request, layout=None):

    layout = layout or ResourceRecipientsLayout(self, request)

    def recipient_links(recipient):
        yield Link(
            text=_("Edit"),
            url=request.link(recipient, 'edit')
        )

        yield DeleteLink(
            text=_("Delete"),
            url=layout.csrf_protected_url(request.link(recipient)),
            confirm=_('Do you really want to delete "${name}"?', mapping={
                'name': recipient.name
            }),
            target='#{}'.format(recipient.id.hex),
            yes_button_text=_("Delete Recipient")
        )

    return {
        'layout': layout,
        'title': _("Recipients"),
        'recipients': self.query().options(undefer(ResourceRecipient.content)),
        'recipient_links': recipient_links
    }


@OrgApp.form(

0 Source : integration.py
with MIT License
from OneGov

    def es_perform_reindex(self, fail=False):
        """ Reindexes all content.

        This is a heavy operation and should be run with consideration.

        By default, all exceptions during reindex are silently ignored.

        """

        self.es_configure_client(usage='reindex')
        self.es_indexer.ixmgr.created_indices = set()

        # delete all existing indices for this town
        ixs = self.es_indexer.ixmgr.get_managed_indices_wildcard(self.schema)
        self.es_client.indices.delete(ixs)

        # have no queue limit for reindexing (that we're able to change
        # this here is a bit of a CPython implementation detail) - we can't
        # necessarily always rely on being able to change this property
        self.es_orm_events.queue.maxsize = 0

        # load all database objects and index them
        def reindex_model(model):
            session = self.session()

            try:
                q = session.query(model).options(undefer('*'))
                i = inspect(model)

                if i.polymorphic_on is not None:
                    q = q.filter(i.polymorphic_on == i.polymorphic_identity)

                for obj in q:
                    self.es_orm_events.index(self.schema, obj)
            finally:
                session.invalidate()
                session.bind.dispose()

        # by loading models in threads we can speed up the whole process
        with ThreadPoolExecutor() as executor:
            results = executor.map(
                reindex_model, (
                    model
                    for base in self.session_manager.bases
                    for model in searchable_sqlalchemy_models(base)
                )
            )
            if fail:
                tuple(results)

        self.es_indexer.bulk_process()


@ElasticsearchApp.tween_factory(over=transaction_tween_factory)

See More Examples