Skip to content

Commit

Permalink
Merge pull request #191 from BenediktMKuehne/nudev
Browse files Browse the repository at this point in the history
Labeling and search update
  • Loading branch information
m-1-k-3 authored Feb 23, 2024
2 parents a4bbff7 + 73580ba commit 6c17ddb
Show file tree
Hide file tree
Showing 10 changed files with 480 additions and 336 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ www/
safe/
.embark_db_backup

# temp dirs/files
.tmp/
**/.tmp/
**/*.tmp

# other stuff
*.zip
*.bin
Expand Down
617 changes: 318 additions & 299 deletions Pipfile.lock

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion embark/dashboard/forms.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import logging
from django import forms

from uploader.models import FirmwareAnalysis
from uploader.models import FirmwareAnalysis, Label

logger = logging.getLogger(__name__)


class StopAnalysisForm(forms.Form):
analysis = forms.ModelChoiceField(queryset=FirmwareAnalysis.objects, empty_label='Select Analysis to stop')


class LabelSelectForm(forms.Form):
label = forms.ModelChoiceField(queryset=Label.objects.all(), empty_label='Select Label to add')
4 changes: 3 additions & 1 deletion embark/dashboard/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@
path('dashboard/individualReport/<uuid:analysis_id>', views.individual_report_dashboard, name='embark-IndividualReportDashboard'),
path('dashboard/stop/', views.stop_analysis, name='embark-stop-analysis'),
path('dashboard/log/<uuid:analysis_id>', views.show_log, name='embark-show-log'),
path('dashboard/logviewer/<uuid:analysis_id>', views.show_logviewer, name='embark-show-logviewer')
path('dashboard/logviewer/<uuid:analysis_id>', views.show_logviewer, name='embark-show-logviewer'),
path('dashboard/report/createlabel/', views.create_label, name='embark-dashboard-create-label'),
path('dashboard/report/addlabel/<uuid:analysis_id>', views.add_label, name='embark-dashboard-add-label')
]
48 changes: 46 additions & 2 deletions embark/dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
from django.views.decorators.http import require_http_methods
from django.contrib import messages
from django.shortcuts import redirect
from django.views.decorators.csrf import csrf_protect
from tracker.forms import AssociateForm
from uploader.boundedexecutor import BoundedExecutor
from uploader.forms import LabelForm

from uploader.models import FirmwareAnalysis
from dashboard.models import Result
from dashboard.forms import StopAnalysisForm
from dashboard.forms import LabelSelectForm, StopAnalysisForm
from porter.views import make_zip


logger = logging.getLogger(__name__)
req_logger = logging.getLogger("requests")


@require_http_methods(["GET"])
Expand Down Expand Up @@ -97,7 +100,9 @@ def report_dashboard(request):
"""
# show all not hidden by others and ALL of your own
firmwares = (FirmwareAnalysis.objects.filter(hidden=False) | FirmwareAnalysis.objects.filter(user=request.user)).distinct()
return render(request, 'dashboard/reportDashboard.html', {'firmwares': firmwares, 'username': request.user.username})
label_form = LabelForm()
label_select_form = LabelSelectForm()
return render(request, 'dashboard/reportDashboard.html', {'firmwares': firmwares, 'username': request.user.username, 'label_form': label_form, 'label_select_form': label_select_form})


@login_required(login_url='/' + settings.LOGIN_URL)
Expand Down Expand Up @@ -239,3 +244,42 @@ def hide_analysis(request, analysis_id):
analysis.save(update_fields=["hidden"])
messages.success(request, 'Analysis: ' + str(analysis_id) + ' successfully hidden')
return redirect('..')


@csrf_protect
@require_http_methods(["POST"])
@login_required(login_url='/' + settings.LOGIN_URL)
def create_label(request):
req_logger.info("User %s called create label", request.user.username)
form = LabelForm(request.POST)
if form.is_valid():
logger.info("User %s tryied to create label %s", request.user.username, request.POST['label_name'])
new_label = form.save()
messages.info(request, 'creation successful of ' + str(new_label))
return redirect('..')
logger.error("label form invalid %s ", request.POST)
if 'label_name' in form.errors:
messages.error(request, 'Label already exists')
else:
messages.error(request, 'creation failed.')
return redirect('..')


@csrf_protect
@require_http_methods(["POST"])
@login_required(login_url='/' + settings.LOGIN_URL)
def add_label(request, analysis_id):
req_logger.info("User %s called add label", request.user.username)
form = LabelSelectForm(request.POST)
if form.is_valid():
new_label = form.cleaned_data["label"]
logger.info("User %s tryied to add label %s", request.user.username, new_label.label_name)
# get analysis obj
analysis = FirmwareAnalysis.objects.get(id=analysis_id)
analysis.label.add(new_label)
analysis.save()
messages.info(request, 'adding successful of ' + str(new_label))
return redirect('..')
logger.error("label form invalid %s ", request.POST)
messages.error(request, 'Adding Label failed')
return redirect('..')
26 changes: 26 additions & 0 deletions embark/static/scripts/reportDashboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// jshint unused:false
function search(input) {
"use strict";
var value = input.toLowerCase();
if (value.startsWith("label=")){
value = value.slice(6);
$("#report-table tr").filter(function() {
$(this).toggle($(this).find(".label").text().toLowerCase().indexOf(value) > -1);
});
} else{
$("#report-table tr").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1);
});
}
}

function onclick_label(value) {
"use strict";
var search_bar = $('#search');
if (search_bar.val().startsWith("label=")){
search_bar.val(search_bar.val() + value.slice(6));
} else {
search_bar.val(search_bar.val() + value);
}
search(value);
}
10 changes: 10 additions & 0 deletions embark/templates/dashboard/label.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% load django_bootstrap5 %}

<form action="{% url 'embark-dashboard-create-label' %}" method="post" id="form">
{% csrf_token %}
<div class="innerBlock">
{% load filters %}
{% bootstrap_form label_form layout='grid' %}
{% bootstrap_button "Create" button_type="submit" button_class="btn-primary" %}
</div>
</form>
86 changes: 54 additions & 32 deletions embark/templates/dashboard/reportDashboard.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% extends "base.html" %}
{% load static %}
{% load tz %}
{% load django_bootstrap5 %}
{% block style %}<link rel="stylesheet" type="text/css" href="{% static 'content/css/reportDashboard.css' %}"/>{% endblock style %}
{% block title %}EMBArk report dashboard{% endblock title %}
{% block navigation %}{% include "navigation.html" %}{% endblock navigation %}
Expand All @@ -9,17 +10,11 @@
<div class="col-sm" id="reportDashboardMainContainer">

{% csrf_token %}
<!-- Iterate over all fields and set expmode on fields in expert mode -->
<div class="col-sm-2 input-group float-right searchBar">
<input type="text" class="form-control input-lg" placeholder="Search..." id="mySearchText"/>
<span class="input-group-btn">
<button class="btn btn-info btn-sm" type="submit" id="mySearchButton">
<svg height="21" viewBox="0 0 21 21" width="21" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd" stroke="#000" stroke-linecap="round" stroke-linejoin="round"><circle cx="8.5" cy="8.5" r="5"/><path d="m17.571 17.5-5.571-5.5"/></g></svg>
</button>
</span>
<input type="text" class="form-control input-lg" placeholder="Search..." id="search"/>
</div>
<div class="tableData">
<table class="table table-striped table-light table-hover" id="report-table">
<table class="table table-striped table-light table-hover" id="report-table-head">
<thead>
<tr>
<th data-priority="2" scope="col">ID</th>
Expand All @@ -34,7 +29,7 @@
</tr>
</thead>

<tbody>
<tbody id="report-table">
{% timezone request.session.django_timezone %}
{% for firmware in firmwares %}
<tr>
Expand All @@ -61,16 +56,25 @@
</td>
<td>
<div class="container">
{% if firmware.archived is True %}
<button class="btn btn-primary" type="button" disabled>
<span class="badge badge-danger">Archived</span>
</button>
{% elif firmware.failed is True %}
<button class="btn btn-primary" type="button" disabled>
<span class="badge badge-danger">Failed/Error</span>
</button>
{% endif %}
<!--TODO grid with firmware.labels() -->
<div class="row row-cols-auto">
{% if firmware.label.all is not None %}
{% for label in firmware.label.all %}
<div class="col-sm">
<button onclick="onclick_label('label={{ label.label_name }}');" class="label badge rounded-pill bg-info">{{ label.label_name }}</button>
</div>
{% endfor %}
{% endif %}
<div class="col-sm">
<a class="badge rounded-pill bg-primary" data-bs-toggle="modal" data-bs-target="#AddLabelModal-{{ firmware.id }}" role="button" aria-expanded="false">+</a>
</div>
</div>
<div class="row">
{% if firmware.archived is True %}
<span class="badge bg-warning">Archived</span>
{% elif firmware.failed is True %}
<span class="badge bg-danger">Failed/Error</span>
{% endif %}
</div>
</div>
</td>
<td>
Expand Down Expand Up @@ -180,6 +184,32 @@ <h5 class="modal-title" id="HideModal-{{ firmware.id }}-Label">Hide {{ firmware.
</div>
</div>
</div>
<div class="modal fade" id="AddLabelModal-{{ firmware.id }}" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="AddLabelModal-{{ firmware.id }}-Label">Add Label to {{ firmware.id }}</h5>
<button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
<span aria-hidden="true">Close</span>
</button>
</div>
<div class="modal-body">
<!-- TODO click select with all options-->
<form action="{% url 'embark-dashboard-add-label' firmware.id %}" method="post" id="form">
{% csrf_token %}
<div class="innerBlock">
{% load filters %}
{% bootstrap_form label_select_form %}
{% bootstrap_button "Add" button_type="submit" button_class="btn-primary" %}
</div>
</form>
{% block label %}{% include "dashboard/label.html" %}{% endblock label %}
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>
</td>
</tr>
{% endfor %}
Expand All @@ -192,18 +222,10 @@ <h5 class="modal-title" id="HideModal-{{ firmware.id }}-Label">Hide {{ firmware.
</div>
{% endblock maincontent %}
{% block inlinejs %}
<script>
$(document).ready(function() {
let table = $('#report-table').DataTable({sDom: 'lrtip'});

$('#mySearchButton').on( 'keyup click', function () {
table.search($('#mySearchText').val()).draw();
} );

$("#mySearchText").change(function(){
table.search($('#mySearchText').val()).draw();
});
} );

<script type="text/javascript">
$("#search").on("keyup", function() {
search($(this).val());
});
</script>
<script type="text/javascript" src="{% static 'scripts/reportDashboard.js' %}"></script>
{% endblock inlinejs %}
3 changes: 3 additions & 0 deletions embark/uploader/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ class Firmware
# status/logreader-stuff
status = models.JSONField(null=False, default=jsonfield_default_value)

# additional Labels
label = models.ManyToManyField(Label, help_text='tag/label', related_query_name='analysis-label', editable=True, max_length=MAX_LENGTH, blank=True)

class Meta:
app_label = 'uploader'

Expand Down
11 changes: 10 additions & 1 deletion embark/uploader/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,16 @@ def start_analysis(request):
new_analysis.user = new_firmware_file.user
logger.debug(" FILE_NAME is %s", new_analysis.firmware.file.name)
new_analysis.firmware_name = os.path.basename(new_analysis.firmware.file.name)
new_analysis = form.save()
# save form
new_analysis = form.save(commit=True)
# add labels from devices
devices = form.cleaned_data["device"]
logger.debug("Got %d devices in this analysis", devices.count())
for device in devices:
logger.debug(" Adding Label=%s", device.device_label.label_name)
new_analysis.label.add(device.device_label)
new_analysis.save()
logger.debug("new_analysis %s has label: %s", new_analysis, new_analysis.label)
# inject into bounded Executor
if BoundedExecutor.submit_firmware(firmware_flags=new_analysis, firmware_file=new_firmware_file):
return redirect('embark-dashboard-service')
Expand Down

0 comments on commit 6c17ddb

Please sign in to comment.