From 40b7d59cc197a162a054e6e3f459c139aa6b8e1b Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand <benjamin.bertrand@esss.se> Date: Thu, 14 Dec 2017 10:46:16 +0100 Subject: [PATCH] Add edit item page --- app/inventory/views.py | 34 ++++++++++++++++++ app/templates/_helpers.html | 3 +- app/templates/inventory/create_item.html | 2 +- app/templates/inventory/edit_item.html | 44 ++++++++++++++++++++++++ app/templates/inventory/view_item.html | 3 ++ 5 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 app/templates/inventory/edit_item.html diff --git a/app/inventory/views.py b/app/inventory/views.py index 87e76ec..18f8b8b 100644 --- a/app/inventory/views.py +++ b/app/inventory/views.py @@ -109,6 +109,40 @@ def view_item(ics_id): return render_template('inventory/view_item.html', item=item.to_dict(long=True)) +@bp.route('/items/edit/<ics_id>', methods=('GET', 'POST')) +@login_groups_accepted('admin', 'create') +def edit_item(ics_id): + item = models.Item.query.filter_by(ics_id=ics_id).first_or_404() + mac_addresses = ' '.join([str(mac) for mac in item.macs]) + form = ItemForm(request.form, obj=item, mac_addresses=mac_addresses) + if form.validate_on_submit(): + item.serial_number = form.serial_number.data + for key in ('manufacturer_id', 'model_id', 'location_id', 'status_id', 'parent_id'): + setattr(item, key, getattr(form, key).data or None) + new_addresses = form.mac_addresses.data.split() + # Delete the MAC addresses that have been removed + for (index, mac) in enumerate(item.macs): + if mac.address not in new_addresses: + item.macs.pop(index) + db.session.delete(mac) + # Add new MAC addresseses + for address in new_addresses: + if address not in mac_addresses: + mac = models.Mac(address=address) + item.macs.append(mac) + current_app.logger.debug(f'Trying to update: {item!r}') + try: + db.session.commit() + except sa.exc.IntegrityError as e: + db.session.rollback() + current_app.logger.warning(f'{e}') + flash(f'{e}', 'error') + else: + flash(f'Item {item} updated!', 'success') + return redirect(url_for('inventory.view_item', ics_id=ics_id)) + return render_template('inventory/edit_item.html', form=form) + + @bp.route('/qrcodes/<kind>') @login_required def qrcodes(kind='Action'): diff --git a/app/templates/_helpers.html b/app/templates/_helpers.html index d8c2352..431cb81 100644 --- a/app/templates/_helpers.html +++ b/app/templates/_helpers.html @@ -18,6 +18,7 @@ {% macro render_field(field) -%} {% set field_class = kwargs.pop('class_', '') + ' form-control' %} + {% set readonly = kwargs.pop('readonly', False) %} {% if field.errors %} {% set field_class = field_class + ' is-invalid' %} {% endif %} @@ -25,7 +26,7 @@ <div class="form-group row"> {{ field.label(class_="col-sm-2 col-form-label") }} <div class="col-sm-10"> - {{ field(class_=field_class) }} + {{ field(class_=field_class, readonly=readonly) }} {% if field.description %} <small class="form-text text-muted"> {{ field.description|safe }} diff --git a/app/templates/inventory/create_item.html b/app/templates/inventory/create_item.html index 5242386..340d847 100644 --- a/app/templates/inventory/create_item.html +++ b/app/templates/inventory/create_item.html @@ -1,7 +1,7 @@ {%- extends "base.html" %} {% from "_helpers.html" import render_field %} -{% block title %}Items - CSEntry{% endblock %} +{% block title %}Register Item - CSEntry{% endblock %} {% block main %} <ul class="nav nav-tabs"> diff --git a/app/templates/inventory/edit_item.html b/app/templates/inventory/edit_item.html new file mode 100644 index 0000000..743ecef --- /dev/null +++ b/app/templates/inventory/edit_item.html @@ -0,0 +1,44 @@ +{%- extends "base.html" %} +{% from "_helpers.html" import render_field %} + +{% block title %}Edit Item - CSEntry{% endblock %} + +{% block main %} + <ul class="nav nav-tabs"> + <li class="nav-item"> + <a class="nav-link" href="{{ url_for('inventory.list_items') }}">List items</a> + </li> + <li class="nav-item"> + <a class="nav-link" href="{{ url_for('inventory.create_item') }}">Register new item</a> + </li> + <li class="nav-item"> + <a class="nav-link" href="{{ url_for('inventory.view_item', ics_id=form.ics_id.data) }}">View item</a> + </li> + <li class="nav-item"> + <a class="nav-link active" href="{{ url_for('inventory.edit_item', ics_id=form.ics_id.data) }}">Edit item</a> + </li> + </ul> + + <br> + + <form id="itemForm" method="POST"> + {{ form.hidden_tag() }} + {{ render_field(form.ics_id, readonly=True) }} + {{ render_field(form.serial_number) }} + {{ render_field(form.manufacturer_id) }} + {{ render_field(form.model_id) }} + {{ render_field(form.location_id) }} + {{ render_field(form.status_id) }} + {{ render_field(form.parent_id) }} + {{ render_field(form.mac_addresses) }} + <div class="form-group row"> + <div class="col-sm-10"> + <button type="submit" class="btn btn-primary">Update</button> + </div> + </div> + </form> +{%- endblock %} + +{% block csentry_scripts %} + <script src="{{ url_for('static', filename='js/items.js') }}"></script> +{% endblock %} diff --git a/app/templates/inventory/view_item.html b/app/templates/inventory/view_item.html index c30766c..4c0c0d2 100644 --- a/app/templates/inventory/view_item.html +++ b/app/templates/inventory/view_item.html @@ -14,6 +14,9 @@ <li class="nav-item"> <a class="nav-link active" href="{{ url_for('inventory.view_item', ics_id=item['ics_id']) }}">View item</a> </li> + <li class="nav-item"> + <a class="nav-link" href="{{ url_for('inventory.edit_item', ics_id=item['ics_id']) }}">Edit item</a> + </li> </ul> <br> -- GitLab