Create initial asset audit framework (#403)

* WIP: Basic work on audit

* WIP: Audit modal works

Need to get the ID search working.

* WIP: Javascript shenanigans for asset audit search

It's not clean but it works..

* Improve audit search bar

Optimise for APM!

* Filter asset audit list by never-audited

* Added cable functionality to audit form

Also improved styling

* FIX: Revert partialising of asset search

* Various UX Improvements

Also rearranged asset detail/edit to be more space efficient

* FIX: Remove assets from to-be-audited table when audited

Previously required a page reload

* Improve sample data generator

Does reversion properly and sets colours for asset statuses

* FIX: Gracefully handle 404s in audit search

* FEAT: Add buttons for some common defaults on audit form

TODO: Partialise those fragments and add them to the edit/create forms too.

* FIX: Fix asset sample data command when run alone

* FEAT: More handy buttons

* FIX: Stop quickbuttons being tab-selected

If someone's tabbing through, they won't be needing the buttons...

* FIX: Hide asset detail buttons for basic users

* FIX: Migrations

* Start tests for audit

* Some deduplication for testing code

* Improve asset audit testing

* Remember to test the tests Arona

* Potentially make modal tests more consistent

* FIX?: Up WebDriverWait timeout for modal tests

* FIX?: What about this way...

* Remake migrations

* Fix README badges to point to right branch

While I'm here eh :P

* Use aware time in audit

* Fix migrations again

* Fix for my fix...

* Modify audit exclusions to properly prevent data loss

* pep eiiiiiight
This commit is contained in:
2020-04-14 21:11:09 +01:00
committed by GitHub
parent 0fe7d55eab
commit 3be06a7b25
18 changed files with 595 additions and 94 deletions

View File

@@ -2,6 +2,7 @@ from pypom import Page, Region
from selenium.webdriver.common.by import By
from selenium.webdriver import Chrome
from selenium.common.exceptions import NoSuchElementException
from PyRIGS.tests import regions
class BasePage(Page):
@@ -34,37 +35,19 @@ class FormPage(BasePage):
self.driver.execute_script("Array.from(document.getElementsByTagName(\"input\")).forEach(function (el, ind, arr) { el.removeAttribute(\"required\")});")
self.driver.execute_script("Array.from(document.getElementsByTagName(\"select\")).forEach(function (el, ind, arr) { el.removeAttribute(\"required\")});")
def submit(self):
previous_errors = self.errors
self.find_element(*self._submit_locator).click()
self.wait.until(lambda x: self.errors != previous_errors or self.success)
@property
def errors(self):
try:
error_page = self.ErrorPage(self, self.find_element(*self._errors_selector))
error_page = regions.ErrorPage(self, self.find_element(*self._errors_selector))
return error_page.errors
except NoSuchElementException:
return None
class ErrorPage(Region):
_error_item_selector = (By.CSS_SELECTOR, "dl>span")
class ErrorItem(Region):
_field_selector = (By.CSS_SELECTOR, "dt")
_error_selector = (By.CSS_SELECTOR, "dd>ul>li")
@property
def field_name(self):
return self.find_element(*self._field_selector).text
@property
def errors(self):
return [x.text for x in self.find_elements(*self._error_selector)]
@property
def errors(self):
error_items = [self.ErrorItem(self, x) for x in self.find_elements(*self._error_item_selector)]
errors = {}
for error in error_items:
errors[error.field_name] = error.errors
return errors
class LoginPage(BasePage):
URL_TEMPLATE = '/user/login'

View File

@@ -131,3 +131,27 @@ class SingleSelectPicker(Region):
def set_value(self, value):
picker = Select(self.root)
picker.select_by_visible_text(value)
class ErrorPage(Region):
_error_item_selector = (By.CSS_SELECTOR, "dl>span")
class ErrorItem(Region):
_field_selector = (By.CSS_SELECTOR, "dt")
_error_selector = (By.CSS_SELECTOR, "dd>ul>li")
@property
def field_name(self):
return self.find_element(*self._field_selector).text
@property
def errors(self):
return [x.text for x in self.find_elements(*self._error_selector)]
@property
def errors(self):
error_items = [self.ErrorItem(self, x) for x in self.find_elements(*self._error_item_selector)]
errors = {}
for error in error_items:
errors[error.field_name] = error.errors
return errors

View File

@@ -1,6 +1,6 @@
# TEC PA & Lighting - PyRIGS #
[![Build Status](https://travis-ci.org/nottinghamtec/PyRIGS.svg?branch=develop)](https://travis-ci.org/nottinghamtec/PyRIGS)
[![Coverage Status](https://coveralls.io/repos/github/nottinghamtec/PyRIGS/badge.svg?branch=develop)](https://coveralls.io/github/nottinghamtec/PyRIGS)
[![Build Status](https://travis-ci.org/nottinghamtec/PyRIGS.svg)](https://travis-ci.org/nottinghamtec/PyRIGS)
[![Coverage Status](https://coveralls.io/repos/github/nottinghamtec/PyRIGS/badge.svg)](https://coveralls.io/github/nottinghamtec/PyRIGS)
Welcome to TEC PA & Lightings PyRIGS program. This is a reimplementation of the existing Rig Information Gathering System (RIGS) that was developed using Ruby on Rails.

View File

@@ -13,7 +13,7 @@ class AssetForm(forms.ModelForm):
class Meta:
model = models.Asset
fields = '__all__'
exclude = ['asset_id_prefix', 'asset_id_number']
exclude = ['asset_id_prefix', 'asset_id_number', 'last_audited_at', 'last_audited_by']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@@ -21,6 +21,13 @@ class AssetForm(forms.ModelForm):
self.fields['date_acquired'].widget.format = '%Y-%m-%d'
class AssetAuditForm(AssetForm):
class Meta(AssetForm.Meta):
# Prevents assets losing existing data that isn't included in the audit form
exclude = ['asset_id_prefix', 'asset_id_number', 'last_audited_at', 'last_audited_by',
'parent', 'purchased_from', 'purchase_price', 'comments']
class AssetSearchForm(forms.Form):
query = forms.CharField(required=False)
category = forms.ModelMultipleChoiceField(models.AssetCategory.objects.all(), required=False)

View File

@@ -1,7 +1,9 @@
import random
from django.core.management.base import BaseCommand, CommandError
from django.utils import timezone
from reversion import revisions as reversion
from assets import models
from RIGS import models as rigsmodels
class Command(BaseCommand):
@@ -15,6 +17,7 @@ class Command(BaseCommand):
random.seed('Some object to see the random number generator')
self.create_profile()
self.create_categories()
self.create_statuses()
self.create_suppliers()
@@ -22,6 +25,13 @@ class Command(BaseCommand):
self.create_connectors()
self.create_cables()
# Make sure that there's at least one profile if this command is run standalone
def create_profile(self):
name = "Fred Johnson"
models.Profile.objects.create(username=name.replace(" ", ""), first_name=name.split(" ")[0], last_name=name.split(" ")[-1],
email=name.replace(" ", "") + "@example.com",
initials="".join([j[0].upper() for j in name.split()]))
def create_categories(self):
categories = ['Case', 'Video', 'General', 'Sound', 'Lighting', 'Rigging']
@@ -29,17 +39,19 @@ class Command(BaseCommand):
models.AssetCategory.objects.create(name=cat)
def create_statuses(self):
statuses = [('In Service', True), ('Lost', False), ('Binned', False), ('Sold', False), ('Broken', False)]
statuses = [('In Service', True, 'success'), ('Lost', False, 'warning'), ('Binned', False, 'danger'), ('Sold', False, 'danger'), ('Broken', False, 'warning')]
for stat in statuses:
models.AssetStatus.objects.create(name=stat[0], should_show=stat[1])
models.AssetStatus.objects.create(name=stat[0], should_show=stat[1], display_class=stat[2])
def create_suppliers(self):
suppliers = ["Acme, inc.", "Widget Corp", "123 Warehousing", "Demo Company", "Smith and Co.", "Foo Bars", "ABC Telecom", "Fake Brothers", "QWERTY Logistics", "Demo, inc.", "Sample Company", "Sample, inc", "Acme Corp", "Allied Biscuit", "Ankh-Sto Associates", "Extensive Enterprise", "Galaxy Corp", "Globo-Chem", "Mr. Sparkle", "Globex Corporation", "LexCorp", "LuthorCorp", "North Central Positronics", "Omni Consimer Products", "Praxis Corporation", "Sombra Corporation", "Sto Plains Holdings", "Tessier-Ashpool", "Wayne Enterprises", "Wentworth Industries", "ZiffCorp", "Bluth Company", "Strickland Propane", "Thatherton Fuels", "Three Waters", "Water and Power", "Western Gas & Electric", "Mammoth Pictures", "Mooby Corp", "Gringotts", "Thrift Bank", "Flowers By Irene", "The Legitimate Businessmens Club", "Osato Chemicals", "Transworld Consortium", "Universal Export", "United Fried Chicken", "Virtucon", "Kumatsu Motors", "Keedsler Motors", "Powell Motors", "Industrial Automation", "Sirius Cybernetics Corporation", "U.S. Robotics and Mechanical Men", "Colonial Movers", "Corellian Engineering Corporation", "Incom Corporation", "General Products", "Leeding Engines Ltd.", "Blammo", # noqa
"Input, Inc.", "Mainway Toys", "Videlectrix", "Zevo Toys", "Ajax", "Axis Chemical Co.", "Barrytron", "Carrys Candles", "Cogswell Cogs", "Spacely Sprockets", "General Forge and Foundry", "Duff Brewing Company", "Dunder Mifflin", "General Services Corporation", "Monarch Playing Card Co.", "Krustyco", "Initech", "Roboto Industries", "Primatech", "Sonky Rubber Goods", "St. Anky Beer", "Stay Puft Corporation", "Vandelay Industries", "Wernham Hogg", "Gadgetron", "Burleigh and Stronginthearm", "BLAND Corporation", "Nordyne Defense Dynamics", "Petrox Oil Company", "Roxxon", "McMahon and Tate", "Sixty Second Avenue", "Charles Townsend Agency", "Spade and Archer", "Megadodo Publications", "Rouster and Sideways", "C.H. Lavatory and Sons", "Globo Gym American Corp", "The New Firm", "SpringShield", "Compuglobalhypermeganet", "Data Systems", "Gizmonic Institute", "Initrode", "Taggart Transcontinental", "Atlantic Northern", "Niagular", "Plow King", "Big Kahuna Burger", "Big T Burgers and Fries", "Chez Quis", "Chotchkies", "The Frying Dutchman", "Klimpys", "The Krusty Krab", "Monks Diner", "Milliways", "Minuteman Cafe", "Taco Grande", "Tip Top Cafe", "Moes Tavern", "Central Perk", "Chasers"] # noqa
for supplier in suppliers:
models.Supplier.objects.create(name=supplier)
with reversion.create_revision():
for supplier in suppliers:
reversion.set_user(random.choice(rigsmodels.Profile.objects.all()))
models.Supplier.objects.create(name=supplier)
def create_assets(self):
asset_description = ['Large cable', 'Shiny thing', 'New lights', 'Really expensive microphone', 'Box of fuse flaps', 'Expensive tool we didn\'t agree to buy', 'Cable drums', 'Boring amount of tape', 'Video stuff no one knows how to use', 'More amplifiers', 'Heatshrink']
@@ -48,22 +60,24 @@ class Command(BaseCommand):
statuses = models.AssetStatus.objects.all()
suppliers = models.Supplier.objects.all()
for i in range(100):
asset = models.Asset(
asset_id='{}'.format(models.Asset.get_available_asset_id()),
description=random.choice(asset_description),
category=random.choice(categories),
status=random.choice(statuses),
date_acquired=timezone.now().date()
)
with reversion.create_revision():
for i in range(100):
reversion.set_user(random.choice(rigsmodels.Profile.objects.all()))
asset = models.Asset(
asset_id='{}'.format(models.Asset.get_available_asset_id()),
description=random.choice(asset_description),
category=random.choice(categories),
status=random.choice(statuses),
date_acquired=timezone.now().date()
)
if i % 4 == 0:
asset.parent = models.Asset.objects.order_by('?').first()
if i % 4 == 0:
asset.parent = models.Asset.objects.order_by('?').first()
if i % 3 == 0:
asset.purchased_from = random.choice(suppliers)
asset.clean()
asset.save()
if i % 3 == 0:
asset.purchased_from = random.choice(suppliers)
asset.clean()
asset.save()
def create_cables(self):
asset_description = ['The worm', 'Harting without a cap', 'Heavy cable', 'Extension lead', 'IEC cable that we should remember to prep']

View File

@@ -0,0 +1,31 @@
# Generated by Django 3.0.3 on 2020-04-13 00:06
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('assets', '0016_auto_20200413_1632'),
]
operations = [
migrations.AddField(
model_name='asset',
name='last_audited_at',
field=models.DateTimeField(blank=True, null=True),
),
migrations.AddField(
model_name='asset',
name='last_audited_by',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='audited_by', to=settings.AUTH_USER_MODEL),
),
migrations.AlterField(
model_name='asset',
name='csa',
field=models.DecimalField(blank=True, decimal_places=2, help_text='mm²', max_digits=10, null=True),
),
]

View File

@@ -9,7 +9,7 @@ from django.dispatch.dispatcher import receiver
from reversion import revisions as reversion
from reversion.models import Version
from RIGS.models import RevisionMixin
from RIGS.models import RevisionMixin, Profile
class AssetCategory(models.Model):
@@ -104,13 +104,17 @@ class Asset(models.Model, RevisionMixin):
salvage_value = models.DecimalField(blank=True, null=True, decimal_places=2, max_digits=10)
comments = models.TextField(blank=True)
# Audit
last_audited_at = models.DateTimeField(blank=True, null=True)
last_audited_by = models.ForeignKey(Profile, on_delete=models.SET_NULL, related_name='audited_by', blank=True, null=True)
# Cable assets
is_cable = models.BooleanField(default=False)
cable_type = models.ForeignKey(to=CableType, blank=True, null=True, on_delete=models.SET_NULL)
length = models.DecimalField(decimal_places=1, max_digits=10,
blank=True, null=True, help_text='m')
csa = models.DecimalField(decimal_places=2, max_digits=10,
blank=True, null=True, help_text='mm^2')
blank=True, null=True, help_text='mm²')
# Hidden asset_id components
# For example, if asset_id was "C1001" then asset_id_prefix would be "C" and number "1001"

View File

@@ -0,0 +1,142 @@
{% extends request.is_ajax|yesno:'base_ajax.html,base_assets.html' %}
{% load widget_tweaks %}
{% block title %}Audit Asset {{ object.asset_id }}{% endblock %}
{% block content %}
<script>
function setAcquired(today) {
var date = new Date(1970, 0, 1);
if(today) {
date = new Date();
}
$('#id_date_acquired').val([date.getFullYear(), ('0' + (date.getMonth()+1)).slice(-2), ('0' + date.getDate()).slice(-2)].join('-'));
}
function setLength(length) {
$('#id_length').val(length);
}
function setCSA(CSA) {
$('#id_csa').val(CSA);
}
function checkIfCableHidden() {
if (document.getElementById("id_is_cable").checked) {
document.getElementById("cable-table").hidden = false;
} else {
document.getElementById("cable-table").hidden = true;
}
}
checkIfCableHidden();
</script>
<form class="form-horizontal" method="POST" id="asset_audit_form" action="{{ form.action|default:request.path }}">
{% include 'form_errors.html' %}
{% csrf_token %}
<input type="hidden" name="id" value="{{ object.id|default:0 }}" hidden=true>
<div class="form-group">
<label for="{{ form.asset_id.id_for_label }}" class="col-sm-2 control-label">Asset ID</label>
<div class="col-sm-10">
{% render_field form.asset_id|add_class:'form-control' value=object.asset_idz %}
</div>
</div>
<div class="form-group">
<label for="{{ form.description.id_for_label }}" class="col-sm-2 control-label">Description</label>
<div class="col-sm-10">
{% render_field form.description|add_class:'form-control' value=object.description %}
</div>
</div>
<div class="form-group">
<label for="{{ form.category.id_for_label }}" class="col-sm-2 control-label">Category</label>
<div class="col-sm-10">
{% render_field form.category|add_class:'form-control'%}
</div>
</div>
<div class="form-group">
<label for="{{ form.status.id_for_label }}" class="col-sm-2 control-label">Status</label>
<div class="col-sm-10">
{% render_field form.status|add_class:'form-control'%}
</div>
</div>
<div class="form-group">
<label for="{{ form.serial_number.id_for_label }}" class="col-sm-2 control-label">Serial Number</label>
<div class="col-sm-10">
{% render_field form.serial_number|add_class:'form-control' value=object.serial_number %}
</div>
</div>
<div class="form-group">
<label for="{{ form.date_acquired.id_for_label }}" class="col-sm-2 control-label">Date Acquired</label>
<div class="col-sm-6">
{% render_field form.date_acquired|add_class:'form-control' value=object.date_acquired %}
</div>
<div class="col-sm-4">
<btn class="btn btn-default" onclick="setAcquired(true);" tabindex="-1">Today</btn>
<btn class="btn btn-default" onclick="setAcquired(false);" tabindex="-1">Unknown</btn>
</div>
</div>
<div class="form-group">
<label for="{{ form.date_sold.id_for_label }}" class="col-sm-2 control-label">Date Sold</label>
<div class="col-sm-6">
{% render_field form.date_sold|add_class:'form-control' value=object.date_sold %}
</div>
</div>
<div class="form-group">
<label for="{{ form.salvage_value.id_for_label }}" class="col-sm-2 control-label">Salvage Value</label>
<div class="col-sm-10">
<div class="input-group">
<span class="input-group-addon">£</span>
{% render_field form.salvage_value|add_class:'form-control' value=object.salvage_value %}
</div>
</div>
</div>
<hr>
<div class="form-group">
<label for="{{ form.is_cable.id_for_label }}" class="col-sm-2 control-label">Cable?</label>
<div class="col-sm-10">
{% render_field form.is_cable|attr:'onchange=checkIfCableHidden()' %}
</div>
</div>
<div id="cable-table">
<div class="form-group">
<label for="{{ form.cable_type.id_for_label }}" class="col-sm-2 control-label">Cable Type</label>
<div class="col-sm-10">
{% render_field form.cable_type|add_class:'form-control' %}
</div>
</div>
<div class="form-group">
<label for="{{ form.length.id_for_label }}" class="col-sm-2 control-label">Length</label>
<div class="col-sm-6">
<div class="input-group">
{% render_field form.length|add_class:'form-control' %}
<span class="input-group-addon">{{ form.length.help_text }}</span>
</div>
</div>
<div class="col-sm-4">
<btn class="btn btn-danger" onclick="setLength('5');" tabindex="-1">5{{ form.length.help_text }}</btn>
<btn class="btn btn-success" onclick="setLength('10');" tabindex="-1">10{{ form.length.help_text }}</btn>
<btn class="btn btn-info" onclick="setLength('20');" tabindex="-1">20{{ form.length.help_text }}</btn>
</div>
</div>
<div class="form-group">
<label for="{{ form.csa.id_for_label }}" class="col-sm-2 control-label">Cross Sectional Area</label>
<div class="col-sm-6">
<div class="input-group">
{% render_field form.csa|add_class:'form-control' value=object.csa %}
<span class="input-group-addon">{{ form.csa.help_text }}</span>
</div>
</div>
<div class="col-sm-4">
<btn class="btn btn-default" onclick="setCSA('1.5');" tabindex="-1">1.5{{ form.csa.help_text }}</btn>
<btn class="btn btn-default" onclick="setCSA('2.5');" tabindex="-1">2.5{{ form.csa.help_text }}</btn>
</div>
</div>
</div>
{% if not request.is_ajax %}
<div class="form-group pull-right">
<button class="btn btn-success" type="submit" form="asset_audit_form" id="id_mark_audited">Mark Audited</button>
</div>
{% endif %}
</form>
{% endblock %}
{% block footer %}
<div class="form-group">
<button class="btn btn-success pull-right" type="submit" form="asset_audit_form" onclick="onAuditClick({{form.asset_id.value}});" id="id_mark_audited">Mark Audited</button>
</div>
{% endblock %}

View File

@@ -0,0 +1,87 @@
{% extends 'base_assets.html' %}
{% block title %}Asset Audit List{% endblock %}
{% load static %}
{% load paginator from filters %}
{% load widget_tweaks %}
{% block js %}
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script src="{% static "js/interaction.js" %}"></script>
<script src="{% static "js/modal.js" %}"></script>
<script>
$('document').ready(function(){
$('#asset-search-form').submit(function () {
$('#searchButton').focus().click();
return false;
});
$('#searchButton').click(function (e) {
e.preventDefault();
var url = "{% url 'asset_audit' None %}";
var id = $("#{{form.query.id_for_label}}").val();
url = url.replace('None', id);
$.ajax({
url: url,
success: function(){
$link = $(this);
// Anti modal inception
if ($link.parents('#modal').length == 0) {
modaltarget = $link.data('target');
modalobject = "";
$('#modal').load(url, function (e) {
$('#modal').modal();
});
}
},
error:function(){
$("#error404").attr("hidden", false);
}
});
});
});
function onAuditClick(assetID) {
$('#' + assetID).remove();
}
</script>
{% endblock %}
{% block content %}
<div class="page-header">
<h1 class="text-center">Asset Audit List</h1>
</div>
<div id="error404" class="alert alert-danger alert-dismissable" hidden=true>
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
<span>Asset with that ID does not exist!</span>
</div>
<h3>Audit Asset:</h3>
<form id="asset-search-form" class="form-horizontal" method="POST">
<div class="input-group input-group-lg" style="width: auto;">
{% render_field form.query|add_class:'form-control' placeholder='Enter Asset ID' autofocus="true" %}
<label for="query" class="sr-only">Asset ID:</label>
<span class="input-group-btn"><a id="searchButton" class="btn btn-default" class="submit" type="submit">Search</a></span>
</div>
</form>
<h3>Assets Requiring Audit:</h3>
<table class="table">
<thead>
<tr>
<th>Asset ID</th>
<th>Description</th>
<th>Category</th>
<th>Status</th>
<th class="hidden-xs"></th>
</tr>
</thead>
<tbody id="asset_table_body">
{% include 'partials/asset_list_table_body.html' with audit="true" %}
</tbody>
</table>
{% if is_paginated %}
<div class="text-center">
{% paginator %}
</div>
{% endif %}
{% endblock %}

View File

@@ -1,4 +1,4 @@
{% extends 'base_assets.html' %}
{% extends request.is_ajax|yesno:'base_ajax.html,base_assets.html' %}
{% load widget_tweaks %}
{% block title %}Asset {{ object.asset_id }}{% endblock %}
@@ -23,18 +23,23 @@
</div>
</div>
<div class="row">
{% if perms.assets.asset_finance %}
<div class="col-md-6">
{% include 'partials/purchasedetails_form.html' %}
</div>
{%endif%}
<div class="col-md-6"
<div class="col-md-4"
{% if not object.is_cable %} hidden="true" {% endif %} id="cable-table">
{% include 'partials/cable_form.html' %}
</div>
{% if perms.assets.asset_finance %}
<div class="col-md-4">
{% include 'partials/purchasedetails_form.html' %}
</div>
{%endif%}
<div class="col-md-4">
{% include 'partials/parent_form.html' %}
</div>
{% if not edit %}
<div class="col-md-4">
{% include 'partials/audit_details.html' %}
</div>
{% endif %}
</div>
<div class="row">
<div class="col-md-12">

View File

@@ -1,25 +1,28 @@
{% if edit and object %}
<!--edit-->
<button type="submit" class="btn btn-success"><i class="glyphicon glyphicon-floppy-disk"></i> Save</button>
<a class="btn btn-default" href="{% url 'asset_duplicate' object.pk %}"><i class="glyphicon glyphicon-duplicate"></i> Duplicate</a>
{% elif duplicate %}
<!--duplicate-->
<button type="submit" class="btn btn-success"><i class="glyphicon glyphicon-ok-sign"></i> Create Duplicate</button>
{% elif create %}
<!--create-->
<button type="submit" class="btn btn-success"><i class="glyphicon glyphicon-floppy-disk"></i> Save</button>
{% else %}
<!--detail view-->
<div class="btn-group">
<a href="{% url 'asset_update' object.asset_id %}" class="btn btn-default"><i class="glyphicon glyphicon-edit"></i> Edit</a>
<a class="btn btn-default" href="{% url 'asset_duplicate' object.asset_id %}"><i class="glyphicon glyphicon-duplicate"></i> Duplicate</a>
</div>
{% endif %}
{% if create or edit or duplicate %}
<br>
<button type="reset" class="btn btn-link" onclick="
{%if duplicate%}
{% url 'asset_detail' previous_asset_id %}
{%else%}
history.back(){%endif%}">Cancel</button>
{% if perms.assets.change_asset %}
{% if edit and object %}
<!--edit-->
<button type="submit" class="btn btn-success"><i class="glyphicon glyphicon-floppy-disk"></i> Save</button>
<a class="btn btn-default" href="{% url 'asset_duplicate' object.pk %}"><i class="glyphicon glyphicon-duplicate"></i> Duplicate</a>
{% elif duplicate %}
<!--duplicate-->
<button type="submit" class="btn btn-success"><i class="glyphicon glyphicon-ok-sign"></i> Create Duplicate</button>
{% elif create %}
<!--create-->
<button type="submit" class="btn btn-success"><i class="glyphicon glyphicon-floppy-disk"></i> Save</button>
{% else %}
<!--detail view-->
<div class="btn-group">
<a href="{% url 'asset_update' object.asset_id %}" class="btn btn-default"><i class="glyphicon glyphicon-edit"></i> Edit</a>
<a class="btn btn-default" href="{% url 'asset_duplicate' object.asset_id %}"><i class="glyphicon glyphicon-duplicate"></i> Duplicate</a>
<a type="button" class="btn btn-info" href="{% url 'asset_audit' object.asset_id %}"><i class="glyphicon glyphicon-object-align-left"></i> Audit</a>
</div>
{% endif %}
{% if create or edit or duplicate %}
<br>
<button type="reset" class="btn btn-link" onclick="
{%if duplicate%}
{% url 'asset_detail' previous_asset_id %}
{%else%}
history.back(){%endif%}">Cancel</button>
{% endif %}
{% endif %}

View File

@@ -1,19 +1,21 @@
{% for item in object_list %}
{# <li><a href="{% url 'asset_detail' item.pk %}">{{ item.asset_id }} - {{ item.description }}</a></li>#}
<!---TODO: When the ability to filter the list is added, remove the colours from the filter - specifically, stop greying out sold/binned stuff if it is being searched for-->
<tr class="{{ item.status.display_class|default:'' }} assetRow">
<tr class="{{ item.status.display_class|default:'' }} assetRow" id="{{item.asset_id}}">
<td style="vertical-align: middle;"><a class="assetID" href="{% url 'asset_detail' item.asset_id %}">{{ item.asset_id }}</a></td>
<td class="assetDesc" style="vertical-align: middle; text-overflow: ellipsis; white-space: nowrap; overflow: hidden; max-width: 25vw">{{ item.description }}</td>
<td class="assetCategory" style="vertical-align: middle;">{{ item.category }}</td>
<td class="assetStatus" style="vertical-align: middle;">{{ item.status }}</td>
<td class="hidden-xs">
<div class="btn-group" role="group">
{% if audit %}
<a type="button" class="btn btn-info modal-href" href="{% url 'asset_audit' item.asset_id %}"><i class="glyphicon glyphicon-object-align-left"></i> Audit</a>
{% else %}
<a type="button" class="btn btn-default btn-sm" href="{% url 'asset_detail' item.asset_id %}"><i class="glyphicon glyphicon-eye-open"></i> View</a>
{% if perms.assets.change_asset %}
<a type="button" class="btn btn-default btn-sm" href="{% url 'asset_update' item.asset_id %}"><i class="glyphicon glyphicon-edit"></i> Edit</a>
<a type="button" class="btn btn-default btn-sm" href="{% url 'asset_duplicate' item.asset_id %}"><i class="glyphicon glyphicon-duplicate"></i> Duplicate</a>
{% endif %}
</div>
{% endif %}
</td>
</tr>
{% endfor %}

View File

@@ -0,0 +1,8 @@
<div class="panel {% if object.last_audited_at is not None %} panel-success {% else %} panel-warning {% endif %}">
<div class="panel-heading">
Audit Details
</div>
<div class="panel-body">
<p>Audited at <span class="label label-default">{{ object.last_audited_at|default_if_none:'-' }}</span> by <span class="label label-info">{{ object.last_audited_by|default_if_none:'-' }}</span></p>
</div>
</div>

View File

@@ -6,7 +6,7 @@ from selenium.webdriver import Chrome
from django.urls import reverse
from PyRIGS.tests import regions
from PyRIGS.tests.pages import BasePage, FormPage
import pdb
from selenium.common.exceptions import NoSuchElementException
class AssetList(BasePage):
@@ -95,11 +95,6 @@ class AssetForm(FormPage):
def parent_selector(self):
return regions.BootstrapSelectElement(self, self.find_element(*self._parent_select_locator))
def submit(self):
previous_errors = self.errors
self.find_element(*self._submit_locator).click()
self.wait.until(lambda x: self.errors != previous_errors or self.success)
class AssetEdit(AssetForm):
URL_TEMPLATE = '/assets/asset/id/{asset_id}/edit/'
@@ -162,11 +157,6 @@ class SupplierForm(FormPage):
'name': (regions.TextBox, (By.ID, 'id_name')),
}
def submit(self):
previous_errors = self.errors
self.find_element(*self._submit_locator).click()
self.wait.until(lambda x: self.errors != previous_errors or self.success)
class SupplierCreate(SupplierForm):
URL_TEMPLATE = reverse('supplier_create')
@@ -183,3 +173,90 @@ class SupplierEdit(SupplierForm):
@property
def success(self):
return '/edit' not in self.driver.current_url
class AssetAuditList(AssetList):
URL_TEMPLATE = reverse('asset_audit_list')
_search_text_locator = (By.ID, 'id_query')
_go_button_locator = (By.ID, 'searchButton')
_modal_locator = (By.ID, 'modal')
_errors_selector = (By.CLASS_NAME, "alert-danger")
@property
def modal(self):
return self.AssetAuditModal(self, self.find_element(*self._modal_locator))
@property
def query(self):
return self.find_element(*self._search_text_locator).text
def set_query(self, queryString):
element = self.find_element(*self._search_text_locator)
element.clear()
element.send_keys(queryString)
def search(self):
self.find_element(*self._go_button_locator).click()
@property
def error(self):
try:
return self.find_element(*self._errors_selector)
except NoSuchElementException:
return None
class AssetAuditModal(Region):
_errors_selector = (By.CLASS_NAME, "alert-danger")
# Don't use the usual success selector - that tries and fails to hit the '10m long cable' helper button...
_submit_locator = (By.ID, "id_mark_audited")
form_items = {
'asset_id': (regions.TextBox, (By.ID, 'id_asset_id')),
'description': (regions.TextBox, (By.ID, 'id_description')),
'is_cable': (regions.CheckBox, (By.ID, 'id_is_cable')),
'serial_number': (regions.TextBox, (By.ID, 'id_serial_number')),
'salvage_value': (regions.TextBox, (By.ID, 'id_salvage_value')),
'date_acquired': (regions.DatePicker, (By.ID, 'id_date_acquired')),
'category': (regions.SingleSelectPicker, (By.ID, 'id_category')),
'status': (regions.SingleSelectPicker, (By.ID, 'id_status')),
'plug': (regions.SingleSelectPicker, (By.ID, 'id_plug')),
'socket': (regions.SingleSelectPicker, (By.ID, 'id_socket')),
'length': (regions.TextBox, (By.ID, 'id_length')),
'csa': (regions.TextBox, (By.ID, 'id_csa')),
'circuits': (regions.TextBox, (By.ID, 'id_circuits')),
'cores': (regions.TextBox, (By.ID, 'id_cores'))
}
@property
def errors(self):
try:
error_page = regions.ErrorPage(self, self.find_element(*self._errors_selector))
return error_page.errors
except NoSuchElementException:
return None
def submit(self):
previous_errors = self.errors
self.root.find_element(*self._submit_locator).click()
# self.wait.until(lambda x: not self.is_displayed) TODO
def remove_all_required(self):
self.driver.execute_script("Array.from(document.getElementsByTagName(\"input\")).forEach(function (el, ind, arr) { el.removeAttribute(\"required\")});")
self.driver.execute_script("Array.from(document.getElementsByTagName(\"select\")).forEach(function (el, ind, arr) { el.removeAttribute(\"required\")});")
def __getattr__(self, name):
if name in self.form_items:
element = self.form_items[name]
form_element = element[0](self, self.find_element(*element[1]))
return form_element.value
else:
return super().__getattribute__(name)
def __setattr__(self, name, value):
if name in self.form_items:
element = self.form_items[name]
form_element = element[0](self, self.find_element(*element[1]))
form_element.set_value(value)
else:
self.__dict__[name] = value

View File

@@ -9,8 +9,13 @@ from RIGS import models as rigsmodels
from PyRIGS.tests.base import BaseTest, AutoLoginTest
from assets import models, urls
from reversion import revisions as reversion
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from RIGS.test_functional import animation_is_finished
import datetime
from django.utils import timezone
class TestAssetList(AutoLoginTest):
@@ -255,6 +260,76 @@ class TestSupplierCreateAndEdit(AutoLoginTest):
self.assertTrue(self.page.success)
class TestAssetAudit(AutoLoginTest):
def setUp(self):
super().setUp()
self.category = models.AssetCategory.objects.create(name="Haulage")
self.status = models.AssetStatus.objects.create(name="Probably Fine", should_show=True)
self.supplier = models.Supplier.objects.create(name="The Bazaar")
self.connector = models.Connector.objects.create(description="Trailer Socket", current_rating=1, voltage_rating=40, num_pins=13)
models.Asset.objects.create(asset_id="1", description="Trailer Cable", status=self.status, category=self.category, date_acquired=datetime.date(2020, 2, 1))
models.Asset.objects.create(asset_id="11", description="Trailerboard", status=self.status, category=self.category, date_acquired=datetime.date(2020, 2, 1))
models.Asset.objects.create(asset_id="111", description="Erms", status=self.status, category=self.category, date_acquired=datetime.date(2020, 2, 1))
models.Asset.objects.create(asset_id="1111", description="A hammer", status=self.status, category=self.category, date_acquired=datetime.date(2020, 2, 1))
self.page = pages.AssetAuditList(self.driver, self.live_server_url).open()
self.wait = WebDriverWait(self.driver, 5)
def test_audit_process(self):
asset_id = "1111"
self.page.set_query(asset_id)
self.page.search()
mdl = self.page.modal
self.wait.until(EC.visibility_of_element_located((By.ID, 'modal')))
# Do it wrong on purpose to check error display
mdl.remove_all_required()
mdl.description = ""
mdl.submit()
# self.wait.until(EC.visibility_of_element_located((By.ID, 'modal')))
self.wait.until(animation_is_finished())
# self.assertTrue(self.driver.find_element_by_id('modal').is_displayed())
self.assertIn("This field is required.", mdl.errors["Description"])
# Now do it properly
new_desc = "A BIG hammer"
mdl.description = new_desc
mdl.submit()
self.wait.until(animation_is_finished())
self.assertFalse(self.driver.find_element_by_id('modal').is_displayed())
# Check data is correct
audited = models.Asset.objects.get(asset_id="1111")
self.assertEqual(audited.description, new_desc)
# Make sure audit 'log' was filled out
self.assertEqual(self.profile.initials, audited.last_audited_by.initials)
self.assertEqual(timezone.now().date(), audited.last_audited_at.date())
self.assertEqual(timezone.now().hour, audited.last_audited_at.hour)
self.assertEqual(timezone.now().minute, audited.last_audited_at.minute)
# Check we've removed it from the 'needing audit' list
self.assertNotIn(asset_id, self.page.assets)
def test_audit_list(self):
self.assertEqual(len(models.Asset.objects.filter(last_audited_at=None)), len(self.page.assets))
assetRow = self.page.assets[0]
assetRow.find_element(By.CSS_SELECTOR, "td:nth-child(5) > div:nth-child(1) > a:nth-child(1)").click()
self.wait.until(EC.visibility_of_element_located((By.ID, 'modal')))
self.assertEqual(self.page.modal.asset_id, assetRow.id)
# First close button is for the not found error
self.page.find_element(By.XPATH, '(//button[@class="close"])[2]').click()
self.wait.until(animation_is_finished())
self.assertFalse(self.driver.find_element_by_id('modal').is_displayed())
# Make sure audit log was NOT filled out
audited = models.Asset.objects.get(asset_id=assetRow.id)
self.assertEqual(None, audited.last_audited_by)
# Check that a failed search works
self.page.set_query("NOTFOUND")
self.page.search()
self.wait.until(animation_is_finished())
self.assertFalse(self.driver.find_element_by_id('modal').is_displayed())
self.assertIn("Asset with that ID does not exist!", self.page.error.text)
class TestSupplierValidation(TestCase):
@classmethod
def setUpTestData(cls):

View File

@@ -36,6 +36,9 @@ urlpatterns = [
views.AssetOembed.as_view(),
name='asset_oembed'),
path('asset/audit/', permission_required_with_403('assets.change_asset')(views.AssetAuditList.as_view()), name='asset_audit_list'),
path('asset/id/<str:pk>/audit/', permission_required_with_403('assets.change_asset')(views.AssetAudit.as_view()), name='asset_audit'),
path('supplier/list', views.SupplierList.as_view(), name='supplier_list'),
path('supplier/<int:pk>', views.SupplierDetail.as_view(), name='supplier_detail'),
path('supplier/create', permission_required_with_403('assets.add_supplier')

View File

@@ -4,13 +4,17 @@ from django.http import HttpResponse, Http404
from django.views import generic
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from django.urls import reverse
from django.urls import reverse_lazy, reverse
from django.db.models import Q
from django.shortcuts import get_object_or_404
from django.core import serializers
from django.contrib import messages
from assets import models, forms
from RIGS import versioning
import simplejson
import datetime
from django.utils import timezone
@method_decorator(csrf_exempt, name='dispatch')
@@ -109,7 +113,14 @@ class AssetEdit(LoginRequiredMixin, AssetIDUrlMixin, generic.UpdateView):
return context
def get_success_url(self):
return reverse("asset_detail", kwargs={"pk": self.object.asset_id})
if self.request.is_ajax():
url = reverse_lazy('closemodal')
update_url = str(reverse_lazy('asset_update', kwargs={'pk': self.object.pk}))
messages.info(self.request, "modalobject=" + serializers.serialize("json", [self.object]))
messages.info(self.request, "modalobject[0]['update_url']='" + update_url + "'")
else:
url = reverse_lazy('asset_detail', kwargs={'pk': self.object.asset_id, })
return url
class AssetCreate(LoginRequiredMixin, generic.CreateView):
@@ -173,6 +184,30 @@ class AssetEmbed(AssetDetail):
template_name = 'asset_embed.html'
@method_decorator(csrf_exempt, name='dispatch')
class AssetAuditList(AssetList):
template_name = 'asset_audit_list.html'
hide_hidden_status = False
# TODO Refresh this when the modal is submitted
def get_queryset(self):
self.form = forms.AssetSearchForm(data={})
return self.model.objects.filter(Q(last_audited_at__isnull=True))
class AssetAudit(AssetEdit):
template_name = 'asset_audit.html'
form_class = forms.AssetAuditForm
def get_success_url(self):
# TODO For some reason this doesn't stick when done in form_valid??
asset = self.get_object()
asset.last_audited_by = self.request.user
asset.last_audited_at = timezone.now()
asset.save()
return super().get_success_url()
class SupplierList(generic.ListView):
model = models.Supplier
template_name = 'supplier_list.html'

View File

@@ -37,5 +37,6 @@
</li>
{% if perms.assets.view_asset %}
<li><a href="{% url 'asset_activity_table' %}">Recent Changes</a></li>
<li><a href="{% url 'asset_audit_list' %}">Audit</a></li>
{% endif %}
{% endblock %}