Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendered Jinja params for system props in control implementation statements #3

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
GovReady-Q Release Notes
========================

v0.xx.xx (December 10, 2023)
---------------------------

**Feature changes**

* Support dynamically rendered Jinja parameters for system properties in control implementation statements.

**UI changes**

* Display rendered parameters for system properties in system control implementation statements.

**Developer changes**

* Add statement.body_rendered property containing jinja rendered content in system control implementation statements.

v0.11.8 (June 11, 2023)
---------------------------

Expand All @@ -24,9 +39,6 @@ v0.11.6 (March 14, 2023)
**Developer changes**

* Upgrade Python libraries.

**Developer changes**

* Add siteapp.management_views as webhooks for calling Django management commands.


Expand Down
22 changes: 21 additions & 1 deletion controls/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from django.db import transaction
from django.core.validators import RegexValidator
from django.core.validators import validate_email
from jinja2 import Environment, DictLoader

import structlog
from structlog import get_logger
Expand Down Expand Up @@ -881,7 +882,16 @@ def combined_smt_partial(smt):
# print(f"self.pid_current: {self.pid_current} XXXXXXXXXXXXX") # DEBUG
# self.pid_current = smt.pid
if smt.producer_element:
smt_formatted = smt.body.replace('\n','<br/>')
#smt_formatted = smt.body.replace('\n','<br/>')
# Add 'body_rendered' property to smt containing rendered version of smt.body in case we have embedded parameters
# System is available here as `self`
smt_env = Environment(loader=DictLoader({'smt_body_template': smt.body}))
template = smt_env.get_template('smt_body_template')
try:
smt.body_rendered = template.render(system=self)
except:
smt.body_rendered = "<ERROR: incorrect jinja variable>\n" + smt.body
smt_formatted = smt.body_rendered.replace('\n','<br/>')
# TODO: Clean up special characters
smt_formatted = smt_formatted.replace(u"\u2019", "'").replace(u"\u2022", "<li>")
# Poor performance, at least in some instances, appears to being caused by `smt.producer_element.name`
Expand Down Expand Up @@ -1011,6 +1021,16 @@ def add_event(self, event_type, description, info={}):

return se

def answer(self, module='speedy_ssp_basic_info',answer='internal_customer'):
# To Do:
try:
module_question_answer = self.projects.all()[0].export_json()['project']['answers'][module]['value']['answers'][answer]['text']
return module_question_answer
except:
return f"<missing param: {module} or {answer}>"



class SystemEvent(auto_prefetch.Model, TagModelMixin, BaseModel):
system = auto_prefetch.ForeignKey('System', related_name='events', on_delete=models.CASCADE, blank=True,
null=True, help_text="Events related to the system")
Expand Down
14 changes: 14 additions & 0 deletions controls/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from django.db.models.functions import Lower
from django.http import Http404, HttpResponse, HttpResponseRedirect, HttpResponseForbidden, JsonResponse, \
HttpResponseNotAllowed
from jinja2 import Environment, DictLoader
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse
from django.utils.text import slugify
Expand Down Expand Up @@ -2285,6 +2286,19 @@ def editor(request, system_id, catalog_key, cl_id):
# need parties and roles to not be empty
# Build OSCAL SSP
# Example: https://github.com/usnistgov/oscal-content/tree/master/examples/ssp/json/ssp-example.json

# Add 'body_rendered' property to smt containing rendered version of smt.body in case we have embedded parameters
# Get the system
system = System.objects.get(pk=system_id)
for smt in impl_smts:
smt_env = Environment(loader=DictLoader({'smt_body_template': smt.body}))
template = smt_env.get_template('smt_body_template')
try:
smt.body_rendered = template.render(system=system)
except:
smt.body_rendered = "<ERROR: incorrect jinja variable>\n" + smt.body
#rendered = template.render(project=project, system=system)

# oscalize key
cl_id = oscalize_control_id(cl_id)

Expand Down
2 changes: 1 addition & 1 deletion siteapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
url(r"^systems/", include("controls.urls")),
url(r"^api/v1/systems/", include("controls.urls_api")),

url(r"^controls$", include("controls.urls")),
# url(r"^controls$", include("controls.urls")),
url(r"^controls/", include("controls.urls")),

# portfolios
Expand Down
2 changes: 1 addition & 1 deletion templates/controls/editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ <h3>
<span class="component-state">{{ smt.producer_element.component_state }}</span>
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6 col-xl-6 statement-text-block">{% if smt.pid is not None and smt.pid != "" %}<div class="panel-heading-smt">{{ smt.pid }}.</div>{% endif %}{{ smt.body }}</div>
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6 col-xl-6 statement-text-block">{% if smt.pid is not None and smt.pid != "" %}<div class="panel-heading-smt">{{ smt.pid }}.</div>{% endif %}{{ smt.body_rendered }}</div>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 col-xl-3 remark-text-block">
{% spaceless %}
<span>Status: {% if smt.status != "" and smt.status is not None %}{{ smt.status }}{% else %}TBD{% endif %}</span>
Expand Down