Passed
Pull Request — 2.x (#1772)
by Jordi
04:57
created

senaite.core.browser.viewlets.sample_dispatched   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 35
dl 0
loc 52
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A SampleDispatchedViewlet.index() 0 4 2
A SampleDispatchedViewlet.ulocalized_time() 0 3 1
A SampleDispatchedViewlet.__init__() 0 6 1
A SampleDispatchedViewlet.is_dispatched() 0 4 1
A SampleDispatchedViewlet.get_state_info() 0 16 2
1
# -*- coding: utf-8 -*-
2
3
from bika.lims import api
4
from bika.lims.browser import ulocalized_time
5
from plone.app.layout.viewlets import ViewletBase
6
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
7
8
9
class SampleDispatchedViewlet(ViewletBase):
10
    """Print a viewlet showing the WF history comment
11
    """
12
    template = ViewPageTemplateFile("templates/sample_dispatched_viewlet.pt")
13
14
    def __init__(self, context, request, view, manager=None):
15
        super(SampleDispatchedViewlet, self).__init__(
16
            context, request, view, manager=manager)
17
        self.context = context
18
        self.request = request
19
        self.view = view
20
21
    def is_dispatched(self):
22
        """Returns whether the current sample is dispatched
23
        """
24
        return api.get_review_status(self.context) == "dispatched"
25
26
    def get_state_info(self):
27
        """Returns the WF state information
28
        """
29
        history = api.get_review_history(self.context)
30
        entry = len(history) and history[0] or {}
31
        actor = entry.get("actor")
32
        user = api.user.get_user(actor)
33
        if user:
34
            actor = user.getProperty("fullname", actor)
35
        date = entry.get("time")
36
        comments = entry.get("comments", "")
37
38
        return {
39
            "actor": actor,
40
            "date": self.ulocalized_time(date, long_format=1),
41
            "comments": api.safe_unicode(comments),
42
        }
43
44
    def index(self):
45
        if self.is_dispatched():
46
            return ""
47
        return self.template()
48
49
    def ulocalized_time(self, time, long_format=None, time_only=None):
50
        return ulocalized_time(time, long_format, time_only,
51
                               context=self.context, request=self.request)
52