Passed
Push — 2.x ( 814943...eeb59a )
by Ramon
09:33
created

RemarksField.__init__()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.CORE.
4
#
5
# SENAITE.CORE is free software: you can redistribute it and/or modify it under
6
# the terms of the GNU General Public License as published by the Free Software
7
# Foundation, version 2.
8
#
9
# This program is distributed in the hope that it will be useful, but WITHOUT
10
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12
# details.
13
#
14
# You should have received a copy of the GNU General Public License along with
15
# this program; if not, write to the Free Software Foundation, Inc., 51
16
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
#
18
# Copyright 2018-2025 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
from bika.lims import api
22
from bika.lims.api.security import get_user_id
23
from bika.lims.utils import tmpID
24
from senaite.core.api.dtime import now
25
from senaite.core.schema.interfaces import IRemarksField
26
from senaite.core.schema.fields import BaseField
27
from zope.interface import implementer
28
from zope.schema import List
29
from zope.schema.interfaces import IFromUnicode
30
31
32
def fill_remark_object(value):
33
    user_id = get_user_id()
34
    properties = api.get_user_properties(user_id)
35
    fullname = properties and properties.get("fullname") or user_id
36
    return {
37
        "id": tmpID(),
38
        "user_id": user_id,
39
        "user_name": fullname,
40
        "created": now(),
41
        "content": value,
42
    }
43
44
45
@implementer(IRemarksField, IFromUnicode)
46
class RemarksField(List, BaseField):
47
    """A field that handles a remarks for DX content types
48
    """
49
50
    def __init__(self, **kwargs):
51
        default = kwargs.get("default")
52
        kwargs["default"] = default or []
53
        List.__init__(self, **kwargs)
54
        BaseField.__init__(self, **kwargs)
55
56
    def set(self, object, value):
57
        """Set a remarks record or records
58
        :param object: the instance of the field
59
        :param value: dict with remark information or list of dicts
60
        :type value: list/tuple/dict
61
        """
62
        if not isinstance(value, list):
63
            value = [value]
64
        super(RemarksField, self).set(object, value)
65
66
    def get(self, object):
67
        """Returns the remarks records
68
        :param object: the instance of this field
69
        :returns: list of dicts with remark information for each remark item
70
        """
71
        return super(RemarksField, self).get(object) or []
72
73
    def add(self, object, value):
74
        remarks = self.get(object)
75
        new_remark = fill_remark_object(value)
76
        remarks.append(new_remark)
77
        self.set(object, remarks)
78