Skip to content
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
38 changes: 38 additions & 0 deletions fec/data/templatetags/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re

from dateutil.parser import parse as parse_date
from difflib import SequenceMatcher

from django.conf import settings
from django_jinja import library
Expand Down Expand Up @@ -164,6 +165,43 @@ def filesize(value):
return '%d %s' % (value, units[unit])


@library.filter
def compare(string, string_1):
"""Compares two strings to determine if they are nearly identical or semantically similar.
Returns False if `string` is >= than 50% similar to `string_1`. Else returns True.
Uses difflib.SequenceMatcher: https://docs.python.org/3/library/difflib.html
"""

# Calculate the similarity ratio
similarity_ratio = SequenceMatcher(None, string, string_1).ratio()

# Define a threshold for redundancy (e.g., 50% similar)
threshold = .5

if similarity_ratio >= threshold:
return False

else:
return True


@library.filter
def compare_commenter(string, string_1):
"""Convert `string_1` from "LastName, FirstName" format to "FirstName LastName"
Returns False if `converted_string_1` is in `string`.
Returns False if `string` == 'Comment'.
Else returns True.
"""

# Convert name from "LastName, FirstName" format to "FirstName LastName"
converted_string_1 = ' '.join(reversed(string_1.split(', ')))

if converted_string_1 in string or string == 'Comment':
return False
else:
return True


@library.global_function
def path_for_css(key):
"""Looks up the hashed asset key in rev-manifest-css.json
Expand Down
28 changes: 16 additions & 12 deletions fec/legal/templates/rulemaking.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,15 @@
<ul>
<li>
{%- if docL1.url -%}
<a class="embed-pdf" href="{{ CANONICAL_BASE }}{{ docL1.url }}" data-doc_id="{{ docL1.doc_id }}" target="_blank">{{ docL1.label }}</a>
<a class="embed-pdf" href="{{ CANONICAL_BASE }}{{ docL1.url }}" data-doc_id="{{ docL1.doc_id }}" target="_blank">{{ docL1.label }}</a>
{%- else -%}
{{ docL1.label }}
{{ docL1.label }}
{%- endif -%}
{%- if docL1.doc_description and 'pdf' not in docL1.doc_description.lower() -%}
<br>{{ docL1.doc_description }}
{%- if docL1.doc_description and 'pdf' not in docL1.doc_description.lower() and docL1.doc_description | compare(docL1.label) -%}
<br>{{ docL1.doc_description }}
{%- endif -%}
</li>
{%- if docL1.doc_entities -%}

{%- for entity in docL1.doc_entities -%}
<li class="indent">{{ entity.name }}, {{ entity.role }}</li>
{%- endfor -%}
Expand All @@ -105,7 +104,7 @@
<ul>
{% if (doc.label == 'Comments' or doc.label == 'Hearing Schedule') and doc.doc_entities %}
{% if doc.label == 'Hearing Schedule' %}
<li><a class="embed-pdf" href="{{ CANONICAL_BASE }}{{ doc.url }}" data-doc_id="{{ doc.doc_id }}" target="_blank">{{ doc.label }}</a></li>
<li><a class="embed-pdf" href="{{ CANONICAL_BASE }}{{ doc.url }}" data-doc_id="{{ doc.doc_id }}" target="_blank">{{ doc.label }}</a></li>
{% for entity in doc.doc_entities %}
<li class="indent">{{ entity.name }}, {{ entity.role }}</li>
{% endfor %}
Expand All @@ -114,25 +113,30 @@
{% if doc.commenters|length == 1 %}
{# Single commenter: show commenter name #}
<li>
<a class="embed-pdf" href="{{ CANONICAL_BASE }}{{ doc.url }}" data-doc_id="{{ doc.doc_id }}" target="_blank">{{ doc.commenters[0].name }} | {{ doc.commenters[0].role }}</a>
{% if doc.doc_description and 'pdf' not in doc.doc_description.lower() %}<br>{{ doc.doc_description }}{% endif %}
<a class="embed-pdf" href="{{ CANONICAL_BASE }}{{ doc.url }}" data-doc_id="{{ doc.doc_id }}" target="_blank">{{ doc.commenters[0].name }} | {{ doc.commenters[0].role }}</a>
{% if doc.doc_description and 'pdf' not in doc.doc_description.lower() and doc.doc_description | compare_commenter(doc.commenters[0].name) %}
<br>{{ doc.doc_description }}
{% endif %}
</li>
{% for entity in doc.doc_entities %}
{% for entity in doc.doc_entities if entity.role != 'Commenter' %}
<li class="indent">{{ entity.name }}, {{ entity.role }}</li>
{% endfor %}
{% else %}
{# Multiple commenters: show "Joint comment" #}
<li><a class="embed-pdf" href="{{ CANONICAL_BASE }}{{ doc.url }}" data-doc_id="{{ doc.doc_id }}" target="_blank">Joint comment</a>
{% if doc.doc_description and 'pdf' not in doc.doc_description.lower() %}<br>{{ doc.doc_description }}{% endif %}</li>
{% if doc.doc_description and 'pdf' not in doc.doc_description.lower() %}
<br>{{ doc.doc_description }}
{% endif %}
</li>
{% for entity in doc.doc_entities %}
<li class="indent">{{ entity.name }}, {{ entity.role }}</li>
{% endfor %}
{% endif %}
{% endif %}
{% else %}
<a class="embed-pdf" href="{{ CANONICAL_BASE }}{{ doc.url }}" data-doc_id="{{ doc.doc_id }}" target="_blank">{{ doc.label }}</a>
{% if doc.doc_description and 'pdf' not in doc.doc_description.lower() %}
<br>{{ doc.doc_description }}
{% if doc.doc_description and 'pdf' not in doc.doc_description.lower() and doc.doc_description | compare(doc.label) %}
<br>{{ doc.doc_description }}
{% endif %}
{% endif %}
</ul>
Expand Down