diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index a87442c4..d2e6dafe 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -1499,7 +1499,7 @@ class UploadPlaceholderForm(FlaskForm): if _file.content_type == 'text/csv': self.source = "CSV File: {}".format(_file.filename) delimiter = ';' - data = pd.read_csv(_file).to_dict() + data = pd.read_csv(_file).fillna('').to_dict() head = list(data.keys())[0].split(delimiter) values = [ {k: v.split(delimiter)} for x in data.values() for k, v in x.items() @@ -1513,7 +1513,7 @@ class UploadPlaceholderForm(FlaskForm): else: self.source = "Excel File: {}".format(_file.filename) try: - data = pd.read_excel(_file).to_dict() + data = pd.read_excel(_file).fillna('').to_dict() except ValueError: self.placeholder_file.errors = ["File don't have a correct format"] return False diff --git a/ereuse_devicehub/resources/device/sync.py b/ereuse_devicehub/resources/device/sync.py index 3f284e71..926b3238 100644 --- a/ereuse_devicehub/resources/device/sync.py +++ b/ereuse_devicehub/resources/device/sync.py @@ -88,7 +88,8 @@ class Sync: # We only want to perform Add/Remove to not new components actions = self.add_remove(db_device, not_new_components) db_device.components = db_components - self.create_placeholder(db_device) + + self.create_placeholder(db_device) return db_device, actions def execute_register_component( @@ -300,21 +301,22 @@ class Sync: dict_device.pop('actions_one', None) dict_device.pop('components', None) dev_placeholder = device.__class__(**dict_device) - for c in device.components: - c_dict = copy.copy(c.__dict__) - c_dict.pop('_sa_instance_state') - c_dict.pop('id', None) - c_dict.pop('devicehub_id', None) - c_dict.pop('actions_multiple', None) - c_dict.pop('actions_one', None) - c_placeholder = c.__class__(**c_dict) - c_placeholder.parent = dev_placeholder - c.parent = device - component_placeholder = Placeholder( - device=c_placeholder, binding=c, is_abstract=True - ) - db.session.add(c_placeholder) - db.session.add(component_placeholder) + if hasattr(device, 'components'): + for c in device.components: + c_dict = copy.copy(c.__dict__) + c_dict.pop('_sa_instance_state') + c_dict.pop('id', None) + c_dict.pop('devicehub_id', None) + c_dict.pop('actions_multiple', None) + c_dict.pop('actions_one', None) + c_placeholder = c.__class__(**c_dict) + c_placeholder.parent = dev_placeholder + c.parent = device + component_placeholder = Placeholder( + device=c_placeholder, binding=c, is_abstract=True + ) + db.session.add(c_placeholder) + db.session.add(component_placeholder) placeholder = Placeholder( device=dev_placeholder, binding=device, is_abstract=True diff --git a/tests/test_render_2_0.py b/tests/test_render_2_0.py index 0c1615a5..a6cefe58 100644 --- a/tests/test_render_2_0.py +++ b/tests/test_render_2_0.py @@ -2206,3 +2206,35 @@ def test_unbindingnot_used(user3: UserClientFlask): assert Placeholder.query.filter_by(id=old_placeholder.id).first() assert Device.query.filter_by(id=old_placeholder.device.id).first() assert Device.query.filter_by(id=dev_wb.id).first() + + +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_upload_snapshot_smartphone(user3: UserClientFlask): + uri = '/inventory/upload-snapshot/' + file_name = 'smartphone.snapshot.json' + body, status = user3.get(uri) + + assert status == '200 OK' + assert "Select a Snapshot file" in body + + snapshot = conftest.yaml2json(file_name.split(".json")[0]) + b_snapshot = bytes(json.dumps(snapshot), 'utf-8') + file_snap = (BytesIO(b_snapshot), file_name) + + data = { + 'snapshot': file_snap, + 'csrf_token': generate_csrf(), + } + body, status = user3.post(uri, data=data, content_type="multipart/form-data") + + txt = f"{file_name}: Ok" + assert status == '200 OK' + assert txt in body + db_snapthot = Snapshot.query.one() + dev = db_snapthot.device + assert dev.type == 'Smartphone' + assert dev.serial_number == 'abcdef' + assert dev.binding.device.serial_number == 'abcdef' + assert dev.placeholder is None + assert len(dev.actions) == 2 diff --git a/tests/test_snapshot.py b/tests/test_snapshot.py index bbdec86a..f7a31cce 100644 --- a/tests/test_snapshot.py +++ b/tests/test_snapshot.py @@ -33,7 +33,7 @@ from ereuse_devicehub.resources.action.models import ( from ereuse_devicehub.resources.action.views.snapshot import save_json from ereuse_devicehub.resources.device import models as m from ereuse_devicehub.resources.device.exceptions import NeedsId -from ereuse_devicehub.resources.device.models import SolidStateDrive, Device +from ereuse_devicehub.resources.device.models import Device, SolidStateDrive from ereuse_devicehub.resources.device.sync import ( MismatchBetweenProperties, MismatchBetweenTagsAndHid, @@ -114,7 +114,9 @@ def test_snapshot_post(user: UserClient): key = itemgetter('serialNumber') snapshot['components'].sort(key=key) device['components'].sort(key=key) - assert {(x['id'], x['type']) for x in device['components']} == {(x['id'], x['type']) for x in snapshot['components']} + assert {(x['id'], x['type']) for x in device['components']} == { + (x['id'], x['type']) for x in snapshot['components'] + } assert {c['type'] for c in snapshot['components']} == { m.GraphicCard.t, @@ -228,11 +230,15 @@ def test_snapshot_component_add_remove(user: UserClient): pc1, _ = user.get(res=m.Device, item=pc1_devicehub_id) update1_pc1 = pc1['updated'] # Parent contains components - assert tuple(c['serialNumber'] for c in pc1['components']) == ( - 'p1c1s', - 'p1c2s', - 'p1c3s', - ) == tuple(x.serial_number for x in pc1_dev.binding.device.components) + assert ( + tuple(c['serialNumber'] for c in pc1['components']) + == ( + 'p1c1s', + 'p1c2s', + 'p1c3s', + ) + == tuple(x.serial_number for x in pc1_dev.binding.device.components) + ) # Components contain parent assert all(c['parent'] == pc1_id for c in pc1['components']) # pc has three actions: Snapshot, BenchmarkProcessor and RateComputer @@ -579,7 +585,6 @@ def test_erase_privacy_standards_endtime_sort(user: UserClient): assert storage['privacy']['type'] == 'EraseSectors' dev = m.Device.query.filter_by(id=snapshot['device']['id']).one() pc, _ = user.get(res=m.Device, item=dev.devicehub_id) - # import pdb; pdb.set_trace() assert pc['privacy'] == [storage['privacy']]