From 9969e6594df2fdab30235ee2da23073c430db160 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 12 Sep 2022 17:38:29 +0200 Subject: [PATCH] update the message of upload placeholders --- ereuse_devicehub/inventory/forms.py | 10 +++++++++- ereuse_devicehub/inventory/views.py | 22 +++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 98e71f4c..d72fec04 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -1490,6 +1490,11 @@ class UploadPlaceholderForm(FlaskForm): 'Select a Placeholder File', [validators.DataRequired()] ) + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.dev_new = 0 + self.dev_update = 0 + def get_data_file(self): files = request.files.getlist(self.placeholder_file.name) @@ -1555,6 +1560,7 @@ class UploadPlaceholderForm(FlaskForm): # update one if placeholder: + self.dev_update += 1 device = placeholder.device device.model = "{}".format(data['Model'][i]).lower() device.manufacturer = "{}".format(data['Manufacturer'][i]).lower() @@ -1594,9 +1600,11 @@ class UploadPlaceholderForm(FlaskForm): snapshot_json = schema.load(json_snapshot) device = snapshot_json['device'] device.placeholder = Placeholder(**json_placeholder) + self.dev_new += 1 + typ = 'New device' placeholder_log = PlaceholdersLog( - type="New device", source=self.source, placeholder=device.placeholder + type=typ, source=self.source, placeholder=device.placeholder ) self.placeholders.append((device, placeholder_log)) diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 32199783..69cf3968 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -1169,7 +1169,27 @@ class UploadPlaceholderView(GenericMixin): lot.devices.add(device) db.session.add(lot) db.session.commit() - messages.success('Placeholders uploaded successfully!') + dev_new = form.dev_new + dev_update = form.dev_update + total = dev_new + dev_update + txt = 'Placeholders uploaded successfully!' + + if dev_update == 0: + txt = f'A total of {total} Placeholders have been successfully' + txt += ' uploaded. All of them have been new registrations in' + txt += ' the system.' + + if dev_new == 0: + txt = f'A total of {total} Placeholders have been successfully' + txt += ' uploaded. All of them are updates.' + + if dev_new and dev_update: + txt = f'A total of {total} Placeholders have been successfully' + txt += f' uploaded. Among these {dev_new} are registered for ' + txt += ' the first time in the system and another' + txt += f' {dev_update} have been updated.' + + messages.success(txt) return flask.render_template(self.template_name, **self.context)