Skip to content

Commit b15410e

Browse files
committed
add view affected urls button to title patterns and document-type patterns
1 parent 194929a commit b15410e

File tree

7 files changed

+255
-157
lines changed

7 files changed

+255
-157
lines changed

sde_collections/models/candidate_url.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111

1212
class CandidateURLQuerySet(models.QuerySet):
1313
def with_exclusion_status(self):
14+
# currently this is only handling the exclude_patterns
15+
# need to add functionality of including include_patterns
16+
# exclude=false
17+
# if some_include_pattern exists which matches this url:
18+
# then exclude = false
19+
# else
20+
# if some_exclude_pattern exists which matches this url:
21+
# exclude = true
1422
return self.annotate(
1523
excluded=models.Exists(
1624
ExcludePattern.candidate_urls.through.objects.filter(candidateurl=models.OuterRef("pk"))

sde_collections/urls.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@
4848
view=views.AffectedURLsListView.as_view(),
4949
name="affected_urls",
5050
),
51+
path(
52+
"include-pattern/<int:id>/",
53+
view=views.AffectedURLsListView.as_view(),
54+
name="affected_urls",
55+
),
56+
path(
57+
"title-pattern/<int:id>/",
58+
view=views.AffectedURLsListView.as_view(),
59+
name="affected_urls",
60+
),
61+
path(
62+
"document-type-pattern/<int:id>/",
63+
view=views.AffectedURLsListView.as_view(),
64+
name="affected_urls",
65+
),
5166
path(
5267
"consolidate/",
5368
view=views.WebappGitHubConsolidationView.as_view(),

sde_collections/views.py

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -235,35 +235,56 @@ class AffectedURLsListView(LoginRequiredMixin, ListView):
235235
model = CandidateURL
236236
template_name = "sde_collections/affected_urls.html"
237237
context_object_name = "affected_urls"
238-
# paginate_by = 100
239-
240-
# def get_queryset(self):
241-
# self.pattern = ExcludePattern.objects.get(id=self.kwargs["id"])
242-
# queryset = self.pattern.matched_urls()
243-
# return queryset
244238

245239
def get_queryset(self):
246-
# Get the exclude pattern based on the ID from the URL kwargs
247-
self.pattern = ExcludePattern.objects.get(id=self.kwargs["id"])
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()
240+
241+
if 'exclude-pattern' in self.request.path:
242+
print("Going with exclude patterns......")
243+
# Get the exclude pattern based on the ID from the URL kwargs
244+
self.pattern = ExcludePattern.objects.get(id=self.kwargs["id"])
245+
246+
# Get excluded URLs
247+
excluded_urls = self.pattern.matched_urls().annotate(is_included=Value(False, output_field=BooleanField()))
248+
249+
# Get included URLs for the same collection
250+
include_patterns = IncludePattern.objects.filter(collection=self.pattern.collection)
251+
included_urls = CandidateURL.objects.filter(
252+
collection=self.pattern.collection,
253+
id__in=include_patterns.values_list('candidate_urls__id', flat=True)
254+
).distinct()
255+
256+
# Annotate excluded URLs with is_included if they are also in included_urls
257+
queryset = excluded_urls.annotate(
258+
is_included=Case(
259+
When(id__in=included_urls.values('id'), then=Value(True)),
260+
default=Value(False),
261+
output_field=BooleanField()
262+
)
263+
)
264+
265+
elif 'include-pattern' in self.request.path:
266+
print("Going with include patterns......")
267+
# Get the include pattern based on the ID from the URL kwargs
268+
self.pattern = IncludePattern.objects.get(id=self.kwargs["id"])
269+
270+
# Get included URLs for the pattern
271+
included_urls = self.pattern.matched_urls().annotate(is_excluded=Value(False, output_field=BooleanField()))
272+
273+
# Get excluded URLs for the same collection
274+
exclude_patterns = ExcludePattern.objects.filter(collection=self.pattern.collection)
275+
excluded_urls = CandidateURL.objects.filter(
276+
collection=self.pattern.collection,
277+
id__in=exclude_patterns.values_list('candidate_urls__id', flat=True)
278+
).distinct()
279+
280+
# Annotate included URLs with is_excluded if they are also in excluded_urls
281+
queryset = included_urls.annotate(
282+
is_excluded=Case(
283+
When(id__in=excluded_urls.values('id'), then=Value(True)),
284+
default=Value(False),
285+
output_field=BooleanField()
286+
)
265287
)
266-
)
267288

268289
return queryset
269290

@@ -272,6 +293,8 @@ def get_context_data(self, **kwargs):
272293
context["pattern"] = self.pattern
273294
context["url_count"] = self.pattern.matched_urls().count()
274295
context["collection"] = self.pattern.collection
296+
context["pattern_type"] = "Exclude" if 'exclude-pattern' in self.request.path else "Include"
297+
275298

276299
# print(self.pattern.collection.id)
277300

sde_indexing_helper/static/css/affected_urls.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,4 +479,8 @@ body {
479479

480480
.include-url-btn .tick-mark {
481481
color: green;
482+
}
483+
484+
.cross-mark, .tick-mark {
485+
cursor: pointer; /* Show hand cursor */
482486
}

sde_indexing_helper/static/js/affected_urls.js

Lines changed: 92 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function initializeDataTable() {
1414
pageLength: 100,
1515
colReorder: true,
1616
stateSave: true,
17+
searching: true,
1718
layout: {
1819
bottomEnd: "inputPaging",
1920
topEnd: null,
@@ -43,10 +44,18 @@ function initializeDataTable() {
4344
// }
4445

4546
})
47+
48+
$("#affectedURLsFilter").on(
49+
"beforeinput",
50+
DataTable.util.debounce(function (val) {
51+
affected_urls_table.columns(1).search(this.value).draw();
52+
}, 1000)
53+
);
4654
}
4755

4856
function setupClickHandlers() {
4957
handleIncludeIndividualUrlClick();
58+
handleExcludeIndividualUrlClick();
5059
}
5160

5261
function handleIncludeIndividualUrlClick() {
@@ -65,10 +74,6 @@ function handleIncludeIndividualUrlClick() {
6574
parentCol3.setAttribute('data-sort', '1'); // Set data-sort to '1' for the check-mark
6675
}
6776

68-
// // Change to tick mark
69-
// i.classList.remove('cross-mark');
70-
// i.innerHTML = '&#10004;'; // Tick mark
71-
// then add that URL to the includeURLs list
7277
match_pattern = remove_protocol($(this).attr("value")) ;
7378
match_pattern_type = INDIVIDUAL_URL;
7479
console.log(match_pattern);
@@ -77,20 +82,6 @@ function handleIncludeIndividualUrlClick() {
7782
match_pattern_type = match_pattern_type,
7883
true
7984
);
80-
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);
85-
86-
//Along with this, remove this pattern from the exclude_patterns
87-
// First, check if similar kind of pattern is available in the exclude_pattern table
88-
// If yes, this run this block of code.
89-
// postExcludePatterns(
90-
// match_pattern = match_pattern,
91-
// match_pattern_type = match_pattern_type,
92-
// true
93-
// );
9485

9586

9687
} else {
@@ -133,26 +124,73 @@ function handleIncludeIndividualUrlClick() {
133124
});
134125
}
135126

127+
136128
function handleExcludeIndividualUrlClick() {
137-
// exclude that URL
138-
// check in the include patterns if similar URL is present
139-
// if yes then delete that URL in the
129+
$("body").on("click", ".exclude-url-btn", function () {
140130

141-
}
131+
const i = this.querySelector('i');
132+
if (i.classList.contains('cross-mark')) {
133+
// Change to tick mark
134+
i.classList.remove('cross-mark');
135+
i.classList.add('tick-mark'); // Add the tick-mark class
136+
i.style.color = 'green'; // Change color to green
137+
i.textContent = 'check'; // Set the text to "check"
138+
let parentCol3 = i.closest('.col-3');
139+
// Change the data-sort attribute of the parent element
140+
if (parentCol3) {
141+
parentCol3.setAttribute('data-sort', '1'); // Set data-sort to '1' for the check-mark
142+
}
142143

143-
function postIncludePatterns(match_pattern, match_pattern_type = 0) {
144-
if (!match_pattern) {
145-
toastr.error("Please highlight a pattern to include.");
146-
return;
144+
match_pattern = remove_protocol($(this).attr("value")) ;
145+
match_pattern_type = INDIVIDUAL_URL;
146+
console.log(match_pattern);
147+
postExcludePatterns(
148+
match_pattern = match_pattern,
149+
match_pattern_type = match_pattern_type,
150+
true
151+
);
152+
153+
154+
} else {
155+
// Handle the functionality of including that URL again (maybe delete that exclude pattern which was just created)
156+
var url = $(this).attr("value");
157+
console.log("url", url);
158+
getCorrespondingExcludePattern(url).then(function(patternId) {
159+
if (patternId !== null) {
160+
console.log('Pattern ID:', patternId);
161+
currentURLtoDelete = `/api/exclude-patterns/${patternId}/`;
162+
deletePattern(
163+
currentURLtoDelete,
164+
(data_type = "Exclude Pattern")
165+
);
166+
167+
// Change back to cross mark
168+
i.classList.remove('tick-mark');
169+
i.classList.add('cross-mark'); // Add the cross-mark class
170+
i.style.color = 'red'; // Change color to red
171+
i.textContent = 'close'; // Set the text to "close"
172+
let parentCol3 = i.closest('.col-3');
173+
// Change the data-sort attribute of the parent element
174+
if (parentCol3) {
175+
parentCol3.setAttribute('data-sort', '0'); // Set data-sort to '0' for the cross-mark
176+
}
177+
178+
console.log("URL removed from the exclude pattern")
179+
180+
181+
} else {
182+
console.log('No matching pattern found.');
183+
}
184+
}).catch(function(error) {
185+
console.error("Error occurred:", error);
186+
});
187+
188+
}
189+
190+
});
147191
}
148192

149-
// if pattern exists in table already
150-
// var table = $("#include_patterns_table").DataTable();
151-
// var itemIdColumnData = table.column(0).data().toArray();
152-
// if (itemIdColumnData.includes(match_pattern)) {
153-
// toastr.success("Pattern already exists");
154-
// return;
155-
// }
193+
function postIncludePatterns(match_pattern, match_pattern_type = 0) {
156194

157195
$.ajax({
158196
url: "/api/include-patterns/",
@@ -165,16 +203,6 @@ function postIncludePatterns(match_pattern, match_pattern_type = 0) {
165203
},
166204
success: function (data) {
167205
console.log("Success on adding to the Included URLs");
168-
// $("#candidate_urls_table").DataTable().ajax.reload(null, false);
169-
// $("#include_patterns_table").DataTable().ajax.reload(null, false);
170-
// if(currentTab === ""){ //Only add a notification if we are on the first tab
171-
// newIncludePatternsCount = newIncludePatternsCount + 1;
172-
// $("#includePatternsTab").html(
173-
// `Include Patterns <span class="pill notifyBadge badge badge-pill badge-primary">` +
174-
// newIncludePatternsCount + " new" +
175-
// `</span>`
176-
// );
177-
// }
178206
},
179207
error: function (xhr, status, error) {
180208
var errorMessage = xhr.responseText;
@@ -193,21 +221,6 @@ function remove_protocol(url) {
193221

194222

195223
function postExcludePatterns(match_pattern, match_pattern_type = 0, force) {
196-
// if (!match_pattern) {
197-
// toastr.error("Please highlight a pattern to exclude.");
198-
// return;
199-
// }
200-
// if (!force) {
201-
// //If the user clicked the icon in the table, we make the change regardless
202-
// // if pattern exists in table already (unless another pattern overrules it)
203-
// var table = $("#exclude_patterns_table").DataTable();
204-
// var itemIdColumnData = table.column(0).data().toArray();
205-
// if (itemIdColumnData.includes(match_pattern)) {
206-
// toastr.success("Pattern already exists");
207-
// return;
208-
// }
209-
// }
210-
211224
$.ajax({
212225
url: "/api/exclude-patterns/",
213226
type: "POST",
@@ -218,17 +231,7 @@ function postExcludePatterns(match_pattern, match_pattern_type = 0, force) {
218231
csrfmiddlewaretoken: csrftoken,
219232
},
220233
success: function (data) {
221-
console.log("Success on removing from Excluded URLs");
222-
// $("#candidate_urls_table").DataTable().ajax.reload(null, false);
223-
// $("#exclude_patterns_table").DataTable().ajax.reload(null, false);
224-
// if(currentTab === ""){ //Only add a notification if we are on the first tab
225-
// newExcludePatternsCount = newExcludePatternsCount + 1;
226-
// $("#excludePatternsTab").html(
227-
// `Exclude Patterns <span class="pill notifyBadge badge badge-pill badge-primary">` +
228-
// newExcludePatternsCount + " new" +
229-
// `</span>`
230-
// );
231-
// }
234+
console.log("Success on adding to the Excluded URLs");
232235
},
233236
error: function (xhr, status, error) {
234237
var errorMessage = xhr.responseText;
@@ -291,4 +294,24 @@ function getCorrespondingIncludePattern(url) {
291294
console.error("Error fetching include patterns:", error);
292295
return null;
293296
});
297+
}
298+
299+
function getCorrespondingExcludePattern(url) {
300+
return $.ajax({
301+
url: `/api/exclude-patterns/?format=datatables&collection_id=${collection_id}`,
302+
method: 'GET',
303+
dataType: 'json'
304+
}).then(function(response) {
305+
// Iterate through the 'data' array to find a matching pattern
306+
for (let i = 0; i < response.data.length; i++) {
307+
let pattern = response.data[i].match_pattern;
308+
if ( pattern === remove_protocol(url)) {
309+
return response.data[i].id; // Return the first matching pattern id
310+
}
311+
}
312+
return null; // Return null if no pattern matches
313+
}).catch(function(error) {
314+
console.error("Error fetching exclude patterns:", error);
315+
return null;
316+
});
294317
}

0 commit comments

Comments
 (0)