Skip to content
Snippets Groups Projects
Commit fd3109c4 authored by Benjamin Bertrand's avatar Benjamin Bertrand
Browse files

Define temporary ICS id that can be changed

It shouldn't be possible to change the ICS id of an item.
But when registering a bunch of existing items via the API, we need to
allocate an ics_id otherwise not much can be done in the interface.
The id is required for the view and edit page.

The solution is to define some "temporary" ids that don't correspond to
any pre-printed labels.
Items with a temporary id can be assigned a real ics_id when assigning a
label.

Reserved ids start with 'ZZ' (from ZZA000 to ZZZ999)

ICS id can now be non nullable.
parent f503049c
No related branches found
No related tags found
No related merge requests found
# -*- coding: utf-8 -*-
"""
app.helpers
~~~~~~~~~~~
This module implements helpers functions for the models.
:copyright: (c) 2017 European Spallation Source ERIC
:license: BSD 2-Clause, see LICENSE for more details.
"""
import string
from flask import current_app
from . import models
def temporary_ics_ids():
"""Generator that returns the full list of temporary ICS ids"""
return (f'{current_app.config["TEMPORARY_ICS_ID"]}{letter}{number:0=3d}'
for letter in string.ascii_uppercase
for number in range(0, 1000))
def used_temporary_ics_ids():
"""Generator that returns the list of temporary ICS ids used"""
temporary_items = models.Item.query.filter(
models.Item.ics_id.startswith(
current_app.config['TEMPORARY_ICS_ID'])).all()
return (item.ics_id for item in temporary_items)
def get_temporary_ics_id():
"""Return a temporary ICS id that is available"""
for ics_id in temporary_ics_ids():
if ics_id not in used_temporary_ics_ids():
return ics_id
else:
raise ValueError('No temporary ICS id available')
......@@ -107,6 +107,9 @@ def edit_item(ics_id):
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():
# Only allow to update temporary ics_id
if item.ics_id.startswith(current_app.config['TEMPORARY_ICS_ID']):
item.ics_id = form.ics_id.data
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)
......@@ -130,7 +133,7 @@ def edit_item(ics_id):
flash(f'{e}', 'error')
else:
flash(f'Item {item} updated!', 'success')
return redirect(url_for('inventory.view_item', ics_id=ics_id))
return redirect(url_for('inventory.view_item', ics_id=item.ics_id))
return render_template('inventory/edit_item.html', form=form)
......
......@@ -233,7 +233,7 @@ class Item(db.Model):
id = db.Column(db.Integer, primary_key=True)
_created = db.Column(db.DateTime, default=utcnow())
_updated = db.Column(db.DateTime, default=utcnow(), onupdate=utcnow())
ics_id = db.Column(db.Text, unique=True, index=True)
ics_id = db.Column(db.Text, unique=True, nullable=False, index=True)
serial_number = db.Column(db.Text, nullable=False)
manufacturer_id = db.Column(db.Integer, db.ForeignKey('manufacturer.id'))
model_id = db.Column(db.Integer, db.ForeignKey('model.id'))
......
......@@ -52,3 +52,7 @@ CSENTRY_LDAP_GROUPS = {
}
NETWORK_DEFAULT_PREFIX = 24
# ICS Ids starting with this prefix are considered temporary and can be changed
# (waiting for a real label to be assigned)
# WARNING: This is defined here as a global settings but should not be changed!
TEMPORARY_ICS_ID = 'ZZ'
......@@ -23,7 +23,11 @@
<form id="itemForm" method="POST">
{{ form.hidden_tag() }}
{{ render_field(form.ics_id, readonly=True) }}
{% if form.ics_id.data.startswith(config['TEMPORARY_ICS_ID']) %}
{{ render_field(form.ics_id) }}
{% else %}
{{ render_field(form.ics_id, readonly=True) }}
{% endif %}
{{ render_field(form.serial_number) }}
{{ render_field(form.manufacturer_id) }}
{{ render_field(form.model_id) }}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment