Skip to content

Commit

Permalink
enh #391 - initial deployment value change tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
sbatchelder committed Jun 10, 2021
1 parent 05aacb8 commit 6566136
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 168 deletions.
166 changes: 62 additions & 104 deletions roundabout/builds/views_deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,18 @@
from django.shortcuts import render, get_object_or_404
from django.urls import reverse, reverse_lazy
from django.http import HttpResponseRedirect, HttpResponse, JsonResponse
from django.views.generic import (
View,
DetailView,
ListView,
RedirectView,
UpdateView,
CreateView,
DeleteView,
TemplateView,
FormView,
)
from django.views.generic import View, DetailView, ListView, RedirectView, UpdateView, CreateView, DeleteView, TemplateView, FormView
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin

from common.util.mixins import AjaxFormMixin
from .models import Build, BuildAction
from .forms import *
from roundabout.locations.models import Location
from roundabout.inventory.models import (
Inventory,
Action,
Deployment,
DeploymentAction,
InventoryDeployment,
)
from roundabout.inventory.models import Inventory, Action, Deployment, DeploymentAction, InventoryDeployment
from roundabout.inventory.utils import _create_action_history
from roundabout.calibrations.utils import handle_reviewers

# Get the app label names from the core utility functions
from roundabout.core.utils import set_app_labels

labels = set_app_labels()

## CBV views for Deployments as part of Builds app ##
Expand Down Expand Up @@ -90,13 +72,21 @@ def form_valid(self, form):
self.object.deployment_start_date = action_date
self.object.save()

# Create Deployment Action record
data = dict(updated_values=dict())
for field in form.fields:
val = getattr(self.object, field, None)
if val:
data["updated_values"][field] = {"from": None, "to": str(val)}
#_create_action_history(self.object, Action.ADD, self.request.user, data=data)

# Update the Build instance to match any Deployment changes
build = self.object.build
build.location = self.object.location
build.is_deployed = True
build.save()
# Create Build Action record for deployment
_create_action_history(build, action_type, self.request.user)
_create_action_history(build, action_type, self.request.user, data=data)

# Get all Inventory items on Build, match location and add Action
inventory_items = build.inventory.all()
Expand Down Expand Up @@ -157,7 +147,6 @@ def _update_actions(self, obj_to_update=None, action_to_update=None):

actions = obj_to_update.get_actions()
actions = actions.filter(action_type__in=actions_list)

for action in actions:
if (
action.action_type == Action.STARTDEPLOYMENT
Expand Down Expand Up @@ -189,34 +178,33 @@ def _update_actions(self, obj_to_update=None, action_to_update=None):
):
action.created_at = obj_to_copy.deployment_retire_date
action.save()

return actions

def form_valid(self, form):
action_type = Action.DEPLOYMENTDETAILS

# Update Deployment Action Record
previous_deployment = Deployment.objects.get(id=self.object.pk)
new_deployment = form.save(commit=False)
data = dict(updated_values=dict())
for field in form.fields:
orig_val = getattr(previous_deployment, field, None)
new_val = getattr(new_deployment, field, None)
if orig_val != new_val:
data["updated_values"][field] = { "from":str(orig_val), "to":str(new_val) }
form.instance.approved = False
form.save()
self.object = form.save(commit=True)
handle_reviewers(form)
self.object = form.save()
self.object.build.detail = "%s Details changed." % (
self.object.deployment_number
)
self.object.build.save()
#_create_action_history(self.object, Action.UPDATE, self.request.user, data=data)

# Create Build Action record for deployment
build_record = _create_action_history(
self.object.build,
action_type,
self.request.user,
)
self.object.build.detail = "%s Details changed." % (self.object.deployment_number)
self.object.build.save()
action_type = Action.DEPLOYMENTDETAILS
build_record = _create_action_history(self.object.build, action_type, self.request.user, data=data)

# can only associate one cruise with an action, so for deployment detail change, only show changed cruise value
cruise_deployed_change = (
previous_deployment.cruise_deployed != self.object.cruise_deployed
)
cruise_recovered_change = (
previous_deployment.cruise_recovered != self.object.cruise_recovered
)
cruise_deployed_change = previous_deployment.cruise_deployed != self.object.cruise_deployed
cruise_recovered_change = previous_deployment.cruise_recovered != self.object.cruise_recovered
if cruise_deployed_change and cruise_recovered_change:
pass # can't record both so record neither
elif cruise_deployed_change:
Expand All @@ -234,54 +222,29 @@ def form_valid(self, form):
inventory_deployments = self.object.inventory_deployments.all()

for inventory_deployment in inventory_deployments:
if inventory_deployment.deployment_start_date.replace(
second=0, microsecond=0
) == previous_deployment.deployment_start_date.replace(
second=0, microsecond=0
):
inventory_deployment.deployment_start_date = (
self.object.deployment_start_date
)
if inventory_deployment.deployment_start_date.replace(second=0, microsecond=0) == \
previous_deployment.deployment_start_date.replace(second=0, microsecond=0):
inventory_deployment.deployment_start_date = self.object.deployment_start_date
# Update Deployment Action items to match any date changes
self._update_actions(inventory_deployment, Action.STARTDEPLOYMENT)

if (
inventory_deployment.deployment_burnin_date
== previous_deployment.deployment_burnin_date
):
inventory_deployment.deployment_burnin_date = (
self.object.deployment_burnin_date
)
if inventory_deployment.deployment_burnin_date == previous_deployment.deployment_burnin_date:
inventory_deployment.deployment_burnin_date = self.object.deployment_burnin_date
# Update Deployment Action items to match any date changes
self._update_actions(inventory_deployment, Action.DEPLOYMENTBURNIN)

if (
inventory_deployment.deployment_to_field_date
== previous_deployment.deployment_to_field_date
):
inventory_deployment.deployment_to_field_date = (
self.object.deployment_to_field_date
)
if inventory_deployment.deployment_to_field_date == previous_deployment.deployment_to_field_date:
inventory_deployment.deployment_to_field_date = self.object.deployment_to_field_date
# Update Deployment Action items to match any date changes
self._update_actions(inventory_deployment, Action.DEPLOYMENTTOFIELD)

if (
inventory_deployment.deployment_recovery_date
== previous_deployment.deployment_recovery_date
):
inventory_deployment.deployment_recovery_date = (
self.object.deployment_recovery_date
)
if inventory_deployment.deployment_recovery_date == previous_deployment.deployment_recovery_date:
inventory_deployment.deployment_recovery_date = self.object.deployment_recovery_date
# Update Deployment Action items to match any date changes
self._update_actions(inventory_deployment, Action.DEPLOYMENTRECOVER)

if (
inventory_deployment.deployment_retire_date
== previous_deployment.deployment_retire_date
):
inventory_deployment.deployment_retire_date = (
self.object.deployment_retire_date
)
if inventory_deployment.deployment_retire_date == previous_deployment.deployment_retire_date:
inventory_deployment.deployment_retire_date = self.object.deployment_retire_date
# Update Deployment Action items to match any date changes
self._update_actions(inventory_deployment, Action.DEPLOYMENTRETIRE)

Expand All @@ -305,9 +268,7 @@ def get_success_url(self):
class DeploymentAjaxActionView(DeploymentAjaxUpdateView):
def get_context_data(self, **kwargs):
context = super(DeploymentAjaxActionView, self).get_context_data(**kwargs)
latest_action_record = (
self.object.build.get_actions().filter(deployment=self.object).first()
)
latest_action_record = self.object.build.get_actions().filter(deployment=self.object).first()

context.update({"latest_action_record": latest_action_record})
return context
Expand All @@ -326,28 +287,31 @@ def get_form_class(self):
return form_class_name

def form_valid(self, form):
self.object = form.save()
action_type = self.kwargs["action_type"]
action_date = form.cleaned_data["date"]

# Update Deployment Action Record
previous_deployment = Deployment.objects.get(id=self.object.pk)
new_deployment = form.save(commit=False)
data = dict(updated_values=dict())
for field in form.fields:
orig_val = getattr(previous_deployment, field, None)
new_val = getattr(new_deployment, field, None)
if orig_val != new_val:
data["updated_values"][field] = { "from":str(orig_val), "to":str(new_val) }
self.object = form.save(commit=True)
#_create_action_history(self.object, Action.UPDATE, self.request.user, data=data)

action_type = self.kwargs['action_type']
action_date = form.cleaned_data['date']
# Set Detail and action_type variables
if action_type == Action.DEPLOYMENTBURNIN:
self.object.detail = "%s Burn In initiated at %s. " % (
self.object.deployment_number,
self.object.location,
)
self.object.detail = "%s Burn In initiated at %s. " % (self.object.deployment_number, self.object.location)
self.object.deployment_burnin_date = action_date
if action_type == Action.DEPLOYMENTTOFIELD:
self.object.detail = "%s Deployed to Field: %s. " % (
self.object.deployment_number,
self.object.location,
)
self.object.detail = "%s Deployed to Field: %s. " % (self.object.deployment_number, self.object.location)
self.object.deployment_to_field_date = action_date
self.object.deployed_location = self.object.location
if action_type == Action.DEPLOYMENTRECOVER:
self.object.detail = "%s Recovered to: %s. " % (
self.object.deployment_number,
self.object.location,
)
self.object.detail = "%s Recovered to: %s. " % (self.object.deployment_number, self.object.location)
self.object.deployment_recovery_date = action_date
if action_type == Action.DEPLOYMENTRETIRE:
self.object.detail = "%s Ended." % (self.object.deployment_number)
Expand All @@ -368,12 +332,8 @@ def form_valid(self, form):

build.save()
# Create Build Action record for deployment
build_record = _create_action_history(
build, action_type, self.request.user, None, "", action_date
)
build_record.cruise = (
self.object.cruise_recovered or self.object.cruise_deployed
)
build_record = _create_action_history(build, action_type, self.request.user, None, "", action_date, data=data)
build_record.cruise = self.object.cruise_recovered or self.object.cruise_deployed
build_record.save()

"""
Expand Down Expand Up @@ -411,9 +371,7 @@ def form_valid(self, form):
for item in inventory_items:
item.location = build.location
item.save()
_create_action_history(
item, action_type, self.request.user, build, "", action_date
)
_create_action_history(item, action_type, self.request.user, build, "", action_date)

response = HttpResponseRedirect(self.get_success_url())

Expand Down
23 changes: 21 additions & 2 deletions roundabout/core/updaters.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from roundabout.inventory.models import Inventory, Action, DeploymentAction, Deployment, InventoryDeployment
from roundabout.builds.models import Build, BuildAction
from roundabout.cruises.models import Cruise, Vessel
from roundabout.exports.views import ExportDeployments
from roundabout.inventory.utils import _create_action_history

def remove_extra_actions():
Expand Down Expand Up @@ -52,8 +53,8 @@ def _update_action_data():
_update_action_data_CalEvts()
print('\nCONFEVENTS')
_update_action_data_ConfEvts()
#print('\nDEPLOYMENTS')
#_update_action_data_deployments()
print('\nDEPLOYMENTS')
_update_action_data_deployments()

def _update_action_data_vessels():
vessels = Vessel.objects.filter(actions__isnull=True)
Expand Down Expand Up @@ -118,6 +119,24 @@ def _update_action_data_ConfEvts():
action.data = data
action.save()

def _update_action_data_deployments():
deployments = Deployment.objects.all()
fields = [a for h,a in ExportDeployments.header_att if a]
print('fields:',','.join(fields))
any_updates = False
for deployment in deployments:
actions = Action.objects.filter(deployment__pk=deployment.pk).exclude(data__isnull=True)
if actions.exists(): continue # this deployment already has some Action data, skip
else: any_updates = True
print(' ',deployment)
updated_values=dict()
for field in fields:
val = getattr(deployment, field, None)
if val: updated_values[field] = {"from": None, "to": str(val)}
data = dict(updated_values=updated_values) if updated_values else None
_create_action_history(deployment, Action.UPDATE, user=None, data=data)
if any_updates == False:
print(' No Deployments to update')

# Functions to update legacy content to match new model updates
# ------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 6566136

Please sign in to comment.