Passed
Push — 2.x ( e94308...699219 )
by Jordi
09:28
created

senaite.core.content.label   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 31
dl 0
loc 49
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A ILabelSchema.validate_title() 0 13 4
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