diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 7f51bebf..655bffec 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -1692,3 +1692,81 @@ class BindingForm(FlaskForm): return False 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 diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index ed3939be..15988ddb 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -33,6 +33,7 @@ from ereuse_devicehub.inventory.forms import ( TransferForm, UploadPlaceholderForm, UploadSnapshotForm, + UserTrustsForm, ) from ereuse_devicehub.labels.forms import PrintLabelsForm from ereuse_devicehub.parser.models import PlaceholdersLog, SnapshotsLog @@ -1228,9 +1229,12 @@ class SnapshotListView(GenericMixin): class SnapshotDetailView(GenericMixin): template_name = 'inventory/snapshot_detail.html' + methods = ['GET', 'POST'] + form_class = UserTrustsForm def dispatch_request(self, snapshot_uuid): self.snapshot_uuid = snapshot_uuid + form = self.form_class(snapshot_uuid) self.get_context() self.context['page_title'] = "Snapshot Detail" self.context['snapshots_log'] = self.get_snapshots_log() @@ -1238,6 +1242,10 @@ class SnapshotDetailView(GenericMixin): self.context['snapshot_sid'] = '' if self.context['snapshots_log'].count(): 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) diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index 6060e037..7454cac4 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -701,6 +701,19 @@ class Snapshot(JoinedWithOneDeviceMixin, ActionWithOneDevice): 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: return '{}. {} version {}.'.format(self.severity, self.software, self.version) diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 39677597..54d1fa98 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -875,6 +875,14 @@ class Device(Thing): } 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): return self.id < other.id diff --git a/ereuse_devicehub/templates/inventory/snapshot_detail.html b/ereuse_devicehub/templates/inventory/snapshot_detail.html index 4ea87572..b084f76b 100644 --- a/ereuse_devicehub/templates/inventory/snapshot_detail.html +++ b/ereuse_devicehub/templates/inventory/snapshot_detail.html @@ -20,9 +20,44 @@