base of form for update type of updated

This commit is contained in:
Cayo Puigdefabregas 2022-12-22 12:01:31 +01:00
parent 5d88d4e516
commit 62de2126c7
5 changed files with 143 additions and 1 deletions

View File

@ -1692,3 +1692,81 @@ class BindingForm(FlaskForm):
return False return False
return True return True
class UserTrustsForm(FlaskForm):
snapshot_type = SelectField(
'',
[validators.DataRequired()],
choices=[("new_device", "New Device"), ("update", "Update")],
default="new_device",
render_kw={'class': "form-select"},
)
def __init__(self, snapshot_uuid, *args, **kwargs):
self.snapshot = Snapshot.query.filter_by(uuid=snapshot_uuid).first()
self.device = self.snapshot.device if self.snapshot.device else None
self.snapshot_type.kwargs['default'] = self.snapshot.get_new_device()
super().__init__(*args, **kwargs)
def validate(self, extra_validators=None):
is_valid = super().validate(extra_validators)
if not is_valid:
txt = ""
self.snapthot_type.errors = [txt]
return False
return True
def unic(self):
try:
return self._unic
except Exception:
self._unic = (
Device.query.filter_by(
hid=self.device.hid, owner=g.user, placeholder=None
).count()
< 2
)
return self._unic
def show(self):
if not self.snapshot or not self.device:
return False
if not hasattr(self.device, 'system_uuid'):
return False
if not self.device.system_uuid:
return False
if self.snapshot.get_new_device() == 'update':
# To do Split
return True
if not self.unic():
# To do merge
return True
return False
def save(self, commit=True):
# import pdb; pdb.set_trace()
if not self.show():
return
if self.snapshot_type.data == self.snapshot.get_new_device():
return
if self.snapshot_type.data == 'update' and not self.unic():
self.device.merge()
if self.snapshot_type.data == 'new_device' and self.unic():
self.device.split()
if commit:
db.session.commit()
return self.snapshot

View File

@ -33,6 +33,7 @@ from ereuse_devicehub.inventory.forms import (
TransferForm, TransferForm,
UploadPlaceholderForm, UploadPlaceholderForm,
UploadSnapshotForm, UploadSnapshotForm,
UserTrustsForm,
) )
from ereuse_devicehub.labels.forms import PrintLabelsForm from ereuse_devicehub.labels.forms import PrintLabelsForm
from ereuse_devicehub.parser.models import PlaceholdersLog, SnapshotsLog from ereuse_devicehub.parser.models import PlaceholdersLog, SnapshotsLog
@ -1228,9 +1229,12 @@ class SnapshotListView(GenericMixin):
class SnapshotDetailView(GenericMixin): class SnapshotDetailView(GenericMixin):
template_name = 'inventory/snapshot_detail.html' template_name = 'inventory/snapshot_detail.html'
methods = ['GET', 'POST']
form_class = UserTrustsForm
def dispatch_request(self, snapshot_uuid): def dispatch_request(self, snapshot_uuid):
self.snapshot_uuid = snapshot_uuid self.snapshot_uuid = snapshot_uuid
form = self.form_class(snapshot_uuid)
self.get_context() self.get_context()
self.context['page_title'] = "Snapshot Detail" self.context['page_title'] = "Snapshot Detail"
self.context['snapshots_log'] = self.get_snapshots_log() self.context['snapshots_log'] = self.get_snapshots_log()
@ -1238,6 +1242,10 @@ class SnapshotDetailView(GenericMixin):
self.context['snapshot_sid'] = '' self.context['snapshot_sid'] = ''
if self.context['snapshots_log'].count(): if self.context['snapshots_log'].count():
self.context['snapshot_sid'] = self.context['snapshots_log'][0].sid self.context['snapshot_sid'] = self.context['snapshots_log'][0].sid
self.context['form'] = form
if form.validate_on_submit():
form.save()
return flask.render_template(self.template_name, **self.context) return flask.render_template(self.template_name, **self.context)

View File

@ -701,6 +701,19 @@ class Snapshot(JoinedWithOneDeviceMixin, ActionWithOneDevice):
return hdds return hdds
def get_new_device(self):
if not self.device:
return ''
snapshots = []
for s in self.device.actions:
if s == self:
break
if s.type == self.type:
snapshots.append(s)
return snapshots and 'update' or 'new_device'
def __str__(self) -> str: def __str__(self) -> str:
return '{}. {} version {}.'.format(self.severity, self.software, self.version) return '{}. {} version {}.'.format(self.severity, self.software, self.version)

View File

@ -875,6 +875,14 @@ class Device(Thing):
} }
return types.get(self.type, '') return types.get(self.type, '')
def split(self):
self.user_trusts = False
return
def merge(self):
self.user_trusts = True
return
def __lt__(self, other): def __lt__(self, other):
return self.id < other.id return self.id < other.id

View File

@ -20,9 +20,44 @@
<div class="card-body pt-3"> <div class="card-body pt-3">
<h3>{{ snapshot_sid }} | {{ snapshot_uuid }}</h3> <h3>{{ snapshot_sid }} | {{ snapshot_uuid }}</h3>
<!-- Bordered Tabs --> <!-- Bordered Tabs -->
{% if form.show() %}
<ul class="nav nav-tabs nav-tabs-bordered">
<li class="nav-item">
<button class="nav-link active" data-bs-toggle="tab" data-bs-target="#log">Log</button>
</li>
<li class="nav-item">
<button class="nav-link" data-bs-toggle="tab" data-bs-target="#change-type">Change type updated</button>
</li>
</ul>
{% endif %}
<div class="tab-content pt-2"> <div class="tab-content pt-2">
<div class="tab-pane fade show active"> {% if form.show() %}
<div class="tab-pane fade" id="change-type">
<h5 class="card-title">Change Snapshot Type Upload</h5>
<div class="list-group col-6">
<div class="list-group-item">
<form method="post" class="row g-3 needs-validation">
{{ form.csrf_token }}
{% for f in form %}
{% if f != form.csrf_token %}
<p class="mb-1">
{{ f }}
</p>
{% endif %}
{% endfor %}
<p class="mb-1">
<button class="btn btn-primary" type="submit">Save</button>
</p>
</form>
</div>
</div>
</div>
{% endif %}
<div class="tab-pane fade show active" id="log">
<h5 class="card-title">Traceability log Details</h5> <h5 class="card-title">Traceability log Details</h5>
<div class="list-group col-6"> <div class="list-group col-6">
{% for log in snapshots_log %} {% for log in snapshots_log %}