def save(data, schema=None): """ Create a schema. """ schema_val = SchemaValidator(validator=check_attributes) data = schema_val.deserialize(data) if schema is None: schema = Schema() schema.name = data.get('name') schema.project = data.get('project') schema.label = data.get('label') schema.obj = data.get('obj') schema.hidden = data.get('hidden') schema.meta = data.get('meta') schema.project.updated_at = datetime.utcnow() db.session.add(schema) names = [] for attribute in data.get('attributes', []): attribute['schema'] = schema attr = attributes.save(attribute) schema.attributes.append(attr) names.append(attr.name) for attr in schema.attributes: if attr.name not in names: attributes.delete(attr) return schema
def save(data, schema=None): """ Create a schema. """ data = validate(data) operation = 'create' if schema is None else 'update' if schema is None: schema = Schema() schema.name = data.get('name') schema.project = data.get('project') schema.label = data.get('label') schema.obj = data.get('obj') schema.hidden = data.get('hidden') schema.meta = data.get('meta') schema.parent = data.get('parent') if schema.name in DEFAULTS.values(): schema.parent = None elif schema.parent is None or schema.is_circular(): schema.parent = Schema.by_name(schema.project, DEFAULTS.get(schema.obj)) schema.project.updated_at = datetime.utcnow() db.session.add(schema) inherited = [a.name for a in schema.inherited_attributes] names = [] for attribute in data.get('attributes', []): if attribute.get('name') in inherited: continue attribute['schema'] = schema attr = attributes.save(attribute) schema.local_attributes.append(attr) names.append(attr.name) for attr in schema.local_attributes: if attr.name not in names: attributes.delete(attr) db.session.flush() _schema_changed(schema.project.slug, schema.name, operation) return schema