Skip to content

Commit

Permalink
Moved arplogs into the staff menu and interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jsayles committed Mar 29, 2012
1 parent 3931485 commit 79fd167
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 39 deletions.
2 changes: 1 addition & 1 deletion arpwatch/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def for_range(self, day_start, day_end):

def for_device(self, device_id):
DeviceLog = namedtuple('DeviceLog', 'ip, day')
sql = "select ip_address, date_trunc('day', runtime) from arpwatch_arplog where device_id = %s group by 1, 2 order by 2;"
sql = "select ip_address, date_trunc('day', runtime) from arpwatch_arplog where device_id = %s group by 1, 2 order by 2 desc;"
sql = sql % (device_id)
cursor = connection.cursor()
cursor.execute(sql)
Expand Down
25 changes: 0 additions & 25 deletions arpwatch/templates/arpwatch/day.html

This file was deleted.

File renamed without changes.
38 changes: 38 additions & 0 deletions arpwatch/templates/arpwatch/device_logs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{% extends "base.html" %}

{% block sub-title %}Logs | {% endblock %}

{% block content %}
{% include "arpwatch/logs_menu.frag" %}

<div>
<div style="size:large; float:right;"><strong>{{ device_logs|length }} Logs</strong></div>
<h2>Device Logs - {{ day|date:"M d, Y" }}</h2>
</div>

<h4 align="center">
<a href="{% url arpwatch.views.device_logs_by_day previous_day.year previous_day.month previous_day.day %}">&laquo; Prev</a>
[ <a href="{% url arpwatch.views.device_logs_today %}">Today</a> ]
<a href="{% url arpwatch.views.device_logs_by_day next_day.year next_day.month next_day.day %}">Next &raquo;</a>
</h4>

<table border=1>
<tr>
<th>Device</th>
<th>Start</th>
<th>End</th>
<th>Duration</th>
<th></th>
</tr>
{% for l in device_logs %}
<tr>
<td><a href="{% url arpwatch.views.device l.device.id %}">{{ l.device }}</a></td>
<td>{{ l.start|date:"h:i a" }}</td>
<td>{{ l.end|date:"h:i a" }}</td>
<td>{{ l.diff }}</td>
<td></td>
</tr>
{% endfor %}
</table>

{% endblock %}
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{% extends "base.html" %}

{% block sub-title %}Logs | {% endblock %}

{% block content %}
{% include "arpwatch/logs_menu.frag" %}

<h3>Device Info</h3>
<table border=1 cellpadding=2>
<table>
<tr>
<th>User</th>
<td>{{ device.user }}</td>
Expand All @@ -15,15 +22,19 @@ <h3>Device Info</h3>
</table>

<h3>Logs</h3>
<table border=1 cellpadding=2>
<table>
<tr>
<th>Date</th>
<th>IP Address</th>
<th/>
</tr>
{% for log in logs %}
<tr>
<td><a href="{% url arpwatch.views.logs_by_day log.day|date:'Y' log.day|date:'m' log.day|date:'d' %}">{{ log.day|date:"M d, Y" }}</a></td>
<td><a href="{% url arpwatch.views.device_logs_by_day log.day|date:'Y' log.day|date:'m' log.day|date:'d' %}">{{ log.day|date:"M d, Y" }}</a></td>
<td>{{ log.ip }}</td>
<td/>
</tr>
{% endfor %}
</table>
</table>

{% endblock %}
4 changes: 4 additions & 0 deletions arpwatch/templates/arpwatch/logs_menu.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="submenu">
<a href="{% url arpwatch.views.device_logs_today %}">device logs</a> |
<a href="{% url arpwatch.views.logins_today %}">user logins</a>
</div>
36 changes: 36 additions & 0 deletions arpwatch/templates/arpwatch/user_logins.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{% extends "base.html" %}

{% block sub-title %}Logs | {% endblock %}

{% block content %}
{% include "arpwatch/logs_menu.frag" %}

<div>
<div style="size:large; float:right;"><strong>{{ logs|length }} Logins</strong></div>
<h2>User Logins - {{ day|date:"M d, Y" }}</h2>
</div>

<h4 align="center">
<a href="{% url arpwatch.views.logins_by_day previous_day.year previous_day.month previous_day.day %}">&laquo; Prev</a>
[ <a href="{% url arpwatch.views.logins_today %}">Today</a> ]
<a href="{% url arpwatch.views.logins_by_day next_day.year next_day.month next_day.day %}">Next &raquo;</a>
</h4>

<table border=1>
<tr>
<th>Time</th>
<th>User</th>
<th>IP Address</th>
<th></th>
</tr>
{% for l in logs %}
<tr>
<td>{{ l.logintime|date:"h:i a" }}</td>
<td>{{ l.user }}</td>
<td>{{ l.ip_address }}</td>
<td></td>
</tr>
{% endfor %}
</table>

{% endblock %}
6 changes: 4 additions & 2 deletions arpwatch/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
(r'^devices/$', 'arpwatch.views.device_list'),
(r'^import/$', 'arpwatch.views.import_files'),
(r'^device/(?P<id>[\d]+)/$', 'arpwatch.views.device'),
(r'^day/$', 'arpwatch.views.logs_today'),
(r'^day/(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/$', 'arpwatch.views.logs_by_day'),
(r'^device/$', 'arpwatch.views.device_logs_today'),
(r'^device/(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/$', 'arpwatch.views.device_logs_by_day'),
(r'^user/$', 'arpwatch.views.logins_today'),
(r'^user/(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/$', 'arpwatch.views.logins_by_day'),
)

# Copyright 2011 Office Nomads LLC (http://www.officenomads.com/) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
24 changes: 18 additions & 6 deletions arpwatch/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,38 @@ def import_files(request):
@staff_member_required
def device_list(request):
devices = UserDevice.objects.filter(ignore=False)
return render_to_response('arpwatch/devices.html', {'devices': devices}, context_instance=RequestContext(request))
return render_to_response('arpwatch/device_list.html', {'devices': devices}, context_instance=RequestContext(request))

@staff_member_required
def device(request, id):
device = UserDevice.objects.get(pk=id)
logs = ArpLog.objects.for_device(id)
return render_to_response('arpwatch/device.html', {'device': device, 'logs': logs}, context_instance=RequestContext(request))
return render_to_response('arpwatch/device_view.html', {'device': device, 'logs': logs}, context_instance=RequestContext(request))

@staff_member_required
def logs_today(request):
def device_logs_today(request):
# Kinda clunky but it works
now = datetime.now()
return logs_by_day(request, str(now.year), str(now.month), str(now.day));
return device_logs_by_day(request, str(now.year), str(now.month), str(now.day));

@staff_member_required
def logs_by_day(request, year, month, day):
def device_logs_by_day(request, year, month, day):
log_date = date(year=int(year), month=int(month), day=int(day))
day_start = datetime.strptime(year + month + day + " 00:00", "%Y%m%d %H:%M")
day_end = datetime.strptime(year + month + day + " 23:59", "%Y%m%d %H:%M")
device_logs = ArpLog.objects.for_range(day_start, day_end)
return render_to_response('arpwatch/day.html', {'device_logs':device_logs, 'day': log_date, 'next_day':log_date + timedelta(days=1), 'previous_day':log_date - timedelta(days=1)}, context_instance=RequestContext(request))
return render_to_response('arpwatch/device_logs.html', {'device_logs':device_logs, 'day': log_date, 'next_day':log_date + timedelta(days=1), 'previous_day':log_date - timedelta(days=1)}, context_instance=RequestContext(request))

@staff_member_required
def logins_today(request):
now = datetime.now()
return logins_by_day(request, str(now.year), str(now.month), str(now.day));

def logins_by_day(request, year, month, day):
log_date = date(year=int(year), month=int(month), day=int(day))
start = datetime.strptime(year + month + day + " 00:00", "%Y%m%d %H:%M")
end = datetime.strptime(year + month + day + " 23:59", "%Y%m%d %H:%M")
logs = UserRemoteAddr.objects.filter(logintime__gt=start, logintime__lt=end)
return render_to_response('arpwatch/user_logins.html', {'logs':logs, 'day': log_date, 'next_day':log_date + timedelta(days=1), 'previous_day':log_date - timedelta(days=1)}, context_instance=RequestContext(request))

# Copyright 2011 Office Nomads LLC (http://www.officenomads.com/) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
1 change: 1 addition & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<a id="activity-tab" href="{% url staff.views.activity %}">Activity</a>
<a id="billing-tab" href="{% url staff.views.bills %}">Billing ({{ billing_count }})</a>
<a id="stats-tab" href="{% url staff.views.stats %}">Stats</a>
<a id="stats-tab" href="{% url arpwatch.views.device_logs_today %}">Logs</a>
{% if request.user.is_authenticated %}
<a id="log-tab" href="{% url django.contrib.auth.views.logout_then_login %}">Logout</a>
{% else %}
Expand Down
2 changes: 1 addition & 1 deletion urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
(r'^staff/', include('staff.urls', app_name='staff')),
(r'^member/', include('members.urls', app_name='members')),
(r'^interlink/', include('interlink.urls', app_name='interlink')),
(r'^arpwatch/', include('arpwatch.urls', app_name='arpwatch')),
(r'^logs/', include('arpwatch.urls', app_name='arpwatch')),
(r'^tablet/', include('tablet.urls', app_name='tablet')),

(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
Expand Down

0 comments on commit 79fd167

Please sign in to comment.