todo an automatic merge with sync only of your devices

This commit is contained in:
Cayo Puigdefabregas 2020-11-06 17:10:32 +01:00
parent 52bd6d5271
commit 8a471c98c0
1 changed files with 11 additions and 4 deletions

View File

@ -4,6 +4,7 @@ from itertools import groupby
from typing import Iterable, Set from typing import Iterable, Set
import yaml import yaml
from flask import g
from sqlalchemy import inspect from sqlalchemy import inspect
from sqlalchemy.exc import IntegrityError from sqlalchemy.exc import IntegrityError
from sqlalchemy.util import OrderedSet from sqlalchemy.util import OrderedSet
@ -101,7 +102,7 @@ class Sync:
assert inspect(component).transient, 'Component should not be synced from DB' assert inspect(component).transient, 'Component should not be synced from DB'
try: try:
if component.hid: if component.hid:
db_component = Device.query.filter_by(hid=component.hid).one() db_component = Device.query.filter_by(hid=component.hid, owner_id=g.user.id).one()
assert isinstance(db_component, Device), \ assert isinstance(db_component, Device), \
'{} must be a component'.format(db_component) '{} must be a component'.format(db_component)
else: else:
@ -153,7 +154,7 @@ class Sync:
db_device = None db_device = None
if device.hid: if device.hid:
with suppress(ResourceNotFound): with suppress(ResourceNotFound):
db_device = Device.query.filter_by(hid=device.hid).one() db_device = Device.query.filter_by(hid=device.hid, owner_id=g.user.id).one()
try: try:
tags = {Tag.from_an_id(tag.id).one() for tag in device.tags} # type: Set[Tag] tags = {Tag.from_an_id(tag.id).one() for tag in device.tags} # type: Set[Tag]
except ResourceNotFound: except ResourceNotFound:
@ -202,6 +203,9 @@ class Sync:
This method mutates db_device. This method mutates db_device.
""" """
if db_device.owner_id != g.user.id:
return
for field_name, value in device.physical_properties.items(): for field_name, value in device.physical_properties.items():
if value is not None: if value is not None:
setattr(db_device, field_name, value) setattr(db_device, field_name, value)
@ -232,8 +236,11 @@ class Sync:
return component.parent or Device(id=0) # Computer with id 0 is our Identity return component.parent or Device(id=0) # Computer with id 0 is our Identity
for parent, _components in groupby(sorted(adding, key=g_parent), key=g_parent): for parent, _components in groupby(sorted(adding, key=g_parent), key=g_parent):
if parent.id != 0: # Is not Computer Identity set_components = OrderedSet(_components)
actions.add(Remove(device=parent, components=OrderedSet(_components))) check_owners = (x.owner_id == g.user.id for x in set_components)
# Is not Computer Identity and all components have the correct owner
if parent.id != 0 and all(check_owners):
actions.add(Remove(device=parent, components=set_components))
return actions return actions