From 9cea2479592d0f489b3648999168633fee43b690 Mon Sep 17 00:00:00 2001 From: 0xdade <0xdade@users.noreply.github.com> Date: Thu, 24 Oct 2019 23:50:46 -0700 Subject: [PATCH 1/7] Extract tag modal and script to include, update blacklist route. --- natlas-server/app/admin/routes.py | 2 + .../app/templates/admin/_tagmodal.html | 60 ++++++++++++++++++ .../app/templates/admin/blacklist.html | 2 +- natlas-server/app/templates/admin/scope.html | 61 +------------------ 4 files changed, 64 insertions(+), 61 deletions(-) create mode 100644 natlas-server/app/templates/admin/_tagmodal.html diff --git a/natlas-server/app/admin/routes.py b/natlas-server/app/admin/routes.py index 0f4cf5db..8ead94aa 100644 --- a/natlas-server/app/admin/routes.py +++ b/natlas-server/app/admin/routes.py @@ -135,6 +135,8 @@ def blacklist(): delForm = ScopeDeleteForm() editForm = ScopeToggleForm() importForm = ImportBlacklistForm() + addTagForm = TagScopeForm() + addTagForm.tagname.choices = [(row.name, row.name) for row in Tag.query.all()] if newForm.validate_on_submit(): if '/' not in newForm.target.data: newForm.target.data = newForm.target.data + '/32' diff --git a/natlas-server/app/templates/admin/_tagmodal.html b/natlas-server/app/templates/admin/_tagmodal.html new file mode 100644 index 00000000..1dbc9773 --- /dev/null +++ b/natlas-server/app/templates/admin/_tagmodal.html @@ -0,0 +1,60 @@ + + \ No newline at end of file diff --git a/natlas-server/app/templates/admin/blacklist.html b/natlas-server/app/templates/admin/blacklist.html index 203996f0..71cc26db 100644 --- a/natlas-server/app/templates/admin/blacklist.html +++ b/natlas-server/app/templates/admin/blacklist.html @@ -57,5 +57,5 @@

Import Blacklist

- +{% include 'admin/_tagmodal.html' %} {% endblock %} \ No newline at end of file diff --git a/natlas-server/app/templates/admin/scope.html b/natlas-server/app/templates/admin/scope.html index d8d554dd..bc7da5ae 100644 --- a/natlas-server/app/templates/admin/scope.html +++ b/natlas-server/app/templates/admin/scope.html @@ -62,64 +62,5 @@

Import Scope

- - +{% include 'admin/_tagmodal.html' %} {% endblock %} \ No newline at end of file From 105ed2dcb0347a7f11c439d46ac5a01e4f871ceb Mon Sep 17 00:00:00 2001 From: 0xdade <0xdade@users.noreply.github.com> Date: Wed, 30 Oct 2019 22:05:24 -0700 Subject: [PATCH 2/7] Improve tagging consistency. Fixes #206, makes scope tagging available on both scope and blacklist pages. Tried to make some code cleanup while I was at it, as well as improve other inconsistencies with tags. --- natlas-server/app/admin/routes.py | 6 ++- natlas-server/app/models.py | 2 +- natlas-server/app/static/js/natlas-tagging.js | 36 +++++++++++++++++ .../app/templates/admin/_tagmodal.html | 39 +------------------ .../app/templates/admin/blacklist.html | 2 +- natlas-server/app/templates/admin/scope.html | 6 +-- 6 files changed, 44 insertions(+), 47 deletions(-) create mode 100644 natlas-server/app/static/js/natlas-tagging.js diff --git a/natlas-server/app/admin/routes.py b/natlas-server/app/admin/routes.py index 8ead94aa..ffacdca1 100644 --- a/natlas-server/app/admin/routes.py +++ b/natlas-server/app/admin/routes.py @@ -147,7 +147,8 @@ def blacklist(): current_app.ScopeManager.update() flash('%s blacklisted!' % newTarget.target, 'success') return redirect(url_for('admin.blacklist')) - return render_template("admin/blacklist.html", scope=scope, blacklistSize=blacklistSize, delForm=delForm, editForm=editForm, newForm=newForm, importForm=importForm) + return render_template("admin/blacklist.html", scope=scope, blacklistSize=blacklistSize, delForm=delForm, editForm=editForm, \ + newForm=newForm, importForm=importForm, addTagForm=addTagForm) @@ -427,7 +428,8 @@ def tags(): addForm = AddTagForm() if addForm.validate_on_submit(): - newTag = Tag(name=addForm.tagname.data.lower()) + prepared_tag = addForm.tagname.data.strip() + newTag = Tag(name=prepared_tag) db.session.add(newTag) db.session.commit() flash('Successfully added tag %s' % newTag.name, 'success') diff --git a/natlas-server/app/models.py b/natlas-server/app/models.py index 1edd87cf..570759ff 100644 --- a/natlas-server/app/models.py +++ b/natlas-server/app/models.py @@ -121,7 +121,7 @@ def addTags(scopeitem, tags): if existingTag: scopeitem.addTag(existingTag) else: - newTag = Tag(name=tag) + newTag = Tag(name=tag.strip()) db.session.add(newTag) scopeitem.addTag(newTag) diff --git a/natlas-server/app/static/js/natlas-tagging.js b/natlas-server/app/static/js/natlas-tagging.js new file mode 100644 index 00000000..34b01482 --- /dev/null +++ b/natlas-server/app/static/js/natlas-tagging.js @@ -0,0 +1,36 @@ +$('#tagmodal').on('show.bs.modal', function (event) { + var button = $(event.relatedTarget) // Button that triggered the modal + var scopeid = button.data('scopeid') // Extract info from data-* attributes + var scopetarget = button.data('scopetarget') // Extract info from data-* attributes + var tagaction = button.data('action') + var tagstr = $('#scopeTags-'+scopeid).text().trim() // Trim whitespace in jinja formatting leaves whitespace + var tags = tagstr.split(', ') + var modal = $(this) + if (tagaction == 'add') { + modal.find('.modal-title').text('Add tag to ' + scopetarget) + modal.find('#tagScopeForm').attr('action', "/admin/scope/"+scopeid+"/tag") + modal.find('#tagScopeSubmit').text('Add Tag') + if (tagstr != ''){ + tags.forEach(function(item) { + $("option[value=" + item.trim() + "]").attr('hidden', '') // hide this option because it's already added + $("option[value=" + item.trim() + "]").attr('disabled', '') // disable this option because it's hidden + }); + } + + + } else { + modal.find('.modal-title').text('Remove tag from ' + scopetarget) + modal.find('#tagScopeForm').attr('action', "/admin/scope/"+scopeid+"/untag") + modal.find('#tagScopeSubmit').text('Remove Tag') + $("select[name='tagname']")[0].options.length = 0; + + tags.forEach(function(item) { + $("#tagname").append( + $('