Skip to content

Commit 207f23d

Browse files
authored
Merge pull request #258 from NASA-IMPACT/184-when-trying-to-remove-a-title-pattern-by-deleting-it-from-the-input-box-it-throws-an-error
When trying to remove a title pattern by deleting it from the input box, it throws an error
2 parents cc9bb6e + b893f0f commit 207f23d

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

sde_collections/serializers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ class CandidateURLSerializer(serializers.ModelSerializer):
3232
source="get_document_type_display", read_only=True
3333
)
3434
url = serializers.CharField(required=False)
35+
generated_title_id = serializers.SerializerMethodField(read_only=True)
36+
match_pattern_type = serializers.SerializerMethodField(read_only=True)
37+
candidate_urls_count = serializers.SerializerMethodField(read_only=True)
38+
39+
def get_candidate_urls_count(self, obj):
40+
titlepattern = obj.titlepattern_urls.last()
41+
return titlepattern.candidate_urls.count() if titlepattern else 0
42+
43+
def get_generated_title_id(self, obj):
44+
titlepattern = obj.titlepattern_urls.last()
45+
return titlepattern.id if titlepattern else None
46+
47+
def get_match_pattern_type(self, obj):
48+
titlepattern = obj.titlepattern_urls.last()
49+
return titlepattern.match_pattern_type if titlepattern else None
3550

3651
class Meta:
3752
model = CandidateURL
@@ -41,6 +56,9 @@ class Meta:
4156
"url",
4257
"scraped_title",
4358
"generated_title",
59+
"generated_title_id",
60+
"match_pattern_type",
61+
"candidate_urls_count",
4462
"document_type",
4563
"document_type_display",
4664
"visited",

sde_indexing_helper/static/js/candidate_url_list.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
var csrftoken = $('input[name="csrfmiddlewaretoken"]').val();
33
var collection_id = getCollectionId();
44
var selected_text = "";
5+
var INDIVIDUAL_URL = 1
6+
var MULTI_URL_PATTERN = 2
57

68
$(document).ready(function () {
79
handleAjaxStartAndStop();
@@ -44,6 +46,9 @@ function initializeDataTable() {
4446
getVisitedColumn(true_icon, false_icon),
4547
getDocumentTypeColumn(),
4648
{ "data": "id", "visible": false, "searchable": false },
49+
{ "data": "generated_title_id", "visible": false, "searchable": false },
50+
{ "data": "match_pattern_type", "visible": false, "searchable": false },
51+
{ "data": "candidate_urls_count", "visible": false, "searchable": false },
4752
],
4853
"createdRow": function (row, data, dataIndex) {
4954
if (data['excluded']) {
@@ -151,7 +156,7 @@ function getScrapedTitleColumn() {
151156
function getGeneratedTitleColumn() {
152157
return {
153158
"data": "generated_title", "render": function (data, type, row) {
154-
return `<input type="text" class="form-control individual_title_input" value='${data}' data-url=${remove_protocol(row['url'])} />`;
159+
return `<input type="text" class="form-control individual_title_input" value='${data}' data-generated-title-id=${row['generated_title_id']} data-match-pattern-type=${row['match_pattern_type']} data-candidate-urls-count=${row['candidate_urls_count']} data-url=${remove_protocol(row['url'])} />`;
155160
}
156161
}
157162
}
@@ -273,7 +278,14 @@ function handleNewTitleChange() {
273278
$("body").on("change", ".individual_title_input", function () {
274279
var match_pattern = $(this).data('url');
275280
var title_pattern = $(this).val();
276-
postTitlePatterns(match_pattern, title_pattern, match_pattern_type = 1, title_pattern_type = 1);
281+
var generated_title_id = $(this).data('generated-title-id');
282+
var match_pattern_type = $(this).data('match-pattern-type');
283+
var candidate_urls_count = $(this).data('candidate-urls-count');
284+
if (!title_pattern) {
285+
deletePattern(`/api/title-patterns/${generated_title_id}/`, data_type = 'Title Pattern', url_type = match_pattern_type, candidate_urls_count= candidate_urls_count);
286+
}else{
287+
postTitlePatterns(match_pattern, title_pattern, match_pattern_type = 1, title_pattern_type = 1);
288+
}
277289
});
278290
}
279291

@@ -345,11 +357,6 @@ function postTitlePatterns(match_pattern, title_pattern, match_pattern_type = 1)
345357
return;
346358
}
347359

348-
if (!title_pattern) {
349-
toastr.error('Please enter a title pattern.');
350-
return;
351-
}
352-
353360
$.ajax({
354361
url: '/api/title-patterns/',
355362
type: "POST",
@@ -387,8 +394,12 @@ function postVisited(url) {
387394
});
388395
}
389396

390-
function deletePattern(url, data_type) {
391-
var confirmDelete = confirm(`Are you sure you want to delete this ${data_type}?`);
397+
function deletePattern(url, data_type, url_type=null, candidate_urls_count=null) {
398+
if (url_type === MULTI_URL_PATTERN) {
399+
var confirmDelete = confirm(`YOU ARE ATTEMPTING TO DELETE A MULTI-URL PATTERN. THIS WILL AFFECT ${candidate_urls_count} URLs. \n\nAre you sure you want to do this? Currently there is no way to delete a single URL from a Multi-URL pattern`);
400+
} else {
401+
var confirmDelete = confirm(`Are you sure you want to delete this ${data_type}?`);
402+
}
392403
if (!confirmDelete) {
393404
return;
394405
}

0 commit comments

Comments
 (0)