Source code for xmm.models.attributegroup

from bson import ObjectId
from flask_babel import lazy_gettext as _

from xmm.models import Attribute, fields
from xmm.models.embedded import BaseEmbeddedDocument


[docs]class AttributeGroup(BaseEmbeddedDocument): """A model may have attributes grouped together.""" #: Unique. Visible in frontend under "key" symbol. key = fields.ObjectIdField(required=True, default=ObjectId) #: Name tag that makes sense to the user. label = fields.MultilingualField(fields.StringField(label=_('Bezeichnung'), flexGrow=0.5), required=True) #: Description that makes sense to the user. description = fields.MultilingualField(fields.TextField(label=_('Beschreibung'), flexGrow=0.5)) #: List of attributes in this group. attributes = fields.ListField(fields.ReferenceField(Attribute)) meta = { 'verbose_name': _('Attributgruppe'), 'verbose_name_plural': _('Attributgruppen'), } def __str__(self): return self.label() def update_from_json(self, json): if 'attributes' in json: def ensure_attrs(attr): if self._instance: return attr in self._instance.attributes return attr is not None attr_ids = json.pop('attributes') or [] self.attributes = list(filter(ensure_attrs, map(Attribute.from_id, attr_ids))) return super().update_from_json(json) def remove_attributes(self, *attributes): def _filter(attribute): return attribute not in attributes self.attributes = list(filter(_filter, self.attributes))
[docs] def to_dict(self): return { 'key': self.key, 'label': self.label(), 'description': self.description(), 'attributes': [attr.key for attr in self.attributes], }