Skip to content

Commit

Permalink
Adds sorting functionality for front end and loads initial data on we…
Browse files Browse the repository at this point in the history
…b root
  • Loading branch information
gdbarnes committed Oct 17, 2024
1 parent b422c49 commit 4171fa2
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
18 changes: 18 additions & 0 deletions orp/core/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,21 @@ class RegulationSearchForm(forms.Form):
("departmentfortransport", "Department for Transport"),
],
)

sort = forms.ChoiceField(
# initial="relevance",
required=False,
label="Sort by",
choices=[
("recent", "Recently updated"),
("relevance", "Relevance"),
],
widget=forms.Select(
attrs={
"class": "govuk-select",
"id": "sort",
"name": "sort",
"onChange": "form.submit()",
}
),
)
4 changes: 2 additions & 2 deletions orp/orp_search/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def validate(self):
return False

if self.sort_by:
if self.sort_by not in ["recently", "relevance"]:
logger.error("sort_by must be 'recently' or 'relevance'")
if self.sort_by not in ["recent", "relevance"]:
logger.error("sort_by must be 'recent' or 'relevance'")
return False
return True
12 changes: 6 additions & 6 deletions orp/orp_search/public_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,16 @@ def search(self, config: SearchDocumentConfig):

sorted_df = None

if config.sort_by == "recently":
if config.sort_by == "recent":
# Sort the DataFrame by 'date_modified' in descending order
# Ensure 'date_issued' is in datetime format
filtered_df["date_issued"] = pd.to_datetime(
filtered_df["date_issued"], format="%d/%m/%Y"
# Ensure 'date_modified' is in datetime format
filtered_df["date_modified"] = pd.to_datetime(
filtered_df["date_modified"], format="%d/%m/%Y"
)

# Sort the DataFrame by 'date_issued' in descending order
# Sort the DataFrame by 'date_modified' in descending order
sorted_df = filtered_df.sort_values(
by="date_issued", ascending=False
by="date_modified", ascending=False
)
elif config.sort_by == "relevance":
# Calculate relevance score
Expand Down
31 changes: 11 additions & 20 deletions orp/orp_search/templates/orp.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,12 @@ <h1 class="govuk-heading-xl govuk-!-margin-bottom-4">Find laws and regulations f
<p class="govuk-body govuk-!-margin-bottom-9">You can also use this dataset and its metadata as an application
programming interface
(API).</p>

{% if truncated %}
<div class="govuk-warning-text">
<span class="govuk-warning-text__icon" aria-hidden="true">!</span>
<strong class="govuk-warning-text__text">
Some matches may be missing. Try a more specific search term.
</strong>
</div>
{% endif %}
</div>
</div>

<form method="get" class="search-form">
<div class="govuk-grid-row">
<div class="govuk-grid-column-one-third">
<form method="get" class="search-form">
<div class="govuk-form-group {% if form.search.errors %}govuk-form-group--error{% endif %} search-group">
<label class="govuk-label" for="{{ form.search.id_for_label }}">
{{ form.search.label }}
Expand Down Expand Up @@ -92,20 +83,23 @@ <h2 class="govuk-fieldset__heading">
Published by
</h2>
</legend>

<!-- JAVASCRIPT ENHANCEMENT -->
<!-- <div id="status-hint" class="govuk-hint govuk-!-font-size-14">
1 selected
</div> -->
<!-- <div class="govuk-form-group {% if form.search.errors %}govuk-form-group--error{% endif %} search-group"> -->
<!-- <label class="govuk-label govuk-visually-hidden" for="{{ form.search.id_for_label }}">
Search by publisher
</label> -->
<!-- The below will be added as a JavaScript enhancement -->
<!-- <div class="search-input-button search-input-button--black orp-publisher-search">
<input class="govuk-input orp-publisher-search__input app-site-search__input--default"
name="input-autocomplete" placeholder="Search" type="text" role="combobox">
</div> -->
<!-- </div> -->
<!-- <div class="govuk-checkboxes govuk-checkboxes--small orp-max-height-250" data-module="govuk-checkboxes"> -->
<!-- /JAVASCRIPT ENHANCEMENT -->

<div class="govuk-checkboxes govuk-checkboxes--small" data-module="govuk-checkboxes">
<div class="govuk-checkboxes__item">
<input class="govuk-checkboxes__input" id="publisher" name="publisher" type="checkbox"
Expand Down Expand Up @@ -197,7 +191,7 @@ <h2 class="govuk-fieldset__heading">
class="govuk-link govuk-link--no-visited-state govuk-!-float-right">Download search results as CSV
file</a>
</p>
</form>
{% comment %} </form> {% endcomment %}
</div>

<div class="govuk-grid-column-two-thirds">
Expand All @@ -216,7 +210,7 @@ <h2 class="govuk-fieldset__heading">
</p>
</div>

<!-- Commenting out applied filters for now to avoid confusion -->
<!-- COMMENTING OUT APPLIED FILTERS HEADER FOR NOW -->
<!-- <ul class="orp-applied-filters-container">
<li class="orp-applied-filter-tag">
<a href="#delete">
Expand All @@ -238,13 +232,10 @@ <h2 class="govuk-fieldset__heading">
<div class="govuk-grid-row">
<div class="govuk-grid-column-full">
<div class="orp-flex">
<label class="govuk-label govuk-!-margin-right-3 orp-!-no-text-wrap" for="sort">
Sort by
<label class="govuk-label govuk-!-margin-right-3 orp-!-no-text-wrap" for="{{ form.search.id_for_label }}">
{{ form.sort.label }}
</label>
<select class="govuk-select" id="sort" name="sort">
<option value="date">Recently updated</option>
<option value="sort">Relevance</option>
</select>
{{ form.sort }}
</div>
</div>
</div>
Expand Down Expand Up @@ -357,14 +348,14 @@ <h3 class="govuk-heading-m">Related searches</h2>
<li class="govuk-body">
<a class="govuk-link" href="/?search=aggregate+use">aggregate use</a>
</li>

</ul>
{% endif %}
{% endif %}
</div>
</div>
</div>
</div>
</form>
</div>
{% endif %}
</div>
Expand Down
2 changes: 1 addition & 1 deletion orp/orp_search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def search(request: HttpRequest) -> HttpResponse:
}

# Create a new instance of the RegulationSearchForm
form = RegulationSearchForm(request.GET or None)
form = RegulationSearchForm(request.GET)

# If the form is not valid, return the form
if not form.is_valid():
Expand Down

0 comments on commit 4171fa2

Please sign in to comment.