Skip to content

Commit

Permalink
Merge pull request #113 from andersonluisribeiro/fix-repeated-labels-…
Browse files Browse the repository at this point in the history
…from-templates

bugfix repeated labels from templates
  • Loading branch information
giovannicuriel committed Mar 21, 2019
2 parents 6086f25 + 82d81bb commit 98ce46b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 33 deletions.
70 changes: 38 additions & 32 deletions DeviceManager/DeviceHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
from DeviceManager.DatabaseModels import handle_consistency_exception, assert_device_relation_exists
from DeviceManager.DatabaseModels import DeviceTemplate, DeviceAttr, Device, DeviceTemplateMap, DeviceAttrsPsk
from DeviceManager.DatabaseModels import DeviceOverride
from DeviceManager.SerializationModels import device_list_schema, device_schema
from DeviceManager.SerializationModels import device_list_schema, device_schema, ValidationError
from DeviceManager.SerializationModels import attr_list_schema
from DeviceManager.SerializationModels import parse_payload, load_attrs
from DeviceManager.SerializationModels import parse_payload, load_attrs, validate_repeated_attrs
from DeviceManager.TenancyManager import init_tenant_context, init_tenant_context2
from DeviceManager.app import app
from DeviceManager.Logger import Log
Expand Down Expand Up @@ -126,6 +126,7 @@ def create_orm_override(attr, orm_device, orm_template):
LOGGER.error(f" Unknown attribute {attr['id']} in override list")
raise HTTPRequestError(400, "Unknown attribute \"{}\" in override list".format(target))


def auto_create_template(json_payload, new_device):
if ('attrs' in json_payload) and (new_device.templates is None):
device_template = DeviceTemplate(
Expand Down Expand Up @@ -424,22 +425,24 @@ def create_device(req):
full_device = None
orm_devices = []

for i in range(0, count):
device_data, json_payload = parse_payload(req, device_schema)
device_data['id'] = DeviceHandler.generate_device_id()
device_data['label'] = DeviceHandler.indexed_label(count, c_length, device_data['label'], i)
# handled separately by parse_template_list
device_data.pop('templates', None)
orm_device = Device(**device_data)
parse_template_list(json_payload.get('templates', []), orm_device)
auto_create_template(json_payload, orm_device)
db.session.add(orm_device)
orm_devices.append(orm_device)

try:
for i in range(0, count):
device_data, json_payload = parse_payload(req, device_schema)
validate_repeated_attrs(json_payload)
device_data['id'] = DeviceHandler.generate_device_id()
device_data['label'] = DeviceHandler.indexed_label(count, c_length, device_data['label'], i)
device_data.pop('templates', None)
orm_device = Device(**device_data)
parse_template_list(json_payload.get('templates', []), orm_device)
auto_create_template(json_payload, orm_device)
db.session.add(orm_device)
orm_devices.append(orm_device)
db.session.commit()
except IntegrityError as error:
handle_consistency_exception(error)
except ValidationError as error:
raise HTTPRequestError(400, error.messages)


for orm_device in orm_devices:
devices.append(
Expand Down Expand Up @@ -536,28 +539,31 @@ def update_device(req, device_id):
:raises HTTPRequestError: If this device could not be found in
database.
"""
device_data, json_payload = parse_payload(req, device_schema)

tenant = init_tenant_context(req, db)
old_orm_device = assert_device_exists(device_id)
db.session.delete(old_orm_device)
db.session.flush()

# handled separately by parse_template_list
device_data.pop('templates')
updated_orm_device = Device(**device_data)
parse_template_list(json_payload.get('templates', []), updated_orm_device)
auto_create_template(json_payload, updated_orm_device)
updated_orm_device.id = device_id
updated_orm_device.updated = datetime.now()
updated_orm_device.created = old_orm_device.created

db.session.add(updated_orm_device)
try:
device_data, json_payload = parse_payload(req, device_schema)
validate_repeated_attrs(json_payload)

tenant = init_tenant_context(req, db)
old_orm_device = assert_device_exists(device_id)
db.session.delete(old_orm_device)
db.session.flush()

# handled separately by parse_template_list
device_data.pop('templates')
updated_orm_device = Device(**device_data)
parse_template_list(json_payload.get('templates', []), updated_orm_device)
auto_create_template(json_payload, updated_orm_device)
updated_orm_device.id = device_id
updated_orm_device.updated = datetime.now()
updated_orm_device.created = old_orm_device.created

db.session.add(updated_orm_device)

db.session.commit()
except IntegrityError as error:
# This will throw an exception.
handle_consistency_exception(error)
except ValidationError as error:
raise HTTPRequestError(400, error.messages)

full_device = serialize_full_device(updated_orm_device, tenant)

Expand Down
8 changes: 7 additions & 1 deletion DeviceManager/SerializationModels.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ def validate_attr_label(input):
def validate_children_attr_label(attr_label):
unique = { each['label'] : each for each in attr_label }.values()
if len(attr_label) > len(unique):
raise ValidationError('a template cant not have repeated attributes')
raise ValidationError('a template can not have repeated attributes')

def set_id_with_import_id(data):
if 'import_id' in data and data['import_id'] is not None:
data['id'] = data['import_id']
del(data['import_id'])
return data

def validate_repeated_attrs(data):
if ('attrs' in data):
uniques = { each['label'] : each for each in data['attrs'] }.values()
if len(data['attrs']) > len(uniques):
raise ValidationError('a device can not have repeated attributes')

class MetaSchema(Schema):
id = fields.Int(dump_only=True)
import_id = fields.Int(load_only=True)
Expand Down

0 comments on commit 98ce46b

Please sign in to comment.