Merge pull request #335 from eReuse/bugfix/3748-upload-placeholders

fixing bigs of excel with phid with nan
This commit is contained in:
cayop 2022-09-08 14:27:50 +02:00 committed by GitHub
commit 2edd9fbe6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 26 deletions

View File

@ -1499,7 +1499,7 @@ class UploadPlaceholderForm(FlaskForm):
if _file.content_type == 'text/csv': if _file.content_type == 'text/csv':
self.source = "CSV File: {}".format(_file.filename) self.source = "CSV File: {}".format(_file.filename)
delimiter = ';' delimiter = ';'
data = pd.read_csv(_file).to_dict() data = pd.read_csv(_file).fillna('').to_dict()
head = list(data.keys())[0].split(delimiter) head = list(data.keys())[0].split(delimiter)
values = [ values = [
{k: v.split(delimiter)} for x in data.values() for k, v in x.items() {k: v.split(delimiter)} for x in data.values() for k, v in x.items()
@ -1513,7 +1513,7 @@ class UploadPlaceholderForm(FlaskForm):
else: else:
self.source = "Excel File: {}".format(_file.filename) self.source = "Excel File: {}".format(_file.filename)
try: try:
data = pd.read_excel(_file).to_dict() data = pd.read_excel(_file).fillna('').to_dict()
except ValueError: except ValueError:
self.placeholder_file.errors = ["File don't have a correct format"] self.placeholder_file.errors = ["File don't have a correct format"]
return False return False

View File

@ -88,6 +88,7 @@ class Sync:
# We only want to perform Add/Remove to not new components # We only want to perform Add/Remove to not new components
actions = self.add_remove(db_device, not_new_components) actions = self.add_remove(db_device, not_new_components)
db_device.components = db_components db_device.components = db_components
self.create_placeholder(db_device) self.create_placeholder(db_device)
return db_device, actions return db_device, actions
@ -300,6 +301,7 @@ class Sync:
dict_device.pop('actions_one', None) dict_device.pop('actions_one', None)
dict_device.pop('components', None) dict_device.pop('components', None)
dev_placeholder = device.__class__(**dict_device) dev_placeholder = device.__class__(**dict_device)
if hasattr(device, 'components'):
for c in device.components: for c in device.components:
c_dict = copy.copy(c.__dict__) c_dict = copy.copy(c.__dict__)
c_dict.pop('_sa_instance_state') c_dict.pop('_sa_instance_state')

View File

@ -2206,3 +2206,35 @@ def test_unbindingnot_used(user3: UserClientFlask):
assert Placeholder.query.filter_by(id=old_placeholder.id).first() 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=old_placeholder.device.id).first()
assert Device.query.filter_by(id=dev_wb.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

View File

@ -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.action.views.snapshot import save_json
from ereuse_devicehub.resources.device import models as m from ereuse_devicehub.resources.device import models as m
from ereuse_devicehub.resources.device.exceptions import NeedsId 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 ( from ereuse_devicehub.resources.device.sync import (
MismatchBetweenProperties, MismatchBetweenProperties,
MismatchBetweenTagsAndHid, MismatchBetweenTagsAndHid,
@ -114,7 +114,9 @@ def test_snapshot_post(user: UserClient):
key = itemgetter('serialNumber') key = itemgetter('serialNumber')
snapshot['components'].sort(key=key) snapshot['components'].sort(key=key)
device['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']} == { assert {c['type'] for c in snapshot['components']} == {
m.GraphicCard.t, 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) pc1, _ = user.get(res=m.Device, item=pc1_devicehub_id)
update1_pc1 = pc1['updated'] update1_pc1 = pc1['updated']
# Parent contains components # Parent contains components
assert tuple(c['serialNumber'] for c in pc1['components']) == ( assert (
tuple(c['serialNumber'] for c in pc1['components'])
== (
'p1c1s', 'p1c1s',
'p1c2s', 'p1c2s',
'p1c3s', 'p1c3s',
) == tuple(x.serial_number for x in pc1_dev.binding.device.components) )
== tuple(x.serial_number for x in pc1_dev.binding.device.components)
)
# Components contain parent # Components contain parent
assert all(c['parent'] == pc1_id for c in pc1['components']) assert all(c['parent'] == pc1_id for c in pc1['components'])
# pc has three actions: Snapshot, BenchmarkProcessor and RateComputer # 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' assert storage['privacy']['type'] == 'EraseSectors'
dev = m.Device.query.filter_by(id=snapshot['device']['id']).one() dev = m.Device.query.filter_by(id=snapshot['device']['id']).one()
pc, _ = user.get(res=m.Device, item=dev.devicehub_id) pc, _ = user.get(res=m.Device, item=dev.devicehub_id)
# import pdb; pdb.set_trace()
assert pc['privacy'] == [storage['privacy']] assert pc['privacy'] == [storage['privacy']]