Fix and refactor action_required_by

This commit is contained in:
richard-dds 2018-08-07 16:22:24 -04:00
parent 52bd76e6ee
commit 0c9005aaf6
4 changed files with 57 additions and 19 deletions

View File

@ -1,3 +1,4 @@
from enum import Enum
from sqlalchemy import exists, and_ from sqlalchemy import exists, and_
from sqlalchemy.orm.exc import NoResultFound from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.orm.attributes import flag_modified from sqlalchemy.orm.attributes import flag_modified
@ -26,10 +27,11 @@ def deep_merge(source, destination: dict):
return _deep_merge(source, dict(destination)) return _deep_merge(source, dict(destination))
class RequestStatuses(object): class RequestStatus(Enum):
@classmethod STARTED = "started"
def new(cls, status_name): PENDING_FINANCIAL_VERIFICATION = "pending_financial_verification"
return RequestStatusEvent(new_status=status_name) PENDING_CCPO_APPROVAL = "pending_ccpo_approval"
APPROVED = "approved"
class Requests(object): class Requests(object):
@ -38,8 +40,7 @@ class Requests(object):
@classmethod @classmethod
def create(cls, creator_id, body): def create(cls, creator_id, body):
request = Request(creator=creator_id, body=body) request = Request(creator=creator_id, body=body)
request = Requests.set_status(request, RequestStatus.STARTED)
request.status_events.append(RequestStatuses.new("started"))
db.session.add(request) db.session.add(request)
db.session.commit() db.session.commit()
@ -79,12 +80,13 @@ class Requests(object):
@classmethod @classmethod
def submit(cls, request): def submit(cls, request):
new_status = None
if Requests.should_auto_approve(request): if Requests.should_auto_approve(request):
request.status_events.append( new_status = RequestStatus.PENDING_FINANCIAL_VERIFICATION
RequestStatuses.new("pending_financial_verification")
)
else: else:
request.status_events.append(RequestStatuses.new("pending_ccpo_approval")) new_status = RequestStatus.PENDING_CCPO_APPROVAL
request = Requests.set_status(request, new_status)
db.session.add(request) db.session.add(request)
db.session.commit() db.session.commit()
@ -114,6 +116,25 @@ class Requests(object):
db.session.add(request) db.session.add(request)
db.session.commit() db.session.commit()
@classmethod
def set_status(cls, request: Request, status: RequestStatus):
status_event = RequestStatusEvent(new_status=status.value)
request.status_events.append(status_event)
return request
@classmethod
def action_required_by(cls, request):
try:
status = RequestStatus(request.status)
except ValueError:
return None
return {
RequestStatus.STARTED: "mission_owner",
RequestStatus.PENDING_FINANCIAL_VERIFICATION: "mission_owner",
RequestStatus.PENDING_CCPO_APPROVAL: "ccpo"
}.get(status)
@classmethod @classmethod
def should_auto_approve(cls, request): def should_auto_approve(cls, request):
try: try:

View File

@ -22,11 +22,5 @@ class Request(Base):
def status(self): def status(self):
return self.status_events[-1].new_status return self.status_events[-1].new_status
@property def set_status(self, status):
def action_required_by(self): self.status_events.append(status)
return {
"incomplete": "mission_owner",
"pending_submission": "mission_owner",
"submitted": "ccpo",
"approved": "mission_owner",
}.get(self.status)

View File

@ -1,19 +1,26 @@
import factory import factory
from uuid import uuid4 from uuid import uuid4
from atst.models import Request from atst.models import Request, RequestStatusEvent
from atst.models.pe_number import PENumber from atst.models.pe_number import PENumber
from atst.models.task_order import TaskOrder from atst.models.task_order import TaskOrder
from atst.models.user import User from atst.models.user import User
from atst.models.role import Role from atst.models.role import Role
class RequestStatusFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = RequestStatusEvent
class RequestFactory(factory.alchemy.SQLAlchemyModelFactory): class RequestFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta: class Meta:
model = Request model = Request
id = factory.Sequence(lambda x: uuid4()) id = factory.Sequence(lambda x: uuid4())
status_events = factory.RelatedFactory(RequestStatusFactory, "request", new_status="started")
class PENumberFactory(factory.alchemy.SQLAlchemyModelFactory): class PENumberFactory(factory.alchemy.SQLAlchemyModelFactory):

View File

@ -1,5 +1,21 @@
from tests.factories import RequestFactory from tests.factories import RequestFactory
from atst.domain.requests import Requests, RequestStatus
def test_started_request_requires_mo_action(): def test_started_request_requires_mo_action():
request = RequestFactory.create() request = RequestFactory.create()
assert Requests.action_required_by(request) == "mission_owner"
def test_pending_financial_requires_mo_action():
request = RequestFactory.create()
request = Requests.set_status(request, RequestStatus.PENDING_FINANCIAL_VERIFICATION)
assert Requests.action_required_by(request) == "mission_owner"
def test_pending_ccpo_approval_requires_ccpo():
request = RequestFactory.create()
request = Requests.set_status(request, RequestStatus.PENDING_CCPO_APPROVAL)
assert Requests.action_required_by(request) == "ccpo"