Skip to content

Commit

Permalink
Merge pull request #258 from NASA-IMPACT/184-when-trying-to-remove-a-…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
code-geek authored Jun 16, 2023
2 parents cc9bb6e + b893f0f commit 207f23d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
18 changes: 18 additions & 0 deletions sde_collections/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ class CandidateURLSerializer(serializers.ModelSerializer):
source="get_document_type_display", read_only=True
)
url = serializers.CharField(required=False)
generated_title_id = serializers.SerializerMethodField(read_only=True)
match_pattern_type = serializers.SerializerMethodField(read_only=True)
candidate_urls_count = serializers.SerializerMethodField(read_only=True)

def get_candidate_urls_count(self, obj):
titlepattern = obj.titlepattern_urls.last()
return titlepattern.candidate_urls.count() if titlepattern else 0

def get_generated_title_id(self, obj):
titlepattern = obj.titlepattern_urls.last()
return titlepattern.id if titlepattern else None

def get_match_pattern_type(self, obj):
titlepattern = obj.titlepattern_urls.last()
return titlepattern.match_pattern_type if titlepattern else None

class Meta:
model = CandidateURL
Expand All @@ -41,6 +56,9 @@ class Meta:
"url",
"scraped_title",
"generated_title",
"generated_title_id",
"match_pattern_type",
"candidate_urls_count",
"document_type",
"document_type_display",
"visited",
Expand Down
29 changes: 20 additions & 9 deletions sde_indexing_helper/static/js/candidate_url_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
var csrftoken = $('input[name="csrfmiddlewaretoken"]').val();
var collection_id = getCollectionId();
var selected_text = "";
var INDIVIDUAL_URL = 1
var MULTI_URL_PATTERN = 2

$(document).ready(function () {
handleAjaxStartAndStop();
Expand Down Expand Up @@ -44,6 +46,9 @@ function initializeDataTable() {
getVisitedColumn(true_icon, false_icon),
getDocumentTypeColumn(),
{ "data": "id", "visible": false, "searchable": false },
{ "data": "generated_title_id", "visible": false, "searchable": false },
{ "data": "match_pattern_type", "visible": false, "searchable": false },
{ "data": "candidate_urls_count", "visible": false, "searchable": false },
],
"createdRow": function (row, data, dataIndex) {
if (data['excluded']) {
Expand Down Expand Up @@ -151,7 +156,7 @@ function getScrapedTitleColumn() {
function getGeneratedTitleColumn() {
return {
"data": "generated_title", "render": function (data, type, row) {
return `<input type="text" class="form-control individual_title_input" value='${data}' data-url=${remove_protocol(row['url'])} />`;
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'])} />`;
}
}
}
Expand Down Expand Up @@ -273,7 +278,14 @@ function handleNewTitleChange() {
$("body").on("change", ".individual_title_input", function () {
var match_pattern = $(this).data('url');
var title_pattern = $(this).val();
postTitlePatterns(match_pattern, title_pattern, match_pattern_type = 1, title_pattern_type = 1);
var generated_title_id = $(this).data('generated-title-id');
var match_pattern_type = $(this).data('match-pattern-type');
var candidate_urls_count = $(this).data('candidate-urls-count');
if (!title_pattern) {
deletePattern(`/api/title-patterns/${generated_title_id}/`, data_type = 'Title Pattern', url_type = match_pattern_type, candidate_urls_count= candidate_urls_count);
}else{
postTitlePatterns(match_pattern, title_pattern, match_pattern_type = 1, title_pattern_type = 1);
}
});
}

Expand Down Expand Up @@ -345,11 +357,6 @@ function postTitlePatterns(match_pattern, title_pattern, match_pattern_type = 1)
return;
}

if (!title_pattern) {
toastr.error('Please enter a title pattern.');
return;
}

$.ajax({
url: '/api/title-patterns/',
type: "POST",
Expand Down Expand Up @@ -387,8 +394,12 @@ function postVisited(url) {
});
}

function deletePattern(url, data_type) {
var confirmDelete = confirm(`Are you sure you want to delete this ${data_type}?`);
function deletePattern(url, data_type, url_type=null, candidate_urls_count=null) {
if (url_type === MULTI_URL_PATTERN) {
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`);
} else {
var confirmDelete = confirm(`Are you sure you want to delete this ${data_type}?`);
}
if (!confirmDelete) {
return;
}
Expand Down

0 comments on commit 207f23d

Please sign in to comment.