Skip to content

Commit

Permalink
list available contacts #14
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroq committed May 5, 2019
1 parent 22abb4c commit a2e2fe6
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 14 deletions.
1 change: 1 addition & 0 deletions kraut_api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
url(r'^incident/contacts/$', views.contact_list),
url(r'^incident/handlers/$', views.handler_list),
url(r'^incident/(?P<incident_id>[0-9]+)/handlers/$', views.handler_list_incident),
url(r'^incident/(?P<incident_id>[0-9]+)/contacts/$', views.contact_list_incident),
url(r'^incidents/$', views.incident_list),
# ttp
url(r'^ttps/$', views.ttp_list),
Expand Down
53 changes: 52 additions & 1 deletion kraut_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1961,7 +1961,6 @@ def handler_list_incident(request, incident_id, format=None):
try:
inc = Incident.objects.get(id=incident_id)
except Exception as e:
print(e)
return HttpResponse(status=status.HTTP_400_BAD_REQUEST)
paginator = CustomPaginator()
max_items = 10
Expand Down Expand Up @@ -2055,6 +2054,58 @@ def handler_list(request, format=None):

################### INCIDENT CONTACTS #####################

@api_view(['GET'])
@authentication_classes((SessionAuthentication, ))
@permission_classes((IsAuthenticated,))
def contact_list_incident(request, incident_id, format=None):
if request.method == 'GET':
try:
inc = Incident.objects.get(id=incident_id)
except Exception as e:
return HttpResponse(status=status.HTTP_400_BAD_REQUEST)
paginator = CustomPaginator()
max_items = 10
page = request.query_params.get('page')
if request.query_params:
# number of items to retrieve
if 'length' in request.query_params:
max_items = int(request.query_params['length'])
# page to show
if 'start' in request.query_params:
page = int(int(request.query_params['start'])/int(max_items))+1
# order
if 'order[0][column]' in request.query_params and 'order[0][dir]' in request.query_params:
order_by_column = request.query_params['columns['+str(request.query_params['order[0][column]'])+'][data]']
if request.query_params['order[0][dir]'] == 'desc':
order_direction = '-'
else:
order_direction = ''
else:
order_by_column = 'lastname'
order_direction = '-'
# search
if 'search[value]' in request.query_params:
search_value = request.query_params['search[value]']
else:
search_value = None
else:
order_by_column = 'lastname'
order_direction = '-'
search_value = None
# construct queryset
queryset = Contact.objects.exclude(id__in=inc.contacts.all()).order_by('%s%s' % (order_direction, order_by_column))
if search_value:
queryset = queryset.filter(
Q(firstname__istartswith=search_value)|
Q(lastname__istartswith=search_value)
)
handler = paginator.paginate_queryset(queryset, request)
serializer_context = {'request': request}
serializer = ContactSerializer(instance=handler, context=serializer_context, many=True)
return paginator.get_paginated_response(serializer.data)
return HttpResponse(status=status.HTTP_400_BAD_REQUEST)


@api_view(['GET'])
@authentication_classes((SessionAuthentication, ))
@permission_classes((IsAuthenticated,))
Expand Down
53 changes: 41 additions & 12 deletions kraut_incident/templates/kraut_incident/incident_details.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ <h3 class="panel-title">{{ incident.title }}<i id="toggle-commentsidebar" class=
<a class="btn btn-primary btn-sm" el="tooltip" data-placement="top" data-original-title="View" href="#" data-toggle="modal" data-target="#addIHModal" data-objectid="{{ incident.id }}">
<i class="fa fa-plus-circle fa-sm"></i> Add Incident Handler
</a>
<a class="btn btn-primary btn-sm" href="{% url 'incidents:add_task' incident_id=incident.id %}"><i class="fa fa-plus-circle fa-sm"></i> Add Incident Contact</a>
<a class="btn btn-primary btn-sm" el="tooltip" data-placement="top" data-original-title="View" href="#" data-toggle="modal" data-target="#addCOModal" data-objectid="{{ incident.id }}">
<i class="fa fa-plus-circle fa-sm"></i> Add Incident Contact
</a>
<a class="btn btn-primary btn-sm" href="{% url 'incidents:add_task' incident_id=incident.id %}"><i class="fa fa-plus-circle fa-sm"></i> Add Task</a>
<a class="btn btn-primary btn-sm" href="{% url 'incidents:add_task' incident_id=incident.id %}"><i class="fa fa-plus-circle fa-sm"></i> Add Asset</a>
</div><br/><br/>
Expand Down Expand Up @@ -214,7 +216,7 @@ <h3 class="panel-title">Incident Information
<td>{{ co.firstname }} {{ co.lastname }}</td>
<td>{{ co.email }}</td>
<td>{{ co.phone }}</td>
<td><a href="#"><i data-toggle="tooltip" data-placement="left" data-original-title="remove incident contact" class="fa fa-remove pull-right"></i></a></td>
<td><a href="{% url 'incidents:remove_contact_incident' incident_id=incident_id contact_id=co.id %}"><i data-toggle="tooltip" data-placement="left" data-original-title="remove incident contact" class="fa fa-remove pull-right"></i></a></td>
</tr>
{% endfor %}
</tbody>
Expand Down Expand Up @@ -295,26 +297,21 @@ <h4 class="modal-title" id="addIHLabel">Add Incident Handler</h4>
</div>
</div>
</div>
<!-- Modal to add an existing campaign to the package -->
<div class="modal bs-example-modal-lg" id="addCAModal" tabindex="-1" role="dialog" aria-labelledby="addCALabel" aria-hidden="true">
<!-- Modal to add an existing contact to the incident -->
<div class="modal bs-example-modal-lg" id="addCOModal" tabindex="-1" role="dialog" aria-labelledby="addCOLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="addCALabel">Add Campaign</h4>
<h4 class="modal-title" id="addCALabel">Add Incident Contact</h4>
</div>
<div class="modal-body">
<table id="ca_table" class="table table-condensed table-striped table-bordered" style="font-size: 0.9em;">
<table id="co_table" class="table table-condensed table-striped table-bordered" style="font-size: 0.9em;">
<thead>
<tr>
<th>Name</th>
<th>Add</th><th>Lastname</th><th>Firstname</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</div>
<div class="modal-footer">
Expand Down Expand Up @@ -451,6 +448,38 @@ <h4 class="modal-title" id="addOBLabel">Add Observable</h4>
});
});

$(document).ready( function () {
var cTable = $('#co_table').DataTable({
processing: true,
info: false,
bLengthChange: false,
serverSide: true,
oLanguage: {
sProcessing: "<img src='{% static 'images/loading.gif' %}'>",
},
order: [[ 1, "desc" ]],
ajax: {
processing: true,
url: "/api/incident/{{ incident.id }}/contacts/",
dataSrc: "results",
type: "GET",
dataType: "json"
},
columns: [
{
"data": 'id',
'sName': 'operations',
'bSortable': false,
'aTargets': [ 1 ],
"mRender": function (data, type, full) {
return '<div class="checkbox"><input type="checkbox" id="ContactCheckBox_'+data+'" form="post-incident" value="'+data+'"></div>'
}
},
{'data': 'lastname', 'sName': 'lastname', 'aTargets': [ 2 ]},
{'data': 'firstname', 'sName': 'firstname', 'aTargets': [ 3 ]},
],
});
});

$(function () {
$('[data-toggle="popover"]').popover()
Expand Down
1 change: 1 addition & 0 deletions kraut_incident/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
url(r'^incident/(?P<incident_id>\d+)/add/task/$', views.add_task, name='add_task'),
url(r'^incident/(?P<incident_id>\d+)/add/handler/$', views.add_handler_incident, name='add_handler_incident'),
url(r'^incident/(?P<incident_id>\d+)/remove/handler/(?P<handler_id>\d+)/$', views.remove_handler_incident, name='remove_handler_incident'),
url(r'^incident/(?P<incident_id>\d+)/remove/contact/(?P<contact_id>\d+)/$', views.remove_contact_incident, name='remove_contact_incident'),
url(r'^incident/(?P<incident_id>\d+)/add/comment/$', views.comment_incident, name='comment_incident'),
url(r'^incident/(?P<incident_id>\d+)/del/comment/(?P<comment_id>\d+)/$', views.delete_comment_incident, name='delete_comment_incident'),
url(r'^create_contact/$', views.create_contact, name='create_contact'),
Expand Down
21 changes: 20 additions & 1 deletion kraut_incident/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ def list_incidents(request):
context = {}
return render(request, 'kraut_incident/incident_list.html', context)

@login_required
def remove_contact_incident(request, incident_id, contact_id):
"""remove contact person from incident
"""
context = {}
try:
inc = Incident.objects.get(id=incident_id)
except Incident.DoesNotExist:
messages.error(request, 'The requested incident does not exist!')
return render(request, 'kraut_incident/incident_list.html', context)
try:
contact = Contact.objects.get(id=contact_id)
except Handler.DoesNotExist:
messages.error(request, 'The requested incident contact does not exist!')
return render(request, 'kraut_incident/incident_list.html', context)
inc.contacts.remove(contact)
inc.save()
return HttpResponseRedirect(reverse("incidents:view_incident", kwargs={'incident_id': incident_id}))

@login_required
def remove_handler_incident(request, incident_id, handler_id):
"""remove handler from incident
Expand Down Expand Up @@ -185,7 +204,7 @@ def view_incident(request, incident_id):
elif context['num_incident_contacts'] > 0:
context['tab'] = 'incident_contacts'
else:
context['tab'] = 'incident_handlers'
context['tab'] = 'incident_handler'
return render(request, 'kraut_incident/incident_details.html', context)

@login_required
Expand Down

0 comments on commit a2e2fe6

Please sign in to comment.