Skip to content

Commit

Permalink
Implement filtering Item by different zones, closes #764 (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
shayaniox authored and pylover committed May 16, 2019
1 parent 0d3ecc9 commit 66548a9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
24 changes: 22 additions & 2 deletions dolphin/controllers/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -41,7 +42,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):
Expand Down
4 changes: 2 additions & 2 deletions dolphin/models/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down
31 changes: 30 additions & 1 deletion dolphin/tests/test_item_list.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime

from bddrest import status, response, when, given
from auditor.context import Context as AuditLogContext

Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 66548a9

Please sign in to comment.