Skip to content

Commit 194929a

Browse files
committed
Affected URLs page for excluded patterns working properly
1 parent e89eaa1 commit 194929a

File tree

3 files changed

+157
-30
lines changed

3 files changed

+157
-30
lines changed

sde_collections/views.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151

5252
User = get_user_model()
5353

54+
from django.db.models import BooleanField, Case, Value, When, Q
55+
5456

5557
class CollectionListView(LoginRequiredMixin, ListView):
5658
"""
@@ -235,9 +237,34 @@ class AffectedURLsListView(LoginRequiredMixin, ListView):
235237
context_object_name = "affected_urls"
236238
# paginate_by = 100
237239

240+
# def get_queryset(self):
241+
# self.pattern = ExcludePattern.objects.get(id=self.kwargs["id"])
242+
# queryset = self.pattern.matched_urls()
243+
# return queryset
244+
238245
def get_queryset(self):
246+
# Get the exclude pattern based on the ID from the URL kwargs
239247
self.pattern = ExcludePattern.objects.get(id=self.kwargs["id"])
240-
queryset = self.pattern.matched_urls()
248+
249+
# Get excluded URLs
250+
excluded_urls = self.pattern.matched_urls().annotate(is_included=Value(False, output_field=BooleanField()))
251+
252+
# Get included URLs for the same collection
253+
include_patterns = IncludePattern.objects.filter(collection=self.pattern.collection)
254+
included_urls = CandidateURL.objects.filter(
255+
collection=self.pattern.collection,
256+
id__in=include_patterns.values_list('candidate_urls__id', flat=True)
257+
).distinct()
258+
259+
# Annotate excluded URLs with is_included if they are also in included_urls
260+
queryset = excluded_urls.annotate(
261+
is_included=Case(
262+
When(id__in=included_urls.values('id'), then=Value(True)),
263+
default=Value(False),
264+
output_field=BooleanField()
265+
)
266+
)
267+
241268
return queryset
242269

243270
def get_context_data(self, **kwargs):

sde_indexing_helper/static/js/affected_urls.js

Lines changed: 110 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@ var INDIVIDUAL_URL = 1;
33
var MULTI_URL_PATTERN = 2;
44
collection_id = getCollectionId();
55

6-
// Maybe you need to define a new DataTable for this page as well
7-
// So that you can refresh it any way you want
8-
// Or maybe this is not necessary
9-
106
$(document).ready(function () {
117
// handleAjaxStartAndStop();
128
initializeDataTable();
139
setupClickHandlers();
14-
});
10+
});
1511

1612
function initializeDataTable() {
17-
var affected_urls_table = $("#urlsTable").DataTable({
13+
var affected_urls_table = $("#affectedURLsTable").DataTable({
1814
pageLength: 100,
1915
colReorder: true,
2016
stateSave: true,
@@ -34,7 +30,18 @@ function initializeDataTable() {
3430
orderCellsTop: true,
3531
pagingType: "input",
3632
rowId: "url"
37-
}
33+
},
34+
// createdRow: function (row, data, dataIndex) {
35+
// // Assuming the 'Include URL' column is at index 3
36+
// let includeUrlCell = $(row).find('td').eq(2);
37+
38+
// // Check if the cell has the cross-mark
39+
// if (includeUrlCell.find('i.cross-mark').length > 0) {
40+
// // Highlight the row if it contains a cross-mark
41+
// $(row).css('background-color', 'rgba(255, 61, 87, 0.36)'); // Light red background
42+
// }
43+
// }
44+
3845
})
3946
}
4047

@@ -45,11 +52,22 @@ function setupClickHandlers() {
4552
function handleIncludeIndividualUrlClick() {
4653
$("body").on("click", ".include-url-btn", function () {
4754

48-
const span = this.querySelector('span');
49-
if (span.classList.contains('cross-mark')) {
55+
const i = this.querySelector('i');
56+
if (i.classList.contains('cross-mark')) {
5057
// Change to tick mark
51-
span.classList.remove('cross-mark');
52-
span.innerHTML = '✔'; // Tick mark
58+
i.classList.remove('cross-mark');
59+
i.classList.add('tick-mark'); // Add the tick-mark class
60+
i.style.color = 'green'; // Change color to green
61+
i.textContent = 'check'; // Set the text to "check"
62+
let parentCol3 = i.closest('.col-3');
63+
// Change the data-sort attribute of the parent element
64+
if (parentCol3) {
65+
parentCol3.setAttribute('data-sort', '1'); // Set data-sort to '1' for the check-mark
66+
}
67+
68+
// // Change to tick mark
69+
// i.classList.remove('cross-mark');
70+
// i.innerHTML = '✔'; // Tick mark
5371
// then add that URL to the includeURLs list
5472
match_pattern = remove_protocol($(this).attr("value")) ;
5573
match_pattern_type = INDIVIDUAL_URL;
@@ -60,10 +78,10 @@ function handleIncludeIndividualUrlClick() {
6078
true
6179
);
6280

63-
const row = $(this).closest('tr'); // Get the closest table row
64-
const rowId = $("#urlsTable").DataTable().row(row).index();
65-
console.log(rowId);
66-
deleteRowById(rowId);
81+
// const row = $(this).closest('tr'); // Get the closest table row
82+
// const rowId = $("#affectedURLsTable").DataTable().row(row).index();
83+
// console.log(rowId);
84+
// deleteRowById(rowId);
6785

6886
//Along with this, remove this pattern from the exclude_patterns
6987
// First, check if similar kind of pattern is available in the exclude_pattern table
@@ -74,11 +92,42 @@ function handleIncludeIndividualUrlClick() {
7492
// true
7593
// );
7694

77-
95+
7896
} else {
79-
// Change back to cross mark
80-
span.classList.add('cross-mark');
81-
span.innerHTML = '❌'; // Cross mark
97+
// Handle the functionality of excluding that URL again (maybe delete that include pattern which was just created)
98+
var url = $(this).attr("value");
99+
console.log("url", url);
100+
getCorrespondingIncludePattern(url).then(function(patternId) {
101+
if (patternId !== null) {
102+
console.log('Pattern ID:', patternId);
103+
currentURLtoDelete = `/api/include-patterns/${patternId}/`;
104+
deletePattern(
105+
currentURLtoDelete,
106+
(data_type = "Include Pattern")
107+
);
108+
109+
// Change back to cross mark
110+
i.classList.remove('tick-mark');
111+
i.classList.add('cross-mark'); // Add the cross-mark class
112+
i.style.color = 'red'; // Change color to red
113+
i.textContent = 'close'; // Set the text to "close"
114+
let parentCol3 = i.closest('.col-3');
115+
// Change the data-sort attribute of the parent element
116+
if (parentCol3) {
117+
parentCol3.setAttribute('data-sort', '0'); // Set data-sort to '0' for the cross-mark
118+
}
119+
120+
121+
console.log("URL removed from the include pattern")
122+
123+
124+
} else {
125+
console.log('No matching pattern found.');
126+
}
127+
}).catch(function(error) {
128+
console.error("Error occurred:", error);
129+
});
130+
82131
}
83132

84133
});
@@ -190,7 +239,7 @@ function postExcludePatterns(match_pattern, match_pattern_type = 0, force) {
190239

191240
function deleteRowById(rowId) {
192241
// Find the DataTable instance
193-
var affected_urls_table = $("#urlsTable").DataTable();
242+
var affected_urls_table = $("#affectedURLsTable").DataTable();
194243

195244
// Find the row with ID 1
196245
var rowToDelete = affected_urls_table.row(rowId); // Adjust based on 0-indexing
@@ -202,4 +251,44 @@ function deleteRowById(rowId) {
202251
console.log("Row not found.");
203252
}
204253
}
205-
254+
255+
function deletePattern(
256+
url,
257+
data_type,
258+
url_type = null,
259+
candidate_urls_count = null
260+
) {
261+
$.ajax({
262+
url: url,
263+
type: "DELETE",
264+
data: {
265+
csrfmiddlewaretoken: csrftoken,
266+
},
267+
headers: {
268+
"X-CSRFToken": csrftoken,
269+
},
270+
success: function (data) {
271+
console.log("Successfully deleted.")
272+
},
273+
});
274+
}
275+
276+
function getCorrespondingIncludePattern(url) {
277+
return $.ajax({
278+
url: `/api/include-patterns/?format=datatables&collection_id=${collection_id}`,
279+
method: 'GET',
280+
dataType: 'json'
281+
}).then(function(response) {
282+
// Iterate through the 'data' array to find a matching pattern
283+
for (let i = 0; i < response.data.length; i++) {
284+
let pattern = response.data[i].match_pattern;
285+
if ( pattern === remove_protocol(url)) {
286+
return response.data[i].id; // Return the first matching pattern id
287+
}
288+
}
289+
return null; // Return null if no pattern matches
290+
}).catch(function(error) {
291+
console.error("Error fetching include patterns:", error);
292+
return null;
293+
});
294+
}

sde_indexing_helper/templates/sde_collections/affected_urls.html

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,36 @@ <h3 class="whiteText candidateTitle">
2525

2626
<div class="container mt-4">
2727
<!-- Table to display the URLs -->
28-
<table id="urlsTable" class="table table-striped table-bordered" style="width:100%">
28+
<table id="affectedURLsTable" class="table table-striped table-bordered" style="width:100%">
2929
<thead class="tableHeader">
3030
<tr>
3131
<th scope="col" class="text-center col-1">#</th>
32-
<th scope="col" class="text-center col-8">URL</th>
33-
<th scope="col" class="text-center col-2">Include URL</th> <!-- New Column -->
32+
<th scope="col" class="text-center col-2">URL</th>
33+
<!-- <th scope="col" class="text-center col-4">Is Included?</th> -->
34+
<th scope="col" class="text-center col-3">Include URL</th> <!-- New Column -->
3435
</tr>
3536
</thead>
3637
<tbody>
3738
{% for url in affected_urls %}
3839
<tr>
40+
<!-- <td class="col-3 text-center">
41+
<a class="exclude_individual_url" value="astrobiology.nasa.gov/about/">
42+
<i class="material-icons" style="color: red">close</i></a>
43+
</td> -->
3944
<td class="text-center">{{ forloop.counter }}</td>
4045
<td>
4146
<a href="{{ url.url }}" target="_blank">{{ url.url }}</a>
42-
</td>
43-
<td class="text-center">
44-
<button type="button" class="btn include-url-btn" data-url-id="{{ url.id }}" value = "{{url.url}}">
45-
<span class="cross-mark">&#10060;</span> <!-- Default Cross Mark -->
46-
</button>
47+
</td>
48+
<!-- <td> {{ url.is_included }} </td> -->
49+
<td class="col-3 text-center data-sort="0">
50+
<a class="include-url-btn" data-url-id="{{ url.id }}" value = "{{url.url}}">
51+
{% if url.is_included %}
52+
<i class="material-icons tick-mark" style="color: green">check</i></a>
53+
<!-- <span class="tick-mark">&#10004;</span> Tick Mark if included -->
54+
{% else %}
55+
<i class="material-icons cross-mark" style="color: red">close</i></a>
56+
<!-- <span class="cross-mark">&#10060;</span> Cross Mark if not included -->
57+
{% endif %}
4758
</td>
4859
</tr>
4960
{% empty %}

0 commit comments

Comments
 (0)