Skip to content

Commit

Permalink
track bad scans
Browse files Browse the repository at this point in the history
fixes #142
  • Loading branch information
bradjc committed Sep 5, 2016
1 parent ef65311 commit 309daa1
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions chezbetty/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from .models import tag
from .models import tag_relations
from .models import reimbursee
from .models import badscan
from .models.model import *
from .models.user import LDAPLookup, groupfinder, get_user, User
from .btc import Bitcoin
Expand Down Expand Up @@ -179,6 +180,7 @@ def debug(request):
config.add_route('admin_item_edit', '/admin/item/edit/{item_id}')
config.add_route('admin_item_barcode_pdf', '/admin/item/barcode/{item_id}.pdf')
config.add_route('admin_item_delete', '/admin/item/delete/{item_id}')
config.add_route('admin_badscans_list', '/admin/badscans/list')

config.add_route('admin_tags_list', '/admin/tags/list')

Expand Down
41 changes: 41 additions & 0 deletions chezbetty/models/badscan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from .model import *

from sqlalchemy_utils import ArrowType

class BadScan(Base):
__tablename__ = 'badscans'

id = Column(Integer, primary_key=True, nullable=False)
timestamp = Column(ArrowType, nullable=False, default=datetime.datetime.utcnow)
badscan = Column(String(255), nullable=False)

def __init__(self, scan):
self.badscan = scan

@classmethod
def from_id(cls, id):
return DBSession.query(cls).filter(cls.id == id).one()

@classmethod
def all(cls):
return DBSession.query(cls)\
.order_by(cls.timestamp).all()

@classmethod
def get_scans_with_counts(cls):
out = []
badscans = DBSession.query(cls.badscan, func.count(cls.badscan).label('count'))\
.group_by(cls.badscan)\
.all()
for badscan in badscans:
most_recent = DBSession.query(cls)\
.filter(cls.badscan==badscan.badscan)\
.order_by(cls.timestamp.desc())\
.first()
out.append({
'badscan': badscan.badscan,
'count': badscan.count,
'timestamp': most_recent.timestamp
})

return out
30 changes: 30 additions & 0 deletions chezbetty/templates/admin/badscans.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% extends "base.jinja2" %}
{% set active_page = 'badscans' %}
{% block title %}Bad Scans{% endblock %}

{% block top %}
<h1 class="page-header">Barcode Scans that Errored</h1>
{% endblock %}

{% block contentwide %}

<table class="table table-condensed table-striped sticky sortable" id="events_table">
<thead>
<tr>
<th>Scan</th>
<th>Count</th>
<th>Timestamp</th>
</tr>
</thead>
<tbody>
{% for badscan in badscans %}
<tr>
<td>{{ badscan.badscan }}</td>
<td>{{ badscan.count }}</td>
<td>{{ badscan.timestamp|pretty_date|safe }}</td>
</tr>
{% endfor %}
</tbody>
</table>

{% endblock %}
1 change: 1 addition & 0 deletions chezbetty/templates/admin/sidebar.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<li {% if active_page == 'boxes_add' %}class="active"{% endif %}><a href="/admin/box/add">Add Boxes</a></li>
<li {% if active_page == 'boxes_edit' %}class="active"{% endif %}><a href="/admin/boxes/edit">Edit Boxes<span class="badge pull-right">{{ counts["boxes"] }}</span></a></li>
<li {% if active_page == 'tags_list' %}class="active"{% endif %}><a href="/admin/tags/list">List Tags<span class="badge pull-right">{{ counts["tags"] }}</span></a></li>
<li {% if active_page == 'badscans' %}class="active"{% endif %}><a href="/admin/badscans/list">Bad Scans</a></li>
</ul>
<ul class="nav nav-sidebar">
<li><span>Manage Vendors</span></li>
Expand Down
18 changes: 18 additions & 0 deletions chezbetty/views_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from .models.tag import Tag
from .models.item_tag import ItemTag
from .models.reimbursee import Reimbursee
from .models.badscan import BadScan

from .utility import suppress_emails
from .utility import send_email
Expand Down Expand Up @@ -1594,6 +1595,23 @@ def admin_item_delete(request):
return HTTPFound(location=request.route_url('admin_items_edit'))


################################################################################
# BAD SCANS
################################################################################

@view_config(route_name='admin_badscans_list',
renderer='templates/admin/badscans.jinja2',
permission='manage')
def admin_inventory(request):
badscans = BadScan.get_scans_with_counts()

return {'badscans': badscans}


################################################################################
# TAGS
################################################################################

@view_config(route_name='admin_tags_list',
renderer='templates/admin/tags_list.jinja2',
permission='manage')
Expand Down
5 changes: 5 additions & 0 deletions chezbetty/views_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from .models.pool import Pool
from .models.tag import Tag
from .models.ephemeron import Ephemeron
from .models.badscan import BadScan

from .utility import user_password_reset
from .utility import send_email
Expand Down Expand Up @@ -285,6 +286,10 @@ def get_item_from_barcode(barcode):
else:
return 'Cannot add that entire box to your order. Please scan an individual item.'
except:
badscan = BadScan(barcode)
DBSession.add(badscan)
DBSession.flush()

return 'Could not find that item.'

if not item.enabled:
Expand Down

0 comments on commit 309daa1

Please sign in to comment.