Add roots() method in lots

This commit is contained in:
Xavier Bustamante Talavera 2018-08-10 00:52:01 +02:00
parent 2ed558ac2b
commit 1b66888bd7
3 changed files with 20 additions and 1 deletions

View file

@ -7,6 +7,7 @@ from sqlalchemy import Column
from ereuse_devicehub.resources.device.models import Device from ereuse_devicehub.resources.device.models import Device
from ereuse_devicehub.resources.event.models import Rate from ereuse_devicehub.resources.event.models import Rate
from ereuse_devicehub.resources.lot.models import Lot
from ereuse_devicehub.resources.schemas import Thing from ereuse_devicehub.resources.schemas import Thing
from ereuse_devicehub.resources.tag import Tag from ereuse_devicehub.resources.tag import Tag
from teal.query import Between, FullTextSearch, ILike, Join, Or, Query, Sort, SortField from teal.query import Between, FullTextSearch, ILike, Join, Or, Query, Sort, SortField
@ -97,7 +98,7 @@ class InventoryView(View):
.paginate(page=args['page'], per_page=30) # type: Pagination .paginate(page=args['page'], per_page=30) # type: Pagination
inventory = { inventory = {
'devices': app.resources[Device.t].schema.dump(devices.items, many=True, nested=1), 'devices': app.resources[Device.t].schema.dump(devices.items, many=True, nested=1),
'groups': [], 'lots': Lot.roots(),
'widgets': {}, 'widgets': {},
'pagination': { 'pagination': {
'page': devices.page, 'page': devices.page,

View file

@ -48,6 +48,11 @@ class Lot(Thing):
def __contains__(self, child: 'Lot'): def __contains__(self, child: 'Lot'):
return Edge.has_lot(self.id, child.id) return Edge.has_lot(self.id, child.id)
@classmethod
def roots(cls):
"""Gets the lots that are not under any other lot."""
return set(cls.query.join(cls.edges).filter(db.func.nlevel(Edge.path) == 1).all())
def __repr__(self) -> str: def __repr__(self) -> str:
return '<Lot {0.name} devices={0.devices!r}>'.format(self) return '<Lot {0.name} devices={0.devices!r}>'.format(self)

View file

@ -168,3 +168,16 @@ def test_lot_unite_graphs():
l2.remove_child(l4) l2.remove_child(l4)
assert l4 not in l2 and l5 not in l2 and l6 not in l2 and l7 not in l2 and l8 not in l2 assert l4 not in l2 and l5 not in l2 and l6 not in l2 and l7 not in l2 and l8 not in l2
assert l4 not in l3 and l5 not in l3 and l6 not in l3 and l7 not in l3 and l8 not in l3 assert l4 not in l3 and l5 not in l3 and l6 not in l3 and l7 not in l3 and l8 not in l3
@pytest.mark.usefixtures(conftest.auth_app_context.__name__)
def test_lot_roots():
"""Tests getting the method Lot.roots."""
lots = Lot('1'), Lot('2'), Lot('3')
l1, l2, l3 = lots
db.session.add_all(lots)
db.session.flush()
assert Lot.roots() == {l1, l2, l3}
l1.add_child(l2)
assert Lot.roots() == {l1, l3}