update to use request revisions
This commit is contained in:
@@ -4,33 +4,30 @@ from sqlalchemy.sql import text
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
from sqlalchemy.orm.attributes import flag_modified
|
||||
from werkzeug.datastructures import FileStorage
|
||||
import pendulum
|
||||
|
||||
from atst.database import db
|
||||
from atst.domain.authz import Authorization
|
||||
from atst.domain.task_orders import TaskOrders
|
||||
from atst.domain.workspaces import Workspaces
|
||||
from atst.models.request import Request
|
||||
from atst.models.request_revision import RequestRevision
|
||||
from atst.models.request_status_event import RequestStatusEvent, RequestStatus
|
||||
from atst.utils import deep_merge
|
||||
|
||||
from .exceptions import NotFoundError, UnauthorizedError
|
||||
|
||||
|
||||
def deep_merge(source, destination: dict):
|
||||
"""
|
||||
Merge source dict into destination dict recursively.
|
||||
"""
|
||||
|
||||
def _deep_merge(a, b):
|
||||
for key, value in a.items():
|
||||
if isinstance(value, dict):
|
||||
node = b.setdefault(key, {})
|
||||
_deep_merge(value, node)
|
||||
else:
|
||||
b[key] = value
|
||||
|
||||
return b
|
||||
|
||||
return _deep_merge(source, dict(destination))
|
||||
def create_revision_from_request_body(body):
|
||||
body = {k: v for p in body.values() for k, v in p.items()}
|
||||
TIMESTAMPS = ["start_date", "date_latest_training"]
|
||||
coerced_timestamps = {
|
||||
k: pendulum.parse(v)
|
||||
for k, v in body.items()
|
||||
if k in TIMESTAMPS and isinstance(v, str)
|
||||
}
|
||||
body = {**body, **coerced_timestamps}
|
||||
return RequestRevision(**body)
|
||||
|
||||
|
||||
class Requests(object):
|
||||
@@ -39,7 +36,8 @@ class Requests(object):
|
||||
|
||||
@classmethod
|
||||
def create(cls, creator, body):
|
||||
request = Request(creator=creator, body=body)
|
||||
revision = create_revision_from_request_body(body)
|
||||
request = Request(creator=creator, revisions=[revision])
|
||||
request = Requests.set_status(request, RequestStatus.STARTED)
|
||||
|
||||
db.session.add(request)
|
||||
@@ -105,7 +103,10 @@ class Requests(object):
|
||||
@classmethod
|
||||
def update(cls, request_id, request_delta):
|
||||
request = Requests._get_with_lock(request_id)
|
||||
request = Requests._merge_body(request, request_delta)
|
||||
|
||||
new_body = deep_merge(request_delta, request.body)
|
||||
revision = create_revision_from_request_body(new_body)
|
||||
request.revisions.append(revision)
|
||||
|
||||
db.session.add(request)
|
||||
db.session.commit()
|
||||
@@ -129,13 +130,7 @@ class Requests(object):
|
||||
|
||||
@classmethod
|
||||
def _merge_body(cls, request, request_delta):
|
||||
request.body = deep_merge(request_delta, request.body)
|
||||
|
||||
# Without this, sqlalchemy won't notice the change to request.body,
|
||||
# since it doesn't track dictionary mutations by default.
|
||||
flag_modified(request, "body")
|
||||
|
||||
return request
|
||||
return deep_merge(request_delta, request.body)
|
||||
|
||||
@classmethod
|
||||
def approve_and_create_workspace(cls, request):
|
||||
@@ -264,12 +259,7 @@ WHERE requests_with_status.status = :status
|
||||
if task_order:
|
||||
request.task_order = task_order
|
||||
|
||||
request = Requests._merge_body(
|
||||
request, {"financial_verification": request_data}
|
||||
)
|
||||
|
||||
db.session.add(request)
|
||||
db.session.commit()
|
||||
request = Requests.update(request.id, {"financial_verification": request_data})
|
||||
|
||||
return request
|
||||
|
||||
|
@@ -16,7 +16,7 @@ class DateField(DateField):
|
||||
if values:
|
||||
self.data = values[0]
|
||||
else:
|
||||
self.data = []
|
||||
self.data = None
|
||||
|
||||
|
||||
class NewlineListField(Field):
|
||||
|
@@ -7,7 +7,23 @@ import pendulum
|
||||
from atst.models import Base
|
||||
from atst.models.types import Id
|
||||
from atst.models.request_status_event import RequestStatus
|
||||
from atst.utils import first_or_none
|
||||
from atst.utils import deep_merge, first_or_none
|
||||
|
||||
|
||||
def map_properties_to_dict(properties, instance):
|
||||
return {
|
||||
field: getattr(instance, field)
|
||||
for field in properties
|
||||
if getattr(instance, field) is not None
|
||||
}
|
||||
|
||||
|
||||
def update_dict_with_properties(instance, body, top_level_key, properties):
|
||||
new_properties = map_properties_to_dict(properties, instance)
|
||||
if new_properties:
|
||||
body[top_level_key] = new_properties
|
||||
|
||||
return body
|
||||
|
||||
|
||||
class Request(Base):
|
||||
@@ -28,7 +44,77 @@ class Request(Base):
|
||||
task_order_id = Column(ForeignKey("task_order.id"))
|
||||
task_order = relationship("TaskOrder")
|
||||
|
||||
revisions = relationship("RequestRevision", back_populates="request")
|
||||
revisions = relationship(
|
||||
"RequestRevision", back_populates="request", order_by="RequestRevision.sequence"
|
||||
)
|
||||
|
||||
@property
|
||||
def latest_revision(self):
|
||||
if self.revisions:
|
||||
return self.revisions[-1]
|
||||
|
||||
else:
|
||||
return RequestRevision(request=self)
|
||||
|
||||
PRIMARY_POC_FIELDS = ["am_poc", "dodid_poc", "email_poc", "fname_poc", "lname_poc"]
|
||||
DETAILS_OF_USE_FIELDS = [
|
||||
"jedi_usage",
|
||||
"start_date",
|
||||
"cloud_native",
|
||||
"dollar_value",
|
||||
"dod_component",
|
||||
"data_transfers",
|
||||
"expected_completion_date",
|
||||
"jedi_migration",
|
||||
"num_software_systems",
|
||||
"number_user_sessions",
|
||||
"average_daily_traffic",
|
||||
"engineering_assessment",
|
||||
"technical_support_team",
|
||||
"estimated_monthly_spend",
|
||||
"average_daily_traffic_gb",
|
||||
"rationalization_software_systems",
|
||||
"organization_providing_assistance",
|
||||
]
|
||||
INFORMATION_ABOUT_YOU_FIELDS = [
|
||||
"citizenship",
|
||||
"designation",
|
||||
"phone_number",
|
||||
"email_request",
|
||||
"fname_request",
|
||||
"lname_request",
|
||||
"service_branch",
|
||||
"date_latest_training",
|
||||
]
|
||||
FINANCIAL_VERIFICATION_FIELDS = [
|
||||
"pe_id",
|
||||
"task_order_number",
|
||||
"fname_co",
|
||||
"lname_co",
|
||||
"email_co",
|
||||
"office_co",
|
||||
"fname_cor",
|
||||
"lname_cor",
|
||||
"email_cor",
|
||||
"office_cor",
|
||||
"uii_ids",
|
||||
"treasury_code",
|
||||
"ba_code",
|
||||
]
|
||||
|
||||
@property
|
||||
def body(self):
|
||||
current = self.latest_revision
|
||||
body = {}
|
||||
for top_level_key, properties in [
|
||||
("primary_poc", Request.PRIMARY_POC_FIELDS),
|
||||
("details_of_use", Request.DETAILS_OF_USE_FIELDS),
|
||||
("information_about_you", Request.INFORMATION_ABOUT_YOU_FIELDS),
|
||||
("financial_verification", Request.FINANCIAL_VERIFICATION_FIELDS),
|
||||
]:
|
||||
body = update_dict_with_properties(current, body, top_level_key, properties)
|
||||
|
||||
return body
|
||||
|
||||
@property
|
||||
def status(self):
|
||||
@@ -40,7 +126,7 @@ class Request(Base):
|
||||
|
||||
@property
|
||||
def annual_spend(self):
|
||||
monthly = self.body.get("details_of_use", {}).get("estimated_monthly_spend", 0)
|
||||
monthly = self.latest_revision.estimated_monthly_spend or 0
|
||||
return monthly * 12
|
||||
|
||||
@property
|
||||
|
@@ -1,5 +1,15 @@
|
||||
import pendulum
|
||||
from sqlalchemy import Column, func, ForeignKey, String, Boolean, Integer, Date
|
||||
from sqlalchemy import (
|
||||
Column,
|
||||
func,
|
||||
ForeignKey,
|
||||
String,
|
||||
Boolean,
|
||||
Integer,
|
||||
Date,
|
||||
BigInteger,
|
||||
Sequence,
|
||||
)
|
||||
from sqlalchemy.types import DateTime
|
||||
from sqlalchemy.dialects.postgresql import JSONB
|
||||
from sqlalchemy.orm import relationship
|
||||
@@ -15,6 +25,9 @@ class RequestRevision(Base, TimestampsMixin):
|
||||
id = Id()
|
||||
request_id = Column(ForeignKey("requests.id"), nullable=False)
|
||||
request = relationship("Request", back_populates="revisions")
|
||||
sequence = Column(
|
||||
BigInteger, Sequence("request_revisions_sequence_seq"), nullable=False
|
||||
)
|
||||
|
||||
# primary_poc
|
||||
am_poc = Column(Boolean, default=False)
|
||||
@@ -26,20 +39,20 @@ class RequestRevision(Base, TimestampsMixin):
|
||||
# details_of_use
|
||||
jedi_usage = Column(String)
|
||||
start_date = Column(Date())
|
||||
cloud_native = Column(Boolean)
|
||||
cloud_native = Column(String)
|
||||
dollar_value = Column(Integer)
|
||||
dod_component = Column(String)
|
||||
data_transfers = Column(String)
|
||||
expected_completion_date = Column(String)
|
||||
jedi_migration = Column(Boolean)
|
||||
jedi_migration = Column(String)
|
||||
num_software_systems = Column(Integer)
|
||||
number_user_sessions = Column(Integer)
|
||||
average_daily_traffic = Column(Integer)
|
||||
engineering_assessment = Column(Boolean)
|
||||
technical_support_team = Column(Boolean)
|
||||
engineering_assessment = Column(String)
|
||||
technical_support_team = Column(String)
|
||||
estimated_monthly_spend = Column(Integer)
|
||||
average_daily_traffic_gb = Column(Integer)
|
||||
rationalization_software_systems = Column(Boolean)
|
||||
rationalization_software_systems = Column(String)
|
||||
organization_providing_assistance = Column(String)
|
||||
|
||||
# information_about_you
|
||||
@@ -66,13 +79,3 @@ class RequestRevision(Base, TimestampsMixin):
|
||||
uii_ids = Column(String)
|
||||
treasury_code = Column(String)
|
||||
ba_code = Column(String)
|
||||
|
||||
_BOOLS = ["am_poc", "jedi_migration", "engineering_assessment", "technical_support_team", "rationalization_software_systems", "cloud_native"]
|
||||
_TIMESTAMPS = ["start_date", "date_latest_training"]
|
||||
|
||||
@classmethod
|
||||
def create_from_request_body(cls, request, **body):
|
||||
coerced_bools = {k: v == "yes" for k,v in body.items() if k in RequestRevision._BOOLS}
|
||||
coerced_timestamps = {k: pendulum.parse(v) for k,v in body.items() if k in RequestRevision._TIMESTAMPS}
|
||||
body = {**body, **coerced_bools, **coerced_timestamps}
|
||||
return RequestRevision(request=request, **body)
|
||||
|
@@ -1,2 +1,20 @@
|
||||
def first_or_none(predicate, lst):
|
||||
return next((x for x in lst if predicate(x)), None)
|
||||
|
||||
|
||||
def deep_merge(source, destination: dict):
|
||||
"""
|
||||
Merge source dict into destination dict recursively.
|
||||
"""
|
||||
|
||||
def _deep_merge(a, b):
|
||||
for key, value in a.items():
|
||||
if isinstance(value, dict):
|
||||
node = b.setdefault(key, {})
|
||||
_deep_merge(value, node)
|
||||
else:
|
||||
b[key] = value
|
||||
|
||||
return b
|
||||
|
||||
return _deep_merge(source, dict(destination))
|
||||
|
Reference in New Issue
Block a user