diff --git a/dolphin/controllers/items.py b/dolphin/controllers/items.py index cdf6c02f..8b377a1f 100644 --- a/dolphin/controllers/items.py +++ b/dolphin/controllers/items.py @@ -4,8 +4,9 @@ from restfulpy.authorization import authorize from restfulpy.controllers import ModelRestController from restfulpy.orm import DBSession, commit +from sqlalchemy import select, func -from ..models import Item, Dailyreport, Event +from ..models import Item, Dailyreport, Event, Member from ..validators import update_item_validator, dailyreport_update_validator @@ -56,7 +57,26 @@ def get(self, id): @json(prevent_form='709 Form Not Allowed') @Item.expose def list(self): - return DBSession.query(Item) + resource = Member.current() + query = DBSession.query(Item).filter(Item.member_id == resource.id) + active_item_cte = select([func.max(Item.id).label('max_item_id')]) \ + .group_by(Item.issue_id) \ + .cte() + + if 'zone' in context.query: + if context.query['zone'] == 'needEstimate': + query = query.filter(Item.id == active_item_cte.c.max_item_id) \ + .filter(Item.estimated_hours.is_(None)) + + elif context.query['zone'] == 'upcomingNuggets': + query = query.filter(Item.id == active_item_cte.c.max_item_id) \ + .filter(Item.start_date > datetime.now()) + + elif context.query['zone'] == 'inProcessNuggets': + query = query.filter(Item.id == active_item_cte.c.max_item_id) \ + .filter(Item.start_date < datetime.now()) + + return query class ItemDailyreportController(ModelRestController): diff --git a/dolphin/models/item.py b/dolphin/models/item.py index eb7bbe6d..1697ac78 100644 --- a/dolphin/models/item.py +++ b/dolphin/models/item.py @@ -34,7 +34,7 @@ class Item(TimestampMixin, OrderingMixin, FilteringMixin, PaginationMixin, example=1, protected=False, ) - start_time = Field( + start_date = Field( DateTime, python_type=datetime, label='Start Date', @@ -48,7 +48,7 @@ class Item(TimestampMixin, OrderingMixin, FilteringMixin, PaginationMixin, required=False, readonly=True, ) - end_time = Field( + end_date = Field( DateTime, python_type=datetime, label='Target Date', diff --git a/dolphin/tests/test_item_list.py b/dolphin/tests/test_item_list.py index 8da05c69..592631e3 100644 --- a/dolphin/tests/test_item_list.py +++ b/dolphin/tests/test_item_list.py @@ -1,3 +1,5 @@ +from datetime import datetime + from bddrest import status, response, when, given from auditor.context import Context as AuditLogContext @@ -124,7 +126,10 @@ def mockup(cls): cls.item2 = Item( issue_id=cls.issue2.id, phase_id=cls.phase3.id, - member_id=cls.member2.id, + member_id=cls.member1.id, + start_date=datetime.strptime('2020-2-2', '%Y-%m-%d'), + end_date=datetime.strptime('2020-2-3', '%Y-%m-%d'), + estimated_hours=3, ) session.add(cls.item2) session.flush() @@ -133,6 +138,9 @@ def mockup(cls): issue_id=cls.issue3.id, phase_id=cls.phase2.id, member_id=cls.member1.id, + start_date=datetime.strptime('2018-2-2', '%Y-%m-%d'), + end_date=datetime.strptime('2020-2-3', '%Y-%m-%d'), + estimated_hours=3, ) session.add(cls.item3) session.commit() @@ -167,6 +175,27 @@ def test_list_item(self): when('Filter by id', query=dict(id=f'!{self.item1.id}')) assert len(response.json) == 2 + when( + 'Filter by `needEstimate` zone', + query=dict(zone='needEstimate') + ) + assert len(response.json) == 1 + assert response.json[0]['id'] == self.item1.id + + when( + 'Filter by `upcomingNuggets` zone', + query=dict(zone='upcomingNuggets') + ) + assert len(response.json) == 1 + assert response.json[0]['id'] == self.item2.id + + when( + 'Filter by `inProcessNuggets` zone', + query=dict(zone='inProcessNuggets') + ) + assert len(response.json) == 1 + assert response.json[0]['id'] == self.item3.id + when( 'Paginate item', query=dict(sort='id', take=1, skip=2)