Total Complexity | 4 |
Total Lines | 49 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | # -*- coding: utf-8 -*- |
||
2 | |||
3 | from bika.lims import senaiteMessageFactory as _ |
||
4 | from plone.supermodel import model |
||
5 | from senaite.core.catalog import SETUP_CATALOG |
||
6 | from senaite.core.content.base import Container |
||
7 | from senaite.core.interfaces import IHideActionsMenu |
||
8 | from senaite.core.api import label as label_api |
||
9 | from senaite.core.interfaces import ILabel |
||
10 | from zope import schema |
||
11 | from zope.interface import Invalid |
||
12 | from zope.interface import implementer |
||
13 | from zope.interface import invariant |
||
14 | |||
15 | |||
16 | class ILabelSchema(model.Schema): |
||
17 | """Schema interface |
||
18 | """ |
||
19 | title = schema.TextLine( |
||
20 | title=u"Title", |
||
21 | required=False, |
||
22 | ) |
||
23 | |||
24 | description = schema.Text( |
||
25 | title=u"Description", |
||
26 | required=False, |
||
27 | ) |
||
28 | |||
29 | @invariant |
||
30 | def validate_title(data): |
||
31 | """Checks if the title is unique |
||
32 | """ |
||
33 | # https://community.plone.org/t/dexterity-unique-field-validation |
||
34 | context = getattr(data, "__context__", None) |
||
35 | if context is not None: |
||
36 | if context.title == data.title: |
||
37 | # nothing changed |
||
38 | return |
||
39 | labels = label_api.list_labels(inactive=True) |
||
40 | if data.title in labels: |
||
41 | raise Invalid(_("Label names must be unique")) |
||
42 | |||
43 | |||
44 | @implementer(ILabel, ILabelSchema, IHideActionsMenu) |
||
45 | class Label(Container): |
||
46 | """A container for labels |
||
47 | """ |
||
48 | _catalogs = [SETUP_CATALOG] |
||
49 |