add placeholder log in form

This commit is contained in:
Cayo Puigdefabregas 2022-07-07 13:10:05 +02:00
parent d93f040453
commit d4824a4333
6 changed files with 143 additions and 12 deletions

View File

@ -30,6 +30,7 @@ from wtforms.fields import FormField
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
from ereuse_devicehub.inventory.models import DeliveryNote, ReceiverNote, Transfer from ereuse_devicehub.inventory.models import DeliveryNote, ReceiverNote, Transfer
from ereuse_devicehub.parser.models import PlaceholdersLog
from ereuse_devicehub.parser.parser import ParseSnapshotLsHw from ereuse_devicehub.parser.parser import ParseSnapshotLsHw
from ereuse_devicehub.parser.schemas import Snapshot_lite from ereuse_devicehub.parser.schemas import Snapshot_lite
from ereuse_devicehub.resources.action.models import Snapshot, Trade from ereuse_devicehub.resources.action.models import Snapshot, Trade
@ -540,6 +541,11 @@ class NewDeviceForm(FlaskForm):
device.placeholder = self.get_placeholder() device.placeholder = self.get_placeholder()
db.session.add(device) db.session.add(device)
placeholder_log = PlaceholdersLog(
type="New device", source='Web form', placeholder=device.placeholder
)
db.session.add(placeholder_log)
def reset_ids(self): def reset_ids(self):
if self.amount.data > 1: if self.amount.data > 1:
self.phid.data = None self.phid.data = None
@ -595,6 +601,10 @@ class NewDeviceForm(FlaskForm):
and self.functionality.data != self._obj.functionality().name and self.functionality.data != self._obj.functionality().name
): ):
self._obj.set_functionality(self.functionality.data) self._obj.set_functionality(self.functionality.data)
placeholder_log = PlaceholdersLog(
type="Update", source='Web form', placeholder=self._obj.placeholder
)
db.session.add(placeholder_log)
class TagDeviceForm(FlaskForm): class TagDeviceForm(FlaskForm):
@ -1445,6 +1455,7 @@ class UploadPlaceholderForm(FlaskForm):
_file = files[0] _file = files[0]
if _file.content_type == 'text/csv': if _file.content_type == 'text/csv':
self.source = "CSV File: {}".format(_file.filename)
delimiter = ';' delimiter = ';'
data = pd.read_csv(_file).to_dict() data = pd.read_csv(_file).to_dict()
head = list(data.keys())[0].split(delimiter) head = list(data.keys())[0].split(delimiter)
@ -1458,6 +1469,7 @@ class UploadPlaceholderForm(FlaskForm):
for k, v in x.items(): for k, v in x.items():
data[head[i]][k] = v[i] data[head[i]][k] = v[i]
else: else:
self.source = "Excel File: {}".format(_file.filename)
try: try:
data = pd.read_excel(_file).to_dict() data = pd.read_excel(_file).to_dict()
except ValueError: except ValueError:
@ -1514,7 +1526,10 @@ class UploadPlaceholderForm(FlaskForm):
placeholder.pallet = "{}".format(data['Pallet'][i]) placeholder.pallet = "{}".format(data['Pallet'][i])
placeholder.info = "{}".format(data['Info'][i]) placeholder.info = "{}".format(data['Info'][i])
self.placeholders.append(device) placeholder_log = PlaceholdersLog(
type="Update", source=self.source, placeholder=device.placeholder
)
self.placeholders.append((device, placeholder_log))
continue continue
# create a new one # create a new one
@ -1540,14 +1555,18 @@ class UploadPlaceholderForm(FlaskForm):
device = snapshot_json['device'] device = snapshot_json['device']
device.placeholder = Placeholder(**json_placeholder) device.placeholder = Placeholder(**json_placeholder)
self.placeholders.append(device) placeholder_log = PlaceholdersLog(
type="New device", source=self.source, placeholder=device.placeholder
)
self.placeholders.append((device, placeholder_log))
return True return True
def save(self, commit=True): def save(self, commit=True):
for device in self.placeholders: for device, placeholder_log in self.placeholders:
db.session.add(device) db.session.add(device)
db.session.add(placeholder_log)
if commit: if commit:
db.session.commit() db.session.commit()

View File

@ -34,7 +34,7 @@ from ereuse_devicehub.inventory.forms import (
UploadSnapshotForm, UploadSnapshotForm,
) )
from ereuse_devicehub.labels.forms import PrintLabelsForm from ereuse_devicehub.labels.forms import PrintLabelsForm
from ereuse_devicehub.parser.models import SnapshotsLog from ereuse_devicehub.parser.models import PlaceholdersLog, SnapshotsLog
from ereuse_devicehub.resources.action.models import Trade from ereuse_devicehub.resources.action.models import Trade
from ereuse_devicehub.resources.device.models import Computer, DataStorage, Device from ereuse_devicehub.resources.device.models import Computer, DataStorage, Device
from ereuse_devicehub.resources.documents.device_row import ActionRow, DeviceRow from ereuse_devicehub.resources.documents.device_row import ActionRow, DeviceRow
@ -880,7 +880,7 @@ class UploadPlaceholderView(GenericMixin):
if lot_id: if lot_id:
lots = self.context['lots'] lots = self.context['lots']
lot = lots.filter(Lot.id == lot_id).one() lot = lots.filter(Lot.id == lot_id).one()
for device in snapshots: for device, p in snapshots:
lot.devices.add(device) lot.devices.add(device)
db.session.add(lot) db.session.add(lot)
db.session.commit() db.session.commit()
@ -889,6 +889,24 @@ class UploadPlaceholderView(GenericMixin):
return flask.render_template(self.template_name, **self.context) return flask.render_template(self.template_name, **self.context)
class PlaceholderLogListView(GenericMixin):
template_name = 'inventory/placeholder_log_list.html'
def dispatch_request(self):
self.get_context()
self.context['page_title'] = "Placeholder Logs"
self.context['placeholders_log'] = self.get_placeholders_log()
return flask.render_template(self.template_name, **self.context)
def get_placeholders_log(self):
placeholder_log = PlaceholdersLog.query.filter(
PlaceholdersLog.owner == g.user
).order_by(PlaceholdersLog.created.desc())
return placeholder_log
devices.add_url_rule('/action/add/', view_func=NewActionView.as_view('action_add')) devices.add_url_rule('/action/add/', view_func=NewActionView.as_view('action_add'))
devices.add_url_rule('/action/trade/add/', view_func=NewTradeView.as_view('trade_add')) devices.add_url_rule('/action/trade/add/', view_func=NewTradeView.as_view('trade_add'))
devices.add_url_rule( devices.add_url_rule(
@ -970,3 +988,6 @@ devices.add_url_rule(
'/lot/<string:lot_id>/upload-placeholder/', '/lot/<string:lot_id>/upload-placeholder/',
view_func=UploadPlaceholderView.as_view('lot_upload_placeholder'), view_func=UploadPlaceholderView.as_view('lot_upload_placeholder'),
) )
devices.add_url_rule(
'/placeholder-logs/', view_func=PlaceholderLogListView.as_view('placeholder_logs')
)

View File

@ -44,7 +44,7 @@ def upgrade():
sa.Column('id', sa.BigInteger(), nullable=False), sa.Column('id', sa.BigInteger(), nullable=False),
sa.Column('source', citext.CIText(), nullable=True), sa.Column('source', citext.CIText(), nullable=True),
sa.Column('type', citext.CIText(), nullable=True), sa.Column('type', citext.CIText(), nullable=True),
sa.Column('status', sa.SmallInteger(), nullable=True), sa.Column('severity', sa.SmallInteger(), nullable=False),
sa.Column('placeholder_id', sa.BigInteger(), nullable=True), sa.Column('placeholder_id', sa.BigInteger(), nullable=True),
sa.Column('owner_id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('owner_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.ForeignKeyConstraint( sa.ForeignKeyConstraint(
@ -56,6 +56,7 @@ def upgrade():
['common.user.id'], ['common.user.id'],
), ),
sa.PrimaryKeyConstraint('id'), sa.PrimaryKeyConstraint('id'),
schema=f'{get_inv()}',
) )

View File

@ -52,13 +52,11 @@ class PlaceholdersLog(Thing):
id = Column(BigInteger, Sequence('snapshots_log_seq'), primary_key=True) id = Column(BigInteger, Sequence('snapshots_log_seq'), primary_key=True)
source = Column(CIText(), default='', nullable=True) source = Column(CIText(), default='', nullable=True)
type = Column(CIText(), default='', nullable=True) type = Column(CIText(), default='', nullable=True)
status = Column(CIText(), default='', nullable=True) severity = Column(SmallInteger, default=Severity.Info, nullable=False)
placeholder_id = Column( placeholder_id = Column(BigInteger, db.ForeignKey(Placeholder.id), nullable=True)
UUID(as_uuid=True), db.ForeignKey(Snapshot.id), nullable=True
)
placeholder = db.relationship( placeholder = db.relationship(
Snapshot, primaryjoin=placeholder_id == Placeholder.id Placeholder, primaryjoin=placeholder_id == Placeholder.id
) )
owner_id = db.Column( owner_id = db.Column(
UUID(as_uuid=True), UUID(as_uuid=True),
@ -84,6 +82,9 @@ class PlaceholdersLog(Thing):
@property @property
def dhid(self): def dhid(self):
if self.placeholder: if self.placeholder:
return self.placeholder.device.dhid return self.placeholder.device.devicehub_id
return '' return ''
def get_status(self):
return Severity(self.severity)

View File

@ -148,6 +148,15 @@
</a> </a>
</li> </li>
<li class="nav-heading">Placeholders</li>
<li class="nav-item">
<a class="nav-link collapsed" href="{{ url_for('inventory.placeholder_logs') }}">
<i class="bi-menu-button-wide"></i>
<span>Uploaded Placeholders</span>
</a>
</li>
<li class="nav-heading">Devices</li> <li class="nav-heading">Devices</li>
<li class="nav-item"> <li class="nav-item">

View File

@ -0,0 +1,80 @@
{% extends "ereuse_devicehub/base_site.html" %}
{% block main %}
<div class="pagetitle">
<h1>{{ page_title }}</h1>
<nav>
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="{{ url_for('inventory.devicelist')}}">Inventory</a></li>
<li class="breadcrumb-item active">Placeholders</li>
</ol>
</nav>
</div><!-- End Page Title -->
<section class="section profile">
<div class="row">
<div class="col-xl-12">
<div class="card">
<div class="card-body pt-3" style="min-height: 650px;">
<!-- Bordered Tabs -->
<div class="tab-content pt-5">
<div id="devices-list" class="tab-pane fade devices-list active show">
<div class="tab-content pt-2">
<table class="table">
<thead>
<tr>
<th scope="col">PHID</th>
<th scope="col">Placeholder source</th>
<th scope="col">Type</th>
<th scope="col">DHID</th>
<th scope="col">Status</th>
<th scope="col" data-type="date" data-format="DD-MM-YYYY">Time</th>
</tr>
</thead>
<tbody>
{% for log in placeholders_log %}
<tr>
<td>
{{ log.phid }}
</td>
<td>
{{ log.source }}
</td>
<td>
{{ log.type }}
</td>
<td>
{% if log.dhid %}
<a href="{{ url_for('inventory.device_details', id=log.dhid)}}">{{ log.dhid }}</a>
{% endif %}
</td>
<td>
{{ log.get_status() }}
</td>
<td>{{ log.created.strftime('%H:%M %d-%m-%Y') }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div><!-- End Bordered Tabs -->
</div>
</div>
</div>
<div id="NotificationsContainer" style="position: absolute; bottom: 0; right: 0; margin: 10px; margin-top: 70px; width: calc(100% - 310px);"></div>
</div>
</div>
</section>
<!-- Custom Code -->
<script>
const table = new simpleDatatables.DataTable("table")
</script>
{% endblock main %}