Iterate over SQLA Result / Query instead of .all()
This commit is contained in:
parent
39c79aef04
commit
aa52367f03
|
@ -28,5 +28,4 @@ class DeviceView(View):
|
|||
|
||||
def find(self, args: dict):
|
||||
"""Gets many devices."""
|
||||
devices = Device.query.all()
|
||||
return self.schema.jsonify(devices, many=True)
|
||||
return self.schema.jsonify(Device.query, many=True)
|
||||
|
|
|
@ -76,7 +76,7 @@ class Lot(Thing):
|
|||
@classmethod
|
||||
def roots(cls):
|
||||
"""Gets the lots that are not under any other lot."""
|
||||
return set(cls.query.join(cls.paths).filter(db.func.nlevel(Path.path) == 1).all())
|
||||
return cls.query.join(cls.paths).filter(db.func.nlevel(Path.path) == 1)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return '<Lot {0.name} devices={0.devices!r}>'.format(self)
|
||||
|
|
|
@ -1,15 +1,17 @@
|
|||
import uuid
|
||||
from datetime import datetime
|
||||
from typing import Set, Union
|
||||
from typing import Iterable, Set, Union
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy import Column
|
||||
from sqlalchemy.orm import relationship
|
||||
from sqlalchemy.orm import Query, relationship
|
||||
from sqlalchemy_utils import Ltree
|
||||
|
||||
from ereuse_devicehub.resources.device.models import Device
|
||||
from ereuse_devicehub.resources.models import Thing
|
||||
|
||||
LotQuery = Union[Query, Iterable['Lot']]
|
||||
|
||||
|
||||
class Lot(Thing):
|
||||
id = ... # type: Column
|
||||
|
@ -26,18 +28,18 @@ class Lot(Thing):
|
|||
self.devices = ... # type: Set[Device]
|
||||
self.paths = ... # type: Set[Path]
|
||||
|
||||
def add_child(self, child: Union['Lot', uuid.UUID]):
|
||||
def add_child(self, child: Union[Lot, uuid.UUID]):
|
||||
pass
|
||||
|
||||
def remove_child(self, child: 'Lot'):
|
||||
def remove_child(self, child: Lot):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def roots(cls):
|
||||
def roots(cls) -> LotQuery:
|
||||
pass
|
||||
|
||||
@property
|
||||
def children(self) -> Set['Lot']:
|
||||
def children(self) -> LotQuery:
|
||||
pass
|
||||
|
||||
|
||||
|
|
2
setup.py
2
setup.py
|
@ -44,7 +44,7 @@ setup(
|
|||
'psycopg2-binary',
|
||||
'python-stdnum',
|
||||
'PyYAML',
|
||||
'teal>=0.2.0a13',
|
||||
'teal>=0.2.0a14',
|
||||
'requests',
|
||||
'requests-toolbelt',
|
||||
'sqlalchemy-utils[password, color, phone]',
|
||||
|
|
|
@ -179,9 +179,9 @@ def test_lot_roots():
|
|||
db.session.add_all(lots)
|
||||
db.session.flush()
|
||||
|
||||
assert Lot.roots() == {l1, l2, l3}
|
||||
assert set(Lot.roots()) == {l1, l2, l3}
|
||||
l1.add_child(l2)
|
||||
assert Lot.roots() == {l1, l3}
|
||||
assert set(Lot.roots()) == {l1, l3}
|
||||
|
||||
|
||||
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
|
||||
|
|
|
@ -239,7 +239,7 @@ def test_snapshot_tag_inner_tag(tag_id: str, user: UserClient, app: Devicehub):
|
|||
snapshot_and_check(user, b,
|
||||
event_types=(WorkbenchRate.t, AggregateRate.t, BenchmarkProcessor.t))
|
||||
with app.app_context():
|
||||
tag, *_ = Tag.query.all() # type: Tag
|
||||
tag = Tag.query.one() # type: Tag
|
||||
assert tag.device_id == 1, 'Tag should be linked to the first device'
|
||||
|
||||
|
||||
|
|
Reference in a new issue