<commit_before>

{% extends "cygapp/base.html" %}

{% load i18n %}
{% block title %}{%trans "Policy Management" %}{% endblock %}

{% block hero %}
<h1>{{ title }}</h1>
{% endblock %}


{% block content %}
<div class="container-fluid">
  <div class="row-fluid">
    <div class="span3" id="line">
      <h4>{% trans "Policy" %}
        <div class="pull-right">
          <a class="btn btn-primary btn-mini" href="/policies/add"
          value="addNew" rel="tooltip" title="add new">
            <i class="icon-plus-sign icon-white"></i>
          </a>
        </div>
      </h4>
      <table class="table table-hover">
        <tbody>
          {% for p in policies %}
          <tr><td><a href=/policies/{{ p.id }}>{{ p }}</a></td></tr>
          {% endfor %}
        </tbody>
      </table>
    </div>

    <div class="span9" id="content">
      {% if policy %}
      <h4>{% trans "Policy Info"%}</h4>
      <form class="form-horizontal" method="POST" action="/policies/save" id="policyform">
        {% csrf_token %}
        <input type="hidden" name="enforcementId" id="enforcementId" value="{{enforcement.id}}" />

        <div class="control-group">
          <label class="control-label" for="id">{% trans "Name" %}</label>
          <div class="controls">
            <input type="hidden" name="policyId" value="{{policy.id}}" />
            <input type="text" id="name" name="name" placeholder="{% trans 'Name' %}"
              rel="tooltip" title="{% trans 'Policy Name' %}"
            value="{{ policy.name }}">
          </div>
        </div>

        <div class="control-group">
          <label class="control-label" for="description">{% trans "Type" %}</label> 
          <div class="controls">   
            <select id="type" name="type" onchange="type_change()" rel="tooltip" title="{% trans 'Policy Type' %}">
              <option value="">{% trans 'Select one...' %}</option>
              {% for t in types %}
              <option value="{{ forloop.counter0 }}"
              {% if policy.type == forloop.counter0 %}
              selected="selected"
              {% endif %}
              >
              {{ t }}</option>
              {% endfor %}
            </select>
          </div>
        </div>
        
        <div class="control-group hide" id="file">
          <label class="control-label" for="file2">{% trans "File" %}</label> 
          <div class="controls">   
            <select id="file2" name="file2" rel="tooltip"
              title="{% trans 'File to check #2' %}">
              <option value="">{% trans 'Select one...' %}</option>
              {% for f in files %}
              <option value="{{forloop.counter0}}" {% if policy.file == forloop.counter0 %} selected {% endif %}>{{ f }}</option>
              {% endfor %}
            </select>
          </div>
        </div>
        
        <div class="control-group hide" id="dir">
          <label class="control-label" for="dir2">{% trans "Directory" %}</label> 
          <div class="controls">   
            <select id="dir2" name="dir2" rel="tooltip"
              title="{% trans 'Directory to check #2' %}">
              <option value="">{% trans 'Select one...' %}</option>
              {% for d in dirs %}
              <option value="{{forloop.counter0}}"
              {% if policy.directory == forloop.counter0 %}
              selected="selected"
              {% endif %} 
              >{{ d }}</option>
              {% endfor %}
            </select>
          </div>
        </div>        
        
        <div class="control-group hide" id="port">
          <label class="control-label" for="range2">{% trans "Port Range" %}</label> 
          <div class="controls">   
            <input type="text" id="range2" name="range2" placeholder="{% trans 'Port Range' %}"
              rel="tooltip" title="{% trans 'Comma separated range(s) of ports, f.e: 5,10-1024,2450-4000' %}"
              value="{%if not policy.file%}{%if not policy.dir%}{{policy.argument}}{%endif%}{%endif%}">
            </select>
          </div>
        </div>

        <div class="control-group">
          <label class="control-label" for="fail">{% trans "Fail-Action" %}</label> 
          <div class="controls">   
            <select id="fail" name="fail" rel="tooltip"
              title="{% trans 'Recommendation when a client does not meet the policy requirement' %}">
              <option value="">{% trans 'Select one...' %}</option>
              {% for a in action %}
              <option value="{{forloop.counter0}}"
              {% if policy.fail == forloop.counter0 %}
              selected="selected"
              {% endif %} 
              >{{ a }}</option>
              {% endfor %}
            </select>
          </div>
        </div>

        <div class="control-group">
          <label class="control-label" for="noresult">{% trans "Noresult-Action" %}</label> 
          <div class="controls">   
            <select id="noresult" name="noresult" rel="tooltip"
              title="{% trans 'Recommendation when the policy does not apply to the client' %}">
              <option value="">{% trans 'Select one...' %}</option>
              {% for a in action %}
              <option value="{{forloop.counter0}}"
              {% if policy.noresult == forloop.counter0 %}
              selected="selected"
              {% endif %} 
              >{{ a }}</option>
              {% endfor %}
            </select>
          </div>
        </div>
      </form>

      <hr>

      <div class="btn-toolbar" style="margin: 0;">
        <div class="btn-group">
          <a class="btn btn-success" href="#" onclick="policyform.submit()"><i class="icon-ok icon-white"></i> {%trans "Save" %}</a>
        </div><!-- /btn-group -->
        {% if policy.id %}
        <div class="btn-group">
      <a class="btn btn-danger" href="#delModal" data-toggle="modal"><i class="icon-remove icon-white"></i> Delete</a>
          {% endif %}
        </div><!-- /btn-group -->
      </div><!-- /btn-toolbar-->
      {% endif %}

    </div><!--content-->
  </div><!--/row-->
</div>

<div id="delModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3 id="deleteModalLabel">Are you sure to delete this policy?</h3>
  </div>
  <div class="modal-body">
    <h4>Warning</h4>
    <p>Are you sure you want to delete this policy?</p>
  </div>
  <form method="POST" action="/policies/{{ policy.id }}/delete" id="deleteform">
    {% csrf_token %}
  </form>
  <div class="modal-footer">
    <a class="btn" data-dismiss="modal">Cancel</a>
    <a class="btn btn-danger" onclick="deleteform.submit()" href="#">Delete policy</a>
  </div>
</div>
<script type="text/javascript">

$(document).ready(function() {
    type_change();
});

function type_change(){
  hide_file_selector()
  hide_dir_selector()
  hide_range_input()

  type = $("select#type option:selected").attr("value")
  type ++; //Because empty defaults to 0

  if (type == 1 || type == 5 || type == 6)
    show_file_selector()
  else if (type == 2)
    show_dir_selector()
  else if (type == 3 || type == 4)
    show_range_input()
}

function show_file_selector(){
  $(document.getElementById('file')).show();
}

function show_dir_selector(){
  $(document.getElementById('dir')).show();
}

function show_range_input(){ 
  $(document.getElementById('port')).show();
}

function hide_file_selector(){
  $(document.getElementById('port')).hide();
}

function hide_dir_selector(){
  $(document.getElementById('dir')).hide();
}

function hide_range_input(){
  $(document.getElementById('file')).hide();
}

</script>
{% endblock %}

<commit_msg>refactoring of function type_change() in policy template<commit_after>

{% extends "cygapp/base.html" %}

{% load i18n %}
{% block title %}{%trans "Policy Management" %}{% endblock %}

{% block hero %}
<h1>{{ title }}</h1>
{% endblock %}


{% block content %}
<div class="container-fluid">
  <div class="row-fluid">
    <div class="span3" id="line">
      <h4>{% trans "Policy" %}
        <div class="pull-right">
          <a class="btn btn-primary btn-mini" href="/policies/add"
          value="addNew" rel="tooltip" title="add new">
            <i class="icon-plus-sign icon-white"></i>
          </a>
        </div>
      </h4>
      <table class="table table-hover">
        <tbody>
          {% for p in policies %}
          <tr><td><a href=/policies/{{ p.id }}>{{ p }}</a></td></tr>
          {% endfor %}
        </tbody>
      </table>
    </div>

    <div class="span9" id="content">
      {% if policy %}
      <h4>{% trans "Policy Info"%}</h4>
      <form class="form-horizontal" method="POST" action="/policies/save" id="policyform">
        {% csrf_token %}
        <input type="hidden" name="enforcementId" id="enforcementId" value="{{enforcement.id}}" />

        <div class="control-group">
          <label class="control-label" for="id">{% trans "Name" %}</label>
          <div class="controls">
            <input type="hidden" name="policyId" value="{{policy.id}}" />
            <input type="text" id="name" name="name" placeholder="{% trans 'Name' %}"
              rel="tooltip" title="{% trans 'Policy Name' %}"
            value="{{ policy.name }}">
          </div>
        </div>

        <div class="control-group">
          <label class="control-label" for="description">{% trans "Type" %}</label> 
          <div class="controls">   
            <select id="type" name="type" onchange="type_change()" rel="tooltip" title="{% trans 'Policy Type' %}">
              <option value="">{% trans 'Select one...' %}</option>
              {% for t in types %}
              <option value="{{ forloop.counter0 }}"
              {% if policy.type == forloop.counter0 %}
              selected="selected"
              {% endif %}
              >
              {{ t }}</option>
              {% endfor %}
            </select>
          </div>
        </div>
        
        <div class="control-group hide" id="file">
          <label class="control-label" for="file">{% trans "File" %}</label> 
          <div class="controls">   
            <select id="file" name="file" rel="tooltip"
              title="{% trans 'File to check' %}">
              <option value="">{% trans 'Select one...' %}</option>
              {% for f in files %}
              <option value="{{forloop.counter0}}" {% if policy.file == forloop.counter0 %} selected {% endif %}>{{ f }}</option>
              {% endfor %}
            </select>
          </div>
        </div>
        
        <div class="control-group hide" id="dir">
          <label class="control-label" for="dir">{% trans "Directory" %}</label> 
          <div class="controls">   
            <select id="dir" name="dir" rel="tooltip"
              title="{% trans 'Directory to check' %}">
              <option value="">{% trans 'Select one...' %}</option>
              {% for d in dirs %}
              <option value="{{forloop.counter0}}"
              {% if policy.directory == forloop.counter0 %}
              selected="selected"
              {% endif %} 
              >{{ d }}</option>
              {% endfor %}
            </select>
          </div>
        </div>        
        
        <div class="control-group hide" id="port">
          <label class="control-label" for="range">{% trans "Port Range" %}</label> 
          <div class="controls">   
            <input type="text" id="range" name="range" placeholder="{% trans 'Port Range' %}"
              rel="tooltip" title="{% trans 'Comma separated range(s) of ports, f.e: 5,10-1024,2450-4000' %}"
              value="{%if not policy.file%}{%if not policy.dir%}{{policy.argument|default:""}}{%endif%}{%endif%}">
            </select>
          </div>
        </div>

        <div class="control-group">
          <label class="control-label" for="fail">{% trans "Fail-Action" %}</label> 
          <div class="controls">   
            <select id="fail" name="fail" rel="tooltip"
              title="{% trans 'Recommendation when a client does not meet the policy requirement' %}">
              <option value="">{% trans 'Select one...' %}</option>
              {% for a in action %}
              <option value="{{forloop.counter0}}"
              {% if policy.fail == forloop.counter0 %}
              selected="selected"
              {% endif %} 
              >{{ a }}</option>
              {% endfor %}
            </select>
          </div>
        </div>

        <div class="control-group">
          <label class="control-label" for="noresult">{% trans "Noresult-Action" %}</label> 
          <div class="controls">   
            <select id="noresult" name="noresult" rel="tooltip"
              title="{% trans 'Recommendation when the policy does not apply to the client' %}">
              <option value="">{% trans 'Select one...' %}</option>
              {% for a in action %}
              <option value="{{forloop.counter0}}"
              {% if policy.noresult == forloop.counter0 %}
              selected="selected"
              {% endif %} 
              >{{ a }}</option>
              {% endfor %}
            </select>
          </div>
        </div>
      </form>

      <hr>

      <div class="btn-toolbar" style="margin: 0;">
        <div class="btn-group">
          <a class="btn btn-success" href="#" onclick="policyform.submit()"><i class="icon-ok icon-white"></i> {%trans "Save" %}</a>
        </div><!-- /btn-group -->
        {% if policy.id %}
        <div class="btn-group">
      <a class="btn btn-danger" href="#delModal" data-toggle="modal"><i class="icon-remove icon-white"></i> Delete</a>
          {% endif %}
        </div><!-- /btn-group -->
      </div><!-- /btn-toolbar-->
      {% endif %}
    
    </div><!--content-->
  </div><!--/row-->
</div>

<div id="delModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3 id="deleteModalLabel">Are you sure to delete this policy?</h3>
  </div>
  <div class="modal-body">
    <h4>Warning</h4>
    <p>Are you sure you want to delete this policy?</p>
  </div>
  <form method="POST" action="/policies/{{ policy.id }}/delete" id="deleteform">
    {% csrf_token %}
  </form>
  <div class="modal-footer">
    <a class="btn" data-dismiss="modal">Cancel</a>
    <a class="btn btn-danger" onclick="deleteform.submit()" href="#">Delete policy</a>
  </div>
</div>
<script type="text/javascript">

$(document).ready(function () {
  type_change();
})

function type_change(){
  $(document.getElementById('file')).hide();
  $(document.getElementById('dir')).hide();
  $(document.getElementById('port')).hide();

  type = $("select#type option:selected").attr("value")
  console.log(type);
  //type ++; //Because empty defaults to 0

  if (type == 0 || type == 4 || type == 5)
    $(document.getElementById('file')).show();
  else if (type == 1)
    $(document.getElementById('dir')).show();
  else if (type == 2 || type == 3)
    $(document.getElementById('port')).show();
}

</script>
{% endblock %}


{'fraction_non_alphanumeric': 0.19707552712318036, 'fraction_numerical': 0.006984790129904041, 'mean_word_length': 2.143853888774882, 'pattern_counts': {'":': 0, '<': 309, '<?xml version=': 0, '>': 309, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4769138', 'n_tokens_mistral': 4768, 'n_tokens_neox': 4456, 'n_words': 1069}